–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.

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

Structure of a YACC File Has the same three-part structure as Lex Each part is separated by a % symbol The three parts are even identical: – definition.
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.
Lecture 10 YACC – Yet Another Compiler Compiler Introduction to YACC and Bison Topics Yacc/Bison IntroductionReadings: February 13, 2006 CSCE 531 Compiler.
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.
Language processing: introduction to compiler construction Andy D. Pimentel Computer Systems Architecture group
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
LEX and YACC work as a team
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
1 October 14, October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Using CookCC.  Use *.l and *.y files.  Proprietary file format  Poor IDE support  Do not work well for some languages.
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
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
Introduction to Yacc Ying-Hung Jiang
Introduction to YACC Panfeng Xue
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.
Syntactic Analysis Tools
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.
Applications of Context-Free Grammars (CFG) Parsers. The YACC Parser-Generator. by: Saleh Al-shomrani.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
國立台灣大學 資訊工程學系 薛智文 98 Spring Syntax-Directed Translation (textbook ch#5.1–5.6, 4.8, 4.9 )
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.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
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.
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:
Yacc.
Language processing: introduction to compiler construction
Syntax Analysis Part III
Compiler Construction
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Chapter 4 Syntax Analysis.
Context-free Languages
Syntax Analysis Part III
Bison: Parser Generator
Syntax Analysis Part III
Bison Marcin Zubrowski.
Syntax Analysis Part III
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Syntax-Directed Translation
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
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Systems Programming & Operating Systems Unit – III
Presentation transcript:

–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct translation by writing grammar productions and semantic actions –LALR(1) is more powerful than LL(1). Work with lex. YACC calls yylex to get the next token. –YACC and lex must agree on the values for each token. Like lex, YACC pre-dated c++, need workaround for some constructs when using c++ (will give an example).

–Writing a parser with YACC (Yet Another Compiler Compiler). YACC file format: declarations /* specify tokens, and non-terminals */ % translation rules /* specify grammar here */ % supporting C-routines Command “yacc yaccfile” produces y.tab.c, which contains a routine yyparse(). –yyparse() calls yylex() to get tokens. yyparse() returns 0 if the program is grammatically correct, non-zero otherwise

The declarations part specifies tokens, non-terminals symbols, other C/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 to store attributes (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 a yacc specification file and produces 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(const char *str) { printf("yyerror: %s at line %d\n", str, yyline); } main() { if (!yyparse()) {printf("accept\n");} else printf("reject\n"); }

Hooking yacc and lex together, see example0.y and lexer.l Matching the tokens –In lex: #define INTEGERCONST 2 #define PLUSNUM 4 –In yacc: %token INTEGERCONST 2 %token PLUSNUM 4 All tokens used in the yacc grammar need to be specified. Some tokens recognized by lex may not be in the yacc grammar token. See lexer.l Non- terminals do not need to be specified. lex.yy.c and y.tab.c may be compiled separately, or yacc file may just include lex.yy.c as in example0.y Global variables such as yyline, yycolumn, and yylval can be used in yacc routines.

–YACC automatically builds a parser for the grammar (LALR parser). May have shift/reduce and reduce/reduce conflicts when the grammar is not LALR –In this case, you will need to modify grammar to make it LALR in order for yacc to work properly. YACC tries to resolve conflicts automatically –Default conflict resolution: »shift/reduce --> shift »reduce/reduce --> first production in the state –Not very informative, not clear if such action is what you wanted. ‘yacc -v *.y’ will generate a report in file ‘y.output’. See example1.y

–Resolving conflicts modify the grammar. See example1.y  example0.y 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

Attribute grammar with yacc –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 INTEGERCONST 2 %type exp %type term %type item Semantic actions associate with productions can be specified. The union is used to define yylval (don’t need to redeclare again, but you can directly using yylval.semantic_value in the lex code).

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 anywhere in the production, it is also counted as a symbol.

Semantic actions –Semantic actions can be in anywhere in the production, an action is also counted as a symbol. item : LPARENnumber {cout << “debug”;} exp RPARENnumber {$$ = $3;} | ICONSTnumber {$$ = $1;} ;

Multiple attributes and C/C++ issues Multiple attributes can be associated with a symbol by declaring a structure in the union. See cal_trans_c.y (in yacc1_cop4020). –Unfortunately C++ does not like union with a structure or a class. –A workaround example is given in cal_trans_cpp.y.