Download presentation
Presentation is loading. Please wait.
1
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans
2
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
3
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 Y 1 … Y n add children Y 1, …, Y n to node X You need parse trees to build ASTs
4
Prof. Bodik CS 164 Lecture 8 4 Derivation Example Grammar String
5
Prof. Bodik CS 164 Lecture 8 5 Derivation Example (Cont.) E E EE E+ id*
6
Prof. Bodik CS 164 Lecture 8 6 Derivation in Detail (1) E
7
Prof. Bodik CS 164 Lecture 8 7 Derivation in Detail (2) E EE+
8
Prof. Bodik CS 164 Lecture 8 8 Derivation in Detail (3) E E EE E+ *
9
Prof. Bodik CS 164 Lecture 8 9 Derivation in Detail (4) E E EE E+ * id
10
Prof. Bodik CS 164 Lecture 8 10 Derivation in Detail (5) E E EE E+ * id
11
Prof. Bodik CS 164 Lecture 8 11 Derivation in Detail (6) E E EE E+ id*
12
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
13
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
14
Prof. Bodik CS 164 Lecture 8 14 Right-most Derivation in Detail (1) E
15
Prof. Bodik CS 164 Lecture 8 15 Right-most Derivation in Detail (2) E EE+
16
Prof. Bodik CS 164 Lecture 8 16 Right-most Derivation in Detail (3) E EE+ id
17
Prof. Bodik CS 164 Lecture 8 17 Right-most Derivation in Detail (4) E E EE E+ id*
18
Prof. Bodik CS 164 Lecture 8 18 Right-most Derivation in Detail (5) E E EE E+ id*
19
Prof. Bodik CS 164 Lecture 8 19 Right-most Derivation in Detail (6) E E EE E+ id*
20
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
21
ambiguity
22
Prof. Bodik CS 164 Lecture 8 22 Ambiguity Grammar E E + E | E * E | ( E ) | int Strings int + int + int int * int + int
23
Prof. Bodik CS 164 Lecture 8 23 Ambiguity. Example This string has two parse trees E E EE E + int+ E E EE E+ + + is left-associative
24
Prof. Bodik CS 164 Lecture 8 24 Ambiguity. Example This string has two parse trees E E EE E * int+ E E EE E+ * * has higher precedence than +
25
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
26
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 *
27
Prof. Bodik CS 164 Lecture 8 27 Ambiguity. Example The int * int + int has ony one parse tree now E E EE E * int+ E T T T+ * E
28
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
29
Prof. Bodik CS 164 Lecture 8 29 The Dangling Else: Example The expression if E 1 then if E 2 then S 3 else S 4 has two parse trees if E1E1 E2E2 S3S3 S4S4 E1E1 E2E2 S3S3 S4S4 Typically we want the second form
30
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
31
Prof. Bodik CS 164 Lecture 8 31 The Dangling Else: Example Revisited The expression if E 1 then if E 2 then S 3 else S 4 if E1E1 E2E2 S3S3 S4S4 E1E1 E2E2 S3S3 S4S4 Not valid because the then expression is not a MIF A valid parse tree (for a UIF)
32
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
33
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 …
34
Prof. Bodik CS 164 Lecture 8 34 Associativity Declarations Consider the grammar E E + E | int Ambiguous: two parse trees of int + int + int E E EE E + int+ E E EE E+ + Left-associativity declaration: %left +
35
Prof. Bodik CS 164 Lecture 8 35 Precedence Declarations Consider the grammar E E + E | E * E | int –And the string int + int * int E E EE E + int* E E EE E* + Precedence declarations: %left + %left *
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.