Joey Paquet, 2000, 2002, 2012, 20141 Lecture 6 Bottom-Up Parsing.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Mooly Sagiv and Roman Manevich School of Computer Science
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
Pertemuan 12, 13, 14 Bottom-Up Parsing
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
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.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 CIS 461 Compiler Design & Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #12 Parsing 4.
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
Lexical and syntax analysis
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
Syntax and Semantics Structure of programming languages.
BOTTOM-UP PARSING Sathu Hareesh Babu 11CS10039 G-8.
Parsing. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
 an efficient Bottom-up parser for a large and useful class of context-free grammars.  the “ L ” stands for left-to-right scan of the input; the “ R.
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.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 LR Parsers  The most powerful shift-reduce parsing (yet efficient) is: LR(k) parsing. LR(k) parsing. left to right right-most k lookhead scanning derivation.
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.
Syntax and Semantics Structure of programming languages.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
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.
Bottom-up parsing. Bottom-up parsing builds a parse tree from the leaves (terminals) to the start symbol int E T * TE+ T (4) (2) (3) (5) (1) int*+ E 
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.
Syntax and Semantics Structure of programming languages.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
lec04-bottomupparser June 6, 2018 Bottom-Up Parsing
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
CS 488 Spring 2012 Lecture 4 Bapa Rao Cal State L.A.
Compiler Construction
Compiler design Bottom-up parsing: Canonical LR and LALR
Fall Compiler Principles Lecture 4: Parsing part 3
Syntax Analysis Part II
Shift Reduce Parsing Unit -3
Subject Name:COMPILER DESIGN Subject Code:10CS63
4d Bottom Up Parsing.
Lecture 8 Bottom Up Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
4d Bottom Up Parsing.
Syntax Analysis - 3 Chapter 4.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Chap. 3 BOTTOM-UP PARSING
4d Bottom Up Parsing.
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing

2 Part I Concepts of Bottom-Up Parsing Joey Paquet, 2000, 2002, 2012, 2014

3 Bottom-Up Parsing Top-down parsers construct a derivation from the root to the leaves of a parse tree Bottom-up parsers construct a reverse derivation from the leaves up to the root of the parse tree –can handle a larger class of grammars –most parser generators use bottom-up methods –needs less grammar transformations (generally no transformation at all) Joey Paquet, 2000, 2002, 2012, 2014

4 Bottom-Up Parsing Often called “shift-reduce” parsers because they implements two main operations: –shift: push the lookahead symbol onto the stack and reads another token –reduce: matches a group of adjacent symbols  on the stack with the right-hand-side of a production and replaces  with the left-hand-side of the production (reverse application of a production). We reduce only if  is a handle. –handle: A handle of a string is a substring that matches the right-hand-side of a production, and whose reduction to the non-terminal on the left- hand-side of the production represents one step along the reverse and rightmost derivation. Joey Paquet, 2000, 2002, 2012, 2014

5 LL vs. LR Parsing id+ id * id  F + id * id  T + id * id  E + id * id  E + F * id  E + T * id  E + T * F  E + T  E E  E + T  T + T  F + T  id + T  id + T * F  id + F * F  id + id * F  id + id * id LR(k) stands for –scan Left to right –compute Rightmost derivation –using k lookahead tokens LL(k) stands for –scan Left to right –compute Leftmost derivation –using k lookahead tokens Joey Paquet, 2000, 2002, 2012, 2014

6 LR Parsers LR parsers can be constructed to recognize virtually all programming language constructs for which a context free grammar can be written The LR method is the most general non-backtracking shift-reduce parsing method known, yet it can be implemented as efficiently as other shift-reduce method The class of grammars that can be parsed using LR methods is a proper superset of the class of grammars that can be parsed with predictive parsers An LR parser can detect a syntactic error as soon as it is possible to do so on a left-to-right scan of the input Joey Paquet, 2000, 2002, 2012, 2014

7 LR Parsers SLR: simple LR –easiest to implement, but the least powerful. May fail to produce a parsing table for some grammars. CLR: canonical LR –most powerful, general and expensive LR method LALR: lookahead LR –intermediate in power and cost. Will work for most programming language constructs. Joey Paquet, 2000, 2002, 2012, 2014

8 LR Parsers stack parsing table token stream Joey Paquet, 2000, 2002, 2012, 2014

9 LR Parsers All bottom-up parsers are table-driven, which is derived from an FSA The same algorithm is used to parse, independently on the parsing method used Only the nature of the method used to generate the table distinguishes the parsing method used. All LR methods use a table that has two parts: –action: dictates whether a shift or reduce operation is needed and signifies a state transition –goto: dictates a state transition after a reduce operation Joey Paquet, 2000, 2002, 2012, 2014

10 LR Parsing Algorithm 4 cases: –action is a shift –action is a reduce –action is accept –error push(0) x = top() a = nextToken() repeat forever if ( action[x,a] == shift s’ ) push(a) push(s’) nextToken() else if ( action[x,a] == reduce A  ) multiPop(2*|  |) s’ = top() push(A) push(goto[s’,A]) write(A  ) else if ( action[x,a] == accept ) return true else return false Joey Paquet, 2000, 2002, 2012, 2014

