# L7-Parsing2 - Introduction to Compiler Design Syntax Analysis More LR Parsing Professor Yi-Ping You Department of Computer Science

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

Introduction to Compiler Design Syntax Analysis: More LR Parsing Professor Yi-Ping You Department of Computer Science http://www.cs.nctu.edu.tw/~ypyou/ Page 1 Introduction to Compiler Design, Spring 2010

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

View Full Document
Outline Canonical-LR Parsing Lookahead-LR Parsing Error Recovery in LR Parsing Using Ambiguous Grammars Summary Page 2 Introduction to Compiler Design, Spring 2010
More LR Parsers Canonical-LR (or just “LR”) ore powerful than SLR i e LR(0) More powerful than SLR, i.e., LR(0) LR(1) LR(k) ookahead- R (or “LALR”) Lookahead LR (or LALR ) More powerful than SLR Parsing tables are no bigger than the SLR tables Page 3 Introduction to Compiler Design, Spring 2010

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

View Full Document
Recall: Shift/Reduce Conflicts in SLR S L=R | R R | I 0 : S’ →⋅ S S → ⋅ L=R I 5 : L id = Unambiguous grammar L *R | id R L onsider S R L *R I 6 : S L= R R *R Canonical LR(0) collection Consider I 2 S’ L =R ACTION[2,=] = “shift 6” L id R L I 1 : S L → ⋅ id I 7 : L *R R L = FOLLOW( R ) CTION[2 =] = “reduce I 2 : =R R I 8 : R I 9 : S L=R “reduce ACTION[2,=] = reduce R L A shift/reduce conflict occurs in the entry of ACTION[2,=] I 3 : S R I 4 : * R S’ L =R means we have seen L and hope to see “ = R → ⋅ L *R id we see “ nd “reduce Page 4 Introduction to Compiler Design, Spring 2010 If we see = and reduce R L , L is replaced by R . However, R=… is not a right-sentential form
The Examples Stack Symbol Input Action 0\$ *id = id \$ s4 4 Ex1 0 4 \$* id = id \$ s5 0 4 5 \$* id = id \$ r4 0 4 8 \$* L = id \$ r5 47 * (1) S L=R (4) L id (2) S R (5) R L 0 4 7 \$ R = id \$ r3 0 2 \$ L = id \$ s6 / r5 0 3 \$ R =id \$ error 26 = (3) *R r5 0 2 6 \$ L id \$ s5 0 2 6 5 \$ L= id \$ r4 0 2 6 8 \$ L = L \$ r5 269 = state ACTION GOTO id =*\$ SL R s6 0 2 6 9 \$ L R \$ r1 0 1 \$S \$ acc 0 s5 s4 1 2 3 1 acc 2 s6/r5 r5 Stack Symbol Input Action x2 3 r2 4s 5 s 4 87 5r 4 r 4 * id \$ s4 0 4 \$* id \$ S5 0 4 5 \$* id \$ r4 Ex2 6 s5 s4 8 9 7r 3 r 3 8r 5 r 5 0 4 8 \$* L \$ r5 0 4 7 \$* R \$ r3 0 2 \$ L \$ r5 Page 5 Introduction to Compiler Design, Spring 2010 9 r1 0 3 \$ R \$ r2 0 1 \$ S \$ acc

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

View Full Document
The Problem of SLR(1), i.e., LR(0) What is the problem? Lookahead information is too crude S L=R | R R | Need to distinguish If L * R is from S L = R *R = R , then OLLOW( = {= \$} FOLLOW( = {=} L *R | id R L FOLLOW( R ) { , \$}, FOLLOW( L ) {} If L * R is from S R L *R , then – FOLLOW( R ) = {\$}, FOLLOW( L ) = {\$} Solution: Carry the specific lookahead information with the canonical LR(0) item, E.g., , g, [ R L ] is a LR(0) item [ R L , \$] is a LR(1) item, where \$ is the lookahead The item becomes canonical LR(1) item Use the lookahead symbol(s) with the item to identify the correct duction rule to apply Page 6 Introduction to Compiler Design, Spring 2010 reduction rule to apply
Canonical LR(1) Items & Canonical Canonical LR(1) Items & Canonical-LR Parsing LR Parsing A canonical LR(1) item is a canonical LR(0) item ssociated with ne okahead mbol associated with one lookahead symbol E.g., a LR(0) item [ A → α ] is a LR(0) item [ A → α ⋅ , a ] is a LR(1) item, where a is the lookahead Canonical-LR (or just “LR”) parsing The parsing scheme based on LR(1) items More powerful than SLR(1) parsing, i.e., LR(0) parsing Page 7 Introduction to Compiler Design, Spring 2010

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.

## This note was uploaded on 12/25/2010 for the course ALL 0204 taught by Professor 79979 during the Spring '10 term at National Chiao Tung University.

### Page1 / 46

L7-Parsing2 - Introduction to Compiler Design Syntax Analysis More LR Parsing Professor Yi-Ping You Department of Computer Science

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

View Full Document
Ask a homework question - tutors are online