{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec16mod

# lec16mod - CS415 Compilers LR(0 SLR(1 LALR(1...

This preview shows pages 1–10. Sign up to view the full content.

CS415 Compilers LR(0), SLR(1), LALR(1) Context-sensitive Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

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

View Full Document
Lecture 16 2 cs415, spring 10 Left Recursion versus Right Recursion Right recursion Required for termination in top-down parsers Produces right-associative operators Left recursion Works fine in bottom-up parsers Limits required stack space Produces left-associative operators Rule of thumb Left recursion for bottom-up parsers Right recursion for top-down parsers * * * w x y z w * ( x * ( y * z ) ) * * * z w x y ( (w * x ) * y ) * z
Lecture 16 3 cs415, spring 10 Associativity What difference does it make? Can change answers in floating-point arithmetic Exposes a different set of common subexpressions Consider x+y+z What if y+z occurs elsewhere? Or x+y? or x+z? What if x = 2 & z = 17 ? Neither left nor right exposes 19. Best choice is function of surrounding context + + x y z xy z + + + z Ideal operator Left association Right association

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

View Full Document
Lecture 16 4 cs415, spring 10 LR(0) versus SLR(1) versus LR(1) Example: LR(0) ? LR(1) ? SLR(1) ? SLR (1): add FOLLOW(A) to each LR(0) item [A →γ •] as its second component: [A →γ •, a ], a FOLLOW(A) S’ S S S ; a | a
Lecture 16 5 cs415, spring 10 LALR(1) versus LR(1) Example: LR(0) ? LR(1) ? LALR(1) ? LALR (1): Merge two sets of LR(1) items (states), if they have the same core . core of set of LR(1) items: set of LR(0) items derived by ignoring the lookahead symbols S’ S S aAd | bBd | aBe | bAe A c B c

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

View Full Document
Lecture 16 6 cs415, spring 10 LALR(1) versus LR(1) Example: LR(0) ? LR(1) ? LALR(1) ? LALR (1): Merge two sets of LR(1) items (states), if they have the same core . core of set of LR(1) items: set of LR(0) items derived by ignoring the lookahead symbols FACT : collapsing LR(1) states into LALR(1) states cannot introcude shift/reduce conflicts S’ S S aAd | bBd | aBe | bAe A c B c
Lecture 16 7 cs415, spring 10 Hierarchy of Context-Free Languages Context-free languages Deterministic languages (LR( k )) LL( k ) languages Simple precedence languages LL(1) languages Operator precedence languages LR( k ) LR(1) The inclusion hierarchy for context-free languages

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

View Full Document
Lecture 16 8 cs415, spring 10 Hierarchy of Context-Free Grammars The inclusion hierarchy for context-free grammars Operator precedence includes some ambiguous grammars LL(1) is a subset of SLR(1) Context-free grammars Unambiguous CFGs Operator Precedence Floyd-Evans Parsable LR( k ) LR(1) LALR(1) SLR(1) LR(0) LL( k ) LL(1)
Lecture 16 9 cs415, spring 10 Error Recovery in Shift-Reduce Parsers The problem: parser encounters an invalid token Goal: Want to parse the rest of the file Basic idea (panic mode): Assume something went wrong while trying to find handle for nonterminal A Pretend handle for A has been found; pop “handle”, skip over input to find terminal that can follow A Restarting the parser (panic mode): find a restartable state on the stack (has transition for nonterminal A) move to a consistent place in the input (token that can follow A) perform (error) reduction (for nonterminal A) print an informative message

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 37

lec16mod - CS415 Compilers LR(0 SLR(1 LALR(1...

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

View Full Document
Ask a homework question - tutors are online