CH4.1 CSE244 Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box.

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

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 Designs and Constructions
Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
CH4.1 CSE244 SLR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
LR-Grammars LR(0), LR(1), and LR(K).
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.
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.
Pertemuan 12, 13, 14 Bottom-Up Parsing
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Section 4.8 Aggelos Kiayias Computer Science & Engineering Department
CS 536 Spring Bottom-Up Parsing: Algorithms, part 1 LR(0), SLR Lecture 12.
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.
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,
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
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
LR Parsing Techniques Bottom-Up Parsing
BOTTOM-UP PARSING Sathu Hareesh Babu 11CS10039 G-8.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
CSc 453 Syntax Analysis (Parsing) Saumya Debray The University of Arizona Tucson.
CSc 453 Syntax Analysis (Parsing)
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
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.
Chapter 3-3 Chang Chi-Chung Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation)  LR(0), SLR, Canonical LR = LR(1), LALR 
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.
Compilers ABHISHEK REDDY PAM (11CS30002) DATE : 07/10/2013.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
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.
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.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
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,
Chapter 8. LR Syntactic Analysis Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
COMPILER CONSTRUCTION
CSE P501 – Compilers LR Parsing Build Bottom-Up Parse Tree Handles
Introduction to LR Parsing
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
UNIT - 3 SYNTAX ANALYSIS - II
Compiler Construction
Bottom-Up Syntax Analysis
Syntax Analysis Part II
Subject Name:COMPILER DESIGN Subject Code:10CS63
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
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Compiler Construction
Bottom-up parsing is also known as shift-reduce parsing
4d Bottom Up Parsing.
Chap. 3 BOTTOM-UP PARSING
4d Bottom Up Parsing.
Presentation transcript:

CH4.1 CSE244 Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-1155 Storrs, CT

CH4.2 CSE244Example Consider: S  aABe A  Abc | b B  d Rightmost Derivation of the string abbcde: S  aABe  aAde  aAbcde  abbcde The (unique) handle is underlined for each step. A viable prefix is (1) a string that equals a prefix of a right-sentential form up to (and including) its unique handle. (2) any prefix of a string that satisfies (1) Examples: a, aA, aAd, aAbc, ab, aAb,… Not viable prefixes: aAde, Abc, aAA,…

CH4.3 CSE244 Shift/Reduce Parser $abbcde$SHIFT $ab bcde$REDUCE $aA bcde$SHIFT $aAb cde$SHIFT (?) $aAbc de$REDUCE $aA de$SHIFT $aAd e$REDUCE $aAB e$SHIFT $aABe $REDUCE $S $ACCEPT STACKINPUTRemark Observe: all Strings in the stack are viable prefixes

CH4.4 CSE244 When to shift? When to Reduce?  Sometimes on top of the stack something appears to be a handle (i.e., matches the RHS of a production).  But: maybe we have not shifted enough elements to identify the handle.  Observe the correct sequence of Shift and Reduce steps preserves the property that the stack IS a viable prefix. Example Shift or Reduce? $aAb cde$ Shift or Reduce?  If we shift we obtain in the stack.  If we shift we obtain aAbc in the stack.  Recall that Abc is a handle.  Instead if we reduce we obtain in the stack.  Instead if we reduce we obtain aAA in the stack. (this is NOT a viable prefix!!!)

CH4.5 CSE244 When to Shift? When to Reduce? II  In order to make shift/reduce decisions:  We need to look to perhaps a few elements inside the stack.  We need to make sure that the way we modify the stack preserves the “viable prefix condition.”  For our previous example:  Any b appearing to the right of “A” should not be reduced.  In fact we can come up with heuristic decisions based on the grammar structure:  A “b” is reduced only if it is to the right of “a”  PROBLEM: what kind of information do we need to store inside the stack so that we can make decisions as above just by looking at the top element?

CH4.6 CSE244 LR Parsing  LR (left-to-right, rightmost derivation).  LR(1) = 1 lookahead symbol.  Use stack  Stack contains “more information” (in a compressed form) compared to a Top-Down Table- driven parser.  LR(1):  Decisions are taken looking at the top of the stack + 1 input element.

CH4.7 CSE244 Anatomy of an LR parser a+b$ X s X s Input LR Parsing Program Stack Output Parsing Table action[.,.] goto[.,.] (String + terminator) NT + T symbols of CFG What actions parser should take based on stack / input General parser behavior: s : top of stack a : current input 1. If action[s,a]=“accept” halt, accept, success 2.If action[s,a]=“reduce by production A  ” do the following: 2a. Pop 2*|  | elements from the stack. 2b. Push A 2c. Push goto[s*,A] 3.If action[s,a]=“shift and goto state s*” Shift; push s* “States”

CH4.8 CSE244Example 1. S  aABe 2. A  Abc 3. A  b 4. B  d abcde$SAB 0 s1 s19 1 s3 s32 2 s4 s4 s8 s85 3 r3 r3 4 s6 s6 5 s7 s7 6 r2 r2 7 r1 r1 8 r4 r4 9acc actiongoto

CH4.9 CSE244 Example, II $0 abbcde $ STACKINPUTRemark

CH4.10 CSE244 Interesting Fact + LR Parsing Table Construction Methods HOW TO CONSTRUCT SUCH TABLES?  The set of all viable prefixes is Regular.  It is possible to write a DFA that recognizes it!  Use the DFA as an aid to construction of the table. Design Methodologies:  SLR (simple LR) “short table but limited methodology.”  Canonical LR “general methodology but big table.”  LALR (lookahead LR) “in between”