Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Designs and Constructions

Similar presentations


Presentation on theme: "Compiler Designs and Constructions"— Presentation transcript:

1 Compiler Designs and Constructions
Chapter 7: Bottom-Up Parser Compiler Designs and Constructions Chapter 7: Bottom-Up Parser (page ) Objectives: Shift and Reduce Parsing LR Parser Canonical LR Parser LALR Parser Dr. Mohsen Chitsaz Chapter 7: Bottom-Up Parser COSC 470

2 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

3 Chapter 7: Bottom-Up Parser
Derivation: RMD 1 4 2 3 S --> aAcBe aAcde aAbcde abbcde LMD 1 2 3 4 S --> aAcBe aAbcBe abbcBe abbcde Chapter 7: Bottom-Up Parser

4 Chapter 7: Bottom-Up Parser
Derivation Tree: abbcde Chapter 7: Bottom-Up Parser

5 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

6 Stack Implementation of Shift-Reduce Parser:
Input Stack Handle Action abbcde$ Δ Shift bbcde$ Δa bcde$ Δab b Reduce ΔaA cde$ ΔaAb Ab de$ ΔaAc e$ ΔaAcd d ΔaAcB $ ΔaAcBe aAcBe ΔS Accept Chapter 7: Bottom-Up Parser

7 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

8 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

9 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

10 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

11 Implementing the Parser as a Finite State Machine
Chapter 7: Bottom-Up Parser

12 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

13 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

14 Chapter 7: Bottom-Up Parser
State Action Goto id + * ( ) $ E T F S5 S4 1 2 3 S6 Accept R2 S7 R4 4 8 5 R6 6 9 7 10 S11 R1 R3 11 R5 Chapter 7: Bottom-Up Parser

15 Chapter 7: Bottom-Up Parser
STACK INPUT ACTION 1 id*id+id$ S5 2 0,id,5 *id+id$ R6 3 0,F 4 0,F,3 R4 5 0,T 6 0,T,2 S7 7 0,T,2,*,7 id+id$ 8 0,2,*,7,id,5 +id$ 9 0,T,2,*,7,F,10 10 R2 11 0,E,1 S6 12 0,E,1,+,6 id$ s5 13 0,E,1,+,6,id,5 $ 14 0,E,1,+,6,F,3 15 0,E,1,+,6,T,9 R1 16 Accept Chapter 7: Bottom-Up Parser

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 Chapter 7: Bottom-Up Parser
Transition Diagram Chapter 7: Bottom-Up Parser

28 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

29 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

30 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

31 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

32 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

33 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

34 Chapter 7: Bottom-Up Parser
Stack Input (id + id ) -| 0(5 id + id )-| 0(5id4 +id)-| R5 0(5T3 R4 0(5E7 S6 0(5E7+6 Id)-| S4 0(5E7+6id4 )-| 0(5E7+6T8 R3 S9 0(5E7)9 -| R6 0T3 0E2 R2 0S1 Accept Chapter 7: Bottom-Up Parser

35 Chapter 7: Bottom-Up Parser
Follow (S) = -| Follow (E) = -|, +, ) Follow (T) = -|, +, ) Chapter 7: Bottom-Up Parser

36 Chapter 7: Bottom-Up Parser
Action GoTo State id ( ) + -| S E T S4 S5 1 2 3 Acc S6 R2 R4 4 R5 5 7 6 8 S9 R3 9 R6 Chapter 7: Bottom-Up Parser

37 Canonical LR Parsing or LR(1)
Introduction: 0- S’ --> S I0 = S’ --> .S 1- S --> L = R S --> .L = R 2- S --> R S --> .R 3- L --> * R L --> .*R 4- L --> id L -->. id 5- 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

38 Chapter 7: Bottom-Up Parser
LR(0) Parse Table = id 1 2 R5/S6 State 2: Follow (R) = {=, } R5 State 2: Goto[I2,=]=I5 S5 Chapter 7: Bottom-Up Parser

39 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

40 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

41 Chapter 7: Bottom-Up Parser
LR(0) Closure (I) = I A -->  . XB  | X -->  in G Add [X --> .  ] to I Chapter 7: Bottom-Up Parser

42 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

43 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

44 Chapter 7: Bottom-Up Parser
Example: 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] Chapter 7: Bottom-Up Parser

45 Canonical LR(1) Parsing Table: (LR(1))
0- S’ --> S 1- S --> CC 2- C --> aC 3- C --> d Chapter 7: Bottom-Up Parser

46 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

47 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

48 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

49 Chapter 7: Bottom-Up Parser
Transition Diagram Chapter 7: Bottom-Up Parser

50 Chapter 7: Bottom-Up Parser
Action GoTo a d $ S C S3 S4 1 2 Acc S6 S7 5 3 8 4 R3 R1 6 9 7 R2 R2 Chapter 7: Bottom-Up Parser

