Lecture 4 Context-free grammars

16
Jan Maluszynski - HT 2007 4.1 CS Master – Introduction to the Theory of Computation Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007 http://www.ida.liu.se/~janma janma @ ida.liu.se

description

Lecture 4 Context-free grammars. Jan Maluszynski , IDA, 200 7 http://www.ida.liu.se/~janma janma @ ida.liu.se. Outline. (Sipser 2.1 – 2.3) Motivating example CFG Definition Parse trees, ambiguity Push-down automata Equivalence CFG – PDA Pumping lemma for CFG. Motivating example. - PowerPoint PPT Presentation

Transcript of Lecture 4 Context-free grammars

Page 1: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.1

CS Master – Introduction to the Theory of Computation

Lecture 4

Context-free grammars

Jan Maluszynski, IDA, 2007http://www.ida.liu.se/~janma

janma @ ida.liu.se

Page 2: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.2

CS Master – Introduction to the Theory of Computation

Outline

(Sipser 2.1 – 2.3)

1. Motivating example

2. CFG Definition

3. Parse trees, ambiguity

4. Push-down automata

5. Equivalence CFG – PDA

6. Pumping lemma for CFG

Page 3: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.3

CS Master – Introduction to the Theory of Computation

Motivating example

A (A)

A

Gramatical rules used for rewriting:

A (A) ()

A (A) ((A)) (((A))) ((()))

L(A) : all terminal strings derivable

L(A) is not regular!

Page 4: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.4

CS Master – Introduction to the Theory of Computation

Context Free Grammars

A CFG : (V, , R, S)•V a finite set of nonterminals (variables) a finite set of terminals, disjoint with V•R a finite set of rules of the formX w where XV and w (V+ )*•SV is a start nonterminal

Page 5: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.5

CS Master – Introduction to the Theory of Computation

Context-Free Languages

A CFG G: (V, , R, S)

L(G) denotes the language of G: the set of all terminal strings derivable in G from S

A language is a context-free language iff it is the language of a CFG

Page 6: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.6

CS Master – Introduction to the Theory of Computation

Ambiguity

E E+E

E E#E

E (E)

E a

Page 7: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.7

CS Master – Introduction to the Theory of Computation

Chomsky Normal Form

G is in CNF if every rule is of the form

A BC or A a

In addition, there may be the rule

S Where S is the start nonterminal

Every CFL is generated by a CNF grammar.

An application: the CYK parsing algorithm

Page 8: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.8

CS Master – Introduction to the Theory of Computation

Cocke-Younger- Kasami parsing

G CNF grammar w string

w = s1 s2 …..sn

CYK checks if w is in L(G)

by constructing a table T[i,j], 1i,j|w|

where: T[i,j] = { X | X => si si+1 …..si+j}

Thus w is in L(G) iff S is in T[1,|w|]

Page 9: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.9

CS Master – Introduction to the Theory of Computation

CYK table

Construct T[i,1] for i = 1,…, |w|

X is in T[i,1] iff X si

Having T[i,j] and T[i+j,k] construct T[i,j+k]:

Y is in T[i,j+k] iff

for some X in T[i,j] , Z in T[i+j,k]

Y X Z

Page 10: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.10

CS Master – Introduction to the Theory of Computation

CYK (transposed) table example

S AB | BC A BA | a B CC | b C AB | a b a a b ai = 1 2 3 4 5 substrings:1 B A,C A,C B A,C length 12 S,A B S,C S,A length 23 - B B length 34 - S,A,C length 45 S,A,C length 5e.g. BT[2,3] since BCC, CT[2,1] CT[2+1,2]

Page 11: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.11

CS Master – Introduction to the Theory of Computation

Pushdown Automaton

FA extended with a stack

Top stack symbol is an argument of a transition

can be accessed and replaced by a new one

Page 12: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.12

CS Master – Introduction to the Theory of Computation

(Nondeterministic) Pushdown Automaton

(Q, , , , q0 ,F)

Q states

Input alphabet Stack alphabet: (Q ({}) ({})) P(Q ({})q0Q initial state

FQ final states

Page 13: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.13

CS Master – Introduction to the Theory of Computation

PDA vs. CFG’s

Theorem: A language is context-free iff it is recognized by a pushdown automaton. (p.117)

CFG PDA :

Idea: simulate derivations on the stack

(see example)

PDA CFG

Idea: Nonterminals Xpq derive all strings that bring PDA from p to q starting and ending with empty stack (details see pp121-124).

Page 14: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.14

CS Master – Introduction to the Theory of Computation

Regular Languages are Context-free

Every Regular Language is Context-free:

FA is a special case of PDA

Regular grammar:

Each rule of the form:

A aB, A a or A

A CF language L is regular iff there exists a regular CFG G such that L = L(G).

Page 15: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.15

CS Master – Introduction to the Theory of Computation

Pumping lemma for CFL

a + a # a

a+…a+ a # a…# a

Page 16: Lecture 4 Context-free grammars

Jan Maluszynski - HT 2007 4.16

CS Master – Introduction to the Theory of Computation

Pumping Lemma for CFL

For any CFL L there is p such that

If s L and |s| p then

s=uvxyz for some u,v,x,y,z satisfying:• For each i 0 uvixyiz L• |vy|>0• |vxy| p

Used for proving that a language is not a CFL