Problem Description
In my yacc parser grammar, I have the following rules and corresponding actions defined (see program.y below). Parsing int X;
should have the derivation type => TOK_INT
and variable_list => TOK_VARIABLE
, and then these match against a declaration which ends in a statment ;
. However, reads this as int X
and ;
. That is, two separate statements. Can anyone see why?
program.y
program:
function { exit(0); }
;
function:
function line { printf("goal\n"); printtree_print($2); }
|
;
line:
statement ';' { printf("line\n"); printtree_print($1); }
;
statement:
declaration { printf("declaration\n"); printtree_print($1); }
| assignment { printf("assignment\n"); printtree_print($1); }
;
declaration:
type variable_list { printf("varlist\n"); printtree_print($2); $$ = $2; }
;
type:
TOK_INT { typeMode = typeInt; }
;
variable_list:
TOK_VARIABLE
{ $$ = node_mkVariable($1, typeMode);
printtree_print($$);
}
;
assignment:
TOK_VARIABLE TOK_ASSIGN expr
{ printf("assignment %s = expr\n", $1);
node_setInTable($1, $3);
$$ = node_getFromTable($1); }
;
expr:
TOK_INTEGER { $$ = node_mkConstant($1); }
| TOK_VARIABLE { $$ = node_mkVariable($1, typeVariable); }
;