/* * Written by Matz Kindahl @ Uppsala University */ %{ char* mkstr(); %} %union { char* string; int integer; } %token INT FLOAT STRING %token IDENT %start line %% line: line ';' type_declaration | type_declaration ; type_declaration : INT { $$ = INT; } id_list | FLOAT { $$ = FLOAT; } id_list | STRING { $$ = STRING; } id_list ; /* In this rule $0 is accessible directly as there is no other */ /* reduction of any other rules occur before the semantic action is */ /* reduced. */ id_list: IDENT/* Inherits: type of identifier as $0 */ { printf("%s : %s\n", $1, mkstr($0)); } | id_list ',' IDENT { printf("%s : %s\n", $3, mkstr($0)); } ; %% /* * mkstr: * Function that returns a printable string of the type supplied */ char *mkstr(i) int i; { switch(i) { case INT:return "Integer"; case FLOAT:return "Float"; case STRING:return "String"; default:return "Unknown"; } }