About Grammars Hopcroft Motawi Ullman Chap 7 1
About Grammars Hopcroft, Motawi, Ullman, Chap 7. 1, 6. 3, 5. 4
About grammars n n n Normal forms for grammars Equivalence between CFGs and PDAs Grammar Ambiguity
Grammar Productions n n Formal definition of a grammar provides much leeway Productions can be simplified or restricted to make proofs about CFGs simpler
Simplifications n Removing useless symbols n n Removing є-productions n n A є Removing unit productions n n Those that cannot be derived from S and those that cannot reduce to a terminal string A B Normal forms e. g. , Chomsky Normal Form
Useless symbols n n We want to ensure all productions in the grammar have no useless symbols, i. e. , all symbols are generating and reachable Generating symbols n n All variables that could eventually derive a string of terminals; i. e. , all A in V, such that there exists a string w of terminals where A * w Reachable symbols n All variables that can be reached from the start symbol; i. e. , all A in V, such that S * u. Aw, for some u and w
Removing useless productions n Remove productions with non-generating symbols n n Requires identifying generating symbols recursively: right hand side of production contains only terminals and generating symbols Remove productions with non-reachable symbols n Requires identifying reachable symbols recursively: S is reachable, and so are symbols that exist on the right hand side of productions with reachable symbols on the left hand side
Epsilon Productions n n n є-productions: productions of the form A є Nullable symbols: symbols A where A є or A B 1 B 2…Bn such that each Bi is nullable For each production that has a nullable symbol on the right hand side, add a production without that symbol; apply rule iteratively on resulting productions n n After this step, all є-productions can be removed Note, if the language L generated by the original grammar includes є, then the language generated by the resulting grammar will be L – {є}
Unit Productions n n Unit productions: all productions of the form A B Removing unit productions n n n Identify unit pairs: pairs of variables (A, B) such that A * B, and the derivation involves only unit productions For each unit pair (A, B), add the production A w, whenever B w and w is not a variable Unit productions may now be removed
Chomsky Normal Form n CNF: all productions are of the form n n n A BC A a (B, C are variables) (a is a terminal) How do we convert a grammar to an equivalent CNF grammar?
Greibach Normal Form n GNF: all productions are of the form n n A a. B 1 B 2…Bn Note that A a is allowed Note that if the grammar is GNF, each step in a derivation of a string adds a terminal How do we convert a grammar to an equivalent GNF grammar?
Equivalence between CFGs and PDAs n Converting CFGs to PDAs n n Easier to use PDA version that accepts by empty stack Given a context free grammar G = (V, T, P, S), construct a pushdown automaton M n Need to specify states, input and stack symbols and the transition function
CFG to PDA n M = (Q, , q 0, Z 0), where n n n Q contains a single state, q 0 =T = {V T} Z 0 = S Note: no need for F (final states) since we are accepting by empty stack And is …
CFG to PDA n Transition function is based on the variables, productions and terminals of the grammar: n n n (q 0 , є , A) = (q 0, w) whenever A w (q 0 , a) = (q 0, є ) for each a in T Easier and more intuitive if the grammar is of GNF n (q 0 , a , A) = (q 0, B 1 B 2…Bn) for each production A a. B 1 B 2…Bn
PDA to CFG n n n More elaborate construction Variables in the resulting grammar dependent on states and stack symbols Read section 6. 3
- Slides: 14