Chapter 4-3 Chang Chi-Chung 2008.6.10. SLR and Ambiguity Every SLR grammar is unambiguous, but not every unambiguous grammar is SLR, maybe LR(1) Consider.

Slides:



Advertisements
Similar presentations
Compiler Designs and Constructions
Advertisements

Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
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.
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 – The Items Lecture 10 Mon, Feb 14, 2005.
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.
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.
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #10 Parsing.
Compiler Construction LR Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Section 4.8 Aggelos Kiayias Computer Science & Engineering Department
FE.1 CSE4100 Final Exam Advice and Hints Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 191 Auditorium.
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.
Parsing Theory LHS = non-terminal on left side of production. RHS = possibly empty string of terminals and/or non-terminals on right side of a production.
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.
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.
1 Chapter 6 LR Parsing Techniques. 2 Shift-Reduce Parsers Reviewing some technologies: –Phrase –Simple phrase –Handle of a sentential form S AbC  bCaC.
 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.
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.
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.
Ambiguity in Grammar By Dipendra Pratap Singh 04CS1032.
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.
Announcements/Reading
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.
Earley’s Algorithm J. Earley, "An efficient context-free parsing algorithm", Communications of the Association for Computing Machinery, 13:2:94-102, 1970."An.
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.
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 
March 1, 2009 Dr. Muhammed Al-Mulhem 1 ICS 482 Natural Language Processing SLR Parsing Muhammed Al-Mulhem March 1, 2009.
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.
Announcements/Reading
Lec04-bottomupparser 4/13/2018 LR Parsing.
SLR Parser Problems.
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
50/50 rule You need to get 50% from tests, AND
Compiler Lecture Note, LR Parsing
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
LALR Parsing Canonical sets of LR(1) items
Syntax Analysis Part II
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Lexical and Syntax Analysis
Chapter 4. Syntax Analysis (2)
Compiler SLR Parser.
Syntax Analysis - 3 Chapter 4.
Compiler Construction
Compiler Construction
Chapter 4. Syntax Analysis (2)
Review for the Midterm. Overview (Chapter 1):
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 7, 10/09/2003 Prof. Roy Levow.
Items and Itemsets An itemset is merely a set of items
Presentation transcript:

Chapter 4-3 Chang Chi-Chung

SLR and Ambiguity Every SLR grammar is unambiguous, but not every unambiguous grammar is SLR, maybe LR(1) Consider for example the unambiguous grammar S  L = R | R L  * R | id R  LFOLLOW(R) = {=, $} 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,=]=s6 action[2,=]=r5 no Has no SLR parsing table

LR(0) Automation 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 S L I 0 : S’  S S  L=R S  R L  *R L  id R  L R * id R L R= * L *

Determining Lookaheads for (each item A→ α · β in K) { J = CLOSURE({[A → α · β, # ]}); if ([B →γ·Xδ, a ] is in J, and a is not #) conclude that lookahead a is generated spontaneously for item B →γX·δ in GOTO(I, X); if ([B →γ·Xδ, # ] is in J ) conclude that lookahead propagate from A → α · β in I to B →γX·δ in GOTO(I, X); }

Examples S’→ ·S CLOSURE( [S’→ ·S, #] )  S’→ · S, #  S → · L = R, #  S → ·R, #  L → ·* R, #/=  L → · id, #/=  R → · L, # S  L = R | R L  * R | id R  L L → * · R, = L → id ·, = S’→ S · S → L · = R S → R · L → * · R L → id · R → L · Propagated Spontaneous

Propagation of Lookaheads FromTo I 0 : S’→ ·SI 1 : S’→ S · I 2 : S → L · = R I 2 : R → L · I 3 : S’→ R · I 4 : L → * ·R I 5 : L → id · I 2 : S → L · = RI 6 : S → L = · R I 4 : L → * · R I 5 : L → id · I 7 : L → * R · I 8 : R → L · I 6 : S → L = · RI 4 : L → * ·R I 5 : L → id · I 8 : R → L · I 9 : S → L = R ·

Computation of Lookaheads SET ITEM Lookahead INITPass 1Pass 2Pass 3 I 0 : S’→ ·S $$$$ I 1 : S’→ S · $$$ I 2 : S → L · = R I 2 : R → L · $$$$ $$$$ $$$$ I 3 : S’→ R · $$$ I 4 : L → * ·R ==/$ I 5 : L → id · ==/$ I 6 : S → L = · R $$ I 7 : L → * R · ==/$ I 8 : R → L · ==/$ I 9 : S → L = R · $