context free grammars 2it70 finite automata and process
play

Context-Free Grammars 2IT70 Finite Automata and Process Theory - PowerPoint PPT Presentation

Context-Free Grammars 2IT70 Finite Automata and Process Theory Technische Universiteit Eindhoven May 21, 2014 Generating strings language L 1 = { a n b n n > 0 } language L 2 = ( 01 ) ab L 1 L 2 if w L 1 then awb L


  1. Context-Free Grammars 2IT70 Finite Automata and Process Theory Technische Universiteit Eindhoven May 21, 2014

  2. Generating strings language L 1 = { a n b n ∣ n > 0 } language L 2 = ( 01 ) ∗ ab ∈ L 1 ε ∈ L 2 if w ∈ L 1 then awb ∈ L 1 if w ∈ L 2 then 01 w ∈ L 2 production rules production rules S → ab and S → aSb S → ε and S → 01 S 2 IT70 (2014) Context-Free Grammars 2 / 13

  3. Variables, terminals, production rules, start symbol palindromes over { a , b } S → ε S → a S → b S → aSa S → bSb alternative notation S → ε ∣ a ∣ b ∣ aSa ∣ bSb binary integer expressions E → I E → N E → E + E E → E ∗ E E → ( E ) I → a I → I 0 I → I 1 N → 1 N → N 0 N → N 1 alternative notation E → I ∣ N ∣ E + E ∣ E ∗ E ∣ ( E ) I → a ∣ I 0 ∣ I 1 N → 1 ∣ N 0 ∣ N 1 2 IT70 (2014) Context-Free Grammars 3 / 13

  4. Clicker questions L81 Consider again the grammar given by E → I ∣ N ∣ E + E ∣ E ∗ E ∣ ( E ) I → a ∣ I 0 ∣ I 1 N → 1 ∣ N 0 ∣ N 1 How many of the strings aa 1, a 01, 011, 11 a , a 01 + a 01, a 11 ∗ a 10, + 101, ( 110 ) cannot be generated by the grammar, you expect? A. Two strings B. Three strings C. Four strings D. Six strings E. Can’t tell 2 IT70 (2014) Context-Free Grammars 4 / 13

  5. Language of a CFG context-free grammar G = ( V , T , R , S ) V variables and T terminals R ⊆ V ×( V ∪ T ) ∗ production rules A → α S ∈ V start symbol productions ⇒ G ⊆ ( V ∪ T ) × ( V ∪ T ) γ ⇒ G γ ′ if γ = β 1 A β 2 , A → α rule of G , γ ′ = β 1 αβ 2 production sequences γ 0 ⇒ G γ 1 ⇒ G ⋯ ⇒ G γ n language of a variable L G ( A ) = { w ∈ T ∗ ∣ A ⇒ ∗ G w } language of the grammar L( G ) = L G ( S ) 2 IT70 (2014) Context-Free Grammars 5 / 13

  6. More examples ⟨ expression ⟩ ∶∶= ⟨ term ⟩ ∣ ⟨ expression ⟩ + ⟨ term ⟩ ⟨ term ⟩ ∶∶= ⟨ factor ⟩ ∣ ⟨ term ⟩∗ ⟨ factor ⟩ ⟨ factor ⟩ ∶∶= ⟨ identifier ⟩ ∣ (⟨ expression ⟩) ⟨ identifier ⟩ ∶∶= a ∣ b ∣ c ∣ ... ⟨ char ⟩ ∶∶= a ∣ ... ∣ z ∣ A ∣ ... ∣ Z ∣ ... ⟨ text ⟩ ∶∶= ε ∣ ⟨ char ⟩⟨ text ⟩ ⟨ doc ⟩ ∶∶= ε ∣ ⟨ element ⟩⟨ doc ⟩ ⟨ element ⟩ ∶∶= ⟨ text ⟩ ∣ <EM> ⟨ doc ⟩ </EM> ∣ <P> ⟨ doc ⟩ ∣ <OL> ⟨ list ⟩ </OL> ⟨ listitem ⟩ ∶∶= <LI> ⟨ doc ⟩ ⟨ list ⟩ ∶∶= ε ∣ ⟨ listitem ⟩⟨ doc ⟩ 2 IT70 (2014) Context-Free Grammars 6 / 13

  7. Combining and splitting productions CFG G = ( V , T , R , S ) lemma if X 1 ⇒ n 1 G γ 1 ,... , X k ⇒ n k G γ k then X 1 ⋯ X k ⇒ n G γ 1 ⋯ γ k where n = n 1 + ⋯ + n k if X 1 ⋯ X k ⇒ n G γ then X 1 ⇒ n 1 G γ 1 ,... , X k ⇒ n k G γ k where n = n 1 + ⋯ + n k and γ = γ 1 ... γ k X 1 ,... , X k ∈ ( V ∪ T ) 2 IT70 (2014) Context-Free Grammars 7 / 13

  8. The parentheses language L () CFG S → ε ∣ SS ∣ ( S ) several production sequences for string ()(()) S ⇒ G SS ⇒ G S ( S ) ⇒ G S (( S )) ⇒ G S (()) ⇒ G ( S )(()) ⇒ G ()(()) S ⇒ G SS ⇒ G ( S ) S ⇒ G () S ⇒ G ()( S ) ⇒ G ()(( S )) ⇒ G ()(()) S ⇒ G SS ⇒ G ( S ) S ⇒ G ( S )( S ) ⇒ G ()( S ) ⇒ G ()(( S )) ⇒ G ()(()) leftmost, rightmost, mixed production sequence 2 IT70 (2014) Context-Free Grammars 8 / 13

  9. Clicker question L82 Given the CFG S → () ∣ ( S ) ∣ SS . How many production sequences are there for the string (())((())) ? A. ()(()) has 5 possible production sequences B. ()(()) has 6 possible production sequences C. ()(()) has 10 possible production sequences D. ()(()) has 12 possible production sequences E. Can’t tell 2 IT70 (2014) Context-Free Grammars 9 / 13

  10. Proving a grammar correct CFG G with production rules S → ab and S → aSb for L = { a n b n ∣ n ⩾ 1 } it holds that L( G ) = L proof induction on n : if S ⇒ n G w then w ∈ L , thus L( G ) ⊆ L induction on n : if w = a n b n then w ∈ L( G ) , thus L ⊆ L( G ) ⊠ 2 IT70 (2014) Context-Free Grammars 10 / 13

  11. Avoiding the inductive proofs CFGs G 1 = ( V 1 , T 1 , R 1 , S 1 ) and G 2 = ( V 2 , T 2 , R 2 , S 2 ) lemma moreover V 1 and V 2 disjoint define CFG G = ({ S } ∪ V 1 ∪ V 2 , T 1 ∪ T 2 , R , S ) if R = { S → S 1 ∣ S 2 } ∪ R 1 ∪ R 2 then L( G ) = L( G 1 ) ∪ L( G 2 ) if R = { S → S 1 S 2 } ∪ R 1 ∪ R 2 then L( G ) = L( G 1 ) ⋅ L( G 2 ) if R = { S → ε ∣ S 1 S } ∪ R 1 then L( G ) = L( G 1 ) ∗ 2 IT70 (2014) Context-Free Grammars 11 / 13

  12. Avoiding the inductive proofs (cont.) CFG G with production rules S → S 1 ∣ S 2 B → ε ∣ bB S 1 → aB S 2 → bA A → ε ∣ aA then L( G ) = { ab n , ba m ∣ n , m ⩾ 0 } use the lemma proof L G ( A ) = { a m ∣ m ⩾ 0 } and L G ( B ) = { b n ∣ n ⩾ 0 } L G ( S 1 ) = { a } ⋅ { b n ∣ n ⩾ 0 } and L G ( S 2 ) = { b } ⋅ { a m ∣ m ⩾ 0 } L( G ) = { ab n ∣ n ⩾ 0 } ∪ { ba m ∣ m ⩾ 0 } ⊠ 2 IT70 (2014) Context-Free Grammars 12 / 13

  13. Context-free languages language L is context-free if L = L( G ) for CFG G { a n b n ∣ n ⩾ 0 } and { w w R ∣ w ∈ { 0 , 1 } ∗ } are context-free if L is regular then L is context-free theorem for DFA D = ( Q , Σ , δ, q 0 , F ) put G = ( Q , Σ , R , q 0 ) proof where R = { q → aq ′ ∣ δ ( q , a ) = q ′ } ∪ { q → ε ∣ q ∈ F } then L = L( G ) ⊠ 2 IT70 (2014) Context-Free Grammars 13 / 13

Recommend


More recommend