Computation Theory M.S.C.:ZAINAB F.H. Ambiguity: A context-free grammar G is called ambiguous if some word has more than one leftmost derivation (equivalently: more than one derivation tree). Grammar with strings that have two or more distinct parse trees, are called ambiguous. Assigning a parse tree to a given string in the language is called parsing the string, it is an important first step towards understanding the structure of the string. Ambiguous grammars are of no help in parsing, since they assign no unique parse tree to each string in the language. In fact, there are context free languages with the property that all context free grammars that generate must be ambiguous. Such languages are called inherently ambiguous. An ambiguous grammar has more than one leftmost derivation and more than one rightmost derivation for a single sentential form. Example 1: Consider the sential form E+E*E, it has two derivation from E: 1. E ⇒E+E ⇒E+E*E 2. E⇒ E*E⇒ E+E*E Notice that in derivation (1), the second E is replaced by E*E, while in derivation (2), the first E is replaced by E+E. The figuare shows the two parse trees. In derivation(1), the second and third expressions are multiplied, and the result is added to the first expression, while derivation (2) adds the first two expressions and multiplies the result by the third. In more concrete terms, the first derivation suggests that 1+2*3 should be grouped 1+(2*3)=7, while the second derivation suggests the same expression should be grouped (1+2)*3=9. Consider the sentence ID+ID×ID. This can be parsed in two different ways. Babylon University-Uloom Collage for Women–Computer Science Department–Second Class..1 Computation Theory M.S.C.:ZAINAB F.H. Example 2: If G is the grammar S →SbS|a To prove that G is ambiguous, there are need to find a w ∈ L(G), which is ambiguous. Consider the word abababa. Exercise 1: Consider the grammar P={S→aS | aSbS | є } , construct the string aab with rightmost and leftmost derivation and draw the parse tree, explaining if this grammar ambiguous? Simplifying Context-Free Grammars: In order to construct good and efficient parsing algorithms, we consider now transformations of grammars, essentially simplifications of the form of productions, which lead to certain normal forms for grammars. These normal forms are better to deal with, and they allow more efficient parsing algorithms. Methods for Transforming Grammars . Substitution Rule Removing useless productions Removing λ-productions Removing unit productions Babylon University-Uloom Collage for Women–Computer Science Department–Second Class..2 Computation Theory M.S.C.:ZAINAB F.H. 1-Substitution Rule: If a grammar contains a production A → x1 B x2 with A, B V and A ≠ B and B → y1 | y2 | ... | yn (alternatives) then we can construct a new grammar G' with a modified set of productions P', in which we replace the rules for A and B above with one set of rules: A → x1 y1 x2 | x1 y2 x2 | ... | x1 yn x2 G and G' are equivalent, i.e. the generate the same language. Example 1: Aa|aaA|abBc, BabbA|b is equivalent to Aa|aaA|ababbAc|abbc, BabbA|b 2-Removing useless productions: Definition Useful Variable A variable A is called useful if there is at least one wL(G) such that S*xAy * w with x, y (V T)* Otherwise, it is called useless. A production is useless, if it involves a useless variable. Note that the concept of 'useless variable' includes the case that the variable does not lead to a terminal string, and the case that the variable cannot be reached from the start symbol. The elimination of useless variables and productions from a grammar (or the selection of those variables and productions, which are useful) proceeds in two phases: A. Determine and select those variables, which can lead to a terminal string, and subsequently determine and select the related productions. B. Determine and select those variables, which can be reached from the start symbol, and select related productions. Babylon University-Uloom Collage for Women–Computer Science Department–Second Class..3 Computation Theory M.S.C.:ZAINAB F.H. Algorithm - find useful variables and productions leading to wT* 1. Set V1 = ∅ 2. Repeat until closure: For every AV with a production A → x1 x2 ... xn and xi (V1 T) add A to V1. 3. Set P1 as the subset of productions from P, which contain only symbols from (V1 T). 4. This defines a new grammar G1 = (V1, T, S, P1) Algorithm - eliminate unreachable variables and related productions Draw a 'Dependency Graph' for G1, above. The dependency graph is a graph with vertices labelled with variables and arcs going from vertex A to a vertex B, if the grammar contains a production A → x 1 B x2 . Eliminate from G1 all variables and related productions, which cannot be reached from the start symbol. This gives the new grammar G', which does not contain useless variables or productions. Example 2: Case I: cannot generate a terminal string •SaSb|𝜆|A, AaA •Variable A is useless –Case II: cannot be reached from S •SA, AaA|𝜆, BbA •Variable B is useless Example 3: Eliminate useless symbols from the grammar with productions S B BC |AB, A a, C Step 1: Eliminate non-generating symbols V1 = {A, C, S} P1 = {S a, C Babylon University-Uloom Collage for Women–Computer Science Department–Second Class..4 Computation Theory M.S.C.:ZAINAB F.H. Step 2: Eliminate symbols that are non reachable Draw the dependency graph as given above. All Variables are reachable. So the final variables and productions are same V1 and P1. V2 = {A, C, S} P2 = {S A a, C Exercises: Eliminate useless symbols from the grammar 1. P= {S A Sb | bCC, C abb, E 2. P= {S aBa | BC, A C a, B d} 3. P= {S A B C 4. P= {S A B D E 3-Removing -productions : A production A → is called a -production. Definition Nullable Variable A variable A is called nullable, if there is a derivation A * If G is a CFG and λ∉L(G), then there exists an equivalent grammar G' without λ-productions. Proof by construction This is also an algorithm to eliminate λ-productions from a given grammar. First, we find all nullable variables of G, and put them into the set VN. 1. VN = ∅ 2. For all λ-production A → λ , add A to VN. 3. Repeat until closure: For all productions B → A1 A2 ... An and Ai VN for i = 1,...,n add B to VN. Babylon University-Uloom Collage for Women–Computer Science Department–Second Class..5 Computation Theory M.S.C.:ZAINAB F.H. 4. Determine P' based on the productions from P in the following way: For each production A → x1 x2 ... xm , m≥1, and xi (VT) we add to P' this production, as well as all productions, which we can construct from this production by substituting nullable variables xi with λ, in all possible combinations, which is equivalent to removing one or the other or all of those variables, i.e. for two nullable variable on the RHS, we remove one, remove the other, remove both, remove none. This leads to a bunch of productions, which we collect as P'. λ-productions A → λ will not be included in P'. This defines an equivalent grammar G' = (V, T, S, P'), i.e. L(G)=L(G'), without λ-productions. Example 4: SABaC, ABC, Bb|𝜆, CD|𝜆, Dd •Nullable set VN={A, B, C} Example 5: Find out the grammar without 𝜆 G = ({S, A, B, D}, {a}, {S - Productions A 𝜆, B 𝜆, D b}, S) Solution: Nullable variables = {S, A, B} New Set of productions: S S AB | A | B D G1= ({S, B, D}, {a}, {S aS | a | AB | A | B, D b}, S) Exercises: Eliminate 𝜆 - productions from the grammar 1. S 2. S 𝜆, Y 𝜆 Babylon University-Uloom Collage for Women–Computer Science Department–Second Class..6 Computation Theory 3. S M.S.C.:ZAINAB F.H. Zb, Y AB,W Z, A 𝜆, B Bb| 𝜆 4. S 𝜆, A SbS | A| bb Example 6: Eliminate 𝜆 - productions and useless symbols from the grammar S a |aA|B|C, A aB| 𝜆, B aA, C aCD, D ddd Step 1: Eliminate 𝜆 - productions Nullable ={A} P1={S a |aA | B | C, A aB, B aA|a, C D Step 2: Eliminating useless symbols Step 2a: Eliminate non-generating symbols Generating ={D, B, A, S} P2={S a | aA| B, A B D Step 2b: Eliminate non -reachable symbols D is non-reachable, eliminating D gets P3= {S a |aA|B, A B aA|a} 4-Removing Unit Productions : Definition Unit-Production A production of the form A → B with A, BV is called a unit-production. For any CFG G without λ-production, we can construct an equivalent grammar G' without unit-productions. 1. We set up the dependency graph - for unit-productions only - and determine all dependencies 2. Then, we determine a subset P' of P, which contains all non-unit productions of P. 3. This subset P' we extend in the following way: Babylon University-Uloom Collage for Women–Computer Science Department–Second Class..7 Computation Theory M.S.C.:ZAINAB F.H. For all A, B with B dependent on A as above, and productions for A in P': A → y1 | y2 |... | yn we add new productions to P': B → y1 | y2 |... | yn This way, you eliminate unit-productions by substituting the right-side of a unit-production - even if it goes over several derivation steps A ... B with all possible options for A's productions from productions in P', i.e. not unit-productions. You can imagine that this is the same as if you "inherit" the proper productions of A down to B. This does not change the language since B could have been generated by A without any additional terminals or variables around it (because A ... B), and thus it is legitimate to consider B in this sense as equivalent to A. Example 7: Simlify this grammar SAa|B, BA|bb, Aa|bc|B Remove unit productions SAa, Bbb, •Add Sbb|a|bc Abb Ba|bc Finally Sa|bc|bb|Aa Aa|bc|bb Ba|bc|bb Aa|bc Babylon University-Uloom Collage for Women–Computer Science Department–Second Class..8