I am trying to write grammar supporting array and pointer. Here is a minimal code that can produce shift/reduce error
%token Ident
%token True
%token False
%token L_paren R_paren (* "(" and ")" *)
%token L_bracket R_bracket (* "[" and "]" *)
%token Dot Arrow Star
%token Deref Explicit_parenthesis Arr_sub
%left Star
%right Deref
%nonassoc Arr_sub Arrow Dot
(* Return type is a placeholder *)
%type<int> lvalue
%start lvalue
%%
lvalue :
| ident = Ident; {}
| Star; lv = lvalue; %prec Deref {}
| lv = lvalue; L_bracket; e = exp; R_bracket; %prec Arr_sub {}
| L_paren; lv = lvalue; R_paren;{}
;
exp :
| True; {}
| False; {}
;
%%
The array access []
has higher priority than dereference operator *
in my grammar. However, there is error indicating the parser still cannot figure out what to do when stack is Star lvalue
and lookahead is [
. I am expecting it to shift because array access should be executed first, and then dereference.
You can use command menhir -v test.mly
to generate this error, which is
** Conflict (shift/reduce) in state 11.
** Token involved: L_bracket
** This state is reached from lvalue after reading:
Star lvalue
** The derivations that appear below have the following common factor:
** (The question mark symbol (?) represents the spot where the derivations begin to differ.)
lvalue
(?)
** In state 11, looking ahead at L_bracket, reducing production
** lvalue -> Star lvalue
** is permitted because of the following sub-derivation:
lvalue L_bracket exp R_bracket // lookahead token appears
Star lvalue .
** In state 11, looking ahead at L_bracket, shifting is permitted
** because of the following sub-derivation:
Star lvalue
lvalue . L_bracket exp R_bracket
Is my precedence rule incorrect? I have no idea why it is not working at all