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

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

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

CS 4120 Introduction to Compilers

shift-reduce parsing

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

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

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

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

Context-free grammars

Compiler Construction: Parsing

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

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

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

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

UNIT III & IV. Bottom up parsing

Downloaded from Page 1. LR Parsing

LALR Parsing. What Yacc and most compilers employ.

How do LL(1) Parsers Build Syntax Trees?

Lecture Bottom-Up Parsing

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Bottom-Up Parsing. Lecture 11-12

MODULE 14 SLR PARSER LR(0) ITEMS

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

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

Bottom-Up Parsing. Lecture 11-12

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

Introduction to Parsing Ambiguity and Syntax Errors

LR Parsing LALR Parser Generators

CS415 Compilers. LR Parsing & Error Recovery

LR Parsing Techniques

Wednesday, August 31, Parsers

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

Introduction to Parsing Ambiguity and Syntax Errors

Syntax Analysis Part I

Monday, September 13, Parsers

In One Slide. Outline. LR Parsing. Table Construction

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

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 Parsing LALR Parser Generators

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

Bottom-Up Parsing II. Lecture 8

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

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

Wednesday, September 9, 15. Parsers

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

LR Parsing E T + E T 1 T

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.

Formal Languages and Compilers Lecture VII Part 4: Syntactic A


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

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

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

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

Principles of Programming Languages


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

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

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

The following deflniüons h:i e been establish for the tokens: LITERAL any group olcharacters surrounded by matching quotes.

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

LR Parsing - The Items

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

Syntax-Directed Translation

CSE302: Compiler Design

Question Points Score

Principles of Programming Languages

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

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

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

Using an LALR(1) Parser Generator

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

Compilation 2013 Parser Generators, Conflict Management, and ML-Yacc

UNIT-III BOTTOM-UP PARSING

Lecture 8: Deterministic Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Error Recovery. Computer Science 320 Prof. David Walker - 1 -

LR Parsing. Table Construction

Lecture 7: Deterministic Bottom-Up Parsing

Building a Parser Part III

Recursive Descent Parsers

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

Top down vs. bottom up parsing

I 1 : {E E, E E +E, E E E}

CS 164 Programming Languages and Compilers Handout 8. Midterm I

4. Lexical and Syntax Analysis

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

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

4. Lexical and Syntax Analysis

More Bottom-Up Parsing

Error Management in Compilers and Run-time Systems

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

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

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

Compilers and Language Processing Tools

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

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

Chapter 4. Lexical and Syntax Analysis

Programming Language Syntax and Analysis

Transcription:

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

Shift-reduce parsing in an LR parser LR(k) parser Left-to-right parse Right-most derivation K-token look ahead LR parsing algorithm using an LR parse table Performs a shift-reduce parse 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 2

Interpreting the javacup.dump file Shows the whole state transition diagram, or finite state machine for push down automata Items A production from a grammar with a dot indicating position in parse. item is (A à alpha. X beta, z) A is nonterminal alpha and beta are possibly empty strings of terminals and/or nonterminals where alpha is on top of the stack X is a terminal or nonterminal z is a terminal/token CS453 Shift-reduce Parsing 3

Interpreting the javacup.dump file cont Transitions in the LALR_state transition table in javacup.dump (A à alpha. X beta, z) When X is a terminal, then shift X and transition to another state determined by X. When X is a non-terminal, then goto another state determined by the state under X in the stack and by X, once X has been reduced. An item with the parsing dot at the end of the production ( A à gamma., w) Causes a reduction to the production A à gamma when the next token in the input is w. CS453 Shift-reduce Parsing 4

Summary on Precedence and Associativity in JavaCUP Tokens and productions are given precedence. Tokens are given precedence level in precedence declarations. Productions are given precedence level of last operator on right-hand side, ie. top of the Stack When in shift-reduce conflict, shift if look ahead token has higher precedence than reduce production reduce if reduce production has higher precedence than the look ahead token Within the same precedence level associativity determines whether to shift or reduce. left associativity results in a reduce right associativity results in a shift CS453 Shift-reduce Parsing 5

Error detection and recovery Given a SLR, LR(1) or LALR parse table, what constitutes an error? An empty slot in the parse table When the parser encounters an empty slot in its table, it has detected an error. What are the possible reactions to this? 1. Stop easy but not user friendly 2. Try to get back (recover) and parse more of the program more complicated, why? How to resume? What to do with unfinished stack symbols and states CS453 Shift-reduce Parsing 6

How to resume and clean up the parsing stack The simplest approach to resume parsing is called panic mode error recovery: discard input tokens until a so called anchor token appears. An anchor token is much like a look ahead token, such as ; for statement. - Imagine the parser is parsing a statement and an error occurs, then discarding input tokens until ; and declaring an erroneous statement gets the parser back and allows it to parse a next statement or an } - At the same time, certain grammar symbols and states corresponding to part of the statement (shifted tokens, reduced non-terminals) are on the stack. So, while the parser skips input tokens to the end of statement, it should also remove these grammar symbols and states off the stack. CS453 Shift-reduce Parsing 7

Error recovery in JCup Approach to error recovery same as in (original) YACC. The compiler writer decides which major non terminals will have error recovery associated with them Typical choices: expression, statement, block by adding to the grammar error productions of the form A à error α where error is a reserved word. On encountering an error, JCup pops grammar symbols and states off the stack until it reaches a symbol with an item: A à. error α The parser shifts error onto the stack. If α is empty a reduction A à error α occurs immediately. If α is a string of terminals the parser skips input symbols until α occurs on input. It shifts α onto the stack and performs reduction A à error α CS453 Shift-reduce Parsing 8

Simple example In PA0, we can add en error production to stmt:! stmts ::= stmts stmt!! stmt! ;! stmt ::= PRINT exp:e EOL! {: System.out.println( + e); :}!! error EOL! {: System.out.println( Erroneous statement skipped ); :}! ;! Now the parser skips erroneous statements and resumes with the next. Let s do it to it CS453 Shift-reduce Parsing 9