问题描述
在我的 yacc 解析器语法中,我定义了以下规则和相应的操作(参见下面的program.y)。解析int X;
应该有推导type => TOK_INT
and variable_list => TOK_VARIABLE
,然后这些与以 a 结尾的声明匹配statment ;
。但是,将其读作int X
and ;
。也就是说,两个单独的语句。谁能明白为什么?
程序.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); }
;