
On Sun, Nov 08, 2009 at 05:53:48PM +0000, John Moore wrote:
Hi, I'm trying to find a way to parseRPN (Reverse Polish Numbers) to expressions rather than to just numbers. e.g. I want the answer to be in the form
(Multiply (Val 2) (Val 3)) rather than just the answer.
You don't need to code all the parser by hand. You can use, for example, Parsec parsers. Spoilers ahead!!! If your data type is data Expr a = Multiply (Expr a) (Expr a) | Val a then you may write something like expression :: Parser a -> Parser (Expr a) expression valParser = spaces >> (mult <|> val) where expr = expression valParser mult = char "*" >> (Multiply <$> expr <*> expr) val = Val <$> valParser using Parsec for a concrete example. HTH, -- Felipe.