1
play

1 CS Master Introduction to the Theory of Computation CS Master - PDF document

CS Master Introduction to the Theory of Computation CS Master Introduction to the Theory of Computation Outline (Sipser 2.1 2.3) Lecture 4 1. Motivating example 2. CFG Definition Context-free grammars 3. Parse trees, ambiguity


  1. CS Master – Introduction to the Theory of Computation CS Master – Introduction to the Theory of Computation Outline (Sipser 2.1 – 2.3) Lecture 4 1. Motivating example 2. CFG Definition Context-free grammars 3. Parse trees, ambiguity 4. Push-down automata 5. Equivalence CFG – PDA Jan Maluszynski, IDA, 2007 6. Pumping lemma for CFG http://www.ida.liu.se/~janma janma @ ida.liu.se Jan Maluszynski - HT 2007 4.1 Jan Maluszynski - HT 2007 4.2 CS Master – Introduction to the Theory of Computation CS Master – Introduction to the Theory of Computation Motivating example Context Free Grammars A CFG : (V, Σ , R, S) A → (A) • V a finite set of nonterminals (variables) A → ε • Σ a finite set of terminals , disjoint with V • R a finite set of rules of the form Gramatical rules used for rewriting: X → w A ⇒ (A) ⇒ () where X ∈ V and w ∈ (V+ Σ )* A ⇒ (A) ⇒ ((A)) ⇒ (((A))) ⇒ ((())) • S ∈ V is a start nonterminal L(A) : all terminal strings derivable L(A) is not regular! Jan Maluszynski - HT 2007 4.3 Jan Maluszynski - HT 2007 4.4 CS Master – Introduction to the Theory of Computation CS Master – Introduction to the Theory of Computation Context-Free Languages Ambiguity A CFG G: (V, Σ , R, S) E → E+E L(G) denotes the language of G: E → E#E the set of all terminal strings derivable E → (E) in G from V E → a A language is a context-free language iff it is the language of a CFG Jan Maluszynski - HT 2007 4.5 Jan Maluszynski - HT 2007 4.6 1

  2. CS Master – Introduction to the Theory of Computation CS Master – Introduction to the Theory of Computation Chomsky Normal Form Cocke-Younger- Kasami parsing G is in CNF if every rule is of the form G CNF grammar w string A → BC or A → a w = s 1 s 2 …..s n In addition, there may be the rule CYK checks if w is in L(G) S → ε by constructing a table T[i,j], 1 ≤ i ≤ j ≤ | w | Where S is the start nonterminal where: T[i,j] = { X | X => s i s i+1 …..s i+j } Every CFL is generated by a CNF grammar. Thus w is in L(G) iff S is in T[1,|w|] An application: the CYK parsing algorithm Jan Maluszynski - HT 2007 4.7 Jan Maluszynski - HT 2007 4.8 CS Master – Introduction to the Theory of Computation CS Master – Introduction to the Theory of Computation CYK table CYK (transposed) table example S → AB | BC A → BA | a B → CC | b Construct T[i,1] for i = 1,…, |w| C → AB | a X → s i X is in T[i,1] iff b a a b a i = 1 2 3 4 5 substrings: Having T[i,j] and T[i+j,k] construct T[i,j+k]: 1 B A,C A,C B A,C length 1 Y is in T[i,j+k] iff 2 S,A B S,C S,A length 2 for some X in T[i,j] , Z in T[i+j,k] 3 - B B length 3 Y → X Z 4 - S,A,C length 4 5 S,A,C length 5 e.g. B ∈ T[2,3] since B → CC, C ∈ T[2,1] C ∈ T[2+1,2] Jan Maluszynski - HT 2007 4.9 Jan Maluszynski - HT 2007 4.10 CS Master – Introduction to the Theory of Computation CS Master – Introduction to the Theory of Computation Pushdown Automaton (Nondeterministic) Pushdown Automaton FA extended with a stack (Q, Σ , Γ , δ , q 0 ,F) Top stack symbol is an argument of a transition Q states can be accessed and replaced by a new one Σ Input alphabet Γ Stack alphabet δ : (Q × ( Σ∪ { ε }) × ( Γ∪ { ε })) → P (Q × ( Γ∪ { ε }) q 0 ∈ Q initial state F ⊆ Q final states Jan Maluszynski - HT 2007 4.11 Jan Maluszynski - HT 2007 4.12 2

  3. CS Master – Introduction to the Theory of Computation CS Master – Introduction to the Theory of Computation PDA vs. CFG’s Regular Languages are Context-free Theorem: A language is context-free iff it is Every Regular Language is Context-free: recognized by a pushdown automaton. (p.117) FA is a special case of PDA CFG → PDA : Regular grammar : Idea: simulate derivations on the stack Each rule of the form: (see example) A → aB, A → a or A → ε PDA → CFG Idea: Nonterminals X pq derive all strings that bring A CF language L is regular iff there exists a PDA from p to q starting and ending with empty regular CFG G such that L = L(G). stack (details see pp121-124). Jan Maluszynski - HT 2007 4.13 Jan Maluszynski - HT 2007 4.14 CS Master – Introduction to the Theory of Computation CS Master – Introduction to the Theory of Computation Pumping lemma for CFL 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 uv i xy i z ∈ L • |vy|>0 a + a # a • |vxy| ≤ p a+…a+ a # a…# a Used for proving that a language is not a CFL Jan Maluszynski - HT 2007 4.15 Jan Maluszynski - HT 2007 4.16 3

Recommend


More recommend