11 Example stateactiongoto id+*()$ETF 0s5s4123 1s6acc 2r2s7r2 3r4 4s5s4823 5r6 6s5s493 7s5s410 8s6s11 9r1s7r1 10r3 11r5 1 E  E + T 2 E  T 3 T  T * F 4 T  F 5 F  (E) 6 F  id FOLLOW E{+,),$} T{+,*,),$} F Joey Paquet, 2000, 2002, 2012, 2014

12 Example stackinputactionderivation 10id+id*id$shift 5id+id*id 20id5+id*id$ reduce (Fid) F+id*id 30F3+id*id$ reduce (TF) T+id*id 40T2+id*id$ reduce (ET) E+id*id 50E1+id*id$shift 6  E+id*id 60E1+6id*id$shift 5  E+id*id 70E1+6id5*id$ reduce (Fid) E+F*id 80E1+6F3*id$ reduce (TF) E+T*id 90E1+6T9*id$shift 7  E+T*id 100E1+6T9*7id$shift 5  E+T*id 110E1+6T9*7id5$ reduce (Fid) E+T*F 120E1+6T9*7F10$ reduce (TT*F) E+T 130E1+6T9$ reduce (EE+T) E E 140E1$accept Joey Paquet, 2000, 2002, 2012, 2014

13 Part II Construction of an LR Parsing Table Joey Paquet, 2000, 2002, 2012, 2014

14 LR Parsing Tables Representation of the states of an FSA The states: –represent what has been parsed in the recent past –control how the FSA will respond to the next token in input The goal is to find and reduce handles. To keep track of how far we have gotten through the growth of the handle, we use items Joey Paquet, 2000, 2002, 2012, 2014

15 Constructing LR Parsing Tables An item is a production with a place marker inserted somewhere in its right-hand-side, indicating the current parsing state in the production, e.g. EE+T has 4 items: Symbols to the left of the dot are already on the stack, those to the right are expected to be coming in input [E E+T] [E E+T] [E E+T] [E E+T] Joey Paquet, 2000, 2002, 2012, 2014

16 Constructing LR Parsing Tables Initial item: an item beginning with a dot Completed item: an item ending with a dot. These are candidates for reduction. When we have a completed item, a handle has been recognized. We can then reduce this handle to its corresponding left hand side non-terminal. All items correspond to a state transition in the FSA Starting from state 0 (starting symbol), we find all possible state transitions using items Joey Paquet, 2000, 2002, 2012, 2014

17 Constructing the item sets State 0 : V[  ] : [S  E]state 1 : V[E] closure(S  E) :[E  E+T]state 1 [E  T]state 2 : V[T] [T  T*F]state 2 [T  F]state 3 : V[F] [F  (E)]state 4 : V[(] [F  id]state 5 : V[id] State 1 : V[E] : [S  E  ]accept [E  E  +T]state 6 : V[E+] State 2 : V[T] : [E  T  ]handle [T  T  *F]state 7 : V[T*] State 3 : V[F] :[T  F  ]handle Joey Paquet, 2000, 2002, 2012, 2014

18 Constructing the item sets State 4 : V[(] : [F  (  E)]state 8 : V[(E] closure(F  (  E)) :[E  E+T]state 8 [E  T]state 2 [T  T*F]state 2 [T  F]state 3 [F  (E)]state 4 [F  id]state 5 State 5 : V[id] : [F  id  ]handle State 6 : V[E+] :[E  E+  T]state 9 : V[E+T] closure(E  E+  T) :[T  T*F]state 9 [T  F]state 3 [F  (E)]state 4 [F  id] state 5 Joey Paquet, 2000, 2002, 2012, 2014

19 Constructing the item sets State 7 : V[T*] : [F  T*  F]state 10 : V[T*F] closure(F  T*  F) :[F  (E)]state 4 [F  id]state 5 State 8 : V[(E] : [F  (E  )]state 11 : V[(E)] [E  E  +T]state 6 State 9 : V[E+T] :[E  E+T  ]handle [T  T  *F]state 7 State 10 : V[T*F] :[F  T*F  ]handle State 11 : V[(E)] :[F  (E)  ]handle Joey Paquet, 2000, 2002, 2012, 2014

20 Filling the Table (1) goto table for each state for each transition A  ... that ( is not a completed item AND has a non-terminal symbol after the dot ) put state(V[  ]) in column  (2) action table : shift actions for each state for each transition A  ... that ( is not a completed item AND has a terminal symbol after the dot ) put a shift action state(V[  ]) in column  (3) action table : reduce actions for each state for each transition that ( is a completed item A   ) for all elements f of FOLLOW(A) put a reduce n action in column f where n is the production number (4) action table : accept action find the completed start symbol item S   and its corresponding state s put an accept action in TT[s,$] Joey Paquet, 2000, 2002, 2012, 2014

21 Filling the Table stateactiongoto id+*()$ETF 0s5s4123 1s6acc 2r2s7r2 3r4 4s5s4823 5r6 6s5s493 7s5s410 8s6s11 9r1s7r1 10r3 11r5 Joey Paquet, 2000, 2002, 2012, E  E + T 2 E  T 3 T  T * F 4 T  F 5 F  (E) 6 F  id FOLLOW E{+,),$} T{+,*,),$} F