Introduction to CS Theory

17
Introduction to CS Theory Lecture 11 – Context-Free Grammars Piotr Faliszewski [email protected]

description

Introduction to CS Theory. Lecture 11 –Context-Free Grammars Piotr Faliszewski [email protected]. Definition A context-free grammar (CFG) is a quadruple G = (V, Σ , S, P), where V is a finite set of variables (non-terminal symbols) Σ is a finite set of terminal symbols - PowerPoint PPT Presentation

Transcript of Introduction to CS Theory

Page 1: Introduction to CS Theory

Introduction to CS Theory

Lecture 11 – Context-Free GrammarsPiotr [email protected]

Page 2: Introduction to CS Theory

Context-Free Grammars

Definition A context-free grammar (CFG) is a

quadruple G = (V, Σ, S, P), where

V is a finite set of variables (non-terminal symbols)

Σ is a finite set of terminal symbols S V is the start symbol P is a finite set of productions

Each production is of the form A α, whereA V, α {V Σ}*

V and Σ are disjoint

Example

G = (V, Σ, S, P)V = {S}Σ = {a,b}P = { S

aSb, S ε}

Page 3: Introduction to CS Theory

Derivations

Grammar G:S εS aSb

Intuitively, aaabbb L(G) because we can transform S into aaabbb in a finite number of steps:

S aSb aaSbb aaaSbbb aaabbb

Notation. If x = α1Aα2 and grammar G contains a rule A β then x G α1βα2

Notation.

By x *G y we mean that y can be

derived from x by 0, 1, or more steps.

Def. Let G be a context-free grammar. L(G) is the language {xΣ* | S *

G x }

Def. A language L is context-free if there is a CFG G s.t. L(G) = L.

Page 4: Introduction to CS Theory

Examples

Consider the following languages (Σ = {a,b})

{x | x is a palindrome} {x | x has at least two b’s} {x | |x| is even} {x | x has as many a’s as b’s}

Give context-free grammars for each of them.

How to argue that your grammar is correct?

Page 5: Introduction to CS Theory

Closure Properties of CFLs

CFL = the set of context-free languages

Closure properties of CFLs? Union? Concatenation? Kleene’s star? Complementation? Intersection?

Closure properties Union

S S1 | S2

Concatenation S S1S2

Kleene’s star S S1S | ε

What about intersection and union?

Page 6: Introduction to CS Theory

Derivation Trees

Simplified grammars of expressions

S S + S S S * S S (S) S x

Can you derive string:x + x * x

Now… give all derivations!

Derivation trees We can represent a

derivation in a graphical form!

Two derivations are the same if they yield the same derivation tree.

We need to “normalize” our derivations Leftmost derivations: In

each step we replace the leftmost variable

1-1 correspondence between leftmost derivations and derivation trees

Page 7: Introduction to CS Theory

Ambiguity

Def. A CFG G is ambiguous if

and only if there is a string x in L(G) s.t. x has two or more distinct leftmost derivations

Ambiguity Usually inconvenient Sometimes inherent in

the language Sometimes we can find

an unambiguous version of the grammar

Example Finding an unambiguous

grammar for the language of expressions

What should we get forx + x * x

What should we get forx + x + x

Disabled Fly to See Carter

Page 8: Introduction to CS Theory

Ambiguity: Dangling Else

Dangling else problem Many programming languages have the

following construct in their grammar:

<statement> if ( <expr> ) <statement> |if ( <expr> ) <statement> else <statement>

This grammar is amiguous!

How do programming languages deal with this issue?

Can we fix the grammar?

Page 9: Introduction to CS Theory

CFLs Versus Regular Languages

Not every CFL is regular aibi

But, every regular language is context free. How to show this? Convert an FA to a

grammar? Convert a regular

expression to a grammar?

Somehow else?

Example: (011+1)*(01)*

Page 10: Introduction to CS Theory

Machines for CFLs?

Can we define an automaton (a variant of NFA-ε) for CFLs? What extra properties

do we need? Extra memory!

A counter? Ability to read the

input back and forth?

Page 11: Introduction to CS Theory

Pushdown Automata

A machine to recognize context-free languages An NFA-ε + a stack Called: pushdown

automaton (PDA)

A move of a PDA depends on: Current state The next input symbol The symbol on the top

of the stack

A move consists of: Changing the state Replacing the symbol on

the top of the stack by 0 or more symbols

What if the stack is empty? The machine is stuck!

Start condition: Special start symbol on

the stack (stack bottom)

Acceptance condition A PDA accepts a string x

if there is a way to process the whole string without getting stuck, and finishing in an accepting state

Page 12: Introduction to CS Theory

Examples

Consider the followign languages: L(G), where G:

S (S) | SS | ε {x | x is a palindrome}

A move of a PDA depends on: Current state The next input symbol The symbol on the top

of the stack

A move consists of: Changing the state Replacing the symbol on

the top of the stack by 0 or more symbols

Page 13: Introduction to CS Theory

Formal Definition

A PDA is a septuple (Q, Σ, Γ, q0, Z0, A, δ): Q – is a finite set of

states Σ – is the input alphabet Γ – is the stack alphabet q0 – is the start state Z0 – is the initial stack

symbol, Z0 Γ A – set of accepting

states, A Q δ – is the transition

function

How to define the transition function: δ: Q(Σ {ε})Γ

QΓ*

Input: Current state Viewed symbol (or ε) Top symbol on the

stack

Output: New state Symbols to replace

the current stack top (if ε then we pop a single element from the stack)

Page 14: Introduction to CS Theory

Example

Goal: PDA for the language

{aibi | i ≥ 0 }

But before… How to describe a

configuration of a PDA? (so we can trace a PDA’s execution)

(q, x, α) – a configuration q – current state x – unprocessed input α – contents of the

stack (written right to left)

A single step of a PDA (p, x, α) (q, y, β) We write

(p, x, α) (q, y, β)

if the PDA can go from the left one to the right one in one step

How to formally define acceptance by a PDA?

Give a PDA for our goal language!

Page 15: Introduction to CS Theory

Deterministic PDAs

Determinism in PDAs By default: Nondeterministic We can get deterministic machines!

What does it mean for a PDA to be deterministic?

Give a DPDA (deterministic PDA) for our goal language

Show that L(G), for G: S SS | (S) | εis in DCFL (class of CFL languages accepted by DPDAs)

Page 16: Introduction to CS Theory

Equivalence of PDAs and CFLs

Theorem Let G be a context-free grammar. Then there is a PDA M such that L(M) = L(G)

Theorem Let M be a PDA. Then there is a context-free

grammar G such that L(G) = L(M)

Comments: PDA CFG – seems hard CFG PDA – quite intuitive! Try it!

Page 17: Introduction to CS Theory

Example

Convert G: S S + T | T T T + F | F F (S) | a

to a PDA!