1 / 19

Ambiguity of Grammar

Ambiguity of Grammar

Tamim12
Download Presentation

Ambiguity of Grammar

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Name : Bayazid Hasan Tamim • ID: 171-35-212 • Dep: software Engineering • Course Name: Theory of computing

  2. 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.

  3. 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.

  4. 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]

  5. 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 + ×

  6. For example ,Consider grammar G1 p + p | p × p |(p )| a The string is a+a×a p p p p p p × +

  7. 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 + ×

  8. 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 + ×

  9. 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 + ×

  10. 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 + ×

  11. 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

  12. 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 + -

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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)

  19. 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

More Related