I'm writing a parser for a project and got stuck on an issue. Here's a self contained example of the problem:
%error-verbose
%token ID
%token VAR
%token END_VAR
%token CONSTANT
%token AT
%token VALUE
%%
unit: regular_var_decl
| direct_var_decl;
regular_var_decl: VAR constant_opt ID ':' VALUE ';' END_VAR;
constant_opt: /* empty */ | CONSTANT;
direct_var_decl: VAR ID AT VALUE ':' VALUE ';' END_VAR;
%%
#include <stdlib.h>
#include <stdio.h>
yylex() {
static int i = 0;
static int tokens[] = {
VAR,
ID, ':', VALUE, ';',
END_VAR,
0
};
return tokens[i++];
};
yyerror(str) char *str; {
printf("fail: %s\n", str);
};
main() {
yyparse();
return 0;
};
One could build it bison test.y && cc test.tab.c && ./a.out
.
It warns me that constant_opt
is useless due to conflicts.
This ambiguity could be solved by using LALR(2), since after ID
it could find ':' or AT
... How could I solve this issue on bison?