Joey Paquet, 2000, 20021 Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.

Slides:



Advertisements
Similar presentations
Error Handling A compiler should: detect errors locate errors recover from errors Errors may occur in any of the three main analysis phases of compiling:
Advertisements

Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Lecture # 7 Chapter 4: Syntax Analysis. What is the job of Syntax Analysis? Syntax Analysis is also called Parsing or Hierarchical Analysis. A Parser.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
Top-Down Parsing.
9/27/2006Prof. Hilfinger, Lecture 141 Parsing Odds and Ends Lecture 14 (P. N. Hilfinger, plus slides adapted from R. Bodik)
Lexical and syntax analysis
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
ERROR HANDLING Lecture on 27/08/2013 PPT: 11CS10037 SAHIL ARORA.
Joey Paquet, 2000, 2002, Lecture 3 Syntactic Analysis Part I.
Top-Down Parsing - recursive descent - predictive parsing
Chapter 5 Top-Down Parsing.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
Lexical Analysis Hira Waseem Lecture
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
SYNTAX ANALYSIS & ERROR RECOVERY By: Sarthak Swaroop.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
1 Problems with Top Down Parsing  Left Recursion in CFG May Cause Parser to Loop Forever.  Indeed:  In the production A  A  we write the program procedure.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
Joey Paquet, 2000, Lecture 2 Lexical Analysis.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
More Parsing CPSC 388 Ellen Walker Hiram College.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Overview of Previous Lesson(s) Over View  In our compiler model, the parser obtains a string of tokens from the lexical analyzer & verifies that the.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
Joey Paquet, 2000, 2002, 2008, 2012, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Error recovery in top-down.
Error recovery in predictive parsing An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input.
Semantic analysis Jakub Yaghob
LESSON 16.
Lecture 2 Lexical Analysis Joey Paquet, 2000, 2002, 2012.
Introduction to Top Down Parser
Chapter 4 Syntax Analysis.
Syntax Analysis Chapter 4.
Compiler design Bottom-up parsing: Canonical LR and LALR
4 (c) parsing.
Syntax Analysis Sections :.
Lexical and Syntax Analysis
CS 540 George Mason University
Top-Down Parsing The parse tree is created top to bottom.
Chapter 4 Top Down Parser.
Compiler design Error recovery in top-down predictive parsing
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Computing Follow(A) : All Non-Terminals
Compiler design Syntactic analysis – part II First and follow sets
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Syntactic Analysis Part II
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis

Joey Paquet, 2000, Syntax Error Recovery A syntax error happens when the stream of tokens coming from the lexical analyzer disobeys the grammatical rules defining the programming language. The next token in input is not expected according to the syntactic definition of the language One of the main roles of a compiler is to identify programming errors and give meaningful indications about the location and nature of errors in the input program.

Joey Paquet, 2000, Goals of Error Handling Detect all compile-time errors Report the presence of errors clearly and accurately Recover from each error quickly enough to be able to detect subsequent errors Should not slow down the processing of correct programs Beware of spurious errors that are just a consequence of earlier errors

Joey Paquet, 2000, Reporting Errors Give the position of the error in the source file, maybe print the offending line and point at the error location If the nature of the error is easily identifiable, give a meaningful error message The compiler should not provide erroneous information about the nature of errors If the error is easily correctable, a solution can be proposed or applied by the compiler and a simple warning issued

Joey Paquet, 2000, Good Error Recovery Should recover from each error quickly enough to be able to detect subsequent errors. Error recovery should skip as less tokens as possible. Should not identify more errors than there really is. Cascades of errors should be avoided. Should give meaningful information about the errors, while avoiding to give erroneous information. Should not significantly slow down the compilation of correct programs. Error recovery should induce processing overhead only when errors are encountered. Should not report errors that are consequences of the application of error recovery, e.g. semantic errors.

Joey Paquet, 2000, Error Recovery Strategies There are many different strategies that a parser can employ to recover from syntactic errors. Although some are better than others, none of these methods provide a universal solution. –Panic mode –Phrase level –Error productions –Global correction

Joey Paquet, 2000, Error Recovery Strategies Panic Mode –On discovering an error, the parser discards input tokens until an element of a designated set of synchronizing tokens is found. Synchronizing tokens are typically delimiters such as semicolons or end of block delimiters. –Skipping tokens often has a side-effect of skipping other errors. Choosing the right set of synchronizing tokens is of prime importance. –Simplest method to implement. –Can be integrated in most parsing methods. –Cannot enter an infinite loop.

Joey Paquet, 2000, Error Recovery Strategies Phrase Level –On discovering an error, the parser performs a local correction on the remaining input, e.g. replace a comma by a semicolon, delete an extraneous semicolon, insert a missing semicolon, etc. –Replacements are done in specific contexts. There are myriads of different such contexts. –Cannot cope with errors that occurred before the point of detection. –Can enter an infinite loop, e.g. insertion of an expected token.

Joey Paquet, 2000, Error Recovery Strategies Error Productions –The grammar is augmented with “error” productions. For each possible error, an error production is added. An error is trapped when an error production is used. –Assumes that all kinds of errors are known in advance. –One error production is needed for each possible error. –Error productions are specific to the rules in the grammar. A change in the grammar implies a change of the corresponding error productions. –Extremely hard to maintain.

