Context-Free Grammars 2IT70 Finite Automata and Process Theory Technische Universiteit Eindhoven Quartile2, 2014-2015
Generating strings language L 1 èa n b n Ë n 0 í ab L 1 if w L 1 then awb L 1 2 IT70 (2014) Context-Free Grammars 2/ 13
Generating strings language L 1 èa n b n Ë n 0 í ab L 1 if w L 1 then awb L 1 production rules S ab and S asb 2 IT70 (2014) Context-Free Grammars 2/ 13
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 01w L 2 production rules S ab and S asb 2 IT70 (2014) Context-Free Grammars 2/ 13
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 01w L 2 production rules S ab and S asb production rules S ε and S 01S 2 IT70 (2014) Context-Free Grammars 2/ 13
Variables, terminals, production rules, start symbol palindromes over èa, bí S ε S a S b S asa S bsb binary integer expressions E I E N E E E E E E E E I a I I0 I I1 N 1 N N0 N N1 2 IT70 (2014) Context-Free Grammars 3/ 13
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 I0 I I1 N 1 N N0 N N1 alternative notation E I Ë N Ë E E Ë E E Ë E I aëi0 Ë I1 N 1ËN0 Ë N1 2 IT70 (2014) Context-Free Grammars 3/ 13
Question Consider again the grammar given by E I Ë N Ë E E Ë E E Ë E I aëi0 Ë I1 N 1ËN0 Ë N1 How many of the strings aa1, a01, 011, 11a, a01 a01, a11 a10, 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
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 2 IT70 (2014) Context-Free Grammars 5/ 13
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 Ä G A èw T Ë A G w í language of the grammar Ä G Ä G S 2 IT70 (2014) Context-Free Grammars 5/ 13
More examples expression term Ë expression + term term factor Ë term factor factor identifier Ë expression identifier a Ë b Ëc Ë... 2 IT70 (2014) Context-Free Grammars 6/ 13
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
Combining and splitting productions lemma CFG G V, T, R, S if X 1 n1 G γ 1,...,X k nk G γ k then X 1 X k ng γ 1 γ k where n n 1 n k if X 1 X k ng γ then X 1 n1 G γ 1,...,X k nk 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
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
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
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 Ä G L proof induction on n: if S ng w then w L, thus Ä G L induction on n: if w a n b n then w Ä G, thus L Ä G Ù 2 IT70 (2014) Context-Free Grammars 10/ 13
Avoiding the inductive proofs lemma CFGs G 1 V 1, T 1, R 1, S 1 and G 2 V 2, T 2, R 2, S 2 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 Ä G Ä G 1 Ä G 2 if R ès S 1 S 2 í R 1 R 2 then Ä G Ä G 1 Ä G 2 if R ès εës 1 Sí R 1 then Ä G Ä G 1 2 IT70 (2014) Context-Free Grammars 11/ 13
Avoiding the inductive proofs (cont.) CFG G with production rules S S 1 Ë S 2 S 1 ab B εëbb S 2 ba A εëaa then Ä G èab n, ba m Ë n,m 0 í proof use the lemma Ä G A èa m Ëm 0 í and Ä G B èb n Ën 0 í Ä G S 1 èaí èb n Ën 0 í and Ä G S 2 èbí èa m Ëm 0 í Ä G èab n Ë n 0 í èba m Ë m 0 í Ù 2 IT70 (2014) Context-Free Grammars 12/ 13
Context-free languages language L is context-free if L Ä G for CFG G èa n b n Ën 0 í and èww R Ë w è0,1í í are context-free theorem if L is regular then L is context-free proof for DFA D Q, Σ, δ, q 0, F put G Q, Σ, R, q 0 where R èq aq ì Ë δ q,a q ì í èq εëq F í then L Ä G Ù 2 IT70 (2014) Context-Free Grammars 13/ 13