[Haskell-beginners] Simple parser question

Martin Drautzburg Martin.Drautzburg at web.de
Thu Feb 14 13:59:58 CET 2013


Hello all,

I just hit a sticking point when trying to parse something  like

data Exp = Lit Int -- literal integer
         | Plus Exp Exp

where something like "1+2" should be parsed to "Plus (Lit 1) (Lit 2)".

When I try to parse "1+2" my parser enters an infinite loop. I can understand 
why: it thinks 

"hmm, this expression could be a plus, but then it must start with an 
expression, lets check". 

and it tries to parse expression again and again considers Plus.

When I change the rules, so it first checks for "Lit", it does parse the "1" 
just fine, but then gives up, because the remainder is not an expression 
anymore, but just a "+2".

My parser is written in the style shown in Graham Hutton's book: 

Parser a :: String -> (a, String).

I believe I am missing something obvious, but I can't see it.




-- 
Martin



More information about the Beginners mailing list