Ch4b - 1 ! Syntax Analysis" Part II! Chapter 4! COP5621...

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

View Full Document Right Arrow Icon
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011
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 Bottom-Up Parsing • LR methods (Left-to-right, Rightmost derivation) – SLR, Canonical LR, LALR • Other special cases: – Shift-reduce parsing – Operator-precedence parsing
Background image of page 2
3 Operator-Precedence Parsing • Special case of shift-reduce parsing • We will not further discuss (you can skip textbook section 4.6)
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Shift-Reduce Parsing Grammar: S ! a A B e A ! A b c | b B ! d Shift-reduce corresponds to a rightmost derivation: S " rm a A B e " rm a A d e " rm a A b c d e " rm a b b c d e Reducing a sentence: a b b c d e a A b c d e a A d e a A B e S S a b b c d e A A B a b b c d e A A B a b b c d e A A a b b c d e A These match production’s right-hand sides
Background image of page 4
5 Handles Handle Grammar: S ! a A B e A ! A b c | b B ! d A handle is a substring of grammar symbols in a right-sentential form that matches a right-hand side of a production NOT a handle, because further reductions will fail (result is not a sentential form) a b b c d e a A b c d e a A A e … ? a b b c d e a A b c d e a A d e a A B e S
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Stack Implementation of Shift-Reduce Parsing Stack $ $id $ E $ E + $ E + id $ E + E $ E + E * $ E + E *id $ E + E * E $ E + E $ E Input id+id*id$ +id*id$ +id*id$ id*id$ *id$ *id$ id$ $ $ $ $ Action shift reduce E ! id shift shift reduce E ! id shift (or reduce?) shift reduce E ! id reduce E ! E * E reduce E ! E + E accept Grammar: E ! E + E E ! E * E E ! ( E ) E ! id Find handles to reduce How to resolve con±icts?
Background image of page 6
7 Conficts Shift-reduce and reduce-reduce conficts are caused by – The limitations oF the LR parsing method (even when the grammar is unambiguous) – Ambiguity oF the grammar
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Shift-Reduce Parsing: Shift-Reduce ConFicts Stack $ $ if E then S Input $ else $ Action shift or reduce? Ambiguous grammar: S ! if E then S | if E then S else S | other Resolve in favor of shift, so else matches closest if
Background image of page 8
9 Shift-Reduce Parsing: Reduce-Reduce ConFicts Stack $ $a Input aa$ a$ Action shift reduce A ! a or B ! a ? Grammar: C ! A B A ! a B ! a Resolve in favor of reduce A ! a , otherwise we’re stuck!
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 LR( k ) Parsers: Use a DFA for Shift/Reduce Decisions 1 2 4 5 3 0 start a A C B a Grammar: S ! C C ! A B A ! a B ! a State I 0 : S ! C C ! A B A ! a State I 1 : S ! C State I 2 : C ! A B B ! a State I 3 : A ! a State I 4 : C ! A B State I 5 : B ! a goto ( I 0 , C ) goto ( I 0 , a ) goto ( I 0 , A ) goto ( I 2 , a ) goto ( I 2 , B ) Can only reduce A ! a ( not B ! a)
Background image of page 10
11 DFA for Shift/Reduce Decisions Stack $ 0 $ 0 $ 0 a 3 $ 0 A 2 $ 0 A 2 a 5 $ 0 A 2 B 4 $ 0 C 1 Input aa$ a a$ a$ a$ $ $ $ Action start in state 0 shift (and goto state 3) reduce A ! a (goto 2) shift (goto 5) reduce B ! a (goto 4) reduce C ! AB (goto 1) accept ( S ! C ) Grammar: S ! C C ! A B A ! a B ! a The states of the DFA are used to determine if a handle is on top of the stack State I 0 : S ! C C !
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/01/2012 for the course COP 5621 taught by Professor Vanengelen during the Spring '11 term at FSU.

Page1 / 55

Ch4b - 1 ! Syntax Analysis" Part II! Chapter 4! COP5621...

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

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