Compiler Designs and Constructions

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Compilation (Semester A, 2013/14) Lecture 6a: Syntax (Bottom–up parsing) Noam Rinetzky 1 Slides credit: Roman Manevich, Mooly Sagiv, Eran Yahav.
Compiler Principles Fall Compiler Principles Lecture 4: Parsing part 3 Roman Manevich Ben-Gurion University.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
CS 31003: Compilers  Difference between SLR and LR(1)  Construction of LR(1) parsing table  LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013.
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Pertemuan 12, 13, 14 Bottom-Up Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
Chapter 4-2 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR Other.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
LESSON 24.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
1 Compiler Construction Syntax Analysis Top-down parsing.
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
11 Outline  6.0 Introduction  6.1 Shift-Reduce Parsers  6.2 LR Parsers  6.3 LR(1) Parsing  6.4 SLR(1)Parsing  6.5 LALR(1)  6.6 Calling Semantic.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
CSI 3120, Syntactic analysis, page 1 Syntactic Analysis and Parsing Based on A. V. Aho, R. Sethi and J. D. Ullman Compilers: Principles, Techniques and.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
111 Chapter 6 LR Parsing Techniques Prof Chung. 1.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
LR Parser: LR parsing is a bottom up syntax analysis technique that can be applied to a large class of context free grammars. L is for left –to –right.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Three kinds of bottom-up LR parser SLR “Simple LR” –most restrictions on eligible grammars –built quite directly from items as just shown LR “Canonical.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 5: LR Parsing CS 540 George Mason University.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
CSE P501 – Compilers LR Parsing Build Bottom-Up Parse Tree Handles
Chapter 4 - Parsing CSCE 343.
Introduction to LR Parsing
Programming Languages Translator
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Bottom-up Parsing.
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Compiler Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Bottom-Up Syntax Analysis
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
4d Bottom Up Parsing.
Bottom Up Parsing.
Chapter 4. Syntax Analysis (2)
Chapter 4. Syntax Analysis (2)
Parsing Bottom-Up LR Table Construction.
Parsing Bottom-Up LR Table Construction.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 7, 10/09/2003 Prof. Roy Levow.
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Compiler Designs and Constructions Chapter 7: Bottom-Up Parser 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 COSC 470

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

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

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

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: 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Canonical LR(1) Parsing Table: (LR(1)) 0- S’ --> S 1- S --> CC 2- C --> aC 3- C --> d 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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