Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:

Slides:



Advertisements
Similar presentations
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
Advertisements

1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Parser construction tools: YACC
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
Saumya Debray The University of Arizona Tucson, AZ 85721
LALR Parsing Canonical sets of LR(1) items
LEX and YACC work as a team
1 Using Yacc: Part II. 2 Main() ? How do I activate the parser generated by yacc in the main() –See mglyac.y.
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Introduction to YACC CS 540 George Mason University.
Yacc. Yacc 2 Yacc takes a description of a grammar as its input and generates the table and code for a LALR parser. Input specification file is in 3 parts.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
More LR Parsing and Bison CPSC 388 Ellen Walker Hiram College.
LECTURE 11 Semantic Analysis and Yacc. REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
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.
YACC Primer CS 671 January 29, CS 671 – Spring Yacc Yet Another Compiler Compiler Automatically constructs an LALR(1) parsing table from.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
Parser Generation Tools (Yacc and Bison) CS 471 September 24, 2007.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Overview of Previous Lesson(s) Over View  Structure of the LR Parsing Table  Consists of two parts: a parsing-action function ACTION and a goto function.
Yacc.
Syntax Analysis Part III
A Simple Syntax-Directed Translator
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Unit-3 Bottom-Up-Parsing.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
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
Syntax Analysis Part III
Syntax Analysis Part II
Syntax Analysis Part III
Bison Marcin Zubrowski.
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Parsing #2 Leonidas Fegaras.
Syntax Analysis Part III
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Ambiguity in Grammar, Error Recovery
Syntax Analysis Part III
Parsing #2 Leonidas Fegaras.
Syntax-Directed Translation
Syntax Analysis - 3 Chapter 4.
Compiler Lecture Note, Miscellaneous
Yacc Yacc.
Compiler Structures 7. Yacc Objectives , Semester 2,
Appendix B.2 Yacc Appendix B.2 -- Yacc.
Saumya Debray The University of Arizona Tucson, AZ 85721
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime: reference a NULL pointer –Goals of error-handling in a parser To detect and report the presence of errors To recover from an error and detect subsequent errors To not slow down the processing of correct programs

Error recovery strategies Panic mode recovery –On discovering an error, discard input symbols one at a time until one of a designated set of synchronizing token is found. Phrase-level recovery –On discovering an error, perform a local fix to allow the parser to continue.

Error recovery in predictive parsing –Recovery in a non-recursive predictive parser is easier than in a recursive descent parser. –Panic mode recovery If a terminal on stack, pop the terminal. If a non-terminal on stack, shift the input until the terminal can expand. –Phrase-level recovery Carefully filling in the blank entries about what to do.

–Error recover in LR parsing Canonical LR parsers never make extra reductions when recognizing an error. SLR and LALR may make extra reductions, but will never shift an erroneous input symbol on the stack. Panic mode recovery –Scan down stack until a state representing a major program construct is found. Input symbols are discarded until one is found that is in the follow of the nonterminal. Trying to isolate the phrase containing the error. Phrase level recovery –Implement an error recovery routine for each error entry in the table.

–Writing a parser with YACC (Yet Another Compiler Compiler). Generates LALR parsers Work with lex. YACC calls yylex to get next token. –YACC and lex must agree on the values for each token. Produce y.tab.c file by “yacc yaccfile”, which contains a routine yyparse(). yyparse() returns 0 if the program is ok, non-zero otherwise YACC file format: declarations % translation rules % supporting C-routines

The declarations part specifies tokens, non-terminals symbols, other C constructs. –To specify token AAA BBB %token AAA BBB –To assign a token number to a token (needed when using lex), a nonnegative integer followed immediately to the first appearance of the token %token EOFnumber 0 %token SEMInumber 101 –Non-terminals do not need to be declared unless you want to associated it with a type (will be discussed later).

Translations rules specify the grammar productions exp : exp PLUSnumber exp | exp MINUSnumber exp | exp TIMESnumber exp | exp DIVIDEnumber exp | LPARENnumber exp RPARENnumber | ICONSTnumber ; exp : exp PLUSnumber exp ; exp : exp MINUSnumber exp ;

Yacc environment –Yacc processes the specification file and produce a y.tab.c file. –An integer function yyparse() is produced by Yacc. Calls yylex() to get tokens. Return non-zero when an error is found. Return 0 if the program is accepted. –Need main() and and yyerror() functions. –Example: yyerror(str) char *str; { printf("yyerror: %s at line %d\n", str, yyline); } main() { if (!yyparse()) {printf("accept\n");} else printf("reject\n"); }

–YACC builds a LALR parser for the grammar. May have shift/reduce and reduce/reduce conflicts if there are problems with the grammar. Default conflict resolution: –shift/reduce --> shift –reduce/reduce --> first production in the state –should always avoid reduce/reduce conflicts ‘yacc -v *.y’ will generate a report in file ‘y.output’. See example1.y The programmer MUST resolve all conflicts (unless you really know what you are doing). –modify the grammar. See example2.y –Use precedence and associativity of operators.

Use precedence and associativity of operators. –Using keywords %left, %right, %nonassoc in the declarations section. All tokens on the same line are the same precedence level and associativity. The lines are listed in order of increasing precedence. %left PLUSnumber, MINUSnumber %left TIMESnumber, DIVIDEnumber –See example3.y

Symbol attributes –Each symbol can be associated with some attributes. Data structure of the attributes can be specified in the union in the declarations. (see example4.y). %union { int semantic_value; } %token ICONSTnumber 119 %type exp %type term %type item Semantic actions associate with productions can be specified

Semantic actions –Semantic actions associate with productions can be specified. item : LPARENnumber exp RPARENnumber {$$ = $2;} | ICONSTnumber {$$ = $1;} ; $$ is the attribute associated with the left handside of the production $1 is the attribute associated with the first symbol in the right handside, $2 for the second symbol, … –An action can be in anyway in the production, it is also counted as a symbol. –Checkout example5.y for examples with multiple types associated with different symbol.