02-06ambiguityErrors

02-06ambiguityErrors - Recall: LR parsing algorithm Parsing...

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

View Full Document Right Arrow Icon
1 CSE 450: Compilers K. Stirewalt Parsing and syntax analysis Part 6: Ambiguous grammars Topics: – quick tangent on semantic actions in yacc – ambiguous grammars – error handling and recovery Readings: Read Section 4.6 in Dragon Book CSE 450: Compilers K. Stirewalt Recall: LR parsing algorithm Input: String w and an LR-parsing table with ACTION and GOTO tables for grammar G Output: If w in L(G), rightmost derivation of w, else error Method: Initially, parser has s0 on stack and w$ in buffer let a be the first symbol of w$ ; while(1) { let s be the state on top of stack; if ( ACTION[s, a] = shift t ) { push t onto stack; let a be next symbol in input; } else if ( ACTION[s, a] = reduce A β ) { pop | | symbols off stack; let t now be on top of stack; push GOTO[t, A] onto stack; output production A ; } else if ( ACTION[s,a] = accept ) break; /* success */ else error; } Notice: This location is a useful place to insert “semantic actions” into the parsing process CSE 450: Compilers K. Stirewalt Semantic actions in Yacc When the yacc - generated parser reduces a production, it executes user -s upplied C/C++ code known as the production’s action The action is enclosed in curly braces between right -hand side of the production and the semi- colon or vertical bar – Action may refer to the values of the right-hand side symbols as $1 , $2 , etc and may set the value of the left-hand side symbol using $$ – For today, we won’t use these features CSE 450: Compilers K. Stirewalt <<Example parser with actions>> CSE 450: Compilers K. Stirewalt Using ambiguous grammars Shift- reduce parsers can use ambiguous grammars if: – Ambiguity manifests as a shift/reduce conflict, and – External rules are provided to make the decision Common use is in expression grammars CSE 450: Compilers K. Stirewalt Recall the ambiguous grammar E E + E | E * E | ( E ) | id Ambiguous because says nothing about associativity or precedence of + and * operators
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 CSE 450: Compilers K. Stirewalt Consider the LR(0) state… I 7 : E E + E . E E . + E E E . * E What action should be taken in this state if the next token on the input is a ‘+’? CSE 450: Compilers K. Stirewalt Consider the LR(0) state… I 7 : E E + E .
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.

This note was uploaded on 07/25/2008 for the course CSE 450 taught by Professor Stirewalt during the Spring '08 term at Michigan State University.

Page1 / 5

02-06ambiguityErrors - Recall: LR parsing algorithm Parsing...

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