CS314: FORMAL LANGUAGES AND AUTOMATA THEORY L. NADA ALZABEN Chapter 2: Context Free Grammar
Quick Note 2 Cs314pnu.wordpress.com
3 2.1 Context Free Grammar (CFG) Useful in App s that use recursion feature also it occurs in the specification and compilation of a programming language. Also used in the study of human languages Parsers in compilers extracts the meaning of a program before generating the compiled code. That is done when CFG is available. The collection of languages associated with the CFG is called Context Free Language (CFL). The machine that recognizes the CFL is called pushdown automata (PDA).
4 2.1 Context Free Grammar (CFG) E.g. G1: (Each part is called substitution rule or production.) Start variable A 0A1 A B B # Or can be used to simplify the production. Strings can be generated as follows: terminal
5 2.1 Context Free Grammar (CFG) The sequence of substitutions to obtain a string is called derivation. A derivation is: Parse tree:
6 2.1 Context Free Grammar (CFG) E.g. G2:
Formal definition of CFG 7 When we say A w as rule in the grammar this means uav is uwv. When we say u drives v, u v this means u=v
8 2.1 Context Free Grammar (CFG) E.g. G3: E.g. G4: define if the string is generated by the G4 grammar 1. a + a x a 2. ( a + a) x a
2.1 Context Free Grammar (CFG) 9
2.1 Context Free Grammar (CFG) 10 A compiler translates the code to another form to be executed. To do so the compiler extracts the meaning of the code by the Parsing process as we did in parse tree.
11 Designing CFG Designing CFG require creativity same as the finite automata. First method: is to break the CFL to smaller ones then combine them with operator. E.g. design the CFG for
Designing CFG 12 Second method: if the language is regular we can construct the CFG by first giving the DFA of the language Third method: if the CFG contains strings with two substrings that are linked which need to remember an unbounded amount of information about one of the substrings to verify that it corresponds to the other substrings. Fourth method: for complex languages with strings of many structures that are recursively appearing as part of other or the same structure (mostly happens in the grammar that generate arithmetic expression. )
13 Ambiguity When a grammar generates the same string in several different ways (different parse trees) we say the string is derived ambiguously in that grammar. We call that grammar ambiguous grammar. E.g. G5 test a + a x a
14 TEST YOUR SELF Use G2 to show the two different derivation of: Submit on Thursday's lecture
Ambiguity 15 Some languages can be generated by ambiguous grammars and also unambiguous grammar BUT for those who only are generated by the ambiguous grammar is called inherently ambiguous
CHOMSKY normal form 16 Simplifying the CFG is by using CHOMSKY normal form rules. Any CFL is generated by a CFG in the CHOMSKY normal form.
CHOMSKY normal form E.g. convert the G6 into CHOMSKY normal form: 17 Step1: make new start variable S 0 S S ASA ab A B S B b ᵋ S ASA ab A B S B b ᵋ Step2: Remove ᵋ S 0 S S ASA ab a A B S ᵋ B b ᵋ S 0 S S ASA ab a AS SA S A B S ᵋ B b Step3: Remove unit rule S 0 S ASA ab a AS SA S ASA ab a AS SA S A B S B b S 0 ASA ab a AS SA S ASA ab a AS SA A B S b ASA ab a AS SA B b
CHOMSKY normal form 18 E.g. convert the G6 into CHOMSKY normal form: S 0 ASA ab a AS SA S ASA ab a AS SA A b ASA ab a AS SA B b Step4: convert it to proper form S 0 AA1 UB a AS SA S AA1 UB a AS SA A b AA1 UB a AS SA B b A1 SA U a
2.2 Pushdown Automata (PDA) 19 PDA is a generalization of NFA. Read /write Head Stack (unbounded) NFA part (state diagram) Read Head W string Input tape (unbounded) PDA structure
2.2 Pushdown Automata (PDA) 20 PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached when the input tape and the stack are empty Reading ᵋ from input tape or stack means ignoring the reading part. Formal Definition of PDA
Formal Definition of PDA 21
2.2 Pushdown Automata (PDA) 22 E.g. the formal description for PDA machine recognizing the language {0 n 1 n n 0. Let M1=( Q,, Γ, δ, q1, F where: Q={q1,q2,q3,q4} input = 0,1, Γ={0,$} F={q1,q4}, Stack 0 $ Q1 Q2 Q3 Q4 0 1 ᵋ ᵋ {(q2,0)} δ is givin by the table: 0 $ {(q3,ᵋ)} {(q3,ᵋ)} ᵋ 0 $ {(q4,ᵋ)} ᵋ {(q2,$)}
2.2 Pushdown Automata (PDA) 23
2.2 Pushdown Automata (PDA) 24 E.g. M2 recognizes the language {a i b j c k i, j, k 0 and i = j or i = k
2.2 Pushdown Automata (PDA) 25