Pertemuan 12, 13, 14 Bottom-Up Parsing

Slides:



Advertisements
Similar presentations
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

Compiler Designs and Constructions
CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
Review: LR(k) parsers a1 … a2 … an $ LR parsing program Action goto Sm xm … s1 x1 s0 output input stack Parsing table.
LR Parsing Table Costruction
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.
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.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
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.
– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:
Bottom-up parsing Goal of parser : build a derivation
LALR Parsing Canonical sets of LR(1) items
 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.
LR Parsing Techniques Bottom-Up Parsing
BOTTOM-UP PARSING Sathu Hareesh Babu 11CS10039 G-8.
Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
SLR PARSING TECHNIQUES Submitted By: Abhijeet Mohapatra 04CS1019.
LR(k) Parsing CPSC 388 Ellen Walker Hiram College.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
 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.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
Lesson 9 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
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.
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
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.
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.
Conflicts in Simple LR parsers A SLR Parser does not use any lookahead The SLR parsing method fails if knowing the stack’s top state and next input token.
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Lec04-bottomupparser 4/13/2018 LR Parsing.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
The role of parser Lexical Analyzer Parser Rest of Front End Symbol
lec04-bottomupparser June 6, 2018 Bottom-Up Parsing
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Revision ? E  TE  E   + TE  |  T  FT  T   * FT  | 
Bottom-up Parsing.
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
LALR Parsing Canonical sets of LR(1) items
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis source program lexical analyzer tokens syntax analyzer
4d Bottom Up Parsing.
BOTTOM UP PARSING Lecture 16.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Bottom Up Parsing.
Bottom-Up Parsing “Shift-Reduce” Parsing
Chap. 3 BOTTOM-UP PARSING
Presentation transcript:

Pertemuan 12, 13, 14 Bottom-Up Parsing Matakuliah : T0174 / Teknik Kompilasi Tahun : 2005 Versi : 1/6 Pertemuan 12, 13, 14 Bottom-Up Parsing

Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mahasiswa dapat menjelaskan prinsip kerja bottom up parsing yang diimplementasikan dengan stack (C2) Mahasiswa dapat mendemonstrasikan pembuatan LR parsing (C3) Mahasiswa dapat mendemonstrasikan pembuatan SLR parsing table dan proses parsingnya (C3)

Jenis-jenis bottom-up parsing Shift reduce parsing Outline Materi Jenis-jenis bottom-up parsing Shift reduce parsing Implementasi dengan stack Operator precedence parsing LR-parsing algoritma LR parsing Konstruksi LR parsing table Konstruksi SLR parsing tabel

Contents Introduction of Bottom-Up Parsing Handles of String Stack Implementation of Shift-Reduce Parsing Conflict During Shift-Reduce Parsing LR Parsers LR(k) Parsers Constructing SLR (Simple LR) Parser Constructing LR Parsing Table LALR Parsing Table Using Ambiguous Grammars

Introduction of Bottom-Up Parser Also called shift-reduce parser Construct a parse tree for an input string beginning at the leaves and working up toward the root Reducing a string w to the start symbol S At each reduction step, a particular substring RHS of production is replaced with by the symbol on LHS e.g. S → aABe A → Abc | b B → d process w = abbcde Then abbcde → aAbcde → aAde → aABe → S (reduction steps) S → aABe → aAde → aAbcde → abbcde (rightmost derivation)

Handles of String (1/2) Handles of a string A substring that matches the right side of a production and whose reduction to the non-terminal on LHS presents one step along the reverse of a right derivation Formally, handle of a right sentential form γ is a production A → β and a position of γ where the string β may be found and replaced by A to produce the previous right sentential form in a rightmost derivation of γ e.g. From the above example abbcde is a right sentential form whose handle is A → b and aAbcde has a handle A → Abc and so on. If a grammar is unambiguous, there exist only one handle for every right sentential form

Handles of String (2/2) Ambiguous Grammar Case Example 1) E → E + E E → id Example 1 has two different rightmost derivations of the same string id + id * id implies that some of the right sentential form has more than one handle e.g E → id and E → E + E are handles from E + E * id

Stack Implementation of Shift-Reduce Parsing Next input symbol is shifted onto the top of the stack Reduce A handle on the stack is replaced by the corresponding non-terminal (A handle always appears on the top of the stack) Accept Announce the successful completion Stack Content Input Action 1 $ id + id * id $ shift 2 id $ + id * id $ reduce by E → id 3 E $ 4 + E $ id * id $ 5 id + E $ * id $ 6 E + E $ 7 * E + E $ 8 id * E + E $ 9 E * E + E $ reduce by E → E*E 10 reduce by E+E 11 accept

Conflict During Shift-Reduce Parsing Shift/Reduce conflict Cannot decide shift or reduce Reduce/Reduce conflict Cannot decide which production to use for reduce e.g. stmt → if expr then stmt | if expr then stmt else stmt | other stack has a handle "if expr them stmt"  : shift/reduce conflict

LR(k) Parsers Concept left to right scan and rightmost derivation with k lookahead symbols Input tape Stack LR Parsing Program output Action/Goto Table Parsing Table

Constructing SLR (Simple LR) parser (1/9) Viable Prefix A prefix of a right sentential form that does not continue past the rightmost handle of that sentential form. It always appears the top of the stack of the shift-reduce parser LR(0) item of G A production of G with a dot at some position of the RHS e.g. A → XYZ ⇒ A → ·XYZ , A → X·YZ , A → XY·Z , A → XYZ· Central idea of SLR construct a DFA that recognize viable prefixed. The state of the DFA consists of a set of items

