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

Similar documents
CS415 Compilers. LR Parsing & Error Recovery

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

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

Context-free grammars

Compiler Construction: Parsing

LALR Parsing. What Yacc and most compilers employ.

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

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

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

Lecture 7: Deterministic Bottom-Up Parsing

Lecture 8: Deterministic Bottom-Up Parsing

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

LR Parsing Techniques

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

Principles of Programming Languages

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Wednesday, August 31, Parsers

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

Conflicts in LR Parsing and More LR Parsing Types

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

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

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

Monday, September 13, Parsers

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

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


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

Lexical and Syntax Analysis. Bottom-Up Parsing

Wednesday, September 9, 15. Parsers

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

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.

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

Bottom-Up Parsing. Lecture 11-12

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


LR Parsing LALR Parser Generators

Bottom-Up Parsing. Lecture 11-12

Concepts Introduced in Chapter 4

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

LR Parsing LALR Parser Generators

UNIT III & IV. Bottom up parsing

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

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

UNIT-III BOTTOM-UP PARSING

In One Slide. Outline. LR Parsing. Table Construction

Compiler Construction 2016/2017 Syntax Analysis

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

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

Lecture Bottom-Up Parsing

4. Lexical and Syntax Analysis

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

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

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

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

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

4. Lexical and Syntax Analysis

Bottom-up Parser. Jungsik Choi

LR Parsing Techniques

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

CS 4120 Introduction to Compilers

Downloaded from Page 1. LR Parsing

Chapter 4. Lexical and Syntax Analysis

3. Parsing. Oscar Nierstrasz

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

LR Parsers. Aditi Raste, CCOEW

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

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

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

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

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.

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

COMPILER (CSE 4120) (Lecture 6: Parsing 4 Bottom-up Parsing )

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Configuration Sets for CSX- Lite. Parser Action Table

Lecture Notes on Shift-Reduce Parsing

More Bottom-Up Parsing

MODULE 14 SLR PARSER LR(0) ITEMS

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

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

Lecture Notes on Bottom-Up LR Parsing

Acknowledgements. The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal

Bottom-Up Parsing II. Lecture 8

How do LL(1) Parsers Build Syntax Trees?

Syntax Analysis Part I

VIVA QUESTIONS WITH ANSWERS

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

Bottom-Up Parsing LR Parsing

CSCI312 Principles of Programming Languages

Chapter 4: LR Parsing

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

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

CSE431 Translation of Computer Languages

Lecture Notes on Bottom-Up LR Parsing

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

Syntactic Analysis. Top-Down Parsing

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

CS502: Compilers & Programming Systems

Transcription:

Parsing Wrapup Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing LR(1) items Computing closure Computing goto LR(1) canonical collection This lecture LR(1) parsing Building ACTION / GOTO tables Shift/reduce and reduce/reduce conflicts SLR(1), LALR(1), operator precedence Error recovery 2 1

Filling in the LR(1) ACTION and GOTO Tables The algorithm set s x S item i s x if i is [A β a γ,b] and goto(s x,a) = s k, a T then ACTION[x,a] shift k x is the number of the state for s x // to left of terminal a // shift if lookahead = a else if i is [S S,$] // start production done, then ACTION[x, $] accept // accept if lookahead = $ else if i is [A β,a] // all the way to right then ACTION[x,a] reduce A β // production done n NT // reduce if lookahead = a if goto(s x,n) = s k then GOTO[x,n] k // store transitions for nonterminals Many items generate no table entry e.g., [A β Bα,a] does not, but closure ensures that all the rhs for B are in s x 3 Example Building LR(1) ACTION and GOTO Table Entries for Shift 4 2

Example Building LR(1) ACTION and GOTO Table Entry for Accept 5 Example Building LR(1) ACTION and GOTO Table Entries for Reduce 6 3

