Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Bottom-Up Parser1 Compiler Designs and Constructions Chapter 7: Bottom-Up Parser (page 195-278) Objectives: Shift and Reduce Parsing LR Parser.

Similar presentations


Presentation on theme: "Chapter 7: Bottom-Up Parser1 Compiler Designs and Constructions Chapter 7: Bottom-Up Parser (page 195-278) Objectives: Shift and Reduce Parsing LR Parser."— Presentation transcript:

1 Chapter 7: Bottom-Up Parser1 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

2 Chapter 7: Bottom-Up Parser2 Bottom- Up Parsing (Shift and Reduce) 1- S ---> aAcBe 2- A ---> Ab 3- A ---> b 4- B ---> d Input abbcde

3 Chapter 7: Bottom-Up Parser3 Derivation: RMD LMD 1423 S-->aAcBe-->aAcde-->aAbcde-->abbcde 1234 S-->aAcBe-->aAbcBe-->abbcBe-->abbcde

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

5 Chapter 7: Bottom-Up Parser5 Handle: S ==>  A  ==>  B  A---> B Implementation: Shift: Push(NextInputSymbol), Advance Reduce: Pop(Handle), Push(LHS) Accept Error: Definition of Handles

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

7 Chapter 7: Bottom-Up Parser7 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

8 Chapter 7: Bottom-Up Parser8 Example: ---> While do ---> = ---> >= ---> ---> id WHY?

9 Chapter 7: Bottom-Up Parser9 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

10 Chapter 7: Bottom-Up Parser10 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

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

12 Chapter 7: Bottom-Up Parser12 Stack: Element of Stack: S 0, a 1, S 1, a 2, …. S m, a m Where: a: grammar Symbol (Token) S: State Stack Operations: Shift(State, Input) = Push(Input), Push(State) Reduce (State, Input) = Replace (LHS) Accept Error

13 Chapter 7: Bottom-Up Parser13 Example: 1- E ---> E+T 2- E ---> T 3- T ---> T*F 4- T ---> F 5- F ---> (E) 6- F ---> id

14 Chapter 7: Bottom-Up Parser14 id+*()$ETF 0S5S4123 1S6Accept 2R2S7R2 3R4 4S5S4823 5R6 6S5S493 7S5S410 8S6S11 9R1S7R1 10R3 11R5 StateActionGoto

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

16 Chapter 7: Bottom-Up Parser16 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.

17 Chapter 7: Bottom-Up Parser17 IF X ---> Produce one item: X --->. SLR Table: Augmented grammar S'---> S Functions Closure Goto

18 Chapter 7: Bottom-Up Parser18 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)

19 Chapter 7: Bottom-Up Parser19 Example: E’ --> E E --> E + T E --> T T --> T * F Closure of (I): E’ -->.E E -->.E + T E -->.T T -->.T * F

20 Chapter 7: Bottom-Up Parser20 GoTo: Goto [I,X] = closure of [A --->  X.  ] such that A ---> . X   I Def: I 0 closure [S' --->.S] C = {I 0,I 1,...I n } set of canonical collection of items for grammar G with starting symbol S

21 Chapter 7: Bottom-Up Parser21 Example: If I= E' ---> E. E ---> E. + T Then Goto [I, +] is: E ---> E +.T T --->.T * F T --->.F F --->.(E) F --->.id

22 Chapter 7: Bottom-Up Parser22 I 0 =CLOSURE (E’ -->.E) I 0 =E’ -->.E E -->.E+T E -->.T T -->.T*F T -->.F F -->.(E) F -->.id 0. E’ --> E 1. E --> E + T 2. E --> T 3. T --> T * F 4. T --> F 5. F --> (E) 6. F --> id Example 1

23 Chapter 7: Bottom-Up Parser23 GOTO(I 0,E) =I 1 E’ --> E. E --> E.+T GOTO(I 0,T) =I 2 E --> T. T --> T.*F GOTO(I 0,F) = I 3 T --> F. GOTO(I 0,( ) = I 4 F --> (.E) E -->.E+T E -->.T T -->.T*F T -->.F F -->.(E) F -->.id Example 1

24 Chapter 7: Bottom-Up Parser24 GOTO(I 0,id) = I 5 F --> id. GOTO(I 1,+) = I 6 E --> E +.T T -->.T*F T -->.F F -->.(E) F -->.id GOTO(I 2,*) = I 7 T --> T*.F F -->.(E) F -->.id GOTO (I 4,E) = I 8 F --> (E.) E --> E.+T Example 1

25 Chapter 7: Bottom-Up Parser25 GOTO(I 6,T) = I 9 E --> E+T. T --> T.*F GOTO(I 7,F) = I 10 T --> T*F. GOTO(I 8,) ) = I 11 F --> (E).  GOTO (I 4, T ) = I 2  GOTO (I 4, F ) = I 3  GOTO (I 4, ( ) = I 4  GOTO (I 4, id) = I 5  GOTO (I 6, F ) = I 3  GOTO (I 6, ( ) = I 4  GOTO (I 6, id) = I 5  GOTO (I 7, ( ) = I 4  GOTO (I 7, id) = I 5  GOTO (I 8, +) = I 6 Example 1

26 Chapter 7: Bottom-Up Parser26 C=(I 0,I 1,I 2,I 3,I 4,I 5,I 6,I 7,I 8,I 9,I 10,I 11 ) Follow(E) = {+, ), $} Follow (T) = {*, +, ), $} Follow(F)= { *, +, ), $} Canonical Collections

27 Chapter 7: Bottom-Up Parser27 Transition Diagram

28 Chapter 7: Bottom-Up Parser28 Algorithm to construct SLR Parsing: 1-Construct a canonical collection C = {I 0,...} 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

29 Chapter 7: Bottom-Up Parser29 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. )  I 0 then Set Action [i,$] = Accept.

