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

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

Topic 3: Syntax Analysis I

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

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

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

Syntax Analysis. Chapter 4

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

shift-reduce parsing

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Top down vs. bottom up parsing

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

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

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

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

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

LR Parsing LALR Parser Generators

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

CS415 Compilers. LR Parsing & Error Recovery

CS 4120 Introduction to Compilers

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Context-free grammars

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

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

Recursive Descent Parsers

CSE3322 Programming Languages and Implementation

LR Parsing LALR Parser Generators

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

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

Wednesday, September 9, 15. Parsers

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

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

Types of parsing. CMSC 430 Lecture 4, Page 1

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

Left to right design 1

Monday, September 13, Parsers

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

For example, we might have productions such as:

HW 3: Bottom-Up Parsing Techniques

Compiler Construction: Parsing

Wednesday, August 31, Parsers


Syntax-Directed Translation. Lecture 14

Compiler construction lecture 3

Using an LALR(1) Parser Generator

LALR Parsing. What Yacc and most compilers employ.

4 (c) parsing. Parsing. Top down vs. bo5om up parsing

Yacc: A Syntactic Analysers Generator

Lecture 7: Deterministic Bottom-Up Parsing

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 3, vrijdag 22 september 2017

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

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

UNIT III & IV. Bottom up parsing

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

Topic 5: Syntax Analysis III

Compilation Lecture 3: Syntax Analysis: Top-Down parsing. Noam Rinetzky

Chapter 3 Parsing. time, arrow, banana, flies, like, a, an, the, fruit

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 97 INSTRUCTIONS

Compilers. Predictive Parsing. Alex Aiken


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

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

Lecture 8: Deterministic Bottom-Up Parsing

CSE3322 Programming Languages and Implementation

Syntax Errors; Static Semantics

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

Lecture Bottom-Up Parsing

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

2068 (I) Attempt all questions.

ECE 468/573 Midterm 1 October 1, 2014

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Syn S t yn a t x a Ana x lysi y s si 1

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

Some Basic Definitions. Some Basic Definitions. Some Basic Definitions. Language Processing Systems. Syntax Analysis (Parsing) Prof.

Syntax Analysis Part I

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

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

QUESTIONS RELATED TO UNIT I, II And III

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila

Compiler construction in4303 lecture 3

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

Software II: Principles of Programming Languages

CS502: Compilers & Programming Systems

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

BSCS Fall Mid Term Examination December 2012

Introduction to Parsing Ambiguity and Syntax Errors

1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004

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

In One Slide. Outline. LR Parsing. Table Construction

Building Compilers with Phoenix

How do LL(1) Parsers Build Syntax Trees?

Question Bank. 10CS63:Compiler Design

CSE431 Translation of Computer Languages

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

CSCI Compiler Design

Introduction to Parsing Ambiguity and Syntax Errors

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

CSE302: Compiler Design

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).

Transcription:

Error Recovery Syntax Errors: A Syntax Error occurs when stream of tokens is an invalid string. In LL(k) or LR(k) parsing tables, blank entries refer to syntax erro How should syntax errors be handled? 1. Report error, terminate compilation not user friendly 2. Report error, recover from error, search for more errors better - 1 -

Error Recovery Error Recovery: process of adjusting input stream so that parsing m syntax error reported. Deletion of token types from input stream Insertion of token types Substitution of token types Two classes of recovery: 1. Local Recovery: adjust input at point where error was detected. 2. Global Recovery: adjust input before point where error was detecte These may be applied to both LL and LR parsing techniques. - 2 -

LL Local Error Recovery Local Recovery Technique: in function A(), delete token types from i token type in follow(a) found synchronizing token types. Z XY Z Y c X Z d Y ɛ X nullable first follow Z no a,b,d Y yes c a,b,d,e X no a,b a,b,c,d a b c d e Z Z XY Z Z XY Z Z d Y Y ɛ Y ɛ Y c Y ɛ Y ɛ X X a X by e - 3 -

LL Local Error Recovery Local Recovery Technique: in function A(), delete token types from i token type in follow(a) found synchronizing token types. datatype token = a b c d e; val tok = ref(gettoken()); fun advance() = tok := gettoken(); fun eat(t) = if(!tok = t) then advance() else e... and X() = case!tok of a => (eat(a)) b => (eat(b); Y(); eat(e)) c => (print "error!"; skipto[a,b,c,d]) d => (print "error!"; skipto[a,b,c,d]) e => (print "error!"; skipto[a,b,c,d]) and skipto(synchtokens) = if member(!tok, synchtokens) then () else (eat(!tok); skipto(synchtokens)) - 4 -

LR Local Error Recovery Consider: 1 E ID 3 E ( E ) 5 ES 2 E E + E 4 ES E Match a sequence of erroneous input tokens using the error token 6 E ( error ) 7 ES error ; E In general, follow error with synchronizing lookahead token. 1. Pop stack (if necessary) until a state is reached in which the ac token is shift. 2. Shift the error token. 3. Discard input symbols (if necessary) until a state is reached tha action in the current state. 4. Resume normal parsing. - 5 -

Global Error Recovery Consider LR(1) parsing: let type a := intarray[10] of 0 in... end Local Recovery Techniques would: 1. report syntax error at := 2. substitute = for := 3. report syntax error at [ 4. delete token types from input stream, synchronizing on in Global Recovery Techniques would substitute var for type : Actual syntax error occurs before point where error was detected. ML-Yacc uses global error recovery technique Burke-Fisher Other Yacc versions employ local recovery techniques. - 6 -

Burke-Fisher Suppose parser gets stuck at n th token in input stream. Burke-Fisher repairer tries every single-token-type insertion, delet tion at all points between (n k) th and n th token. Best repair: one that allows parser to parse furthest past n th token. If languages has N token types, then: total # of repairs = deletions + insertions + substitu total # of repairs = (k) + (k + 1) N + (k) (N - 7 -

Burke-Fisher In order to backup K tokens and reparse repaired input, 2 structures ne 1. k-length buffer/queue - if parser currently processing n th token, q kens (n k) (n 1). (ML-Yacc k = 15) 2. old parse stack - if parser currently processing n th token, old stack state when parser was processing (n k) th token. Whenever token shifted onto current stack, also put onto queue tai Simultaneously, queue head removed, shifted onto old stack. Whenever token shifted onto either stack, appropriate reductions p - 8 -

Burke-Fisher Semantic actions are only applied to old stack. Not desirable if semantic actions affect lexical analysis. Example: typedef in C. (Figure from MCI/ML.) - 9 -

Burke-Fisher For each repair R that can be applied to token (n k) n: 1. copy queue, copy n th token 2. copy old parse stack 3. apply R to copy of queue or copy of n th token 4. reparse queue copy (and copy of n th token) from old stack copy 5. evaluate R Choose best repair R, and apply. - 10 -

Semantic Values Insertions need semantic values %value ID {"bogus"} %value INT {1} %value STRING {"STRING") Programmer-Specified Substitutions Burke-Fisher in ML-YACC Some single token insertions and deletions are common. Some multiple token insertions and deletions are common. %change EQ -> ASSIGN SEMICOLON ELSE -> ELSE -> IN INT END - 11 -