parsing: parser

So far, we have seen how to use the create functions to build terms. Now, we see how easy it is to build a Yacc/Lex parser for our simple language.

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; } ;

next overview up


Last updated at 03 February '98 by kimwitu@cs.utwente.nl