LR Parsers. Aditi Raste, CCOEW

Similar documents
Formal Languages and Compilers Lecture VII Part 3: Syntactic A

S Y N T A X A N A L Y S I S LR

Downloaded from Page 1. LR Parsing

UNIT-III BOTTOM-UP PARSING

Principles of Programming Languages

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MODULE 14 SLR PARSER LR(0) ITEMS

LR Parsing - The Items

SLR parsers. LR(0) items

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Context-free grammars

Compiler Construction: Parsing

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

LR Parsing Techniques

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Lexical and Syntax Analysis. Bottom-Up Parsing

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

Compiler Construction 2016/2017 Syntax Analysis

LR Parsing Techniques

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

Concepts Introduced in Chapter 4


Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

CS 4120 Introduction to Compilers

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

LALR Parsing. What Yacc and most compilers employ.

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

Lecture 8: Deterministic Bottom-Up Parsing

Lecture Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

shift-reduce parsing


Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

UNIT III & IV. Bottom up parsing

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

CS606- compiler instruction Solved MCQS From Midterm Papers

Bottom-Up Parsing LR Parsing

4. Lexical and Syntax Analysis

Compilers. Bottom-up Parsing. (original slides by Sam

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

4. Lexical and Syntax Analysis

CS308 Compiler Principles Syntax Analyzer Li Jiang

Bottom-Up Parsing II. Lecture 8

Syntax Analysis Part I

Exercises II. Exercise: Lexical Analysis

Chapter 4: LR Parsing

Wednesday, August 31, Parsers

Algorithms for NLP. LR Parsing. Reading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section , pp.

Bottom-Up Parsing. Lecture 11-12

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

Parsing. Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Wednesday, September 9, 15. Parsers

Bottom-Up Parsing. Lecture 11-12

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

VIVA QUESTIONS WITH ANSWERS

The role of the parser

Monday, September 13, Parsers

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

Chapter 2 :: Programming Language Syntax

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

CSE302: Compiler Design

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

How do LL(1) Parsers Build Syntax Trees?

Chapter 4. Lexical and Syntax Analysis

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

Syntactic Analysis. Chapter 4. Compiler Construction Syntactic Analysis 1

Conflicts in LR Parsing and More LR Parsing Types

CS 314 Principles of Programming Languages

Top down vs. bottom up parsing

RECURSIVE ASCENT-DESCENT PARSERS

CS453 : Shift Reduce Parsing Unambiguous Grammars LR(0) and SLR Parse Tables by Wim Bohm and Michelle Strout. CS453 Shift-reduce Parsing 1

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

Bottom-up Parser. Jungsik Choi

Revisit the example. Transformed DFA 10/1/16 A B C D E. Start

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

Syntax Analyzer --- Parser

Introduction to Parsing. Comp 412

Introduction. Introduction. Introduction. Lexical Analysis. Lexical Analysis 4/2/2019. Chapter 4. Lexical and Syntax Analysis.

Transcription:

LR Parsers Aditi Raste, CCOEW 1

LR Parsers Most powerful shift-reduce parsers and yet efficient. LR(k) parsing L : left to right scanning of input R : constructing rightmost derivation in reverse k : number of symbols of lookahead that are used in making parsing decisions ( when k is omitted, k is assumed to be 1) Aditi Raste, CCOEW 2

Why LR parsers? Most general table driven non-backtracking and efficient shift-reduce parsing. Class of grammars that can be parsed using LR method is a proper superset of class of grammars that can be parsed with predictive parsers LL(1) grammars C LR(1) grammars Detects a syntactic error as soon as it is possible to do so on a left to right scan of the input. LR parsers can be constructed to recognize virtually all programming language constructs for whi h CFG s a e writte. Aditi Raste, CCOEW 3

LR parser The principle drawback of the LR method is that it is too much work to construct an LR parser by hand for a typical programming language grammar. Fortunately this process can be automated. Many parser generators are available. Parser generators can locate ambiguous constructs in the grammar or constructs that are difficult to parse in a left-to-right scan of the input and also can provide detailed diagnostic messages. Aditi Raste, CCOEW 4

Flavours of LR parsers SLR :- Simple left and right parser LR(1):- Canonical LR. Most general LR parser LALR:- Lookahead LR parser. Intermediate LR SLR, LR(1) and LALR use the same algorithm for parsing but differ only in their parsing tables. Powers relative to each other SLR 1 LALR 1 LR 1 Aditi Raste, CCOEW 5

SLR (Simple LR) Aditi Raste, CCOEW 6

SLR Parser How does a shift-reduce parser know when to shift and when to reduce? T cannot be a handle here Right Sentential form Handle Production id * id id F -> id F * id F T -> F T * id id F -> id T * F T * F T -> T * F T T E-> T Aditi Raste, CCOEW 7

SLR Parser An LR parser makes shift-reduce decisions by maintaining states to keep track of where we are in a parse. States represent set of items. An item indicates how much of a production we have seen at a given point in the parsing process. Aditi Raste, CCOEW 8

Model of LR Parser Input a + b $ Stack X Y LR Parsing Program Output Z Driver program $ s0. ACTION GOTO LR parsing table sn Aditi Raste, CCOEW 9

Tasks of the driver program Invoke lexical analyzer for next token Initialize stack with start symbol Act like an FA Determine the state Sj on tos and ai the current input symbol Determine the action corresponding to [Sj, ai] Aditi Raste, CCOEW 10

Actions of LR parser Si : Shift and stack state i rj : Reduce by production rule numbered j Accept Error Aditi Raste, CCOEW 11

LR Parser Example Input a + b $ Stack X Y LR Parsing Program Output Z $ s0. ACTION GOTO LR parsing table sn Aditi Raste, CCOEW 12

LR Parser Example Consider expression grammar 1. E -> E + T 2. E -> T 3. T -> T * F 4. T -> F 5. F -> (E) 6. F -> id Aditi Raste, CCOEW 13

Parsing table for expression grammar Parse the input string id + id * id State ACTION GOTO id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 Aditi Raste, CCOEW 14

Working of the LR parser Aditi Raste, CCOEW 15

Working of the LR parser CFG Construction of LR(0) items Construction of LR parsing table Parsing of input string Input string Output Aditi Raste, CCOEW 16

LR(0) Items An LR(0) item is a string [α] where, α is a production from Grammar with a. at some position in the RHS. The indicates how much of the item we have seen at a given state in the parse. [A = XYZ] indicates that the parser is looking for a string that can be derived from XYZ. [A = XY Z] indicates that the parser has seen a string derived from XY and is looking for one derivable from Z. Aditi Raste, CCOEW 17

LR(0) Items (no lookahead) A => XYZ generates 4 LR(0) items 1. [ A => XYZ] 2. [ A => X YZ] 3. [ A => XY Z] 4. [ A => XYZ ] Aditi Raste, CCOEW 18

Canonical LR(0) Collection of Items The SLR table construction algorithm uses a specific set of sets of LR(0) items. These sets are called canonical collection of sets of LR(0) items for grammar G. The canonical collection represents the set of valid states for an LR parser. Aditi Raste, CCOEW 19

Canonical LR(0) Collection of Items To construct the canonical LR(0) collection for a grammar, we define Augmented grammar Two functions:- CLOSURE function GOTO function Aditi Raste, CCOEW 20

Canonical LR(0) Collection of Items Augmented Grammar If G is a gra ar with start s ol the G, the augmented grammar for G, is G with a new start s ol a d produ tio => Purpose: Augmented grammar tells the parser when to stop parsing and announce the acceptance of the input. Acceptance only occurs when and only when the parser is about to reduce =>. Aditi Raste, CCOEW 21

States of the PDA (Closure of Item sets) Each LR(0) item corresponds to a point in the parse. To generate a parser state from an LR(0) item we take its closure. Aditi Raste, CCOEW 22

Closure of set of items Suppose I is a set of items, we define CLOSURE(I) as, (i) Every item in I is in CLOSURE(I) (ii) If A => α Bβ is in COSURE(I) and B => ϒ is a production then add the item B => ϒ to I (if not already in I) Apply this rule until no more new items can be added to CLOSURE(I). Aditi Raste, CCOEW 23

Set of Items Two classes a) Kernel items:- (i) I itial ite => (ii) All items whose dots are not at the left end b) Non Kernel items:- All items with their dots at the left end e ept for => Aditi Raste, CCOEW 24

Transitions of PDA ( GOTO function) There will be a transition from one state to another state for each grammar symbol in an item that immediately follows the marker in an item in that state. If an item in the state is [A => α Xβ ] then transition from this state occurs when X is processed. transition is to the state that is the closure of the item [ A => αx β ] Aditi Raste, CCOEW 25

GOTO(I, X) I : set of items X: grammar symbol GOTO ( I,X) function GOTO(I,X) is defined to be the closure of the set of all items [ A => αx β ] such that [ A => α Xβ ] is in I. Aditi Raste, CCOEW 26