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

Similar documents
Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

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

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

CS 4120 Introduction to Compilers

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

shift-reduce parsing

UNIT III & IV. Bottom up parsing

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

Lexical and Syntax Analysis. Bottom-Up Parsing

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.

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

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

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

LR Parsing. The first L means the input string is processed from left to right.

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

LR Parsing - The Items

LR Parsing E T + E T 1 T

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

Lecture Bottom-Up Parsing

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

Context-free grammars

Lecture 7: Deterministic Bottom-Up Parsing

Compiler Construction: Parsing

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

Lecture 8: Deterministic Bottom-Up Parsing

Downloaded from Page 1. LR Parsing

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12

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

Bottom-up Parser. Jungsik Choi

In One Slide. Outline. LR Parsing. Table Construction

LR Parsers. Aditi Raste, CCOEW

LALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the

Conflicts in LR Parsing and More LR Parsing Types

LL Parsing: A piece of cake after LR

More Bottom-Up Parsing

CS308 Compiler Principles Syntax Analyzer Li Jiang

Chapter 3: Lexing and Parsing

BSCS Fall Mid Term Examination December 2012

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

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

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

Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

Chapter 4. Lexical and Syntax Analysis

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

Wednesday, August 31, Parsers

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

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Principles of Programming Languages

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

UNIT-III BOTTOM-UP PARSING

4. Lexical and Syntax Analysis

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

4. Lexical and Syntax Analysis

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

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

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

MODULE 14 SLR PARSER LR(0) ITEMS

Building Compilers with Phoenix


Wednesday, September 9, 15. Parsers

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

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.

LALR Parsing. What Yacc and most compilers employ.

Compiler Construction 2016/2017 Syntax Analysis

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

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

CS 314 Principles of Programming Languages. Lecture 3

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

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

LR Parsing. Table Construction

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

Monday, September 13, Parsers

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

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

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

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

Parsing. COMP 520: Compiler Design (4 credits) Professor Laurie Hendren.

Top down vs. bottom up parsing

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

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

CS 314 Principles of Programming Languages

LR Parsing Techniques

General Overview of Compiler

Models of Computation II: Grammars and Pushdown Automata

CS606- compiler instruction Solved MCQS From Midterm Papers

Programming Language Syntax and Analysis

SLR parsers. LR(0) items

Syntax-Directed Translation. Lecture 14

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Key to Homework #8. (a) S aa A bs bbb (b) S AB aaab A aab aaaaaaab B bba bbbb

CT32 COMPUTER NETWORKS DEC 2015

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

3. Parsing. Oscar Nierstrasz

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Transcription:

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

Plan for Today Finish PA1 this week Friday recitation: help with PA1 Also on Thursday and Friday, form groups for PA2 and on Shift-reduce parsing, LR(1) The problem with predictive top down parsing LR parsing: bottom up Performs a right-most derivation in reverse Parsing unambiguous grammars LR parsing table and parsing algorithm CS453 Shift-reduce Parsing 2

