如何制定正确的规则来解析 if-then[-else] 案例?这是一些语法:
{
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"
}
此语法正确解析以下表达式:
> tparse ["if","1","then","if","1","then","1","else","1"]
"if 1 then (if 1 then (1) else (1))"
但是编译会引发有关移位/减少冲突的警告。快乐的文档包含此类冲突的示例: http ://www.haskell.org/happy/doc/html/sec-conflict-tips.html
显示了两种解决方案,第一种是更改递归类型(在这种情况下不清楚如何做)。第二个是不改变任何东西。这个选项对我来说没问题,但我需要咨询。