I'm working on a project that uses the Happy parser generator. This is what have I done until now:
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 }
The problem is that I get the following error:
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
Do you know how can I solve this?
Update: I solved it but now it works only if I write "sqrt2"(no space between sqrt and 2); if I write "sqrt 2" I get "parse error".
This is what I have in the Alex(lex) file:
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 }
sqrt $2 p
This calls sqrt
with the function $2
as its argument and then applies the resulting function to the argument p
. This would only makes sense if sqrt
could take a function and produce a function as a result, which would be the case if and only if there was a Floating
instance for functions, which there is not. Thus the error message.
What you doubtlessly intended to do was to apply the function $2
to the argument p
and then apply sqrt
to the result, for which you'd write:
sqrt ($2 p)