[Haskell-cafe] Parser question
Hi folks, I have a parser problem. I have a basic calculator program (Graham Hutton's from Nottingham) which contains the following code: -- Define a parser to handle the inputexpr :: Parser Intexpr = do t - term do symbol "+" e - expr return (t + e) +++ return t term :: Parser Intterm = do f - factor do symbol "*" e - expr return (f * t) +++ return f factor :: Parser Intfactor = do symbol "(" e - expr symbol ")" return e +++ natural symbol and natural are defined elsewhere and work fine, but when I compile it I get the error ERROR "C:/HUGS/Calculator.hs":66 - Undefined variable "t" I suspect I'm missing something obvious, but for the life of me I can't see it. Any suggestions? Thanks, Nik (Trying to keep a couple of weeks ahead of her students) Dr Nik Freydís Whitehead University of Akureyri, Iceland * Having the moral high ground is good. Having the moral high ground and an FGMP-15 is better. * ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Parser problem continued
Curiouser and curiouser... expr :: Parser Intexpr = do t - term do symbol "+"e - expr return (t + e) +++ return t solves the undefined variable problem but introduces a new 'Last operator in do {...} must be an _expression_' error, which then disappears if I explicitly return e expr :: Parser Intexpr = do t - term do symbol "+" e - expr return e return (t + e) +++ return t to give me the original undefined variable t error at the line expr = do t - term . It looks in scope to me... :( Thanks, Nik Dr Nik Freydís Whitehead University of Akureyri, Iceland * Having the moral high ground is good. Having the moral high ground and an FGMP-15 is better. * ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
RE: [Haskell-cafe] Parser problem continued
perhaps like this: expr = do t - term (do symbol + e - expr return (t+e) ) +++ (return t) although I think you may also want a 'try' before the first alternative. No, that still gives the same undefined variable error. :( Nik Dr Nik Freydís Whitehead University of Akureyri, Iceland * Having the moral high ground is good. Having the moral high ground and an FGMP-15 is better. * ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
RE: [Haskell-cafe] Parser problem continued
Thanks folks! Writing it in a lispy manner seems to work. I see what Arthur means about the layout - I think I'm still thinking too much in C. :) Nik Dr Nik Freydís Whitehead University of Akureyri, Iceland * Having the moral high ground is good. Having the moral high ground and an FGMP-15 is better. * ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe