Beruflich Dokumente
Kultur Dokumente
1
AN EXAMPLE
As a rst exer
ise, we shall develop a parser for the
following expression language:
exp ! num
j exp + exp
j
exp exp
j exp
j ( exp )
/*C de
larations*/
%{
%}
/* YACC De
larations */
%union
{
int tokenval;
}
%token NUM
%left '-' '+'
%left NEG /* negation--unary minus */
2
/* Grammar follows */
%%
main ()
{
yyparse ();
}
Assume that this ya
s
ript is in the le example1.y. If
the lex s
ript for the language is in example1.l, then the
parser for the language is generated by the following:
# lex example1.l
# ya
-d example1.y
#
-o example1 lex.yy.
y.tab.
-ll
Now suppose that the le junk
ontains:
23.4+6.2--2.5+(1.0+2)
3
then the
ommand
# example1 < junk
will produ
e:
rule 1
rule 1
rule 2
rule 1
rule 4
rule 3
rule 1
rule 1
rule 2
rule 5
rule 2
4
Some observations:
Just like a lex s
ript, a ya
s
ript has three parts:
. . . denition se
tion. . .
%%
. . . rules se
tion. . .
%%
. . . user dened fun
tions. . .
The denition se
tion is divided into two parts, C
denitions and ya
denitions. The C denitions
are pla
ed verbatim in the early part of the gener-
ated parser.
A rule is a produ
tion { a
tion pair. A produ
tion
a
tion pair is of the form:
A : B C D C-statement;
E : G H C-statement;
I : J C-statement;
grammar rule.
6
YACC DISAMBIGUATING RULES
In absen
e of any asso
iativity and pre
eden
e informa-
tion:
1. In
ase of a shift-redu
e
on
i
t, shift.
2. In
ase of a redu
e-redu
e
on
i
t, redu
e with the
earlier rule.
From the pre
eden
e and asso
iativity information of
tokens, asso
iate pre
eden
e and asso
iativity to gram-
mar rules; it is the pre
eden
e and asso
iativity of the
last token in the body of the grammar rule.
In
ase of a shift-redu
e
on
i
t,
ompare the pre
e-
den
e of the symbol to be shifted with the pre
eden
e
of the rule to be used for redu
tion.
3. If pre
eden
e of symbol is higher then shift, else
redu
e,
if the pre
eden
es of both are the same, then:
4. If the symbol (and the grammar rule) is left asso-
iative then redu
e, else shift.
7
ILLUSTRATING DISAMBIGUATION
We shall illustrate the disambiguating rules by using
the debugging fa
ilities of ya
. To do this,
hange the
main program to set a variable yydebug to a non-zero
value. Also run ya
using the -t swit
h.
EXAMPLE 1
For the ya
grammar:
%start A
%%
D: 'b' '
';
A: 'a' D
| 'a' 'b' '
'
;
%%
the output for the string ab
is:
Starting parse
Entering state 0
Reading a token: Next token is 97 ('a')
Shifting token 97 ('a'), Entering state 1
Reading a token: Next token is 98 ('b')
Shifting token 98 ('b'), Entering state 2
Reading a token: Next token is 99 ('
')
Shifting token 99 ('
'), Entering state 4
Redu
ing via rule 1 (line 6), 'b' '
' -> D
state sta
k now 0 1
Entering state 3
Redu
ing via rule 2 (line 8), 'a' D -> A
8
state sta
k now 0
Entering state 5
Reading a token: Now at end of input.
Shifting token 0 ($), Entering state 6
Now at end of input.
EXAMPLE 2
Consider an example in whi
h no asso
iativity and pre
e-
den
e information is given.
%token NUM
%%
exp: NUM
| exp '+' exp
| exp '-' exp
| '-' exp
| '(' exp ')'
;
%%
9
Entering state 4
Reading a token: Next token is 43 ('+')
Shifting token 43 ('+'), Entering state 8
Reading a token: Next token is 258 (NUM)
Shifting token 258 (NUM), Entering state 1
Redu
ing via rule 1 (line 11), NUM -> exp
redu
ing by rule 1
state sta
k now 0 4 8
Entering state 11
Reading a token: Next token is 45 ('-')
Shifting token 45 ('-'), Entering state 7
Reading a token: Next token is 258 (NUM)
Shifting token 258 (NUM), Entering state 1
Redu
ing via rule 1 (line 11), NUM -> exp
redu
ing by rule 1
state sta
k now 0 4 8 11 7
Entering state 10
Reading a token: Now at end of input.
Redu
ing via rule 3 (line 13), exp '-' exp -> exp
redu
ing by rule 3
state sta
k now 0 4 8
Entering state 11
Now at end of input.
Redu
ing via rule 2 (line 12), exp '+' exp -> exp
redu
ing by rule 2
state sta
k now 0
Entering state 4
Now at end of input.
Shifting token 0 ($), Entering state 12
Now at end of input.
10
EXAMPLE 3
For our original example, for the same input, the output
will be:
Starting parse
Entering state 0
Reading a token: Next token is 258 (NUM)
Shifting token 258 (NUM), Entering state 1
Redu
ing via rule 1 (line 14), NUM -> exp
redu
ing by rule 1
state sta
k now 0
Entering state 4
Reading a token: Next token is 43 ('+')
Shifting token 43 ('+'), Entering state 8
Reading a token: Next token is 258 (NUM)
Shifting token 258 (NUM), Entering state 1
Redu
ing via rule 1 (line 14), NUM -> exp
redu
ing by rule 1
state sta
k now 0 4 8
Entering state 11
Redu
ing via rule 2 (line 15), exp '+' exp -> exp
redu
ing by rule 2
state sta
k now 0
Entering state 4
Reading a token: Next token is 45 ('-')
Shifting token 45 ('-'), Entering state 7
Reading a token: Next token is 258 (NUM)
Shifting token 258 (NUM), Entering state 1
Redu
ing via rule 1 (line 14), NUM -> exp
redu
ing by rule 1
11
state sta
k now 0 4 7
Entering state 10
Redu
ing via rule 3 (line 16), exp '-' exp -> exp
redu
ing by rule 3
state sta
k now 0
Entering state 4
Reading a token: Now at end of input.
Shifting token 0 ($), Entering state 12
Now at end of input.
12
INTERACTION
How does the parser intera
t with the parser. One way
is to physi
ally insert the yylex() fun
tion in the gen-
erated parser. Then the de
larations in the generated
parser are available to the lexer.
A better way is to
ommuni
ate through the ya
gen-
erated header le y.tab.h, using the swit
h -d. Here is
y.tab.h for a variation of the rst example. Only the
de
laration part is shown: example.
/* YACC De
larations */
%union
{
double tokenval;
}
/* Grammar follows */
%%
exp: NUM
| exp '+' exp
| exp '-' exp
13
;
%%
The generated y.tab.h le is:
typedef union
{
int double;
} YYSTYPE;
#define NUM 258
#define NEG 259
14
%}
ws [ \t\n℄+
letter [A-Za-z℄
digit [0-9℄
number {digit}+(\.{digit}+)?(E[+\-℄?{digit}+)?
%%
%%
15