Example Building LR(1) ACTION and GOTO Table Entries for Goto 7 What can go wrong? What if set s contains [A β aγ,b] and [B β,a]? First item generates shift, second generates reduce Both define ACTION[s,a] cannot do both actions This is a fundamental ambiguity, called a shift/reduce conflict What if set s contains [A γ, a] and [B γ, a]? Each generates reduce, but with a different production Both define ACTION[s,a] cannot do both reductions This fundamental ambiguity is called a reduce/reduce conflict In either case, the grammar is not LR(1) Solutions Modify grammar to eliminate conflict Specify how conflict should be resolved Can be used to assign precedence & associativity (to operators) 8 4

Shift/reduce Error Example Grammar State in canonical collection of LR(1) items Entry in ACTION / GOTO table for lookahead + 9 Reduce/reduce Error Example Grammar P1 S E P2 E A P3 a P4 A a State in canonical collection of LR(1) items [ E a,, $ ] [ A a,, $ ] Entry in ACTION / GOTO table for lookahead $ reduce P3 ( E a ) reduce P4 ( A a ) 10 5

Left Recursion versus Right Recursion Right recursion Required for termination in top-down parsers Produces right-associative operators Left recursion Works fine in bottom-up parsers Limits required stack space Produces left-associative operators Rule of thumb Left recursion for bottom-up parsers Right recursion for top-down parsers * * w * x z y w * ( x * ( y * z ) ) * * * z y w x ( (w * x ) * y ) * z 11 Associativity Normally defined by programming language What difference does it make? Can change answers in floating-point arithmetic Exposes a different set of common subexpressions Consider x+y+z + x y z x + + + + y z x y z Ideal operator Right association Left association What if y+z occurs elsewhere? Or x+y? or x+z? What if x = 2 & z = 17? Neither left nor right exposes 19 I.e., optimizer cannot replace x+z with 19 in output code 12 6

Resolving Conflicts Precedence and associativity may be used to resolve conflicts in ambiguous grammars When comparing operator on stack with lookahead Shift if lookahead has Higher precedence Same precedence, right associative Reduce if lookahead has Advantage Lower precedence Same precedence, left associative Can use smaller (ambiguous) grammars Example E E + E E E E * E E / E - E id num 13 Operator Precedence Grammars Alternative approach to shift-reduce parsing Given a sentential form aabb, three possibilities 1. A in handle, B not in handle A > B A is reduced before B 2. A and B both in handle A = B A and B reduced at same time 3. B in handle, A not in handle B > A B is reduced before A Handle is composed of < >, < = >, < = = >, < = = = >, etc To decide whether to shift or reduce, compare top of stack with lookahead (ignoring nonterminals) Shift if < or = Reduce if > (left end of handle is closest < to top of stack) 14 7

Operator Precedence Grammar Example 15 Shrinking the ACTION/GOTO Tables Three options: Combine terminals such as number & identifier, + & -, * & / Directly removes a column, may remove a row For expression grammar, 198 (vs. 384) table entries Combine rows or columns (table compression) Implement identical rows once & remap states Requires extra indirection on each lookup Use separate mapping for ACTION & for GOTO Use another construction algorithm Both SLR(1) and LALR(1) produce smaller tables Implementations are readily available 16 8

SLR(1) Parser Build ACTION / GOTO table using LR(0) items Problem when to perform reduction for A β? Solution reduce A β only when lookahead FOLLOW(A) Algorithm set s x S item i s x if i is [A β a γ] and goto(s x,a) = s k, a T then ACTION[x,a] shift k // to left of terminal a // shift if lookahead = a else if i is [S S ] // start production done, then ACTION[x, $] accept // accept if lookahead = $ else if i is [A β ] and a FOLLOW(A) // all the way to right then ACTION[x,a] reduce A β // reduce if lookahead n NT // is in FOLLOW(A) if goto(s x,n) = s k then GOTO[x,n] k // store transitions for nonterminals 17 Example SLR(1) Parser 18 9

