150_Miscellaneous_Parsing

150_Miscellaneous_Parsing - CS143 Handout 15 Summer 2011...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS143 Handout 15 Summer 2011 July 6 th , 2011 Miscellaneous Parsing Handout written by Maggie Johnson and revised by Julie Zelenski. Resolving Ambiguity: A Different Approach Recall that ambiguity means we have two or more leftmost derivations for the same input string, or equivalently, that we can build more than one parse tree for the same input string. A simple arithmetic expression grammar is a common example: E –> E + E | E * E | (E) | id Parsing input id + id * id can produce two different parse trees because of ambiguity E E + E id E * E id id E E * E E + E id id id Earlier we discussed how to fix the problem by re-writing the grammar to introduce new intermediate non-terminals that enforce the desired precedence. Instead, let's consider what happens if we go ahead and create the LR(0) configurating sets for the ambiguous version of this grammar: I 0 : E' –> •E I 5 : E –> E*•E E –> •E + E E –> •E + E E –> •E * E E –> •E * E E –> •(E) E –> •(E) E –> •id E –> •id I 1 : E' –> E• I 6 : E –> (E•) E –> E• + E E –> E• + E E –> E• * E E –> E• * E I 2 : E –> (•E) I 7 : E –> E + E• E –> •E + E E –> E• + E E –> •E * E E –> E• * E E –> •(E) E –> •id I 8 : E –> E * E• E –> E• + E I 3 : E –>id• E –> E• * E
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 I 4 : E –> E+•E I 9 : E –> (E)• E –> •E + E E –> •E * E E –> •(E) E –> •id Let's say we were building an SLR(1) table for this grammar. Look carefully at state 7. In the action table, there are two conflicting entries under the column labeled * : s5 and r1 , a shift/reduce conflict. Trace the parse of input id + id * id up to the point where we arrive in state 7: State stack Remaining input S 0 S 1 S 4 S 7 * id $ state 7: next input is * At this point during the parse, we have the handle E + E on top of the stack, and the lookahead is * . * is in the follow set for E , so we can reduce that E + E to E . But we also could shift the * and keep going. What choice should we make if we want to preserve the usual arithmetic precedence? What about if we were parsing id + id + id? We have a similar shift/reduce in state 7, now on next input +. How do we want to resolve the shift/reduce conflict here? (Because addition is commutative, it actually doesn’t much matter, but it will for subtraction!) State stack Remaining input S 0 S 1 S 4 S 7 + id $ state 7: next input is + Now consider parsing id * id + id. A similar shift/reduce conflict comes up in state 8. State stack Remaining input S 0 S 1 S 5 S 8 + id $ state 8: next input is + And what about parsing id * id * id? State stack Remaining input S 0 S 1 S 5 S 8 * id $ state 8: next input is * Instead of rearranging the grammar to add all the different precedence levels, another way of resolving these conflicts to build the precedence rules right into the table. Where there are two or more entries in the table, we pick the one to keep and throw the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

150_Miscellaneous_Parsing - CS143 Handout 15 Summer 2011...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online