[Haskell-cafe] Is there any way to use somethig like global backtrack with parsec
S. Doaitse Swierstra
doaitse at swierstra.net
Wed Jun 1 09:35:30 UTC 2016
> Op 31 mei 2016, om 3:52 heeft Erik Rantapaa <erantapaa at gmail.com <mailto:erantapaa at gmail.com>> het volgende geschreven:
>
> If don't have to use Parsec, you can use a parser library like ReadP / ReadS.
>
> For example:
>
> import Text.ParserCombinators.ReadP
>
> abc = (choice [ string "a", string "ab" ] ) >> char 'c' >> eof
>
> run p s = case (readP_to_S p) s of
> [] -> Left "no parse"
> ((a,_):_) -> Right a
>
> test1 = run abc "ac"
> test2 = run abc "abc"
> test3 = run abc "ab" -- should fail
>
>
> On Monday, May 30, 2016 at 1:15:25 PM UTC-5, Petr Sokolov wrote:
> For example this parser doesn't parse "abc"
>
> test :: Parser String
> test = ((try $ string "a") <|> (string "ab")) *> (string "c")
>
> I can rewrite it so it would parse "abc", but is there any general approach to force parser to return and try other branches?
If you use uu-parsinglib all your problems will be gone. No need to hasskel around with “try” constructs. All alternatives are tried “in parallel”.
Doaitse
>
> (try a <|> b) *> c
>
> Something like
>
> (tryOtherBranchesIfFail (try a <|> b)) *> c
>
> So if it cannot parse "abc" with (try a *> c) it could return and try (b *> c)
> Or it isn't possible?
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org <mailto:Haskell-Cafe at haskell.org>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe <http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160601/22934cc7/attachment.html>
More information about the Haskell-Cafe
mailing list