DOC PREVIEW
Berkeley COMPSCI 164 - Grammars and ambiguity

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Prof. Bodik CS 164 Lecture 8 1Grammars and ambiguity CS1643:30-5:00 TT10 EvansGGGooooooProf. Bodik CS 164 Lecture 82Overview• derivations and parse trees– different derivations produce may produce same parse tree• ambiguous grammars– what they are– and how to fix themGGGooooooProf. Bodik CS 164 Lecture 83Recall: derivations and parse treesAderivation is a sequence of productionsS G… G…A derivation can be drawn as a parse tree– Start symbol is the tree’s root– For a production X GY1… Ynadd children Y1, …, Ynto node XYou need parse trees to build ASTsooooooGGGProf. Bodik CS 164 Lecture 84Derivation Example• Grammar• StringE E+E | E E | (E) | idoid id + idoooooGGGoProf. Bodik CS 164 Lecture 85Derivation Example (Cont.)EE+EE E+Eid E + Eid id + Eid id + idoooooEEE EE+id*ididGGGooooooProf. Bodik CS 164 Lecture 86Derivation in Detail (1)EEProf. Bodik CS 164 Lecture 87Derivation in Detail (2)EE+EoEE E+oooooooooooooooProf. Bodik CS 164 Lecture 88Derivation in Detail (3)E EEE+EE +ooEEE EE+*oooooooooooooooProf. Bodik CS 164 Lecture 89Derivation in Detail (4)EE+EE E+Eid E + EoooEEEEE+*idoooooooooooooooProf. Bodik CS 164 Lecture 810Derivation in Detail (5)EE+EE E+Eid E + id id + EEooooEEEEE+*ididoooooooooooooooProf. Bodik CS 164 Lecture 811Derivation in Detail (6)EE+EE E+Eid E + Eid id + Eid id + idoooooEEE EE+id*ididoooooooooooooooProf. Bodik CS 164 Lecture 812Notes 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 notProf. Bodik CS 164 Lecture 813Left-most and Right-most Derivations• The example is a left-mostderivation– At each step, replace the left-most non-terminal• There is an equivalent notion of a right-most derivationEE+EE+idE E + idE id + idid id + idooooooooooooooooooooProf. Bodik CS 164 Lecture 814Right-most Derivation in Detail (1)EEoooooooooooooooProf. Bodik CS 164 Lecture 815Right-most Derivation in Detail (2)EE+EoEE E+oooooooooooooooProf. Bodik CS 164 Lecture 816Right-most Derivation in Detail (3)idEE+EE+ooEE E+idoooooooooooooooProf. Bodik CS 164 Lecture 817Right-most Derivation in Detail (4)EE+EE+idE E + idoooEEE EE+id*oooooooooooooooProf. Bodik CS 164 Lecture 818Right-most Derivation in Detail (5)EE+EE+idE E E+ idid + idooooEEE EE+id*idProf. Bodik CS 164 Lecture 819Right-most Derivation in Detail (6)EE+EE+idE E + idE id + idid id + idoooooEEEEE+id*ididGoooooProf. Bodik CS 164 Lecture 820Derivations 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 addedGoooooambiguityGoooooProf. Bodik CS 164 Lecture 822Ambiguity• GrammarE GE + E | E * E | ( E ) | int• Stringsint + int + intint * int + intoooooGProf. Bodik CS 164 Lecture 823Ambiguity. ExampleThis string has two parse treesEEE EE+int +intintEEE EE+int+intint+ is left-associativeoooooGProf. Bodik CS 164 Lecture 824Ambiguity. ExampleThis string has two parse treesEEE EE*int +intintEEE EE+int*intint* has higher precedence than +Prof. Bodik CS 164 Lecture 825Ambiguity (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-ELSEGGooooProf. Bodik CS 164 Lecture 826Dealing with Ambiguity• There are several ways to handle ambiguity• Most direct method is to rewrite the grammar unambiguouslyE GE + T | TT GT * int | int | ( E )• Enforces precedence of * over +• Enforces left-associativity of + and *ooooGGProf. Bodik CS 164 Lecture 827Ambiguity. ExampleThe int * int + int has ony one parse tree nowEEEEE*int +intintETTintT+int*EintooooGGProf. Bodik CS 164 Lecture 828Ambiguity: The Dangling Else• Consider the grammarS oif E then S| if E then S else S| OTHER• This grammar is also ambiguousoooGGoProf. Bodik CS 164 Lecture 829The Dangling Else: Example• The expressionif E1then if E2then S3else S4has two parse treesifE1ifE2S3S4ifE1ifE2S3S4• Typically we want the second formoooGGoProf. Bodik CS 164 Lecture 830The Dangling Else: A Fix• else matches the closest unmatched then• We can describe this in the grammar (distinguish between matched and unmatched “then”)S oMIF /* all then are matched */ | UIF /* some then are unmatched */MIF oif E then MIF else MIF | OTHERUIF oif E then S| if E then MIF else UIF • Describes the same set of stringsProf. Bodik CS 164 Lecture 831The Dangling Else: Example Revisited• The expression if E1then if E2then S3else S4ifE1ifE2S3S4ifE1ifE2S3S4• Not valid because the then expression is not aMIF• A valid parse tree (for a UIF)ooProf. Bodik CS 164 Lecture 832Ambiguity• 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 mechanismsooProf. Bodik CS 164 Lecture 833Precedence 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 …ooProf. Bodik CS 164 Lecture 834Associativity Declarations• Consider the grammar E oE + E | int• Ambiguous: two parse trees of int + int + intEEE EE+int +intintEEE EE+int+intint• Left-associativity declaration: %left +ooProf. Bodik CS 164 Lecture 835Precedence Declarations• Consider the grammar E oE + E | E * E | int– And the string int + int * intEEE EE+int *intintEEE EE*int+intint• Precedence declarations:


View Full Document

Berkeley COMPSCI 164 - Grammars and ambiguity

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Grammars and ambiguity
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Grammars and ambiguity and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Grammars and ambiguity 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?