Error Management in Compilers and Run-time Systems

Similar documents
Error Management in Compilers and Run-time Systems

Error Management in Compilers and Run-time Systems

Syntax Analysis. Chapter 4

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

Syntax Analysis Part I

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

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

Syntax Errors; Static Semantics

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

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

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

How do LL(1) Parsers Build Syntax Trees?

Programming Languages and Techniques (CIS120)

CSE302: Compiler Design

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

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

Object Oriented Programming Exception Handling


1 Lexical Considerations

Chapter 4: Syntax Analyzer

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

VIVA QUESTIONS WITH ANSWERS

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

14. Exception Handling

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Lecture 8: Deterministic Bottom-Up Parsing

Lexical Considerations

CS 4120 Introduction to Compilers

Lecture 7: Deterministic Bottom-Up Parsing

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

The role of semantic analysis in a compiler

CS 6353 Compiler Construction Project Assignments

Bottom-Up Parsing. Lecture 11-12

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Bottom-Up Parsing. Lecture 11-12

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

CSCI Object Oriented Design: Java Review Errors George Blankenship. Java Review - Errors George Blankenship 1

CSCI Object-Oriented Design. Java Review Topics. Program Errors. George Blankenship 1. Java Review Errors George Blankenship

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Preconditions. CMSC 330: Organization of Programming Languages. Signaling Errors. Dealing with Errors

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

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

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

Exception-Handling Overview

ECE 122. Engineering Problem Solving with Java

Error Recovery during Top-Down Parsing: Acceptable-sets derived from continuation

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CSCI312 Principles of Programming Languages

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Semantic actions for declarations and expressions

Program Correctness and Efficiency. Chapter 2

LR Parsing LALR Parser Generators

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Exceptions Handling Errors using Exceptions

Context-free grammars (CFG s)

Programming Languages and Techniques (CIS120e)

Syntactic Analysis. Top-Down Parsing

A Short Summary of Javali

COMPILER DESIGN LECTURE NOTES

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

Configuration Sets for CSX- Lite. Parser Action Table

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS415 Compilers. LR Parsing & Error Recovery

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

Chapter 4. Lexical and Syntax Analysis

Table-Driven Top-Down Parsers

G Programming Languages - Fall 2012

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

UNIT-4 (COMPILER DESIGN)

Semantic actions for declarations and expressions

Compilers and Interpreters

Defensive Programming

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

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

LECTURE 3. Compiler Phases

Recursive Descent Parsers

National University. Faculty of Computer Since and Technology Object Oriented Programming

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

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Exception Handling: Control. Exception handling is the control of error conditions or other unusual events during the execution of a program.

Semantic actions for declarations and expressions. Monday, September 28, 15

Top down vs. bottom up parsing

CS 415 Midterm Exam Spring 2002

shift-reduce parsing

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

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

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

Research on the Novel and Efficient Mechanism of Exception Handling Techniques for Java. Xiaoqing Lv 1 1 huihua College Of Hebei Normal University,

COMPILER CONSTRUCTION Seminar 02 TDDB44

Semantic Analysis. Lecture 9. February 7, 2018

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

BBM 102 Introduction to Programming II Spring Exceptions

COS 320. Compiling Techniques

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Transcription:

TDDB29 Compilers and Interpreters Error Management in Compilers and Run-time Systems Program errors major part of the total cost of software projects is due to testing and debugging. US-Study 2002: Software errors cost the US economy yearly ~ 60 Mrd. $ Classification of of program errors Handling static errors in in the compiler Handling run-time errors by by the run-time system Exception concept and and implementation What error types can occur? Classification Prevention, Diagnosis, Treatment Programming language concepts Compiler, IDE, Run-time support Other tools: Debugger, Verifier,... 2 TDDB29/, 2007 Classification of program errors (1) Design-Time Errors (not considered here) lgorithmic errors e.g.: forgotten special case; non-terminating program Numeric errors ccumulation of rounding errors Contract violation Violating required invariants Static Errors Syntax Error Semantic Error Static type error forgotten semicolon, misspelled keyword Wrong parameter number or type; Downcast without run-time check Undeclared variable Use of uninitialized variable Static overflow Constant too large for target format Compiler Errors Symbol table / constant table / string table / type table overflow 3 TDDB29/, 2007 Classification of program errors (2) Run-time errors usually not checkable statically Memory access error rray index error Pointer error rithmetic error I/O error Communication error Synchronisation error Ressource exhaustion... e.g.: Index out of bounds Dereferenced NULL-pointer Division by 0; Overflow unexpected end of file write to non-opened file Wrong receiver, wrong type Data race, deadlock Stack / heap overflow, time account exhausted Remark: There are further types of errors, and combinations. 4 TDDB29/, 2007 Error prevention, diagnosis, treatment Programming language concepts Type safety Exception concept static type errors run-time errors utomatic memory mgmt memory leaks, pointer errors Compiler frontend Program verifier Code Inspection [Fagan 76] Testing and Debugging Runtime protection monitor Trace Visualiser syntax errors, static semantic errors Contract violation ll error types Run-time errors ccess errors Communication errors, Synchronisation errors The task of the compiler Discover errors Report errors Restart parsing after errors, automatic recovery Correct errors on-the-fly if possible Requirements on error management in the compiler Correct and meaningfull error messages ll static program errors (as defined by language) must be found Not to introduce any new errors Suppress code generation if error encountered 5 TDDB29/, 2007 6 TDDB29/, 2007 1

