1 Using Yacc: Part II. 2 Main() ? How do I activate the parser generated by yacc in the main() –See mglyac.y.

Slides:



Advertisements
Similar presentations
Application: Yacc A parser generator A context-free grammar An LR parser Yacc Yacc input file:... definitions... %... production rules... %... user-defined.
Advertisements

Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
Yacc YACC BNF grammar example.y Other modules example.tab.c Executable
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
9/27/2006Prof. Hilfinger, Lecture 141 Parsing Odds and Ends Lecture 14 (P. N. Hilfinger, plus slides adapted from R. Bodik)
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
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,
ERROR HANDLING Lecture on 27/08/2013 PPT: 11CS10037 SAHIL ARORA.
Saumya Debray The University of Arizona Tucson, AZ 85721
LEX and YACC work as a team
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Lab 3: Using ML-Yacc Zhong Zhuang
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
Lexical and Syntax Analysis
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.
Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad.
LEX (04CS1008) A tool widely used to specify lexical analyzers for a variety of languages We refer to the tool as Lex compiler, and to its input specification.
Compiler Tools Lex/Yacc – Flex & Bison. Compiler Front End (from Engineering a Compiler) Scanner (Lexical Analyzer) Maps stream of characters into words.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Introduction to Yacc Ying-Hung Jiang
D Goforth COSC Translating High Level Languages.
1 Using Lex. 2 Introduction When you write a lex specification, you create a set of patterns which lex matches against the input. Each time one of the.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Introduction to Lex Fan Wu
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
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.
Lecture 7. YACC YACC can parse input streams consisting of tokens with certain values. This clearly describes the relation YACC has with Lex, YACC has.
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.
Lex & Yacc By Hathal Alwageed & Ahmad Almadhor. References *Tom Niemann. “A Compact Guide to Lex & Yacc ”. Portland, Oregon. 18 April 2010 *Levine, John.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
Writing Parsers with Ruby
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.
Set 6 Debugging with Lex & Yacc. DEBUGGING YOUR GRAMMAR WITH YACC Assume your Yacc defn. file is called “Test” 1.OBTAINING THE PARSING MACHINE Employ:
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.
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 (Yet Another Compiler-Compiler) Chung-Ju Wu
Parser Generation Tools (Yacc and Bison) CS 471 September 24, 2007.
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LEX & Yacc Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
YACC SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
User-Written Functions
Syntax Analysis Part III
Tutorial On Lex & Yacc.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Chapter 4 Syntax Analysis.
Syntax Analysis Part III
Bison: Parser Generator
Lexical and Syntax Analysis
Syntax Analysis Part III
Syntax Analysis Part III
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Compiler Lecture Note, Miscellaneous
Appendix B.2 Yacc Appendix B.2 -- Yacc.
Saumya Debray The University of Arizona Tucson, AZ 85721
Compiler Design Yacc Example "Yet Another Compiler Compiler"
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

1 Using Yacc: Part II

2 Main() ? How do I activate the parser generated by yacc in the main() –See mglyac.y

