PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.

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

Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
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.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
Tools for building compilers Clara Benac Earle. Tools to help building a compiler C –Lexical Analyzer generators: Lex, flex, –Syntax Analyzer generator:
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
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
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 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.
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 컴파일러 입문.
Lexical and Syntax Analysis
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
LEX AND YACC.
Introduction to Lex Ying-Hung Jiang
–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
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
Practical 1-LEX Implementation
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.
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.
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.
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.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Syntax error handling –Errors can occur at many levels lexical: unknown operator syntactic: unbalanced parentheses semantic: variable never declared runtime:
Yacc.
Syntax Analysis Part III
Tutorial On Lex & Yacc.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Chapter 4 Syntax Analysis.
Context-free Languages
Syntax Analysis Part III
Bison: Parser Generator
CMPE 152: Compiler Design December 5 Class Meeting
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
Compiler Structures 7. Yacc Objectives , Semester 2,
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:

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 2  Introduction  Input Specification  Rule section  Ambiguity and Conflicts  Precedence  Error Handling  Example Contents 정상

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 3  Yacc provides a general tool for imposing structure on the input to a computer program. cfg + actions C program –The class of specifications accepted is very general one, that is, LALR(1) grammar with disambiguating rules. –Base language : C Introduction Yacc

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 4  Model for Lex and Yacc lexical grammar rules(*.l) rules(*.y) LEXYacc token input output ( lex.yy.c ) ( y.tab.c ) YYlex YYparse

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 5 ▶ lexical analysis –the user must supply a lexical analyzer to read the input stream and communicate tokens(with values, if desired) to the parser. –A very useful tool for constructing lexical analyzer is lex. token number : yylex() return value token value : yylval(external variable). ▶ Parser Actions –An LR Parser : shift, reduce, accept, and error. when a shift takes place, the parser calls a lexical analyzer to get a token and the external variable yylval is copied onto the value stack. when a rule is reduced, the code supplied with the rule is executed before the stack is adjusted. After return from the user code, the reduction is carried out. –In addition to the stack holding states, the value stack running in parallel with it holds the values from the lexical analyzer and the actions associated with rules.

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 6  format : declarations % rules % programs  The declaration section –It is optional part. –%token declares names representing tokens. ex) %token name1 name2... –%start declares the start symbol explicitly. By default, the start symbol is taken to be the left hand side of the first production rule in the rules section. Input Specification

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 7  The rules section Form : A : RHS {action code} ; where, A : Left Hand Side of a production, RHS : Right Hand Side of a production rule, action code : C statements.  The program section is copied into the generated program.

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 8  grammar rules + actions –With each grammar rule, the user may associate actions to be performed each time the rule is recognized in the input process.  Grammar Rule Description - form : A : RHS where, A : a nonterminal symbol, RHS : a sequence of names and literals. ex) BNF ::= + | YACC expression : expression '+' term | term Rule section

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 9 –A literal consists of a character enclosed in single quote "'". As in C, all escape sequences are recognized. ex) '\n' newline '\b' backspace ’ \t' tab '\ooo' ooo in octal '\\' backslash –The names used in the RHS of a grammar rule may represent tokens or nonterminal symbols. Names may be of arbitrary length, and may be made up of letters, dot ".", underscore "_", and noninitial digits. Uppercase and lowercase letters are distinct. –The vertical bar "|" can be used to avoid rewriting the left hand side. ex) A : B C D ; A : B C D A : E F ; | E F A : G ; | G ; –ε-production ex) A -> ε YACC A : ;

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 10  Action Description –An action is an arbitrary C statements enclosed in curly braces { and }. ex) expression : expression '+' term { printf("addition expression detected\n"); } ; expression : term { printf("simple expression detected\n"); } ; –In a real parser, these actions can be used to construct the parse tree(syntax tree) or to generate code directly. –YACC permits an action to be written in the middle of a rule as well as at the end. –YACC parser uses only names beginning with yy; the user should avoid such names.

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 11  YACC provides a facility for associating values with the symbols used in a grammar rule. –$$, $1, $2,... represent the values of each grammar symbol. –Values can be passed to other grammar rules by performing an assignment in the action part to the pseudo variable $$.  Parse tree construction –node(L,n1,n2) creates a node with label L, with the descendants n1 and n2, and returns the index of the newly created node. ex) expr : expr '+' expr { $$ = node('+',$1,$3); }

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 12  Ambiguity –A set of grammar rules is ambiguous if there is some input string that can be structured in two or more different ways.  Conflicts –shift/reduce, reduce/reduce –Yacc invokes two disambiguating rules by default: In a shift/reduce conflict, the default is to do the shift. In a reduce/reduce conflict, the default is to do reduce by the earlier grammar rule. Ambiguity and Conflicts

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 13  %left, %right, %nonassoc ex) %right '=' %left '+' '-' %left '*' '/' % expr : expr '=' expr | expr '+' expr | expr '-' expr | expr '*' expr | expr '/' expr ; a = b = c * d - e - f * g a = (b = (((c * d) - e) - (f * g))) Precedence

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 14  It is seldom acceptable to stop all processing when an error is found; it is more useful to continue scanning the input to find further syntax errors.  The token name error is reserved for error handling. This name can be used in grammar rules; in effect, it suggests places where errors are expected, and recovery might take place. The parser pops its stack until it enters a state where the token "error" is legal. Error Handling

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 15  Problem:a rudimentary desk calculator operating on integer values.  calc.l %{ /* LEX source for calculator program */ %} % [ \t] ; /* ignore blanks and tabs */ [0-9]+ {yylval := atoi(yytext); return NUMBER;} "mod" return MOD; "div" return DIV; "sqr" return SQR; \n|. return yytext[0]; /* return everything else */ Example

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 16  execution sequence % lex calc.l % yacc calc.y % cc y.tab.c -ll -o calc % calc *5 23 (3+4)*5 35 sqr sqr mod 7 4 (3)) syntax error Try again ↑C %

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 17  calc.y %{ /* YACC source for calculator program */ # include %} %token NUMBER DIV MOD SQR %left '+' '-' %left '*' DIV MOD %left SQR % comm : comm '\n' | lambda | comm expr '\n' {printf("%d\n", $2);} | comm error '\n' {yyerrok; printf(" Try again \n");} ; expr : '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | expr '-' expr {$$ = $1 - $3;} | expr '*' expr {$$ = $1 * $3;} If an error is detected in the parse, the parser skips to a newline character, the error status is reset(yyerrok) and an appropriate message is output.

PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 18 | expr MOD expr {$$ = $1 % $3;} | SQR expr {$$ = $2 * $2;} | NUMBER ; lambda: /* empty */ ; % #include "lex.yy.c" yyerror(s) char *s; { printf("%s\n",s); } main() { return yyparse(); }