I have a parser with a prefix "-" and an infix "=". When I try to parse "x=-1", it fails. Does anyone know how to fix this? I stripped my code down as much as possible. It parses "x=1" and "-1", but fails for "x=-1". import Text.Parsec import qualified Text.Parsec.Expr as PE import qualified Text.Parsec.Language as L import qualified Text.Parsec.Token as T lexer = T.makeTokenParser L.emptyDef {T.reservedOpNames = ["=", "-"]} reservedOp = T.reservedOp lexer integer = T.integer lexer >> return "" symbol = T.identifier lexer expression = PE.buildExpressionParser table (integer <|> symbol) where table = [ [ PE.Prefix (reservedOp "-" >> return (\x-> "")) ], [ PE.Infix (reservedOp "=" >> return (\x-> \y->"")) PE.AssocRight ] ] input = do e <- expression eof return e testParse s = case (parse input "(unknown)" s) of Left f -> putStrLn $ s ++ "\n" ++ show f Right f -> putStrLn s main = do testParse "x=1" testParse "-1" testParse "x=-1" _________________________________________________________________ Hotmail: Free, trusted and rich email service. http://clk.atdmt.com/GBL/go/201469228/direct/01/
Hi Adam,
parse "x=-1", it fails. Does anyone know how to fix this?
This issue is in using the 'reservedOp' combinator which rejects '=' when followed by '-'. reservedOp name = lexeme $ try $ do{ string name ; notFollowedBy (opLetter languageDef) <?> ("end of " ++ show name) } If you change: reservedOp "=" to: char '=' then your test passes. -Greg On Tue, Feb 16, 2010 at 2:10 PM, Adam Crume <adamcrume@hotmail.com> wrote:
I have a parser with a prefix "-" and an infix "=". When I try to parse "x=-1", it fails. Does anyone know how to fix this? I stripped my code down as much as possible. It parses "x=1" and "-1", but fails for "x=-1".
import Text.Parsec import qualified Text.Parsec.Expr as PE import qualified Text.Parsec.Language as L import qualified Text.Parsec.Token as T
lexer = T.makeTokenParser L.emptyDef {T.reservedOpNames = ["=", "-"]}
reservedOp = T.reservedOp lexer
integer = T.integer lexer >> return ""
symbol = T.identifier lexer
expression = PE.buildExpressionParser table (integer <|> symbol) where table = [ [ PE.Prefix (reservedOp "-" >> return (\x-> "")) ], [ PE.Infix (reservedOp "=" >> return (\x-> \y->"")) PE.AssocRight ] ]
input = do e <- expression eof return e
testParse s = case (parse input "(unknown)" s) of Left f -> putStrLn $ s ++ "\n" ++ show f Right f -> putStrLn s
main = do testParse "x=1" testParse "-1" testParse "x=-1" ________________________________ Hotmail: Free, trusted and rich email service. Get it now. _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Adam Crume wrote:
I have a parser with a prefix "-" and an infix "=". When I try to parse "x=-1", it fails. Does anyone know how to fix this? I stripped my code down as much as possible. It parses "x=1" and "-1", but fails for "x=-1".
I ran into simialr problems some time ago and blogged about it here: http://www.mega-nerd.com/erikd/Blog/CodeHacking/Haskell/parsec_expression_pa... Not sure if your problem is exactly the same, but my solution might point you in the right direction. BTW, the haskell-cafe mailing list may be a better place for questions like this. It seems to have a larger readership and is more discussion focused while this list is more for announcements. Cheers, Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/
The problem was the same, but I needed a slightly different solution. You got me on the right track, though. Also, thanks for the tip about haskell-cafe.
Date: Wed, 17 Feb 2010 09:50:50 +1100 From: mle+hs@mega-nerd.com To: haskell@haskell.org Subject: Re: [Haskell] Parsec operator issue
Adam Crume wrote:
I have a parser with a prefix "-" and an infix "=". When I try to parse "x=-1", it fails. Does anyone know how to fix this? I stripped my code down as much as possible. It parses "x=1" and "-1", but fails for "x=-1".
I ran into simialr problems some time ago and blogged about it here:
http://www.mega-nerd.com/erikd/Blog/CodeHacking/Haskell/parsec_expression_pa...
Not sure if your problem is exactly the same, but my solution might point you in the right direction.
BTW, the haskell-cafe mailing list may be a better place for questions like this. It seems to have a larger readership and is more discussion focused while this list is more for announcements.
Cheers, Erik
-- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/ _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
_________________________________________________________________ Hotmail: Trusted email with powerful SPAM protection. http://clk.atdmt.com/GBL/go/201469227/direct/01/
participants (3)
-
Adam Crume -
Erik de Castro Lopo -
Greg Fitzgerald