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:

Slides:



Advertisements
Similar presentations
Bottom-up Parser Table Construction David Walker COS 320.
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
CH4.1 CSE244 More on LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 191 Auditorium Road, Box U-155.
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Lecture # 7 Chapter 4: Syntax Analysis. What is the job of Syntax Analysis? Syntax Analysis is also called Parsing or Hierarchical Analysis. A Parser.
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.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
9/27/2006Prof. Hilfinger, Lecture 141 Parsing Odds and Ends Lecture 14 (P. N. Hilfinger, plus slides adapted from R. Bodik)
ERROR HANDLING Lecture on 27/08/2013 PPT: 11CS10037 SAHIL ARORA.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Lexical Analysis Hira Waseem Lecture
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
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.
Joey Paquet, 2000, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
Chapter 1 Introduction Major Data Structures in Compiler
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.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.
The Role of Lexical Analyzer
Top-Down Parsing.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Joey Paquet, 2000, 2002, 2008, 2012, Lecture 5 Error Recovery Techniques in Top-Down Predictive Syntactic Analysis.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
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.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
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.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Semantic analysis Jakub Yaghob
Problems with Top Down Parsing
LESSON 16.
Unit-3 Bottom-Up-Parsing.
Textbook:Modern Compiler Design
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Introduction to Top Down Parser
Chapter 4 Syntax Analysis.
Syntax Analysis Chapter 4.
Compiler design Bottom-up parsing: Canonical LR and LALR
LALR Parsing Canonical sets of LR(1) items
Shift Reduce Parsing Unit -3
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.
Ambiguity in Grammar, Error Recovery
Compiler design Error recovery in top-down predictive parsing
Subject: Language Processor
Bottom-Up Parsing “Shift-Reduce” Parsing
Computing Follow(A) : All Non-Terminals
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

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: lexical analysis syntax analysis semantic analysis

Lexical Errors few possibilities: unexpected character illegal sequence of characters recovery strategy: delete offending character from input and continue

Syntax Errors Most errors in programs can be detected in this phase. The parser should: detect all syntactic errors report errors clearly and accurately recover quickly and detect subsequent errors not degrade compiler efficiency The main difficulty in designing the error handling capabilities of a compiler is balancing these competing objectives.

Error Recovery Poor error recovery causes an avalanche of error messages, whcih are not errors in the source program. There are a number of different strategies: panic mode - ignore all symbols until a "synchronising" token is found e.g. and "end' or ";", etc. - simple to implement - guaranteed to halt - ignores a lot of code phrase level - replace a prefix of current input by string allowing parser to continue. Normally replaces/deletes delimiters. - danger of looping - unable to deal with cases where error is on stack and not on input

Error Recovery (cont.) error productions - include extra productions in grammar which recognise commonly occurring errors - requires analysis of language use - ensures messages and recovery procedures are specific to the actual error global correction - compiler carries out minimum number of changes to get a correct program - algorithms exist to determine minimum change - requires complete traversal of program before correction - extremely expensive in time and space

Error Handling in LR Parsing Detection: detects errors at earliest possible point in input, by reaching error entry in parse table. Recovery: panic mode - look down stack until state s which has a table entry of Si for one (A) of a set of particular non-terminals. Throw away input symbols until one from follow(A) is reached. Discard states above s, place i on stack. Assumes error was found in derivation from A, and pretends instead that an A was parsed.

phrase level - for each error entry in table, insert a pointer to a particular error procedure, which assumes the most likely cause, and takes the appropriate action STFa+*()# 123S5e1e1S4e2 e1 e3S6e4e3e2 A e3 R2S7e3 R2R2 e3 R4R4e3R4R4 823S5 e1 e1 S4 e2 e1 e3 R6R6e3 R6R6 93S5 e1 e1 S4 e2 e1 10S5 e1 e1 S4e2 e1 e3 S6e4 e3 S11 e5 e3 R1S7e3 R1R1 e3 R3R3e3 R3R3 e3 R5R5e3 R5R5

Error Procedures e1:/* expecting an a or an "(", but finding an "+", "*" or a "#" */ put 5 on top of stack/* assume a */ issue "missing operand" message e2:/* finding an unexpected ")" */ remove ")" from input/* ignore it */ issue "unmatched right parenthesis“ message e3:/* expecting "+", finding a or "(" */ put 6 on top of stack/* assume "+" */ issue "missing +" message e4:/* expecting "+", finding "*" */ put 6 on top of stack/* assume "+" */ remove "*" from input issue "* instead of +" message e5:/* expecting ")", finding "#" */ put 11 on stack/* assume ")" */ issue "missing right parenthesis" message

Yacc Error Recovery Yacc uses error productions (but similar to the phrase-level recovery given above). Add productions A -> error a When error is found, scan down stack until a state with a member A -> something error a Is found. Scan through input until a string matching a is found, and remove all input until the end of that string. Parser then reduces to A, and continues. Example: Statement -> error ;