E N D
Name : Bayazid Hasan Tamim • ID: 171-35-212 • Dep: software Engineering • Course Name: Theory of computing
Context –free grammar : Any language that can be generated by some String is called a Context –free grammar . A context free grammar is a 4 tuple (V,∑,R,S), where 1)V is a finite set called the variable 2) ∑ is a finite set, disjoint from v, called the terminals 3)R is a finite set of rules, with each rule being a variable and a string of variables and terminals 4)S € V is the start variable.
Ambiguity of Grammar A CFG is said to ambiguous if there exists more than one derivation tree for the given input string i.e., more than one LeftMostDerivation Tree (LMDT) or RightMostDerivation Tree (RMDT). a strategy is followed that deterministically determines the next nonterminal to rewrite: 1)in a leftmost derivation, it is always the leftmost nonterminal; 2)in a rightmost derivation, it is always the rightmost nonterminal.
p + p | p × p |(p )| a The string is a+a×ahas two left most derivation tree Left Most derivation Left Most derivation p p× p p + p × p [ p p+ p] a + p × p [ p a] a + a × p [ p a] a+ a × a [ p a] p p + p a + p × p [ p a] & [ p p × p] a + a × p [ p a] a+ a × a [ p a]
For example ,Consider grammar G1 p + p | p × p |(p )| a The string is a+a×a p p p p p p p p p p + a a a × a a a + ×
For example ,Consider grammar G1 p + p | p × p |(p )| a The string is a+a×a p p p p p p × +
For example ,Consider grammar G1 p + p | p × p |(p )| a The string is a+a×a p p p p p p p p p p + × a + ×
For example ,Consider grammar G1 p + p | p × p |(p )| a The string is a+a×a p p p p p p p p p p + a × a a + ×
For example ,Consider grammar G1 p + p | p × p |(p )| a The string is a+a×a p p p p p p p p p p + a a × a a a + ×
For example ,Consider grammar G1 p + p | p × p |(p )| a The string is a+a×a p p p p p p p p p p + a a a × a a a + ×
Example2 E E+E | E-E |X | Y | Z String is X+Y-Z for the Right most derivation tree Right most derivation Right most derivation E E E E-E E-z E+E-z E+y-z x+y-z E E+E E+E-E E+E-z E+y-z x+y-z E E E E E E E E + + - - x Z y z x y
Example2 E E+E | E-E |X | Y | Z String is X+Y-Z for the Right most derivation tree Right most derivation Right most derivation E E E E-E E E+E E E E E + -
Example2 E E+E | E-E |X | Y | Z String is X+Y-Z for the Right most derivation tree Right most derivation Right most derivation E E E E-E E-z E E+E E+E-E E E E E E E E E + + - - z
Example2 E E+E | E-E |X | Y | Z String is X+Y-Z for the Right most derivation tree Right most derivation Right most derivation E E E E-E E-z E+E-z E+y-z E E+E E+E-E E+E-z E E E E E E E E + + - - Z z y
Example2 E E+E | E-E |X | Y | Z String is X+Y-Z for the Right most derivation tree Right most derivation Right most derivation E E E E-E E-z E+E-z E+y-z x+y-z E E+E E+E-E E+E-z E+y-z E E E E E E E E + + - - Z y z x y
Example2 E E+E | E-E |X | Y | Z String is X+Y-Z for the Right most derivation tree Right most derivation Right most derivation E E E E-E E-z E+E-z E+y-z x+y-z E E+E E+E-E E+E-z E+y-z x+y-z E E E E E E E E + + - - x Z y z x y
Real Example of compiling design A particularly famous example of ambiguity in compilers happens in the if-then-else construction. The ambiguity happens because many languages allow the conditional clause without the "else" part. Lets consider a typical set of production rules that we can use to derive conditional statements: • <cmd> ::= if <bool> then <cmd> | • if<bool> then <cmd> else <cmd> <cmd> <cmd> if (a > b) then if (c > d) then print(1) else print(2) If <bool> then <cmd> else <cmd> If <bool> then <cmd> If <bool> then <cmd> If <bool> then <cmd> else <cmd> We demonstrate this ambiguity by producing two derivation trees for the same sentence
For instance, with the grammar: String (1) S → S + S (2) S → 1 (3) S → a 1 + 1 + a Left most derivation tree S → S + S (1) → S + S + S (1) → 1 + S + S (2) → 1 + 1 + S (2) → 1 + 1 + a (3) S → S + S (1) → 1 + S (2) → 1 + S + S (1) → 1 + 1 + S (2) → 1 + 1 + a (3)
References 1)https://cs.stackexchange.com/questions/54814/different-between-left-most-and-right-most-derivation 2)https://en.wikipedia.org/wiki/Context-free_grammar 3) https://www.geeksforgeeks.org/parsing-set-1-introduction-ambiguity-and-parsers/ 4) Theory of computing Book