Happy generated parser fails to compile.

Hi, Following is a simple parser definition I am trying to write for simple numerical expressions. A sequence of numerical expressions actually. Happy reports that there are 3 un-used terminals. AFAIK, they should be let, in and '='. Is that correct? Although, happy generates a .hs file without any other message. GHC gives a type mismatch error. I don't suppose it could be a lexer error, could it? a) Could this be a bug in Happy (very unlikely, IMO) it is generating code that has type mismatches. b) If there is a bug in my parser definition, shouldn't Happy beep on it? Any suggestions to fix it will be highly appreciated. Thanks, =================== { module Main where import Tokens_posn } %name calc %tokentype { Token } %token let { Let _ } in { In _ } ';' { Newstatement _ } int { Int _ $$ } var { Var _ $$ } '=' { Sym _ '=' } '+' { Sym _ '+' } '-' { Sym _ '-' } '*' { Sym _ '*' } '/' { Sym _ '/' } '(' { Sym _ '(' } ')' { Sym _ ')' } %% Exprs :: { [ Expr ] } Exprs : Exprs Expr ';' { $2 : $1 } | Expr ';' { [$1] } Expr : Expr '+' Term { PlusE $1 $3 } | Expr '-' Term { MinusE $1 $3 } | Term { $1 } Term : Term '*' Factor { TimesE $1 $3 } | Term '/' Factor { DivE $1 $3 } | Factor { $1 } Factor : '-' Atom { NegE $2 } | Atom { $1 } Atom : int { IntE $1 } | var { VarE $1 } | '(' Expr ')' { $2 } { data Expr = LetE String Expr Expr | PlusE Expr Expr | MinusE Expr Expr | TimesE Expr Expr | DivE Expr Expr | NegE Expr | IntE Int | VarE String deriving Show flist :: [Int] -> Int -> [Int] flist x a = a : x main:: IO () main = interact (show.runCalc) runCalc :: String -> Expr runCalc = calc . alexScanTokens happyError :: [Token] -> a happyError tks = error ("Parse error at " ++ lcn ++ "\n") where lcn = case tks of [] -> "end of file" tk:_ -> "line " ++ show l ++ ", column " ++ show c where AlexPn _ l c = token_posn tk } -- Rohit Garg http://rpg-314.blogspot.com/

Hello If you run happy with the info option and a file name for the output, happy will produce a report on the grammar:
happy -info=INFO.txt Main.g
You are right that the three unused termianls are 'let' 'in' and '=' terminal let is unused terminal in is unused terminal '=' is unused You are also importing a file Tokens_posn (presumably the lexer?) which you haven't posted - without being able to see this or the actual error message its rather hard to decode the error, however if you are using the file Token_posn.x from the Alex distribution there are two problems - one is you need Newstatement as a token, the other is runCalc has this type: runCalc :: String -> [Expr] runCalc ss = calc $ alexScanTokens ss Best wishes Stephen

runCalc :: String -> [Expr] runCalc ss = calc $ alexScanTokens ss
Thanks, that fixed the problem. :)
Best wishes
Stephen _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Rohit Garg http://rpg-314.blogspot.com/

Excerpts from Rohit Garg's message of Sun Sep 26 16:58:05 +0200 2010:
a) Could this be a bug in Happy (very unlikely, IMO) it is generating code that has type mismatches. If you posted the error (and maybe 20 surrounding lines of code it would be easier to help you..
No, happy does not beep on them. happy is a stupid machine which transforms your grammar into code transforming states. Eg if you return "foo" instead of the parser result happy will correctly create a .hs file which will then fail to compile. Happy only copy pastes your code. Marc Weber
participants (3)
-
Marc Weber
-
Rohit Garg
-
Stephen Tetley