30 Chapter 7: Bottom-Up Parser30 4- For all nonterminal symbol A and state I If GOTO(I j,A) = I j 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 Algorithm to construct SLR Parsing:

31 Chapter 7: Bottom-Up Parser31 SLR Parser (Second Example) Example (I) S’ --> S S --> E E --> E + T E --> T T --> id T --> (E) I 0 = Closure [S’.S] S’ -->.S S -->.E E -->.E + T E -->.T T -->.id T -->.(E)

32 Chapter 7: Bottom-Up Parser32 GOTO [I 0, S] = I 1 = S’ --> S. GOTO [I 0, E] = I 2 = S --> E. E --> E. + T GOTO [I 0, T] = I 3 = E --> T. GOTO [I 0,id] = I 4 = T --> id. GOTO [I 0, (] = I 5 = T --> (.E) E -->.E + T E -->.T GOTO [I 5, T] = I 3 T -->.id GOTO [I 5,id] = I 4 T -->.(E) GOTO[I 5,(] = I 5

33 Chapter 7: Bottom-Up Parser33 GOTO [I 2, +] = I 6 = E --> E +.T T -->.id GOTO[I 6,id] = I 4 T -->.(E) GOTO[I 6,(] = I 5 GOTO[I 5,E] = I7 = T --> (E.) E  E. + T GOTO [I 7,+] = I 6 GOTO [I 6,T] = I 8 = E --> E + T. GOTO [I 7,)] = I 9 T --> (E).

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

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

36 Chapter 7: Bottom-Up Parser36 ActionGoTo Stateid()+-|SET 0S4S5123 1Acc 2S6R2 3R4 4R5 5S4S573 6S4S58 7S9S6 8R3 9R6

37 Chapter 7: Bottom-Up Parser37 Canonical LR Parsing or LR(1) Introduction: 0- S’ --> SI 0 = S’ -->.S 1- S --> L = RS -->.L = R 2- S --> RS -->.R 3- L --> * RL -->.*R 4- L --> idL -->. id 5- R --> LR -->.L GOTO[I 0, S] = I 1 = [S’ --> S.] GOTO[I 0, L] = I 2 = [S --> L. = R] R --> L. GOTO[I 2, =] = I 6 = [S --> L =.R]

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

39 Chapter 7: Bottom-Up Parser39 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

40 Chapter 7: Bottom-Up Parser40 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 I 0 : S-->.L = R S -->.R L -->.id L -->.*R R -->.L

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

42 Chapter 7: Bottom-Up Parser42 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 ]

43 Chapter 7: Bottom-Up Parser43 GOTO[ I, X] = Closure(j) where: J=[A -->  X. B, a ] [A --> . XB, a ] in I

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

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

46 Chapter 7: Bottom-Up Parser46 Canonical LR(1) Parsing Table: (LR(1)) I 0 = S’ -->.S, $ S -->.CC, $ C -->.aC, a/d C -->.d, a/d

47 Chapter 7: Bottom-Up Parser47 Canonical LR(1) Parsing Table: (LR(1)) GOTO[I0,S] = I 1 = S 1 ---> S.,$ GOTO[I0,C] = I 2 = S ---> C.C,$ C --->.aC,$ C --->.d,$ GOTO[I0,a] = I 3 = C ---> a.C,a/d C --->.aC,a/d C --->.d.a/d GOTO[I0,d] = I 4 = C ---> d.,a/d

48 Chapter 7: Bottom-Up Parser48 Canonical LR(1) Parsing Table: (LR(1)) GOTO[I2,C] = I 5 = S ---> CC.,$ GOTO[I2,a] = I 6 = C ---> a.C,$ C --->.aC,$ C --->.d,$ GOTO[I2,d] = I 7 = C ---> d.,$ GOTO[I3,C] = I 8 = C ---> aC.,a/d GOTO[I3,a] = I 3 GOTO[I3,d] = I 4 GOTO[I6,C] = I 9 = C ---> aC.,$ GOTO[I6,a] = I 6 GOTO[I6,d] = I 7

49 Chapter 7: Bottom-Up Parser49 Transition Diagram

50 Chapter 7: Bottom-Up Parser50 ActionGoTo ad$SC 0S3S Acc 2S6S7 5 3S3S4 8 4R3 5 R1 6S6S7 9 7 R3 8R2 9

51 Chapter 7: Bottom-Up Parser51 State$InputAction/GoTo 0aadd$

52 Chapter 7: Bottom-Up Parser52 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]

