Download - Introduction to Yacc (Bison)
-
7/30/2019 Introduction to Yacc (Bison)
1/21
Introduction toa parser generator
National
1 Compiler Lab.| Mr. B.
acc (Bison)
Mr. Bhaskar Mondal
nstitute of Technology, Jamshedpur
ondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
2/21
An Over View
yacc stands for "yet another co
Updated version is Bison
parsers what lex is to scanner
input of a grammar specificatio
generates an LALR(1) parser to
grammar.
2 Compiler Lab.| Mr. B.
piler compiler
recognize sentences in that
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
3/21
How It Works
It use with C code and gener
Works conjunction with a le
provide a grammar rules in a
invokeyacc on the .yfile any.tab.c in C
3 Compiler Lab.| Mr. B.
tes a parser written in C.
-generated scanner
file, with .yextension
it creates they.tab.h and
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
4/21
How It Works (cont)
The file provides an extern fun
attempt to successfully par
You compile that C file normall
By default, the parser reads frojust like a lex-generated scanne
4 Compiler Lab.| Mr. B.
tionyyparse.y that will
e a valid sentence.
with any C Compiler.
stdin and writes to stdoudoes.
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
5/21
How It Works Steps
%yacc myFile.ycreatesy.tab.c o
% gcc -c y.tab.c compiles parser c
% gcc o parse y.tab.o lex.yy.o l
% parse invokes parser, reads fro
5 Compiler Lab.| Mr. B.
C code for parser
de
-lylink parser, scanner, libr
stdin
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
6/21
yacc File Format
%{
Declarations
%}
Definitions
%%
Productions
%%
User subroutines
6 Compiler Lab.| Mr. B. Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
7/21
yacc File Format (cont
The optional Declarations and
used for ordinary C code The optional Definitions sectio
features: defining token codes,
establishing operator precedence
setting up the global variables usscanner and parser.
The Productions section is for specifcan associate an action with eaproduction), which allows you to do
reduce using that production.
7 Compiler Lab.| Mr. B.
)
ser subroutines sections ar
Contains various parser
and associativity,
d to communicate between the
the grammar rules. As in lex, yh pattern (this time ahatever processing is needed a
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
8/21
Example:
Write ayacc input fcalculator that recogbinary postfix expre
8 Compiler Lab.| Mr. B.
le for a simplenizes and evaluatesions using a stack
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
9/21
Declaration Part
%{
#include #include
static int Pop();
static int Top();
static void Push(int val);
%}
9 Compiler Lab.| Mr. B. Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
10/21
Definition and Production%token T_Int
%%S : S E '\n' { printf("= %d\n", Top())|;E : E E '+' { Push(Pop() + Pop()); }
| E E '-' { int op2 = Pop(); Push(Pop| E E '*' { Push(Pop() * Pop()); }| E E '/' { int op2 = Pop(); Push(Pop| T_Int { Push(yylval); };
%%
10 Compiler Lab.| Mr. B.
art
; }
) - op2); }
) / op2); }
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
11/21
User Sub Routine
static int stack[100], count = 0;
static int Pop() {assert(count > 0);
return stack[--count];
}
}static void Push(int val) {
assert(count 0);
return stack[count-1];
}
int main() {return yyparse();
}
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
12/21
Note about the Code:
Any token types returned from
using %token Used by scanner to tell the pars
The global variableyylval is usinformation about the lexeme i
A colon(:) is used in place of th a vertical bar(|) separates the v
A semicolon terminates the rul
Yacc dont minds about line bo
enough spaces.
12 Compiler Lab.| Mr. B.
he scanner must be defined
r about each token scanned
ed to store additional attribuself.
arrow() in the rules.rious productions.
.
ndaries like lex. You can use
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
13/21
Note about the Code: (Con
Within the braces C code are th
a production. No Code no actio The Header symbol of first rule
symbol for the grammar.
yyparse is the function genera
stdin. yyparse returns 0 if the parse w
If it encounters an error it callsdefault prints the generic "pars
13 Compiler Lab.| Mr. B.
)
re for the action associated
at reduction.in the file is assumed the sta
ed byyacc. It reads input f
as successful and 1 otherwis
the routineyyerror,whicherror" message and quits.
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
14/21
The lex file used:
%{
#include "y.tab.h"%}
%%
[0-9]+ { yylval = atoi(yytext); ret
[-+*/\n] { return yytext[0];}
. { /* ignore everything else */ }
14 Compiler Lab.| Mr. B.
rn T_Int;}
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
15/21
What does it do
yylex will return the ASCII repr
calculator operators, recognize integers , and
ignore all other characters.
assembles a series of digits intonumeric value and stores inyypassing attributes from the sca
The token type T_Int is return
15 Compiler Lab.| Mr. B.
sentation of the
an integer, it converts to aval (the global reserved for
ner to the parser).
d.
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
16/21
Tie this all together: a calc
runyacc/bison on the grammar
y.tab.c andy.tab.h files run lex/flex on the scanner speci
file.
Compile the two .c files and link
calc: lex.yy.o y.tab.o
gcc -o calc lex.yy.o y.tlex.yy.c: calc.l y.tab.c
flex calc.l
y.tab.c: calc.y
bison -vdty calc.y
16 Compiler Lab.| Mr. B.
lator to born!
pecification to generate the
ication to generate the lex.yy
hem together.
b.o -ly -ll
Mondal | NIT Jamshedpur
Wh h h f d LALR( )
-
7/30/2019 Introduction to Yacc (Bison)
17/21
What happens when you feedgrammar?
yacc reports any conflicts whe
resolving the conflicts and buil In A shift/reduce conflict,yacc
In a reduce/reduce conflict, itfirst in the file.
Must control what happens byand associativity for your opera
17 Compiler Lab.| Mr. B.
yacc a non LALR(1)
trying to fill in the table
ing a table anyway.will choose the shift.
ill reduce using the rule dec
explicitly declaring precedenors.
Mondal | NIT Jamshedpur
I bi h i l d ddi i
-
7/30/2019 Introduction to Yacc (Bison)
18/21
In put an ambiguous grammamultiplication, and exponenti
%token T_Int
%%E : E '+' E
| E '*' E
| E '^' E
| T_Int
;
conflicts: 9 shift/reduce
y.Out...State 6 cState 7 cState 8 c
...
18 Compiler Lab.| Mr. B.
that includes addition,tion
ut
ntains 3 shift/reduce conflictsntains 3 shift/reduce conflictsntains 3 shift/reduce conflicts
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
19/21
Defining precedence levels
one per line, from lowest to hig
associativity (either left, right, Several terminals can be on the
precedence.
%token T_Int
%left '+'
%left '*'
%right ' '
%%
E : E '
| E '*'| E ' '| T_I
;19 Compiler Lab.| Mr. B.
est, and indicate the
r nonassoc).same line to assign them eq
+' E
EE
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
20/21
Defining precedence levels
So if a 8 + 2 is on the stack a
higher precedence than the 8
If 8*2 is on the stack and + i
If 8+2 is on the stack and + ibreaks the tie, a left-to-rightrule and then go on, a right-tthe reduction.
20 Compiler Lab.| Mr. B.
Cont)
d * is coming up, the * ha
+2, so it shifts.
coming up, it reduces.
coming up, the associativssociativity would reduce-left would shift and post
Mondal | NIT Jamshedpur
-
7/30/2019 Introduction to Yacc (Bison)
21/21
References:
J. Levine, T. Mason, and D. Bro
CA: OReilly & Associates, 1992 A. Pyster, Compiler Design and
Van Nostrand Reinhold, 1988.
21 Compiler Lab.| Mr. B.
wn, Lex and Yacc. Sebastopo
.Construction. New York, NY
Mondal | NIT Jamshedpur