1 / 35

Grammars and ambiguity

Grammars and ambiguity. CS164 3:30-5:00 TT 10 Evans. Overview. derivations and parse trees different derivations produce may produce same parse tree ambiguous grammars what they are and how to fix them. Recall: derivations and parse trees. A derivation is a sequence of productions

silver
Download Presentation

Grammars and ambiguity

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. Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans Prof. Bodik CS 164 Lecture 8

  2. Overview • derivations and parse trees • different derivations produce may produce same parse tree • ambiguous grammars • what they are • and how to fix them Prof. Bodik CS 164 Lecture 8

  3. Recall: derivations and parse trees A derivation is a sequence of productions S  …  … A derivation can be drawn as a parse tree • Start symbol is the tree’s root • For a production X  Y1 … Yn add children Y1, …, Yn to node X You need parse trees to build ASTs Prof. Bodik CS 164 Lecture 8

  4. Derivation Example • Grammar • String Prof. Bodik CS 164 Lecture 8

  5. Derivation Example (Cont.) E E + E E * E id id id Prof. Bodik CS 164 Lecture 8

  6. Derivation in Detail (1) E Prof. Bodik CS 164 Lecture 8

  7. Derivation in Detail (2) E E + E Prof. Bodik CS 164 Lecture 8

  8. Derivation in Detail (3) E E + E E * E Prof. Bodik CS 164 Lecture 8

  9. Derivation in Detail (4) E E + E E * E id Prof. Bodik CS 164 Lecture 8

  10. Derivation in Detail (5) E E + E E * E id id Prof. Bodik CS 164 Lecture 8

  11. Derivation in Detail (6) E E + E E * E id id id Prof. Bodik CS 164 Lecture 8

  12. Notes on Derivations • A parse tree has • Terminals at the leaves • Non-terminals at the interior nodes • An in-order traversal of the leaves is the original input • The parse tree shows the association of operations, the input string does not Prof. Bodik CS 164 Lecture 8

  13. Left-most and Right-most Derivations • The example is a left-most derivation • At each step, replace the left-most non-terminal • There is an equivalent notion of a right-most derivation Prof. Bodik CS 164 Lecture 8

  14. Right-most Derivation in Detail (1) E Prof. Bodik CS 164 Lecture 8

  15. Right-most Derivation in Detail (2) E E + E Prof. Bodik CS 164 Lecture 8

  16. Right-most Derivation in Detail (3) E E + E id Prof. Bodik CS 164 Lecture 8

  17. Right-most Derivation in Detail (4) E E + E E * E id Prof. Bodik CS 164 Lecture 8

  18. Right-most Derivation in Detail (5) E E + E E * E id id Prof. Bodik CS 164 Lecture 8

  19. Right-most Derivation in Detail (6) E E + E E * E id id id Prof. Bodik CS 164 Lecture 8

  20. Derivations and Parse Trees • Note that right-most and left-most derivations have the same parse tree • The difference is only in the order in which branches are added Prof. Bodik CS 164 Lecture 8

  21. ambiguity

  22. Ambiguity • Grammar E  E + E | E * E | ( E ) | int • Strings int + int + int int * int + int Prof. Bodik CS 164 Lecture 8

  23. Ambiguity. Example This string has two parse trees E E E + E E E + E E int int E + E + int int int int + is left-associative Prof. Bodik CS 164 Lecture 8

  24. Ambiguity. Example This string has two parse trees E E E + E E E * E E int int E + E * int int int int * has higher precedence than + Prof. Bodik CS 164 Lecture 8

  25. Ambiguity (Cont.) • A grammar is ambiguous if it has more than one parse tree for some string • Equivalently, there is more than one right-most or left-most derivation for some string • Ambiguity is bad • Leaves meaning of some programs ill-defined • Ambiguity is common in programming languages • Arithmetic expressions • IF-THEN-ELSE Prof. Bodik CS 164 Lecture 8

  26. Dealing with Ambiguity • There are several ways to handle ambiguity • Most direct method is to rewrite the grammar unambiguously E  E + T | T T  T * int | int | ( E ) • Enforces precedence of * over + • Enforces left-associativity of + and * Prof. Bodik CS 164 Lecture 8

  27. Ambiguity. Example The int * int + int has ony one parse tree now E E E + T E E * int int E + E T int int T int * int Prof. Bodik CS 164 Lecture 8

  28. Ambiguity: The Dangling Else • Consider the grammar S  if E then S | if E then S else S | OTHER • This grammar is also ambiguous Prof. Bodik CS 164 Lecture 8

  29. if if E1 if E1 S4 if E2 S3 E2 S3 S4 The Dangling Else: Example • The expression if E1 then if E2 then S3 else S4 has two parse trees • Typically we want the second form Prof. Bodik CS 164 Lecture 8

  30. The Dangling Else: A Fix • else matches the closest unmatched then • We can describe this in the grammar (distinguish between matched and unmatched “then”) S  MIF /* all then are matched */ | UIF /* some then are unmatched */ MIF  if E then MIF else MIF | OTHER UIF  if E then S | if E then MIF else UIF • Describes the same set of strings Prof. Bodik CS 164 Lecture 8

  31. if if E1 if E1 S4 if E2 S3 E2 S3 S4 The Dangling Else: Example Revisited • The expression if E1 then if E2 then S3 else S4 • A valid parse tree (for a UIF) • Not valid because the then expression is not a MIF Prof. Bodik CS 164 Lecture 8

  32. Ambiguity • No general techniques for handling ambiguity • Impossible to convert automatically an ambiguous grammar to an unambiguous one • Used with care, ambiguity can simplify the grammar • Sometimes allows more natural definitions • We need disambiguation mechanisms Prof. Bodik CS 164 Lecture 8

  33. Precedence and Associativity Declarations • Instead of rewriting the grammar • Use the more natural (ambiguous) grammar • Along with disambiguating declarations • LR (bottom-up) parsers allow precedence and associativity declarations to disambiguate grammars • Examples … Prof. Bodik CS 164 Lecture 8

  34. E E E + E E E + E E int int E + E + int int int int Associativity Declarations • Consider the grammar E E + E | int • Ambiguous: two parse trees of int + int + int • Left-associativity declaration: %left + Prof. Bodik CS 164 Lecture 8

  35. E E E * E E E + E E int int E * E + int int int int Precedence Declarations • Consider the grammar E E + E | E * E | int • And the string int + int * int • Precedence declarations: %left + • %left * Prof. Bodik CS 164 Lecture 8

More Related