51 Chapter 7: Bottom-Up Parser
State $Input Action/GoTo 0 aadd$ Chapter 7: Bottom-Up Parser

52 Chapter 7: Bottom-Up Parser
LALR(1) Parser LALR(1) = Lookahead Parser Practical Smaller Parse Table Most Programming Languages Merge the States: [A --> . xB, a] [A -->  .xB, b] To [A -->  .xB, a/b] Chapter 7: Bottom-Up Parser

53 Canonical LR(1) Parsing Table: (LR(1))
GOTO[I0,S] = I1 = S’ --> 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 Chapter 7: Bottom-Up Parser

54 Chapter 7: Bottom-Up Parser
GOTO[I0,d] = I4 = C --> d., a/d GOTO[I2,C] = I5 = S --> CC., $ GOTO[I2,a] = I6 = C --> a.C, $ 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 C --> .aC, $ Chapter 7: Bottom-Up Parser

55 Transition Diagram (DFA)
Chapter 7: Bottom-Up Parser

56 Chapter 7: Bottom-Up Parser
LALR Parsing Table:  C = {I0, I1, …In} Combine Ij with identical First Part (Core) and different Lookahead symbols Let C’ {J0, J1,…Jm} be a new set of items Chapter 7: Bottom-Up Parser

57 Chapter 7: Bottom-Up Parser
LALR Parsing Table: State I of the parser is corresponding to set Ji Action [I,a] = Shift Ji If [A --> . A B, b]  Ji GOTO(Ji,a) = Ji Action [I,a] = Reduce A -->  if A --> . , a]  Ji and A  S’  Note: that in case if [A -->  . a/b/c/…/z] the action is included in column a,b,…z Action [I,$] = Accept IF [S’ --> S.,$]  Ji Chapter 7: Bottom-Up Parser

58 Chapter 7: Bottom-Up Parser
Let J = Ii, U I2,… U IR GOTO(J,X) = R where R is union of GOTO(I1,X) GOTO(I2,X) GOTO(IR,X) In case of no conflict, we have a LALR Parsing Table Chapter 7: Bottom-Up Parser

59 Chapter 7: Bottom-Up Parser
LALR Parse Table a d $ S C S36 S47 1 2 Acc 5 36 89 47 R3 R1 R2 Chapter 7: Bottom-Up Parser

60 LALR Parse Table (Revised)
$ S C S3 S4 1 2 Acc 5 3 6 4 R3 R1 R2 Chapter 7: Bottom-Up Parser

61 Chapter 7: Bottom-Up Parser
Constructing LALR Parse Table 1-Use a 2 dimensional array(or make two tables – one for action and one for GOTO) Action Goto a d $ S C 3 4 99 1 2 5 6 -3 -1 -2 + Shift Reduce 0 Accept 99 Error Chapter 7: Bottom-Up Parser

62 2-Use Sparce Matrix Representation
Row Column Action a 3 d 4 S 1 C 2 $ 5 4 d 3 C 5 a -3 $ -1 6 -2 Chapter 7: Bottom-Up Parser

63 Chapter 7: Bottom-Up Parser
3-Pair Method State Number = Number of elements, Pairs Input Symbols Actions Action , ,4 A1 A2 1 ,0 2 3 3 1, , ,-3 A3 4 A4 5 ,-1 6 3 1, , ,-2 A5 Chapter 7: Bottom-Up Parser

64 Chapter 7: Bottom-Up Parser
Pair Method (Array Action) A1 1 A2 2 A1 3 A1 A3 4 A4 5 A5 6 Chapter 7: Bottom-Up Parser

65 Algorithm for the Driver (PARSER)
State <-- State 1; Input <-- a, While (Action <> Accept and Action <> Error) //Stack <-- S0 Xi, S1,…XmSm //Input <-- ai, ai+1, … $ IF {Sm is terminal} Case Action [Sm, ai] of + /*Sn*/: Action <-- Shift{Sn, ai) - /*Rn*/: Action  Reduce{n} 0 /*Accept:/*: Action <-- Accept 99 /*Blank*/: Action <-- Error Else Action <-- GOTO{Sm, nonterminal) Chapter 7: Bottom-Up Parser

66 Algorithm for the Driver (PARSER)
Procedure Reduce(n) Repeat Pop(state) Pop(symbol) Until RHS is empty Push(LHS) Procedure Shift(S,a) Push(a) Push(S) Chapter 7: Bottom-Up Parser

67 Chapter 7: Bottom-Up Parser
Algorithm for the Driver (PARSER) Procedure GOTO(S, nonterminal) Push(S) Procedure ShiftTerminal // A --> Bcd id Shift 7 Push(Id) Push(7) Procedure ShiftNonterminal Push(State) Chapter 7: Bottom-Up Parser COSC 470


Download ppt "Compiler Designs and Constructions"

Similar presentations


Ads by Google