Lecture 10 YACC – Yet Another Compiler Compiler Introduction to YACC and Bison Topics Yacc/Bison IntroductionReadings: February 13, 2006 CSCE 531 Compiler.

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

Compiler construction in4020 – lecture 2 Koen Langendoen Delft University of Technology The Netherlands.
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.
– 1 – CSCE 531 Spring 2006 Lecture 9 SLR Parse Table Construction Topics SLR Parse Table Construction Sets of Items Closure(I) Readings: 4.7 Homework:
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
CS 310 – Fall 2006 Pacific University CS310 Lex & Yacc Today’s reference: UNIX Programming Tools: lex & yacc by: Levine, Mason, Brown Chapter 1, 2, 3 November.
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.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
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,
LEX and YACC work as a team
LR Parsing Compiler Baojian Hua
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 컴파일러 입문.
–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
Lex.
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.
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.
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.
1 Steps to use Flex Ravi Chotrani New York University Reviewed By Prof. Mohamed Zahran.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
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.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
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
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.
CSCE 531 Compiler Construction
Syntax Analysis Part III
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
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
Parser and Scanner Generation: An Introduction
Syntax Analysis Part III
Bison Marcin Zubrowski.
Lecture 9 SLR Parse Table Construction
Syntax Analysis Part III
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Syntax-Directed Translation
Syntax Analysis - 3 Chapter 4.
Compiler Lecture Note, Miscellaneous
Compiler Structures 7. Yacc Objectives , Semester 2,
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Lecture 18 Language Recognized by a PDA
Presentation transcript:

Lecture 10 YACC – Yet Another Compiler Compiler Introduction to YACC and Bison Topics Yacc/Bison IntroductionReadings: February 13, 2006 CSCE 531 Compiler Construction

– 2 – CSCE 531 Spring 2006 Overview Last Time Test 1 Post Mortem Lexical Analysis Hash table Token / Lexeme / Token Code Review of LR Parsing Today’s Lecture YACC introductionReferences: Homework: write a YACC specification for the language “core”

– 3 – CSCE 531 Spring 2006 YACC Generated LALR(1) Parsers % flex lang.l// lex.yy.c % bison lang.y // lang.c % gcc lex.yy.c lang.c –o parse % parse input lang.y lang.lFLEX lex.yy.c yylex() lang.c yyparse() BISON Input source program Executable Program

– 4 – CSCE 531 Spring 2006 YACC Format and Usage Yet Another Compiler Compiler Stephen Johnson 1976 Takes grammar specification and generates the Action and GOTO tables Takes grammar specification and generates the Action and GOTO tables Bison = new and improved YACC YACC Format YACC Format Definitions section % productions / semantic actions section %routines

– 5 – CSCE 531 Spring 2006 Simple0.y in web/Examples/SimpleYacc %token DIGIT % line : expr '\n' ; expr : expr '+' term | term | term ; term : term '*' factor | factor | factor ; factor : '(' expr ')' | DIGIT | DIGIT ;% Grammar  Bison Specification expr  expr '+' term | term term  term '*' factor | factor factor  '(' expr ')' | DIGIT We first augment with line  expr ‘\n’

– 6 – CSCE 531 Spring 2006 Bison Specification File Example deneb> more simple1.y %token DIGIT % expr : expr '+' expr | expr '*' expr | expr '*' expr | '(' expr ')' | '(' expr ')' | DIGIT | DIGIT ;%

– 7 – CSCE 531 Spring 2006 Bison Reporting Conflicts deneb> bison simple1.y simple1.y contains 4 shift/reduce conflicts. bison -v simple1.y simple1.y contains 4 shift/reduce conflicts. So what are they? The “.output file” contains a description of the state machine and actions.

– 8 – CSCE 531 Spring 2006.output deneb> more simple1.output State 8 contains 2 shift/reduce conflicts. State 9 contains 2 shift/reduce conflicts. Grammar Number, Line, Rule Number, Line, Rule 1 5 expr -> expr '+' expr 1 5 expr -> expr '+' expr 2 6 expr -> expr '*' expr 2 6 expr -> expr '*' expr 3 7 expr -> '(' expr ')' 3 7 expr -> '(' expr ')' 4 8 expr -> DIGIT 4 8 expr -> DIGIT

– 9 – CSCE 531 Spring 2006 Terminals, with rules where they appear $ (-1) '(' (40) 3 ')' (41) 3 '*' (42) 2 '+' (43) 1 error (256) DIGIT (257) 4 Nonterminals, with rules where they appear expr (8) on left: , on right: on left: , on right: 1 2 3

