Introduction to LR Parsing

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

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.
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.
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.
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,
CH4.1 CSE244 Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
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.
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.
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 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.
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 
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
Syntax and Semantics Structure of programming languages.
CSE P501 – Compilers LR Parsing Build Bottom-Up Parse Tree Handles
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-Up Parsing.
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Revision ? E  TE  E   + TE  |  T  FT  T   * FT  | 
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Bottom-Up Syntax Analysis
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.
Chapter 4. Syntax Analysis (2)
LR Parsing. Parser Generators.
Bottom-Up Parsing “Shift-Reduce” Parsing
4d Bottom Up Parsing.
Kanat Bolazar February 16, 2010
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Compiler Construction
Bottom-up parsing is also known as shift-reduce parsing
Chapter 4. Syntax Analysis (2)
4d Bottom Up Parsing.
Chap. 3 BOTTOM-UP PARSING
4d Bottom Up Parsing.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-1155 Storrs, CT 06269 aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias

Example 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,…

Shift/Reduce Parser Observe: all STACK INPUT Remark $ abbcde$ SHIFT $a bbcde$ 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 Observe: all Strings in the stack are viable prefixes

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 (real) handle. Observe the correct sequence of Shift and Reduce steps preserves the property that the stack IS a viable prefix. Example $aAb cde$ Shift or Reduce? If we shift we obtain aAbc in the stack. Recall that Abc is a handle. Instead if we reduce we obtain aAA in the stack. (this is NOT a viable prefix!!!)

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?

LR Parsing LR (left-to-right, rightmost derivation). LR(1) = 1 lookahead symbol. Use stack Stack should contain “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.

Parsing Table action[.,.] goto[.,.] Anatomy of an LR parser a + b $ Input (String + terminator) “States” Stack NT + T symbols of CFG LR Parsing Program X s Output What actions parser should take based on stack / input Parsing Table action[.,.] goto[.,.] General parser behavior: s : top of stack a : current input 1. If action[s,a]=“accept” halt, accept, success 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] If action[s,a]=“shift and goto state s*” Shift; push s*

Example action goto S A B s1 9 1 s3 2 s4 s8 5 3 r3 4 s6 s7 6 r2 7 r1 8 1. S  aABe 2. A  Abc 3. A  b 4. B  d action goto a b c d e $ S A B s1 9 1 s3 2 s4 s8 5 3 r3 4 s6 s7 6 r2 7 r1 8 r4 acc

Example, II $0 abbcde$ STACK INPUT Remark

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”