Haskell - Error with parser

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 } 
+4
source share
1 answer
 sqrt $2 p 

This calls sqrt with function $2 as an argument, and then applies the resulting function to argument p . This would only make sense if sqrt could take a function and create a function as a result, which would be when and only if there was a Floating instance for functions that did not exist. Therefore, an error message.

What you surely wanted to do was apply the $2 function to the p argument, and then apply sqrt to the result for which you should write:

 sqrt ($2 p) 
+5
source

Source: https://habr.com/ru/post/1483048/


All Articles