UNIT 2 Hand Written
UNIT 2 Hand Written
3.12 YACC
YACC stands for Yet Another Compiler-Compiler.
I t is a LALR parser generator.
YACC
Figure 3.18 Creating translator using
e.
Yyerror (str)
char *str;
s at line &d\n", str, yylinei
print f ("yyerror:
main ()
if (!yYparse()) {
printf ("accept \n");
else printf ("reject \n") ;
translation rules
supporting C-routines
Declarations part
Declaration ofthe tokens that can be used by translation rules are written.
%token keyword.
I t contains two sections which are optional.
The first section contains C declarations delimited by % and 6.
<body>n
<semantic action >n}
3.12.1 Conventions in YACC Productions
First head is the start symbol.
Unquoted strings are non-terminals.
Quoted strings are terminals.
The character code of the terminal is given to the parser as an integer.
Vertical bar represents alternative bodies.
Each alternative is associated with a semantic action.
Syntax Arnalysta 197
B e m a n t i ca c t i o n
of statements forms
C
associative.
right
It makes right associative.
prec UMINUS
TasS1gn precedence for unary minus operator.
okens listed in declaration part will be in the order of their precedence, lowest first.
dec
198 Compiler Design
lex sample.l
yacc test.y
cc y.tab.c -ly - 11