I'm having trouble with a yacc-like implementation (using ocamlyacc, in particular) of a grammar that includes ordinary infix operations plus infix sections, like in Haskell. I desire all of these to be grammatical:
(+1)
(1+)
(+)
(1+1)
However, I haven't been able to get this working, even by fiddling with associativity/precedence declarations. I can see in the grammar.output where the problem is happening (it's shifting where I want it to reduce), but I haven't been able to coax it to go the way I want. Here is a simplified demonstration of the problem.
lex.mll has:
{
open Parse
exception Eof
}
rule token = parse
| [' ' '\t'] { token lexbuf }
| ['\n'] { EOL }
| ['0'-'9']+ as num {INT(int_of_string num)}
| '+' { PLUS }
| '*' { TIMES }
| '(' { LPAREN }
| ')' { RPAREN }
| eof { raise Eof }
main.ml has:
let _ =
try
let lexbuf = Lexing.from_channel stdin in
while true do
let result = Parse.start Lex.token lexbuf in
print_string result; print_newline(); flush stdout
done
with Lex.Eof -> exit 0
and parse.mly (where the trouble lies) has:
%token <int> INT
%token PLUS TIMES
%token LPAREN RPAREN
%token EOL
%left PLUS
%left TIMES
%start start
%type <string> start
%%
start:
| expr EOL {$1}
;
expr:
| application {$1}
| expr PLUS expr {"[" ^ $1 ^ "+" ^ $3 ^"]"}
| expr TIMES expr {"[" ^ $1 ^ "*" ^ $3 ^"]"}
;
section:
| LPAREN atom PLUS RPAREN { "(" ^ $2 ^ " +)" }
| LPAREN PLUS atom RPAREN { "(+ " ^ $3 ^ ")" }
| LPAREN PLUS RPAREN { "(+)" }
;
application:
| atom {$1}
| application atom {"[" ^ $1 ^ " " ^ $2 ^ "]"}
;
atom:
| INT {string_of_int $1}
| section { $1 }
| LPAREN expr RPAREN { "(" ^ $2 ^ ")" }
;
%%
Running ocamlyacc
on that tells me there is 1 shift/reduce conflict
. In particular here is the relevant part of the verbose log:
Rules:
6 section : LPAREN atom PLUS RPAREN
...
9 application : atom
...
12: shift/reduce conflict (shift 21, reduce 9) on PLUS
state 12
section : LPAREN atom . PLUS RPAREN (6)
application : atom . (9)
PLUS shift 21
INT reduce 9
MINUS reduce 9
TIMES reduce 9
LPAREN reduce 9
RPAREN reduce 9
...
state 21
section : LPAREN atom PLUS . RPAREN (6)
RPAREN shift 26
. error
And running the compiled program will correctly parse all of the following:
(1+)
(+1)
(+)
1+2
but fails with:
(1+2)
If on the other hand, I create a dummy token HIGH
with high precedence:
%left PLUS MINUS
%left TIMES
%nonassoc HIGH
and then put %prec HIGH
on the Rule 9:
application: atom %prec HIGH {$1}
in that case (1+2)
will parse but (1+)
won't.
I understand the general background of shift/reduce conflicts. I just can't figure out how to negotiate it to solve this parsing challenge.