3 main(int argc, char **argv) { char *outfile; char *infile; extern FILE *yyin, *yyout; progname = argv[0]; if(argc > 3) { fprintf(stderr,usage, progname); exit(1); } if(argc > 1) { infile = argv[1]; /* open for read */ yyin = fopen(infile,"r"); if(yyin == NULL) /* open failed */ { fprintf(stderr,"%s: cannot open %s\n", progname, infile); exit(1); } if(argc > 2) { outfile = argv[2]; } else {outfile = DEFAULT_OUTFILE;} yyout = fopen(outfile,"w"); if(yyout == NULL) /* open failed */ { fprintf(stderr,"%s: cannot open %s\n", progname, outfile); exit(1); } yyparse(); end_file(); /* write out any final information */ exit(0); /* no error */ } (Definition section) % (Rules section) % (User subroutines section) Place this code in the User subroutines section

4 Yacc Ambiguities and Conflicts Read the Chapter 8 of “lex & yacc” –It focuses on finding and correcting conflicts within a yacc grammar Rather than telling where your conflicts lie in your yacc grammar, yacc tells where they are in y.output –You can generate y.output by running yacc with the –v (verbose) option “ yacc –dv filename.y ”

5 Shift/Reduce Conflicts Identifying a shift/reduce conflict is a little harder. To identify the conflict, we will do the following: –Find the shift/reduce error in y.output –Pick out the reduce rule –Pick out the relevant shift rules –See where the reduce rule reduces to –Deduce the token stream that will produce the conflict

6 Common Examples of Conflicts Expression Grammars IF-THEN-ELSE –The so-called dangling else problem in C-like programming language Fortran 95, Ada, Perl do not have this problem –Nested List Grammars

7 How Do I Fix the Conflict?

8 Error Reporting and Recovery Read the Chapter 9 of “lex & yacc” How the parser and lexical analyzer detect errors Yacc provides the error taken and the yyerror() routine

9 Error reporting The default yacc error only declares that a syntax error exists exists and to stop parsing –Actually, it calls yyerror(“syntax error”);

10 Error reporting (Cont’d) The duty for error correction does not lie with yacc alone, however. Many fundamental errors are better detected by lex. –Check page 244 about the example of using lex to detect an unterminated quoted string Note that that $ means end of the line in lex.

11 Error reporting (Cont’d) It is a lot more useful to tell the user that the string is the wrong type rather than just saying “syntax error” –In the example shown at P. 245, when the yacc grammar detects an improper string literal or identifier, it can pinpoint the type of error. It introduces a new non-terminal

12 Better Lex Error Reports How to report the line number whenever a syntax error happens? –Trace the error1.y and error1.l You should download ErrHandling.tar –Note that you may need to use yyless(1) ~/Compilers $ more errori1 abc=1 abc bc=abc*2/3 bc d=abc+bc d ~/Compilers $ more errori2 abc=1 abc bc=abc*2/3 bc=bc+1-+2 d=abc+bc d ~/Compilers $./error1<errori1 = 1 = = ~/Compilers $./error1<errori2 = 1 syntax error happened at line 4 ~/Compilers $

13 To reprocess input The function yyless() resets the end point of the current token. yyless() takes a single integer argument: yyless(n) causes the current token to consist of the first n characters of what was originally matched (that is, up to yytext[n-1]). The remaining yyleng-n characters are returned to the input stream. –Check the error3.y and error3.l

14 Error Recovery Why error recovery is necessary? –It may be possible to recover from the error and continue examining the file for additional errors, stopping the compiler before invoking the next stage. –This technique improves the productivity of the programmer by shortening the edit-compile-test cycle, since several errors can be repaired in each iteration of the cycle.

15 Yacc Error Recovery Yacc has some provision for error recovery, by using the error token. After reporting a syntax error, a yacc parser discards any partially parsed rules until it finds one in which it can shift an error token. It then reads and discards input tokens until it finds one which can follow the error token in the grammar.

16 Yacc Error Recovery (Cont’d) When an error occurs, the parser stops unless you provide error-handling subroutines. To continue processing the input to find more errors, restart the parser at a point in the input stream where the parser can try to recognize more input. One way to restart the parser when an error occurs is to discard some of the tokens following the error. Then try to restart the parser at that point in the input stream.

17 Yacc Error Recovery (Cont’d) The yacc command uses a special token name, error, for error handling. Put this token in the rules file at places that an input error might occur so that you can provide a recovery subroutine. If an input error occurs in this position, the parser executes the action for the error token, rather than the normal action.

18 Yacc Error Recovery (Cont’d) For example, a rule of the following form. –stat : error ';' It tells the parser that when there is an error, it should ignore the token and all following tokens until it finds the next semicolon. All tokens after the error and before the next semicolon are discarded. After finding the semicolon, the parser reduces this rule and performs any cleanup action associated with it.

19 Yacc Error Recovery (Cont’d) However, in this example, the parser stays in the error state for three input tokens following the error. To allow for this condition, use the following yacc statement. When the parser finds this statement, it leaves the error state and begins processing normally. –yyerrok;

20 statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } | error '\n' { yyerrok; printf("Something Wrong\n"); } Terminator Error handling routine

21 Yacc Error Recovery (Cont’d) Check the code –error2.y error2.l –./error2<errori3

22 Yacc Error Recovery (Cont’d) Clearing the Look-Ahead Token –The look-ahead token is the next token that the parser examines. –When an error occurs, the look-ahead token becomes the token at which the error was detected. – However, if the error recovery action includes code to find the correct place to start processing again, that code must also change the look-ahead token. –To clear the look-ahead token, include the following statement in the error-recovery action: yyclearin ;

23 Yacc Error Recovery (Cont’d) More complicated example input : error '\n' { yyerrok; printf(" Reenter last line: " ); } input { $$ = $4; } ;