Constructing SLR (Simple LR) parser (2/9) Three operations for construction Augmenting a grammar Add S' → S to indicate the parser when it should stop and accept closure operation Suppose I is a set of items for G, then closure(I) 〓 ① every item in I is added to closure(I) ② If A → α·Bβ ∈ closure(I) & B → γ exists, then add B → ·γ to cloasure(I) e.g.   E' → E, E → E + T | T, T → T * F | F, F → (E) | id Start with I = { E' → ·E}, then closure(I) = { E' → ·E,  E → ·E + T, E →·T, T → ·T * F, T → ·F, F → · (E) F → ·id } kernel items (dots are not at the left end) vs. non-kernel items

Constructing SLR (Simple LR) parser (3/9) Three operations for construction goto operation Suppose I be a set of items and X be a grammar symbol, then goto(I, X) = the closure of the set of all items [A → αX·β] such that A → α·Xβ ∈ I e.g. Suppose I = { E' → E·, E → E·+ T}, then goto(I, +) 〓 { E → E + ·T, T → ·T * F, T → ·F, F → · (E), F → ·id }

Constructing SLR (Simple LR) parser (4/9) Draw state diagram for the following augmented grammar e.g.   E' → E , E → E + T | T, T → T * F | F, F → (E) | id I0 I10 I7 ( TT*F· TT*·F F· (E) F·id I4 E`·E E·E+T E·T T·T*F T·F F· (E) F·id F I11 F(E) · I2 T ET· TT·*F * F ) I3 I8 TF· ( EE·+T F(E·) id E I5 I1 F E`E. EE.+T Fid· I4 + I6 F(·E) E·E+T E·T T·T*F T.F F· (E) F·id E EE+·T T·T*F T·F F· (E) F·id id I6 id + ( F I9 EE+T· TT·*F I3 T ( * I7

Constructing SLR (Simple LR) parser (5/9) SLR Parsing table ① Build a DFA from the given grammar ② Find follow(A) ∀nonterminal ③ determine parsing actions for each I a) if [A → α․aβ]∈Ii and goto(Ii, a) = Ij then set action[i,a] = shift j(Sj) b) if [A → α·] ∈Ii     then set action[i, a] = reduce A → α ∀a in FOLLOW(A) except A = S' c) if [S' → S·] ∈Ii then set action[i, $] = accept ④ For all nonterminal A if goto(Ii, A) = Ij then set goto[i, A] = j

Constructing SLR (Simple LR) parser (6/9) SLR Parsing table ⑤ For all other entries are made "error" ⑥ Initial state is one containing [S' → S·] e.g     1) E → E + T 2) E → T 3) T → T * F, 4) T →  F 5) F → (E) 6) F →  id FOLLOW(E) = { +, $, )} FOLLOW(T) = {*,+,$,)} FOLLOW(F) = {*,+,$,)} 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

Constructing SLR (Simple LR) parser (7/9) Executing a parser with the parsing table configuration (S0X0S1X1 … XmSm, aiai+1…am$) = (stack content, unexpended input) Resulting configuration after action[Sm, ai] i) = Sj (shift and goto state j) (S0X0S1X1 … XmSmaiS, ai+1…an$) ii) = rp (reduce A → β) (S0X0S1X1 … Xm-rSm-rAS, aiai+1…an$) where S = goto[Sm-r, A] and r = iii) accept (parsing is completed) iv) error (error recovery is needed)

Constructing SLR (Simple LR) parser (8/9) Executing a parser with the parsing table stack input action 1 0$ id * id + id$ shift 2 5id0$ * id + id$ reduce F → id 3 3F0$ reduce T →  F 4 2T0$ 5 7*2T0$ id + id$ 6 5id7*2T0$ + id$ reduce F →  id 7 10F7*2T0$ reduce T  →  T * F 8 reduce E  →  T 9 1E0$ 10 6+1E0$ id$ 11 5 id6+1E0$ $ reduce F  →  id 12 3F6+1E0$ reduce T  →  F 13 9T6+1E0$ reduce E  →  E + T 14 accept

Constructing SLR (Simple LR) parser (9/9) A grammar that is not ambiguous, not SLR(1) S → L = R , S → R , L → * R , L → id , R → L Then, FOLLOW(R)  = FLLOW(S) = FOLLOW(L) = { = } Action[2, =] → Shift or Reduce Because SLR is not powerful enough to remember sufficient left context to decide next action on "=" S`·S S·L=R S·R L·*R L·id R·L SL·=R RL· SL=·R R·L L·*R L·id I0 I2 I6 L =

Constructing LR Parsing Table (1/3) Central idea In SLR, reduction of A → α is determined by looking to see if a comes after α while LR sees if βAa is allowed Redefinition of items to include a terminal symbol [A → α·β, a] The lookahead symbol a has no effect when β ≠ ε a ∈ FOLLOW(A) How to find the collection of sets of valid items

Constructing LR Parsing Table (2/3) Closure(I) if [A → α·Bβ, a] ∈ I, for each B → γ in G' add [ B → ·γ, FIRST(βα)] to I repeat until no more production is added. goto[I, x] if [A → α·Xβ, a] ∈ I, create J with [A → αX·β, a], and find closure(J)

Constructing LR Parsing Table (3/3) Example S' → S, S → CC, C → cC | d I8 I4 CcC·, c/d Cd·, c/d State  Action Goto c d $ S C S3 S4 1 2 Accept S6 S7 5 3 8 4 R3 R1 6 9 7 R2 d I3 C I0 Cc·C, c/d C·cC, c/d C·d, c/d S`·S, $ S·CC, $ C·cC, c/d C·d, c/d c c C I2 S SC·C, $ C·cC, $ C·d, $ C I5 I1 S`S, $ c I6 Cc·C, $ C·cC, $ C·d, $ C d I5 I7 <LR(1) Parsing Table > SCC·, $ Cd·, $ I9 c CcC·, $ <LR(1) Finite State Diagram>