53 Chapter 7: Bottom-Up Parser53 Canonical LR(1) Parsing Table: (LR(1)) GOTO[I 0,S] = I 1 = S’ --> S., $ GOTO[I 0,C] = I 2 =S --> C.C, $ C -->.aC, $ C -->.d, $ GOTO[I 0,a] = I 3 =C --> a.C, a/d C -->.aC, a/d C -->.d, a/d

54 Chapter 7: Bottom-Up Parser54 GOTO[I 0,d] = I 4 = C --> d., a/d GOTO[I 2,C] = I 5 =S --> CC.,$ GOTO[I 2,a] = I 6 =C --> a.C, $ C .d, $ GOTO[I 2,d] = I 7 =C --> d., $ GOTO[I 3,C] = I 8 = C --> aC., a/d GOTO[I 3,a] = I 3 GOTO[I 3,d] = I 4 GOTO[I 6,C] = I 9 =C --> aC., $ GOTO[I 6,a] = I 6 GOTO[I 6,d] = I 7 C -->.aC, $

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

56 Chapter 7: Bottom-Up Parser56 LALR Parsing Table: 1. C = {I 0, I 1, …I n } 2. Combine I j with identical First Part (Core) and different Lookahead symbols 3. Let C’ {J 0, J 1,…J m } be a new set of items

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

58 Chapter 7: Bottom-Up Parser58 5. Let J = I i, U I 2,… U I R GOTO(J,X) = R where R is union of GOTO(I 1,X) GOTO(I 2,X) GOTO(I R,X) In case of no conflict, we have a LALR Parsing Table

59 Chapter 7: Bottom-Up Parser59 LALR Parse Table ad$SC 0S36S4712 1Acc 2S36S475 36S36S R3 5R1 89R2

60 Chapter 7: Bottom-Up Parser60 LALR Parse Table (Revised) ad$SC 0S3S412 1Acc 2S3S45 3S3S46 4R3 5R1 6R2

61 Chapter 7: Bottom-Up Parser61 1-Use a 2 dimensional array(or make two tables – one for action and one for GOTO) Constructing LALR Parse Table ad$SC Action Goto + Shift - Reduce 0 Accept 99Error

62 Chapter 7: Bottom-Up Parser62 2-Use Sparce Matrix Representation 0a3 0d4 0S1 0C2 1$0 2a3 2d4 2C5 3a3 4d4 3C5 4a-3 4d 4$ 5$ 6a-2 6d 6$ Row Column Action

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

64 Chapter 7: Bottom-Up Parser64 Pair Method (Array Action) A1 A2 A1 A5 A4 A3

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

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

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


Download ppt "Chapter 7: Bottom-Up Parser1 Compiler Designs and Constructions Chapter 7: Bottom-Up Parser (page 195-278) Objectives: Shift and Reduce Parsing LR Parser."

Similar presentations


Ads by Google