TDDB29 Compilers and Interpreters Handling Syntactic Errors in the lexical analyser and parser Syntax errors Discovered rarely by the lexical analyzer E.g., unterminated string constant; identifier too long Mostly in the parser Usually local errors should be handled locally by lexical analyser or parser LL and LR parsers have the viable prefix property, i.e. discover an error as soon as the substring being analysed together with the next input symbol does not form a viable prefix of the language. Methods for syntax error management: Panic mode (for LL parsing) Coding error entries in the CTION table (for LR parsing) Error productions for typical errors (LL and LR parsing) 8 TDDB29/, 2007 Synchronization points for recovery after a syntax error S B Parser stack contents Panic mode recovery after a syntax error S B? Parser stack contents α input prefix parsed successfully β c β.b γ a? a? α input prefix parsed successfully c a B α.δ in FOLLOW( )? in FOLLOW( )! 9 TDDB29/, 2007 10 TDDB29/, 2007 Panic mode (for predictive (LL) parsing) wrong token c was found for current production β. b γ Skip input tokens until either parsing can continue (find b), or a synchronizing token is found for the current production (e.g. {,, while, if, ; ) tokens in FOLLOW() for current LHS nonterminal then pop and continue tokens in FOLLOW(B) for some LHS nonterminal B on the stack below then pop the stack until and including B, and continue tokens in FIRST() Then resume parsing by the matching production for Further details: [LSU06] 4.4.5 Systematic, easy to implement Does not require extra memory Much input can be removed Semantic information on stack is lost if popped for error recovery 11 TDDB29/, 2007 Error productions For typical beginner s syntax errors E.g. by former Pascal programmers changing to C Define fake productions that allow the error idiom: E.g., <id> := <expr> similarly to <id> = <expr> Error message: Syntax error in line 123, v := 17 should read v = 17? very good error messages can easily repair the error difficult to foresee all such error idioms increases grammar size and thereby parser size 12 TDDB29/, 2007 2

Error entries in the CTION table (LR) Empty fields in the CTION table (= no transition in GOTO graph when seeing a token) correspond to syntax errors. LR Panic-mode recovery: Scan down the stack until a state s with a goto on a particular nonterminal is found such that one of the next input symbols a is in FOLLOW(). Then push the state GOTO(s, ) and resume parsing from a. Eliminates the erroneous phrase (subexpr., stmt., block) completely. LR Phrase-level recovery: For typical error cases (e.g. semicolon before else in Pascal) define a special error transition with pointer to an error handling routine, called if the error is encountered See example and [LSU06] 4.8.3 for details Can provide very good error messages Difficult to foresee all possible cases Much coding Modifying the grammar means recoding the error entries 13 TDDB29/, 2007 Example: LR Phrase-level Recovery 0. S -> L -- 1. L -> L, E 2. E 3. E -> a 4. b Error handling routines triggered by new CTION table error transitions: CTION table: state --, a b 0 E1 E2 S4 S5 1 S2 E4 E4 2 E1 E3 S4 S5 3 R1 R1 E5 E5 4 R3 R3 E6 E6 5 R4 R4 E6 E6 6 R2 R2 E5 E5 E1: errmsg( Found EOF where element expected ); push state 3 = the GOTO target of finding (fictitious) E GOTO table: state L E 0 1 6 1 * * 2 * 3 3 * * 4 * * 5 * * 6 * * E2: errmsg( No leading comma ); read the comma away and stay in state 0 E3: errmsg( Duplicate comma ); read the comma away and stay in state 2 E4: errmsg( Missing comma between elements ); push state 2 (pretend to have seen and shifted a comma) E5: errmsg( Missing comma ); reduce + push state 1 as if seeing the comma E6: errmsg( Missing comma ); reduce + push state 3 as if seeing the comma 14 TDDB29/, 2007 Error productions in Yacc Extend grammar with error productions of the form ::= error α which correspond to most common errors α error: fictitious token, reserved keyword in Yacc Example: <stmt> ::= error <id> := <expr> Panic mode for LR parsing When an error occurs: Pop stack elements until the state on top of the stack has an item of the form [. error α ] in its item set Shift error in as a token If α is ε, reduce using semantic action for this rule: ::= error ε { printf( Error: ); Otherwise, skip tokens until a string derivable from α is found, and reduce for this rule: ::= error α { printf( Error, continued from α ); Example: ::= error ; { printf( Error, continued from semicolon ); 15 TDDB29/, 2007 TDDB29 Compilers and Interpreters Handling Semantic Errors in the compiler front end Semantic errors Can be global (needs not be tied to a specific code location or nesting level) Do not affect the parsing progress Usually hard to recover automatically May e.g. automatically declare an undeclared identifier with a default type (int) in the current local scope but this may lead to further semantic errors later May e.g. automatically insert a missing type conversion Usually handled ad-hoc in the semantic actions / frontend code TDDB29 Compilers and Interpreters Exception handling Concept and Implementation 17 TDDB29/, 2007 3

Exception Concept PL/I (IBM) ca. 1965: ON condition J. B. Goodenough, POPL 1975 und Comm. CM Dec. 1975 Supported in many modern programming languages CLU, da, Modula-3, ML, C++, Java, C# Overview: Terminology: Error vs. Exception Exception Propagation Checked vs. Unchecked Exceptions Implementation 19 TDDB29/, 2007 Exception Concept 2 sorts of run-time errors: Error: cannot be handled by application program terminate execution Exception: may be handled by the program itself Triggered (thrown) by run-time system when recognizing a run-time error, or by the program itself Message (signal) to the program Run-time object defining an uncommon or error situation has a type (Exception class) May have parameters, e.g. a string with clear-text error message lso user-defined exceptions e.g. for boundary cases Exception Handler: Contains a code block for treatment is statically associated with the monitored code block, which it replaces in the case of an exception 20 TDDB29/, 2007 Exception Example (in Java) Propagating Exceptions public class class1 { public static void main ( String[] args ) { try { System.out.println("Hello, " + args[0] ); catch (rrayindexoutofboundsexception e ) { System.out.println( Please provide an argument! " + e); System.out.println( Goodbye"); % java java class1 class1 Christoph Hello, % java Exception Christoph class1 in thread "main" java.lang.rrayindexoutofboundsexception: 0 at Goodbye Please provide an argument! java.lang.rrayindexoutofboundsexception: 0 class1.main(class1.java:4) Goodbye 21 TDDB29/, 2007 If an exception is not handled in the current method, program control returns from the method and triggers the same exception to the caller. This schema will repeat until either a matching handler is found, or main() is left (then error message and program termination). Optional finally-block will always be executed, though. E.g. for releasing of allocated resources or held locks To be determined: When does a handler match? How can we guarantee statically that a certain exception is eventually handled within the program? Implementation? 22 TDDB29/, 2007 When does a handler match? Exception Class Hierarchy User-defined exceptions by subclassing Error Handler catch( XYException e ) { matches, if XYException is of the same type or a supertype of the thrown exception. ThreadDeath VirtualMachineError Object Throwable Exception RunTimeException rithmeticexception rrayindexoutofboundse NullPointerException IllegalccessException NoSuchMethodException 23 TDDB29/, 2007 Checked and Unchecked Exceptions Checked Exception: must be Treated in a method, or Explicitly declared in method declaration as propagated exception: void writeentry( ) throws IOException { Unchecked Exception: will be propagated implicitly In Java: ll Exceptions are checked, except RunTimeException und its subtypes. Checked Exceptions: Encapsulation Consistency can be checked statically become part of the contract of the method s class/interface suitable for component systems, e.g. CORB ( TDDC18) Extensibility 24 TDDB29/, 2007 4

void bar() { Implementation try { -> catch(e1) catch(e1 e) { -> catch(e2) Simple solution: catch(e2 e) { fp(bar): R( bar ) Stack of handlers -> catch(e2) When entering a monitored block (try {): Push all its handlers (catch() {) fp(foo): -> catch() R( foo ) When an exception occurs: main: R( main ) Pop topmost handler and start (test of exception type). If it does not match, re-throw and repeat. (If the last handler in current methode did not match either, pop also the method s activation record exit method.) If leaving the try-block normally: pop its handlers simple Overhead (push/pop) also if no exception occurs More efficient solution: Compiler generates table of pairs (try-block, matching handler) When exception occurs: find try-block by binary search (PC) 25 TDDB29/, 2007 Exceptions: Summary, Literature Exceptions Well-proven concept for treatment of run-time errors Efficiently implementable Suitable for component based software development M. Scott: Programming Language Pragmatics. Morgan Kaufmann, 2000. Section 8.5 about Exception Handling. J. Goodenough: Structured Exception Handling. CM POPL, Jan. 1975 J. Goodenough: Exception Handling: Issues and a proposed notation. Communications of the CM, Dec. 1975 B. Ryder, M. Soffa: Influences on the Design of Exception Handling, 2003 26 TDDB29/, 2007 5