LL vs LR LL(k) must predict which production looking ahead k: S à SS (S) ε is it S à SS or (S) when I see ((((.? produces the parse tree TOP DOWN, with a left to right derivation LR(k) postpones the decision until all tokens of the rhs plus k more tokens have been seen. It therefore is more powerful. It does this by parsing BOTTOM UP CS453 Shift-reduce Parsing 3

Shift-reduce parsing in an LR parser LR(k) parser Left-to-right parse Right-most derivation K-token look ahead LR parsing algorithm Performs a shift-reduce parse with an explicit stack stack contains grammar symbols (T or V) mixed with states Look at state at top of stack and input symbol to find action in table shift(n): advance input, push state n on stack reduce(k): pop rhs of grammar rule k, look up state on top of stack and lhs for goto n, push lhs(k) and n onto stack accept: stop and success error: stop and fail CS453 Shift-reduce Parsing 4

Example LR parse S à AB Aà Aa a B à Bb b S à S $ aaabb$ß Aaabb$ß Aabb$ß Abb$ß ABb$ ß AB$ß S$ß S Notice that this is the rightmost derivation S à S$à AB$à ABb$à Abb$à Aabb$à Aaabb$à aaabb$ in reverse! It does not start with the start symbol, it ends with it LR(k) parsing scans the input Left to right and produces the Rightmost derivation (looking k tokens ahead) in reverse. CS453 Shift-reduce Parsing 5

LR parsing engine The LR parsing engine uses an Explicit Stack (as opposed to the implicit stack of LL) and the input token string and performs two kinds of actions Shift -- push input token onto the stack Reduce -- pop a rhs off the stack and push the corresponding lhs on the stack It accepts the string of tokens when the input is empty or $/(EOF) and the stack contains the start symbol. CS453 Shift-reduce Parsing 6

Simplified example LR parsing engine ac4ons S à AB Aà Aa a B à Bb b S à S $ Stack input action aaabb$ shift a aabb$ reduce : Aà a A aabb$ shift Aa abb$ reduce: Aà Aa A abb$ shift Aa bb$ reduce: Aà Aa A bb$ shift Ab b$ reduce: Bà b AB b$ shift ABb $ reduce: Bà Bb AB $ reduce: Sà AB S $ accept CS453 Shift-reduce Parsing 7

Shift reduce parsing applied to unambiguous grammars [0] S à ( S ) [1] S à S $ [2] S à ID Single parentheses nest Start symbol is S Stack input action ((ID))$ shift ( (ID))$ shift (( ID))$ shift ((ID ))$ reduce: Sà Id ((S ))$ shift ((S) )$ reduce: Sà (S) (S )$ shift (S) $ reduce: Sà (S) S $ accept CS453 Shift-reduce Parsing 8

Table driven LR parse algorithm The stack contains grammar symbols and states: after a symbol is pushed, the next state is pushed The actions can now be formulated as: Sn: push input token; push n (ie goto state n) Rk: rule k: lhs à rhs pop rhs of rule k off the stack, this exposes a state s look up in row s and column lhs the next state gn to goto push lhs; push n (ie goto state n) a: stop parsing, report success _: stop parsing, report error CS453 Shift-reduce Parsing 9

LR(0) SLR LALR LR(k) Given the shift reduce machinery described, there are various algorithms for creating the parse table. These algorithms get more and more sophisticated, starting from simple LR(0) Parser generators such as java-cup use the more sophisticated LALR parse table creation algorithm Today the goal is to understand how to do a shift and reduce parse given a table. Later we will discuss how to get the table. CS453 Shift-reduce Parsing 10

Table Driven LR Parsing The parsing engine is a DFA plus a stack: a determinis7c pushdown automaton. The DFA is described by a transi7on table: Rows are states Columns are Grammar symbols (terminals, non- terminals) terminals: shid or reduce and go to next state non- terminals: decide which state to go to ader a reduc7on So there are four kinds of ac7ons sn: shid input and go to state n rk: reduce according to rule k: lhs à rhs, this exposes a state s look up in row s and column lhs the next state gn to goto push lhs; push n (ie goto state n) a: accept _: error (indicated by blank state) CS453 Shift-reduce Parsing 11

Example LR(0) Parse Table, Single Parentheses Nest [0] S -> ( S ) [1] S -> S $ [2] S -> ID Action Goto State ( ) $ ID S 0 s3 s1 2 1 r2 r2 r2 r2 2 a 3 s3 s1 4 4 s5 5 r0 r0 r0 r0 In LR(0) parse tables, reduce actions happen for all terminals, as the parser does not look ahead. CS453 Shift-reduce Parsing 12

Parse table for List grammar 0: S à S$ 1: Sà ( L ) 2: Sà x 3: Là S 4: Là L, S ( ) x, $ S L 0 s2 s1 g3 1 r2 r2 r2 r2 r2 2 s2 s1 g6 g4 3 a 4 s5 s7 5 r1 r1 r1 r1 r1 6 r3 r3 r3 r3 r3 7 s2 s1 g8 8 r4 r4 r4 r4 r4 parse (x,(x))$ stack input action 0 (x,(x))$ s2 0(2 x,(x))$ s1 0(2x1,(x))$ r2: Sà x 0(2S6,(x))$ r3: Là S 0(2L4,(x))$ s7 0(2L4,7 (x))$ s2 0(2L4,7(2 x))$ s1 0(2L4,7(2x1 ))$ r2: Sà x 0(2L4,7(2S6 ))$ r3: Là S 0(2L4,7(2L4 ))$ s5 0(2L4,7(2L4)5 )$ r1: S à (L) 0(2L4,7S8 0(2L4 )$ s5 0(2L4)5 03S )$ r4: Là L,S $ r1:sà (L) $ a CS453 Shift-reduce Parsing 13