In the Yacc rules we use the create functions to build a parse tree.
Kimwitu generates for us a YYSTYPE C-union (from the phylum definition),
which 'contains' all phyla (including built-in ones like int
).
The 'field names' in the YYSTYPE are the names of the phyla, prefixed by 'yt_
'. We can use these names in the %type
and %token
definitions.
Yacc input for our language:
%{ #include "k.h" /* data type defs (incl. YYSTYPE) */ extern expr the_expr; /* root of the parse tree */ %} %token <yt_int> T_INT %type <yt_expr> Y_expr Y_term Y_factor %% Y_root : Y_expr { the_expr = $1; } ; Y_expr : Y_term { $$ = $1; } | Y_expr '+' Y_term { $$ = Plus( $1, $3 ); } ; Y_term : Y_factor { $$ = $1; } | Y_term '*' Y_factor { $$ = Times( $1, $3 ); } ; Y_factor : T_INT { $$ = Const( $1 ); } | '(' Y_expr ')' { $$ = $2; } ;