next up previous
Next: A first simple example Up: YACC - Yet Another Compiler Previous: YACC - Yet Another Compiler

Structure of a YACC source program

A YACC source program is structurally similar to a LEX one.

declarations
%%
rules
%%
routines


THE DECLARATIONS SECTION may contain the following items.


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

If you need a record type, then add it in the union. Example:

%union {
   struct s {
      double fvalue;
      int ivalue;
   } t;
}


next up previous
Next: A first simple example Up: YACC - Yet Another Compiler Previous: YACC - Yet Another Compiler
Marc Moreno Maza
2004-12-02