/* dates.y * YACC dates example * illustrates union type for YYSTYPE * * to compile, use: lex dates.l yacc dates.y gcc -o dates y.tab.c -ly */ %{ #include typedef union{ int num; char *str; } YYSTYPE; #define YYSTYPE_IS_DECLARED char *monthtab[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; %} %token MONTH %token NUMBER %type monthnum %type day %type year %% line : line date '\n' { printf("\n"); } | /* epsilon */ | error '\n' ; date : day MONTH year { printf("%s %d, %d", $2, $1, $3); } | monthnum '/' day '/' year { printf("%s %d, %d", $1, $3, $5); } | MONTH day ',' year { printf("%s %d, %d", $1, $2, $4); } ; monthnum : NUMBER { $$ = monthtab[$1 - 1 ] ; } ; day : NUMBER ; year : NUMBER ; %% #include "lex.yy.c"