Next: A first simple example
Up: YACC - Yet Another Compiler
Previous: YACC - Yet Another Compiler
A YACC source program is structurally similar
to a LEX one.
declarations
%%
rules
%%
routines
- The declaration section may be empty.
Moreover, if the routines section is omitted, the second
%% mark may be omitted also.
- Blanks, tabs, and newlines are ignored except that they may not appear in names.
THE DECLARATIONS SECTION may contain the following items.
- Declarations of tokens.
Yacc requires token names to be declared as such
using the keyword %token.
- Declaration of the start symbol
using the keyword %start
- C declarations: included files, global variables, types.
- C code between %{ and %}.
RULES SECTION.
A rule has the form:
nonterminal : sentential form
| sentential form
.................
| sentential form
;
Actions may be associated with rules and are executed when
the associated sentential form is matched.
LEX-YACC INTERACTION
yyparse() calls yylex() when it needs a new token.
LEX |
YACC |
return(TOKEN) |
%token TOKEN |
|
TOKEN is used in production |
The external variable yylval
- is used in a LEX source program
to return values of lexemes,
- yylval is assumed to be integer if you take no other action.
- Changes related to yylval must be made
If you need a record type, then add it in the union. Example:
%union {
struct s {
double fvalue;
int ivalue;
} t;
}
- in the LEX specification use the record name and record field in assignments:
yylval.t.ivalue = ......
- in the YACC rules specification use the record field only in the assignment:
$1.ivalue = ......
assuming that $1 has the appropriate type, whatever it denotes.
Next: A first simple example
Up: YACC - Yet Another Compiler
Previous: YACC - Yet Another Compiler
Marc Moreno Maza
2004-12-02