Joey Paquet, 2000, Error Recovery Strategies Global Correction –Ideally, a compiler should make as few changes as possible in processing an incorrect token stream. –Global correction is about choosing the minimal sequence of changes to obtain a least-cost correction. –Given an incorrect input token stream x, such an algorithm will find a parse tree for a related token stream y, such that the number of insertions, deletions, and changes of tokens required to transform x into y is as small as possible. –Too costly to implement. –The closest correct program does not carry the meaning intended by the programmer anyway. –Can be used as a benchmark for other error correction techniques.

Joey Paquet, 2000, Error Recovery in Recursive Descent Predictive Parsers Two possible cases: –The lookahead symbol or  are not in FIRST(RHSs) –The match() function is called in a no match situation Solution : –Create a skipErrors() function that skips tokens until an element of FIRST(LHS) or FOLLOW(LHS) is encountered –Call this function upon entering any production function

Joey Paquet, 2000, Cont. skipErrors([FIRST],[FOLLOW]) if ( lookahead is in [FIRST] ) return (false)// no error detected else write (“syntax error at “ lookahead.location) while (lookahead not in [FIRST  FOLLOW] ) lookahead = nextToken() write (“parsing resumed at “ lookahead.location) return (true)// error detected match(token) if ( lookahead == token ) lookahead = nextToken() return (true) else write (“syntax error at” lookahead.location. “expected” token) lookahead = nextToken() return (false)

Joey Paquet, 2000, Constructing the Parser LHS(){ // LHS  RHS1 | RHS2 | … |  error = skipErrors( FIRST(LHS),FOLLOW(LHS) ) if (lookahead  FIRST(RHS1) ) if (non-terminals()  match(terminals) ) write(“LHS  RHS1”) else error = true else if (lookahead  FIRST(RHS2) ) if (non-terminals()  match(terminals) ) write(“LHS  RHS2”) else error = true else if … // other right hand sides else if (lookahead  FOLLOW(LHS) ) // only if LHS  exists write(“LHS  ”) else error = true error = skipErrors( FOLLOW(LHS),[empty] ) return (error)

Joey Paquet, 2000, Example E(){ error = skipErrors([0,1,(],[),$]) if (lookahead is in [0,1,(]) //FIRST(TE') if (T();E'();) write(E->TE') else error = true error = skipErrors([),$],[empty]) return (error) }

Joey Paquet, 2000, Example E'(){ error = skipErrors([+],[),$]) if (lookahead is in [+]) //FIRST[+TE'] if (match('+');T();E'()) write(E'->TE') else error = true else if (lookahead is in [$,)] //FOLLOW[E'](epsilon) write(E'->epsilon); else error = true error = skipErrors([$,)],[empty]) return (error) }

Joey Paquet, 2000, Example T(){ error = skipErrors([0,1,(],[+,),$]) if (lookahead is in [0,1,(]) //FIRST[FT'] if (F();T'();) write(T->FT') else error = true error = skipErrors([+,),$],[empty]) return (error) }

Joey Paquet, 2000, Example T'(){ error = skipErrors([*],[+,),$]) if (lookahead is in [*]) //FIRST[*FT'] if (match('*');F();T'()) write(T'->*FT') else error = true else if (lookahead is in [+,),$] //FOLLOW[T'] (epsilon) write(T'->epsilon) else error = true error = skipErrors([+,),$],[empty]) return (error) }

Joey Paquet, 2000, Example F(){ error = skipErrors([0,1,(],[*,+,$,)]) if (lookahead is in [0]) //FIRST[0] match('0') write(F->0) else if (lookahead is in [1]) //FIRST[1] match('1') write(F->1) else if (lookahead is in [(]) //FIRST[(E)] if (match('(');E();match(')')) write(F->1); else error = true error = skipErrors([*,+,$,)],[empty]) return (error) }

Joey Paquet, 2000, Error Recovery in Table-Driven Predictive Parsers All empty cells in the state transition table are syntax errors Each case represents a specific kind of error Task: –Recover from the error Either pop the stack or scan tokens –Output an error message

Joey Paquet, 2000, Building the table with error cases Two possible cases: error(){ // A is top() if (lookahead is $ or in FOLLOW(A)) pop() // equivalent to A   else lookahead = nextToken()// scan }

Joey Paquet, 2000, Original table, grammar and sets FLW(E): { $,) } FLW(E’) : { $,) } FLW(T) : { +,$,) } FLW(T’) : { +,$,) } FLW(F) : { ,+,$,) } E  TE E   | +TE T  FT T   | FT F  ( E ) | 0 | 1 FST(E): { 0,1,( } FST(E’) : { ,+ } FST(T) : { 0,1,( } FST(T’) : { ,* } FST(F) : { 0,1,( } 01()+*$ Er1 E’r3r2r3 Tr4 T’r6 r5r6 Fr7r8r9

Joey Paquet, 2000, State transition table with errors 01()+*$ Er1 popscan pop E’scan r3r2scanr3 Tr4 pop scanpop T’scan r6 r5r6 Fr7r8r9pop