– 10 – CSCE 531 Spring 2006 state 0 DIGIT shift, and go to state 1 DIGIT shift, and go to state 1 '(' shift, and go to state 2 '(' shift, and go to state 2 expr go to state 3 expr go to state 3 state 1 expr -> DIGIT. (rule 4) expr -> DIGIT. (rule 4) $default reduce using rule 4 (expr) $default reduce using rule 4 (expr)

– 11 – CSCE 531 Spring 2006 state 2 expr -> '('. expr ')' (rule 3) expr -> '('. expr ')' (rule 3) DIGIT shift, and go to state 1 DIGIT shift, and go to state 1 '(' shift, and go to state 2 '(' shift, and go to state 2 expr go to state expr go to state

– 12 – CSCE 531 Spring 2006 state 3 expr -> expr. '+' expr (rule 1) expr -> expr. '+' expr (rule 1) expr -> expr. '*' expr (rule 2) expr -> expr. '*' expr (rule 2) $ go to state 10 $ go to state 10 '+' shift, and go to state 5 '+' shift, and go to state 5 '*' shift, and go to state 6 '*' shift, and go to state 6 … for states 4 through 7

– 13 – CSCE 531 Spring 2006 Conflicts state 8 expr -> expr. '+' expr (rule 1) expr -> expr. '+' expr (rule 1) expr -> expr '+' expr. (rule 1) expr -> expr '+' expr. (rule 1) expr -> expr. '*' expr (rule 2) expr -> expr. '*' expr (rule 2) '+' shift, and go to state 5 '+' shift, and go to state 5 '*' shift, and go to state 6 '*' shift, and go to state 6 '+' [reduce using rule 1 (expr)] '+' [reduce using rule 1 (expr)] '*' [reduce using rule 1 (expr)] '*' [reduce using rule 1 (expr)] $default reduce using rule 1 (expr) $default reduce using rule 1 (expr)

– 14 – CSCE 531 Spring 2006 state 10 $ go to state 11 $ go to state 11 state 11 $default accept $default accept

– 15 – CSCE 531 Spring 2006 Main and yyerror

– 16 – CSCE 531 Spring 2006 bison simple0.y bison simple0.ydeneb> deneb> ls -lrt … - rw-r--r-- 1 matthews faculty Jun 30 12:04 simple0.tab.c deneb> wc simple0.tab.c simple0.tab.c simple0.tab.c

– 17 – CSCE 531 Spring 2006 gcc simple0.tab.c -ly Undefined first referenced symbol in file symbol in file yylex /var/tmp//ccW88jE5.o ld: fatal: Symbol referencing errors. No output written to a.out collect2: ld returned 1 exit status

– 18 – CSCE 531 Spring 2006 deneb> more simple1.y %token DIGIT % expr : expr '+' expr | expr '*' expr | expr '*' expr | '(' expr ')' | '(' expr ')' | DIGIT | DIGIT ;%

– 19 – CSCE 531 Spring 2006 deneb> bison simple1.y simple1.y contains 4 shift/reduce conflicts. bison -v simple1.y simple1.y contains 4 shift/reduce conflicts. deneb> ls -lrt … - rw-r--r-- 1 matthews faculty 2311 Jun 30 12:10 simple1.output

– 20 – CSCE 531 Spring 2006.output deneb> more simple1.output State 8 contains 2 shift/reduce conflicts. State 9 contains 2 shift/reduce conflicts. Grammar Number, Line, Rule Number, Line, Rule 1 5 expr -> expr '+' expr 1 5 expr -> expr '+' expr 2 6 expr -> expr '*' expr 2 6 expr -> expr '*' expr 3 7 expr -> '(' expr ')' 3 7 expr -> '(' expr ')' 4 8 expr -> DIGIT 4 8 expr -> DIGIT

– 21 – CSCE 531 Spring 2006 Terminals, with rules where they appear $ (-1) '(' (40) 3 ')' (41) 3 '*' (42) 2 '+' (43) 1 error (256) DIGIT (257) 4 Nonterminals, with rules where they appear expr (8) on left: , on right: on left: , on right: 1 2 3

– 22 – CSCE 531 Spring 2006 state 0 DIGIT shift, and go to state 1 DIGIT shift, and go to state 1 '(' shift, and go to state 2 '(' shift, and go to state 2 expr go to state 3 expr go to state 3 state 1 expr -> DIGIT. (rule 4) expr -> DIGIT. (rule 4) $default reduce using rule 4 (expr) $default reduce using rule 4 (expr)4

– 23 – CSCE 531 Spring 2006 state 2 expr -> '('. expr ')' (rule 3) expr -> '('. expr ')' (rule 3) DIGIT shift, and go to state 1 DIGIT shift, and go to state 1 '(' shift, and go to state 2 '(' shift, and go to state 2 expr go to state expr go to state

