I am working on a project using the Happy Boys Generator. This is what I have done so far:
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 I can solve this?
Update: I solved it, but now it works only if I write "sqrt2" (there is no space between sqrt and 2); if I write "sqrt 2", I get a "syntax error".
This is what I have in my 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 }
source share