Parser problem continued

Curiouser and curiouser... expr :: Parser Int expr = 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 Int expr = 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. *********************************************************************

On Tue, 15 Mar 2005, Nicola Whitehead wrote:
Curiouser and curiouser...
expr :: Parser Int expr = 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
the nested 'do' is still there ...

expr :: Parser Int expr = do t <- term do symbol "+" e <- expr return e return (t + e) +++ return t <-----
't' is not in scope at the arrow. t only exists inside the do block, and your code parses like this ( do t <- .... return (t+e) ) +++ ( return t ) 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.

The layout of your code is very important when writing haskell code: Your code : expr = do t <- term do symbol "+" e <- expr return e return (t + e) +++ return t is equivalent to: expr = do { t <- term ; do { symbol "+" ; e <- expr ; return e } ; return (t + e) -- e is not in scope } +++ return t -- t is not in scope Both t and e are not in scope: * e is in a nested do-block * the expression 'return t' is outside the do-block What you probably mean is: expr = do t <- term do symbol "+" e <- expr return (t + e) +++ return t which is equivalent to: expr = do { t <- term ; do { symbol "+" ; e <- expr -- (dropped the return e line) ; return (t + e) } +++ return t } Now t and e are in scope. The parser 'expr' will first recognize a 'term' and then try to recognize a '+' symbol followed by an expression. If that fails it returns 't'. Cheers, Arthur On 15-mrt-05, at 16:28, Nicola Whitehead wrote:
Curiouser and curiouser... expr :: Parser Int expr = 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 Int expr = 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
participants (4)
-
Arthur Baars
-
Henning Thielemann
-
Nicola Whitehead
-
robert dockins