– 24 – CSCE 531 Spring 2006 state 3 expr -> expr. '+' expr (rule 1) expr -> expr. '+' expr (rule 1) expr -> expr. '*' expr (rule 2) expr -> expr. '*' expr (rule 2) $ go to state 10 $ go to state 10 '+' shift, and go to state 5 '+' shift, and go to state 5 '*' shift, and go to state 6 '*' shift, and go to state 6 … for states 4 through 7

– 25 – CSCE 531 Spring 2006 Conflicts state 8 expr -> expr. '+' expr (rule 1) expr -> expr. '+' expr (rule 1) expr -> expr '+' expr. (rule 1) expr -> expr '+' expr. (rule 1) expr -> expr. '*' expr (rule 2) expr -> expr. '*' expr (rule 2) '+' shift, and go to state 5 '+' shift, and go to state 5 '*' shift, and go to state 6 '*' shift, and go to state 6 '+' [reduce using rule 1 (expr)] '+' [reduce using rule 1 (expr)] '*' [reduce using rule 1 (expr)] '*' [reduce using rule 1 (expr)] $default reduce using rule 1 (expr) $default reduce using rule 1 (expr)

– 26 – CSCE 531 Spring 2006 state 9 expr -> expr. '+' expr (rule 1) expr -> expr. '+' expr (rule 1) expr -> expr. '*' expr (rule 2) expr -> expr. '*' expr (rule 2) expr -> expr '*' expr. (rule 2) expr -> expr '*' expr. (rule 2) '+' shift, and go to state 5 '+' shift, and go to state 5 '*' shift, and go to state 6 '*' shift, and go to state 6 '+' [reduce using rule 2 (expr)] '+' [reduce using rule 2 (expr)] '*' [reduce using rule 2 (expr)] '*' [reduce using rule 2 (expr)] $default reduce using rule 2 (expr) $default reduce using rule 2 (expr)

– 27 – CSCE 531 Spring 2006 state 10 $ go to state 11 $ go to state 11 state 11 $default accept $default accept

– 28 – CSCE 531 Spring 2006 YACC Generated LALR(1) Parsers % flex lang.l// lex.yy.c % bison lang.y // lang.c % gcc lex.yy.c lang.c –o parse % parse input lang.y lang.lFLEX lex.yy.c yylex() lang.c yyparse() BISON Input source program Executable Program

– 29 – CSCE 531 Spring 2006 man yacc NAME yacc - yet another compiler-compiler yacc - yet another compiler-compilerSYNOPSIS yacc [-dltVv] [-b file_prefix] [ -Q [y | n] ] [-P parser] [-p sym_prefix] file yacc [-dltVv] [-b file_prefix] [ -Q [y | n] ] [-P parser] [-p sym_prefix] fileDESCRIPTION The yacc command converts a context-free grammar into a set of tables for a simple automaton that executes an LALR(1) parsing algorithm. The grammar may be ambiguous. Specified precedence rules are used to break ambiguities. The yacc command converts a context-free grammar into a set of tables for a simple automaton that executes an LALR(1) parsing algorithm. The grammar may be ambiguous. Specified precedence rules are used to break ambiguities. The output file, y.tab.c, must be compiled by the C compiler to produce a function yyparse(). This program must be loaded with the lexical analyzer program, yylex(), as well as main() and yyerror(), an error handling routine. These routines must be supplied by the user. The output file, y.tab.c, must be compiled by the C compiler to produce a function yyparse(). This program must be loaded with the lexical analyzer program, yylex(), as well as main() and yyerror(), an error handling routine. These routines must be supplied by the user.

– 30 – CSCE 531 Spring 2006 Bison Arguments: Bison –h deneb> bison -h GNU bison generates parsers for LALR(1) grammars. Usage: bison [OPTION]... FILE If a long option shows an argument as mandatory, then it is mandatory for the equivalent short option also. Similarly for optional arguments. Operation modes: -h, --help display this help and exit -h, --help display this help and exit -V, --version output version information and exit -V, --version output version information and exit -y, --yacc emulate POSIX yacc -y, --yacc emulate POSIX yacc

– 31 – CSCE 531 Spring 2006 Bison Arguments continued Output: -d, --defines also produce a header file -d, --defines also produce a header file -v, --verbose also produce an explanation of the automaton -v, --verbose also produce an explanation of the automaton -b, --file-prefix=PREFIX specify a PREFIX for output files -b, --file-prefix=PREFIX specify a PREFIX for output files -o, --output=FILE leave output to FILE -o, --output=FILE leave output to FILE -g, --graph also produce a VCG description of the automaton -g, --graph also produce a VCG description of the automaton Report bugs to. deneb>

