CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,

Slides:



Advertisements
Similar presentations
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155.
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.
CS 31003: Compilers  Difference between SLR and LR(1)  Construction of LR(1) parsing table  LALR parser Bandi Sumanth 11CS30006 Date : 9/10/2013.
CSE 5317/4305 L4: Parsing #21 Parsing #2 Leonidas Fegaras.
1 May 22, May 22, 2015May 22, 2015May 22, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
LR Parsing Table Costruction
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
1 Example S'  S, S  CC, C  cC, C  d State 0. Closure({[S'  S, $]}) = { S'  S, $ S   CC, FIRST( $)  S   CC, $ C   cC, FIRST(C$)  C.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
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.
Section 4.8 Aggelos Kiayias Computer Science & Engineering Department
More SLR /LR(1) Professor Yihjia Tsai Tamkang University.
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,
1 LR parsing techniques SLR (not in the book) –Simple LR parsing –Easy to implement, not strong enough –Uses LR(0) items Canonical LR –Larger parser but.
CH4.1 CSE244 Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box.
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
 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.
LESSON 24.
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.
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,
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 
Spring 2014Jim Hogg - UW - CSE - P501E-1 LR ~ Bottom-Up ~ Shift-Reduce - concluded Dotted Items Building the Handles DFA Building Action & Goto Tables.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
–Exercise: construct the SLR parsing table for grammar: S->L=R, S->R L->*R L->id R->L –The grammar can have shift/reduce conflict or reduce/reduce conflict.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
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.
Bernd Fischer RW713: Compiler and Software Language Engineering.
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.
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.
CH4.1 CSE244 Midterm Subjects Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
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.
Bottom-up Parsing.
Introduction to LR Parsing
Bottom-up Parsing.
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
LALR Parsing Canonical sets of LR(1) items
Simple, efficient;limitated
Canonical LR Parsing Tables
Syntax Analysis Part II
Implementing a LR parser engine
Subject Name:COMPILER DESIGN Subject Code:10CS63
Syntax Analysis - LR(1) and LALR(1) Parsing
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
Parsing #2 Leonidas Fegaras.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4. Syntax Analysis (2)
Parsing #2 Leonidas Fegaras.
Kanat Bolazar February 16, 2010
Chapter 4. Syntax Analysis (2)
Chap. 3 BOTTOM-UP PARSING
Lecture 11 LR Parse Table Construction
Items and Itemsets An itemset is merely a set of items
Presentation transcript:

CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT

CH4.2 CSE244 LR(1) Items  DEF. A LR(1) item is a production with a marker together with a terminal: E.g. []  DEF. A LR(1) item is a production with a marker together with a terminal: E.g. [ S  aA.Be, c ] intuition: it indicates how much of a certain production we have seen already (aA) + what we could expect next (Be) + a lookahead that agrees with what should follow in the input if we ever do Reduce by the production S  aABe By incorporating such lookahead information into the item concept we will make more wise reduce decisions.  Direct use of lookahead in an LR(1) item is only performed in considering reduce actions. (I.e. when marker is in the rightmost).  Core of an LR(1) item [] is the LR(0) item  Core of an LR(1) item [ S  aA.Be, c ] is the LR(0) item S  aA.Be   Different LR(1) items may share the same core.

CH4.3 CSE244 Usefulness of LR(1) items  E.g. if we have two LR(1) items of the form   [ A  ., a ] [ B  ., b ] we will take advantage of the lookahead to decide which reduction to use (the same setting would perhaps produce a reduce/reduce conflict in the SLR approach).   How the Notion of Validity changes:   An item [ A   1.  2, a ] is valid for a viable prefix  1 if we have a rightmost derivation that yields  Aaw which in one step yields  1  2 aw

CH4.4 CSE244 Constructing the Canonical Collection of LR(1) items   Initial item: [ S’ .S, $]   Closure. if [A .B , a] belongs to the set of items, and B   is a production of the grammar, then: we add the item [B . , b] for all b  FIRST(  a)  Goto.  Goto. A state containing [A .X , a] will move to a state containing [A  X. , a] with label X   Every state is closed according to Closure.   Every state has transitions according to Goto.

CH4.5 CSE244 Constructing the LR(1) Parsing Table  Shift actions: If is in state I k and I k moves to state I m with label then we add the action action[k, ] = “shift m”  Shift actions: If [A .b , a] is in state I k and I k moves to state I m with label b then we add the action action[k, b] = “shift m”  Reduce actions: If is in state I k then we add the action: “Reduce ” into action[A, ] Observe that we don’t use information from FOLLOW(A) anymore.  Reduce actions: If [A ., a] is in state I k then we add the action: “Reduce A  ” into action[A, a] Observe that we don’t use information from FOLLOW(A) anymore.  Goto part of the table is as before.

CH4.6 CSE244Example S’  S S  L = R | R L  * R | id R  L FIRST S * id L * id R * id

CH4.7 CSE244 Compare to S’  S S  L = R | R L  * R | id R  L I 0 = { S’ .S, S .L = R, S .R, L .* R, L . id, R .L } I 1 = { S’  S. } I 2 = { S  L. = R, R  L. } I 3 = { S  R. } I 4 = { L  *.R, R .L, L .* R, L . id } I 5 = { L  id. } I 6 = { S  L =. R, R .L, L .* R, L . id } I 7 = { L  *R. } I 8 = { R  L. } I 9 = { S  L = R. } action[2, = ] ?s 6 (because of S  L. = R ) r R  L (because of R  L. and = follows R )

CH4.8 CSE244 LALR Parsing  Canonical sets of LR(1) items  Number of states much larger than in the SLR construction  LR(1) = Order of thousands for a standard prog. Lang.  SLR = order of hundreds for a standard prog. Lang.  LALR (lookahead-LR)  A tradeoff:  Collapse states of the LR(1) table that have the same core.  LALR never introduces a Shift/Reduce Conflict.  It might introduce a Reduce/Reduce Conflict (that did not exist in the LR(1))…  Still much better than SLR (larger set of languages)  … but smaller than LR(1)