LALR(1) Parser Core of set of LR(1) items Set of LR(0) items derived by ignoring lookahead symbols Example LR(1) state LR(0) state [ E a,, b ] [ E a ] [ A a,, c ] [ A a ] LALR(1) parser Merge two sets of LR(1) items (states), if same core Result Potentially much smaller set of states Same as SLR(1) parser May introduce reduce/reduce conflicts Will not introduce shift/reduce conflicts 19 Example LALR(1) Parser Merging states [ E a,, b ] can be merged with [ E a,, d ] [ A ba,, c ] [ A ba,, b ] Resulting state [ E a,, b ] [ A ba,, b ] [ E a,, d ] [ A ba,, c ] Introduces reduce/reduce error Can reduce either E a or A a or A ba ba for lookahead = b 20 10

LR(k) versus LL(k) (Top-down Recursive Descent ) Finding Reductions LR(k) Each reduction in the parse is detectable with Complete left context (everything to left of handle) Reducible phrase (handle) itself k terminal to right of handle LL(k) Parser must select the next rule based on Complete left context (everything up to & including NT to expand) k terminals to right of nonterminal to expand Thus, LR(k) is more powerful since it examines more context For example, consider grammar S ab aa LL(1)? No, cannot decide which production with lookahead = a LR(1)? Yes, can shift ab or aa onto stack before deciding on reduction 21 LR(k) Parsers Properties Strictly more powerful than LL(k) parsers Most general non-backtracking shift-reduce parser Detects error as soon as possible in left-to-right scan of input Contents of stack are viable prefixes Possible for remaining input to lead to successful parse 22 11

LR(k) versus LL(k) Summary Top-down recursive descent Advantages Fast Good locality Simplicity Good error detection Disadvantages Hand-coded High maintenance Right associativity LR(1) Fast Deterministic langs. Automatable Left associativity Large working sets Poor error messages Large table sizes 23 LR(0) versus SLR(1) versus LR(1) Example grammar LR(0)? S S S S ; a a LR(1)? SLR(1)? 24 12

LALR(1) versus LR(1) Example grammar S S S aad bbd abe bae A c B c LR(0)? LR(1)? LALR(1)? 25 Hierarchy of Context-Free Languages Context-free languages LR(k) LR(1) Deterministic languages (LR(k)) LL(k) languages Simple precedence languages LL(1) languages Operator precedence languages The inclusion hierarchy for context-free languages 26 13

Hierarchy of Context-Free Grammars Context-free grammars Floyd-Evans Parsable Unambiguous CFGs LR(k) Operator Precedence LR(1) LALR(1) LL(k) Operator precedence includes some ambiguous grammars SLR(1) LL(1) is a subset of SLR(1) LR(0) LL(1) The inclusion hierarchy for context-free grammars 27 Error Recovery in Shift-Reduce Parsers The problem: parser encounters an invalid token Goal: Want to parse the rest of the file Basic idea (panic mode): Assume something went wrong while trying to find handle for nonterminal A Pretend handle for A has been found; pop handle, skip over input to find terminal that can follow A Restarting the parser (panic mode): find a restartable state on the stack (has transition for nonterminal A) move to a consistent place in the input (token that can follow A) perform (error) reduction (for nonterminal A) print an informative message 28 14

Error Recovery in YACC (ASU p.264) Yacc s (bison s) error mechanism (note: version dependent!) designated token error used in error productions of the form A β error α α specifies synchronization points When error is discovered pops stack until it finds state where it can shift the error token resumes parsing to match α special cases: α = w, where w is string of terminals: skip input until w has been read α = ε : skip input until state transition on input token is defined error productions can have actions 29 Error Recovery in YACC cmpdstmt: BEG stmt_list END stmt_list : stmt stmt_list ; stmt error { yyerror( \n***error: illegal statement\n );} This should throw out the erroneous statement synchronize at ; or end (implicit: α = ε) writes message ***Error: illegal statement to stderr Example: begin a & 5 hello ; a := 3 end resume parsing ***Error: illegal statement 30 15