– 32 – CSCE 531 Spring 2006 Bison Arguments continued Parser: -S, --skeleton=FILE specify the skeleton to use -S, --skeleton=FILE specify the skeleton to use -t, --debug instrument the parser for debugging -t, --debug instrument the parser for debugging --locations enable locations computation --locations enable locations computation -p, --name-prefix=PREFIX prepend PREFIX to the external symbols -p, --name-prefix=PREFIX prepend PREFIX to the external symbols -l, --no-lines don't generate `#line' directives -l, --no-lines don't generate `#line' directives -n, --no-parser generate the tables only -n, --no-parser generate the tables only -k, --token-table include a table of token names -k, --token-table include a table of token names

– 33 – CSCE 531 Spring 2006 Makefiles A makefile is a file that specifies how to build something usually a piece of software The makefile is a collection of targets with each target having a series of commands to build the target To build the system one merely types “make” Make maintains dependencies and recompiles only those files that need to be recompiled Make documents how to build a system and allows a novice to install a piece of software

– 34 – CSCE 531 Spring 2006 Makefiles The makefile is a collection of targets with each target having a series of commands to build the target The target specification has the form targ: files that targ depends on first unix command in a sequence to build target first unix command in a sequence to build target next command in sequence to build target next command in sequence to build target … … The Makefile specifies a forest of targets depending on other targets

– 35 – CSCE 531 Spring 2006 Example Makefile prog: prog.o routines.o prog.o: prog.c prog.h gcc –c prog.c routines.o: routines.c prog.h gcc –c routines.c

– 36 – CSCE 531 Spring 2006 Make Builtin Rules MacrosName=value $(Name)//invocation of macro; replace macro with its value $(Name)//invocation of macro; replace macro with its value Suffixes:.c,.f,.y,.l … Rules prog.o: prog.c $(CC) $(CFLAGS) –c prog.c Actually the real rules are generalizations of the above. Use “make –p” to see all the rules.

– 37 – CSCE 531 Spring 2006 /class/csce /web/Examples/SimpleYacc/Makefile # Makefile for Simple Yacc and Lex examples CC=gccCFLAGS=CFLAGS=-DYYDEBUGLEX=flexYACC=bisonYACCFLAGS=YACCFLAGS=-t simple3: simple3.tab.h simple3.tab.o lex.yy.o $(CC) $(CFLAGS) simple3.tab.o lex.yy.o -ly -o simple3 $(CC) $(CFLAGS) simple3.tab.o lex.yy.o -ly -o simple3 simple3.tab.h: simple3.y bison -d $(YACCFLAGS) simple3.y bison -d $(YACCFLAGS) simple3.y simple3.tab.c: simple3.y bison $(YACCFLAGS) simple3.y bison $(YACCFLAGS) simple3.y lex.yy.c: simple3.l flex simple3.l flex simple3.l

– 38 – CSCE 531 Spring 2006 Lex Specification simple3.l %{ #include "simple0.tab.h" %} digit [0-9]+ % \n return('\n'); \+ return(PLUS); \* return(TIMES); \( return(LPAREN); \) return(RPAREN); {digit} return(DIGIT); %yywrap(){}

– 39 – CSCE 531 Spring 2006 Example Yacc Spec file simple3.y %token DIGIT TIMES PLUS LPAREN RPAREN % line : expr '\n' {printf("recognized an expr\n"); exit(0); exit(0); } ; expr : expr PLUS term | term | term ; term : term TIMES factor | factor | factor ; factor : LPAREN expr RPAREN | DIGIT | DIGIT ;%

– 40 – CSCE 531 Spring 2006 Simple4.l %{ #include "simple4.tab.h" %} digit [0-9]+ % \n return('\n'); \+ return(PLUS); \* return(TIMES); \( return(LPAREN); \) return(RPAREN); {digit} { yylval= atoi(yytext); return(INTEGER);} % yywrap(){ }

– 41 – CSCE 531 Spring 2006 Simple4.y %{ #include #include int yylineno = 1; %} %token INTEGER PLUS TIMES LPAREN RPAREN % line : expr '\n' {printf("%d\n", $1);} | line expr '\n' {printf("%d\n", $2);} | line expr '\n' {printf("%d\n", $2);} ; expr : expr PLUS term {$$ = $1 + $3;} | term | term ; term : term TIMES factor {$$ = $1 * $3;} | factor | factor ; factor : LPAREN expr RPAREN {$$ = $2;} | INTEGER {$$ = $1;} /* the default action */ | INTEGER {$$ = $1;} /* the default action */ ;%