How can I make correct rules for parsing if-then[-else] case? Here is some grammar:
{
module TestGram (tparse) where
}
%tokentype { String }
%token one { "1" }
if { "if" }
then { "then" }
else { "else" }
%name tparse
%%
statement : if one then statement else statement {"if 1 then ("++$4++") else ("++$6++")"}
| if one then statement {"if 1 then ("++$4++")"}
| one {"1"}
{
happyError = error "parse error"
}
This grammar parses the following expression correctly:
> tparse ["if","1","then","if","1","then","1","else","1"]
"if 1 then (if 1 then (1) else (1))"
But compiling raises a warning about shift/reduce conflict. The documentation to the happy contains an example of such conflict: http://www.haskell.org/happy/doc/html/sec-conflict-tips.html
There are two solutions shown, the first is to change the recursion type (not clear how to do it in this case). And the second is not to change anything. This option is ok for me, but I need a consultation.