我正在开发一个使用 Happy 解析器生成器的项目。这是我到目前为止所做的:
Exp : Exp1 { $1 }
Exp1 : Exp1 '+' Term { \p -> $1 p + $3 p }
| Exp1 '-' Term { \p -> $1 p - $3 p }
| Term { $1 }
Term : Term '*' Factor { \p -> $1 p * $3 p }
| Term '/' Factor { \p -> $1 p / $3 p }
| sqrt Factor { \p -> sqrt $2 p }
| Factor { $1 }
Factor
: double { \p -> $1 }
| '(' Exp ')' { $2 }
问题是我收到以下错误:
Parser.hs:158:38:
No instance for (Floating ([a0] -> Double))
arising from a use of `happyReduction_7'
Possible fix:
add an instance declaration for (Floating ([a0] -> Double))
In the second argument of `happySpecReduce_2', namely
`happyReduction_7'
In the expression: happySpecReduce_2 6 happyReduction_7
In an equation for `happyReduce_7':
happyReduce_7 = happySpecReduce_2 6 happyReduction_7
你知道我该如何解决这个问题吗?
更新:我解决了它,但现在它只有在我写“sqrt2”时才有效(sqrt 和 2 之间没有空格);如果我写“sqrt 2”,我会得到“解析错误”。
这就是我在 Alex(lex) 文件中的内容:
tokens :-
$white+ ;
"--".* ;
"sqrt" { \s -> TokenSqrt}
"sin" { \s -> TokenSin}
"log" { \s -> TokenLog}
@doubleNumber { \s -> TokenDouble (read s) }
@var { \s -> TokenVar s }
"+" { \s -> TokenPlus }
"-" { \s -> TokenMinus }
"*" { \s -> TokenMul }
"/" { \s -> TokenDiv }
"(" { \s -> TokenOB }
")" { \s -> TokenCB }
"=" { \s -> TokenEq }