0% found this document useful (0 votes)
28 views77 pages

UNIT 2 Hand Written

Uploaded by

oni6969427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views77 pages

UNIT 2 Hand Written

Uploaded by

oni6969427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

Syntax Analys1s 195

Table 3.2 Macros for error


handiing
YYERROR Causes the parser to initiate error handling.
YYABORT Causes the parser to return with a value of .
YYACCEPT Causes the parser to return with a value of 0.
YYRECOVERINGO Returns a value of 1if a syntax error has
been detected and the
parser has not yet fully
recovered.

3.12 YACC
YACC stands for Yet Another Compiler-Compiler.
I t is a LALR parser generator.

Design of syntax analyzer/parser generator YACC


YACC constructs a translator as depicted in Figure 3.18

translate.y YACC compiler y.tab.c

ytab.c C compiler a.out

Input a.out output

YACC
Figure 3.18 Creating translator using

to YACC compiler which


YACC specification ofthe translator (translate.y) is given
transforms it into y.tab.c that represents LALR parser in C program.
is done by the UNIX
The transformation of YACC specification into C program
which uses the LALR method.
command yacc yaccfile', i.e., yacc translate.y
returns 0 if the program is correct, non-zero
It contains a routine yyparse() which
otherwise.
with ly library
The object program a.out is obtained by the compilation of y.tab.c
using the command cc y.tab.c ly.
get next token.
I t also works with lex. YACC calls yylex()
to
token.
YACC and lex must agree on the values for each
196 Compiler Design

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") ;

YACC file format


declarations

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.

Translation rules part


Grammar productions and associated semantic actions are given by translation rules.
eg, The production<head><body>1 | < body>2 | |<body>n can
given by translation rule as

<head> <body>1 semantic action >1


<body>2 {<semantic action >2}

<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

nwe a semantic action


seque
In semantic action
Convention

mibute value associated with non-terminal of the head


Am
Valueassociated with ith grammar symbol.
symbol in a production
First grammar
e
expr expr + ' term $$=$1 +$3
term

Supporting C-routines part

Eror recovery routines yerror() and the function yylex() is provided.


Lex is used to produce yylex() which produces tokens with token name and its attribute
value.
Token name must be declared in the declarations part and the attribute value is passed
to the parser through yyhval, which isa YACC defined variable.

Precedence and associativity

I f the grammar in YACC specification is ambiguous, parsing-action conflicts will be


generated by LALR parser
Parsing-action conflicts and its description can be obtained by invoking YACC-v which
produces a file y.output containing kemels of set of items, description of parsing-action
conficts and LR parsing table showing how conflicts can be resolved.
YACC invokes two disambiguating rules by default:
I n a shift-reduce conflict, the default is to do the shift.
In a reduce-reduce conflict, the default is to reduce by the earlier grammar rule in
YACC specification.
Conficts can also be resolved by assigning precedences and associativities to terminals,
which give rise to disambiguating rules.
e.g,
left -
tmakes+and - of same precedence and lett

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

3.12.2 Creating YACC Lexical Analyzers with Lex

Lex can be used with YACC


YACC requires the lexical analyzer yylex().
Since the output file of lex is complied as part YACC output file y.tab.c the routine
yylex() is replaced by #include lex.yy.c to access YACC names for tokens.
is
In UNIX to produce a translator, both lex specification and yacc specification compiled
YACC specification is test.y then
together, i.e., if lex specification is sample.l and

lex sample.l
yacc test.y
cc y.tab.c -ly - 11

The pattern to match any character is written


as \n|.

You might also like