690 likes | 923 Views
Compiler Designs and Constructions. Chapter 7: Bottom-Up Parser (page 195-278) Objectives: Shift and Reduce Parsing LR Parser Canonical LR Parser LALR Parser Dr. Mohsen Chitsaz. Bottom- Up Parsing ( Shift and Reduce ). 1- S ---> aAcBe 2- A ---> Ab 3- A ---> b 4- B ---> d
E N D
Compiler Designs and Constructions Chapter 7: Bottom-Up Parser (page 195-278) Objectives: Shift and Reduce Parsing LR Parser Canonical LR Parser LALR Parser Dr. Mohsen Chitsaz Chapter 7: Bottom-Up Parser
Bottom- Up Parsing (Shift and Reduce) • 1- S ---> aAcBe 2- A ---> Ab 3- A ---> b 4- B ---> d • Input abbcde Chapter 7: Bottom-Up Parser
Derivation: • RMD • LMD Chapter 7: Bottom-Up Parser
Derivation Tree: abbcde Chapter 7: Bottom-Up Parser
Definition of Handles Handle: S ==> A ==> B A---> B • Implementation: • Shift: Push(NextInputSymbol), Advance • Reduce: Pop(Handle), Push(LHS) • Accept • Error: Chapter 7: Bottom-Up Parser
Stack Implementation of Shift-Reduce Parser: Chapter 7: Bottom-Up Parser
There are Two (2) types of Bottom-Up Parsers: • 1-Operator Precedence Parser: • a CFG is an Operator Grammar IFF • No • No Adjacent Non-terminal • E ---> E+E Chapter 7: Bottom-Up Parser
Example: • <S> ---> While <EXP> do<EXP> ---> <EXP> = <EXP><EXP> ---> <EXP> >= <EXP><EXP> ---> <EXP> <= <EXP><EXP> ---> id • WHY? Chapter 7: Bottom-Up Parser
Bottom-up Parsing: 2- LR (K) Parsers: • L: Left to right scanning input • R: Right-most derivation • K: Look Ahead Symbols • Why LR Parsing? • Advantages: • Most programming Languages • Most general non-backtracking • Error detection • Cannot parse with recursive-descent Chapter 7: Bottom-Up Parser
Disadvantages: • Parse table harder • Parse table larger • Error recovery is harder • Without YACC • Types of LR Parser: • SLR: Simple • CLR: Canonical • LALR: Look-a-head Chapter 7: Bottom-Up Parser
Implementing the Parser as a Finite State Machine Chapter 7: Bottom-Up Parser
Stack: Element of Stack:S0, a1, S1, a2, …. Sm ,am • Where: • a: grammar Symbol (Token) • S: State Stack Operations: • Shift(State, Input) = Push(Input), Push(State) • Reduce (State, Input) = Replace (LHS) • Accept • Error Chapter 7: Bottom-Up Parser
Example: 1- E ---> E+T 2- E ---> T 3- T ---> T*F 4- T ---> F 5- F ---> (E) 6- F ---> id Chapter 7: Bottom-Up Parser
State Action Goto Chapter 7: Bottom-Up Parser
SLR Parse Table • LR (0) "Item": Original Productions of a grammar with a dot(.) at a given place in RHS • Example: X ---> ABC • X ---> .ABC • X ---> A.BC • X ---> AB.C • X ---> ABC. Chapter 7: Bottom-Up Parser
IF X ---> Produce one item: • X ---> . SLR Table: • Augmented grammar S'---> S • Functions • Closure • Goto Chapter 7: Bottom-Up Parser
S ---> a. • S ---> a.X • S ---> a.Xb Closure (item): Def:Suppose I is a set of items, we define closure (I) as: • Every item in I is in closure (I) • If A ---> .B is in closure (I) and B ---> is a production, then add the item B --->. to I (if not already in) Chapter 7: Bottom-Up Parser
Example: • E’ --> E E --> E + T E --> T T --> T * F Closure of (I): • E’ --> .E E --> .E + T E --> .T T --> .T * F Chapter 7: Bottom-Up Parser
GoTo: • Goto [I,X] = closure of [A ---> X. ] such that A --->.X I Def: • I0 closure [S' ---> .S] • C = {I0,I1,...In} set of canonical collection of items for grammar G with starting symbol S Chapter 7: Bottom-Up Parser
Example: • If I= E' ---> E. E ---> E. + T Then Goto [I, +] is: • E ---> E + .T T ---> .T * F T ---> .F F ---> .(E)F ---> .id Chapter 7: Bottom-Up Parser
Example 1 0. E’ --> E 1. E --> E + T 2. E --> T 3. T --> T * F 4. T --> F 5. F --> (E) 6. F --> id I0=CLOSURE (E’ -->.E) • I0=E’ --> .E E --> .E+T E --> .T T --> .T*F T --> .F F -->.(E) F --> .id Chapter 7: Bottom-Up Parser
Example 1 • GOTO(I0,E) =I1 E’ --> E. E --> E.+T • GOTO(I0,T) =I2 E --> T. T --> T.*F • GOTO(I0,F) = I3 T --> F. • GOTO(I0,( ) = I4 F --> (.E) E --> .E+T E --> .T T --> .T*F T --> .F F --> .(E) F --> .id Chapter 7: Bottom-Up Parser
Example 1 • GOTO(I0,id) = I5 F --> id. • GOTO(I1,+) = I6 E --> E +.T T --> .T*F T --> .F F --> .(E) F --> .id • GOTO(I2,*) = I7 T --> T*.F F --> .(E) F --> .id • GOTO (I4,E) = I8 F --> (E.) E --> E.+T Chapter 7: Bottom-Up Parser
Example 1 • GOTO(I6,T) = I9 E --> E+T. T --> T.*F • GOTO(I7,F) = I10 T --> T*F. • GOTO(I8,) ) = I11 F --> (E). • GOTO (I4, T ) = I2 • GOTO (I4, F ) = I3 • GOTO (I4, ( ) = I4 • GOTO (I4, id) = I5 • GOTO (I6, F ) = I3 • GOTO (I6, ( ) = I4 • GOTO (I6, id) = I5 • GOTO (I7, ( ) = I4 • GOTO (I7, id) = I5 • GOTO (I8, +) = I6 Chapter 7: Bottom-Up Parser
Canonical Collections • C=(I0,I1,I2,I3,I4,I5,I6,I7,I8,I9,I10,I11) • Follow(E) = { +, ), $ } Follow (T) = { *, +, ), $ } Follow(F)= { *, +, ), $ } Chapter 7: Bottom-Up Parser
Transition Diagram Chapter 7: Bottom-Up Parser
Algorithm to construct SLR Parsing: • 1-Construct a canonical collection C = {I0,...} for Augmented grammar G‘ • From the graph create the SLR(0) • for SHIFT and GOTO • 2- Rows are the states Columns are the Terminal Symbols For SHIFT and NonTerminal Symbols for GOTO Chapter 7: Bottom-Up Parser
Algorithm to construct SLR Parsing: • 3-Each item li corresponds to a state i for Terminal symbol a and State I • If [A --->. aB] li & Goto (li,a) = lj then Action [li,a] = Shift (j) • if [A ---> .] li and for all a’s in follow (A) then Reduce A --> But not A S' • If (S'-->S.) I0 then Set Action [i,$] = Accept. Chapter 7: Bottom-Up Parser
Algorithm to construct SLR Parsing: • 4- For all nonterminal symbol A and state I • If GOTO(Ij,A) = Ij then GOTO[I,A] = j • 5- All nonterminal entries are called Error • 6- The initial state of the parser is the state corresponding to the set of items including [S’ --> .S] • If no conflict in creating the table then G is SLR Grammar Chapter 7: Bottom-Up Parser
SLR Parser (Second Example) • Example (I) • S’ --> S • S --> E • E --> E + T • E --> T • T --> id • T --> (E) I0 = Closure [S’ .S] S’ --> .S S --> .E E --> .E + T E --> .T T --> .id T --> .(E) Chapter 7: Bottom-Up Parser
GOTO [I0, S] = I1 = S’ --> S. • GOTO [I0, E] = I2 = S --> E. • E --> E. + T • GOTO [I0, T] = I3 = E --> T. • GOTO [I0,id] = I4 = T --> id. • GOTO [I0, (] = I5 = T --> (.E) • E --> .E + T • E --> .T GOTO [I5, T] = I3 • T --> .id GOTO [I5,id] = I4 • T --> .(E) GOTO[I5,(] = I5 Chapter 7: Bottom-Up Parser
GOTO [I2, +] = I6 = E --> E + .T • T --> .id GOTO[I6,id] = I4 • T --> .(E) GOTO[I6,(] = I5 • GOTO[I5,E] = I7 = T --> (E.) • E E. + T GOTO [I7,+] = I6 • GOTO [I6,T] = I8 = E --> E + T. • GOTO [I7,)] = I9 T --> (E). Chapter 7: Bottom-Up Parser
Stack Input Chapter 7: Bottom-Up Parser
Follow (S) = -| • Follow (E) = -|, +, ) • Follow (T) = -|, +, ) Chapter 7: Bottom-Up Parser
Action GoTo Chapter 7: Bottom-Up Parser
Canonical LR Parsing or LR(1) • Introduction: 0- S’ --> S I0 = S’ --> .S1- S --> L = R S --> .L = R2- S --> R S --> .R3- L --> * R L --> .*R4- L --> id L -->. id5- R --> L R --> .L • GOTO[I0, S] = I1 = [S’ --> S.]GOTO[I0, L] = I2 = [S --> L. = R] R --> L.GOTO[I2, =] = I6 = [S --> L = .R] Chapter 7: Bottom-Up Parser
= id 0 1 2 R5/S6 LR(0) Parse Table State 2: Follow (R) = {=, } R5 State 2: Goto[I2,=]=I5 S5 Chapter 7: Bottom-Up Parser
Canonical LR Parsing Tables: LR(1) item: • [A --> .B, a] where A --> B is a production and a is a terminal or the right endmarker $ • A--> B1.B2 if B2 will not create additional information. But if B2 = it helps to make the right choice • Here we have same production but different lookahead symbols Chapter 7: Bottom-Up Parser
LR(1) item, is the same as canonical collection of sets of LR(0) item. • We need only to modify the functions: • Closure • GOTO • LR(1) Grammar I0 : • S--> .L = R • S --> .R • L --> .id • L --> .*R • R --> .L Chapter 7: Bottom-Up Parser
LR(0) Closure (I) = • I • A --> . XB | X --> in G Add [X --> . ] to I Chapter 7: Bottom-Up Parser
LR(1) Closure (I) = • I • A --> . XB, a | For each X --> in G For each b in first (Ba) Add [X --> . , b ] to I [if it is not already in ] Chapter 7: Bottom-Up Parser
GOTO[ I, X] = Closure(j) where: • J=[A --> X. B, a ] [A --> . XB, a ] in I Chapter 7: Bottom-Up Parser
S’ --> S S --> CC C --> aC C --> d [A --> . XB, a] For each X--> in G And For each b First (Ba) Add [X --> . , b] Example: Chapter 7: Bottom-Up Parser
0- S’ --> S 1- S --> CC 2- C --> aC 3- C --> d Canonical LR(1) Parsing Table: (LR(1)) Chapter 7: Bottom-Up Parser
Canonical LR(1) Parsing Table: (LR(1)) • I0= S’ --> .S, $ S --> .CC, $ C --> .aC, a/d C --> .d, a/d Chapter 7: Bottom-Up Parser
Canonical LR(1) Parsing Table: (LR(1)) GOTO[I0,S] = I1 = S1 ---> S.,$ GOTO[I0,C] = I2 = S ---> C.C,$ C ---> .aC,$ C ---> .d,$ GOTO[I0,a] = I3 = C ---> a.C,a/d C ---> .aC,a/d C ---> .d.a/d GOTO[I0,d] = I4 = C ---> d.,a/d Chapter 7: Bottom-Up Parser
Canonical LR(1) Parsing Table: (LR(1)) • GOTO[I2,C] = I5 = S ---> CC.,$ • GOTO[I2,a] = I6 = C ---> a.C,$ • C ---> .aC,$ • C ---> .d,$ • GOTO[I2,d] = I7 = C ---> d.,$ • GOTO[I3,C] = I8 = C ---> aC.,a/d • GOTO[I3,a] = I3 • GOTO[I3,d] = I4 • GOTO[I6,C] = I9 = C ---> aC.,$ • GOTO[I6,a] = I6 • GOTO[I6,d] = I7 Chapter 7: Bottom-Up Parser
Transition Diagram Chapter 7: Bottom-Up Parser
a d $ S C 0 S3 S4 1 2 1 Acc 2 S6 S7 5 3 S3 S4 8 4 R3 R3 5 R1 6 S6 S7 9 7 R3 8 R2 R2 9 Action GoTo R2 Chapter 7: Bottom-Up Parser