7

Are there any parser combinators library that gives performance comparable to Happy/Alex ?

I know about Attoparsec, but sometimes it operates not well, like in an example below:

isToken c = isLetter c || isDigit c

symbol :: Parser Expr
symbol = do 
    c    <- skipSpace >> satisfy isLetter 
    rest <- takeWhile isToken
    let token = C.cons c rest  -- oops... O(N)
    error $ show token

The workaround is quite ugly:

do { skipSpace; bs <- scan go True; when (null bs) (fail "Not a symbol"); return bs}
    where go True  c = if isLetter c then Just  False else Nothing
          go False c = if isToken c then Just Fasle else Nothing

Also, Attoparsec lacks of error handling.

Happy/Alex are quite unfriendly (for me) comparing to ocamlyacc/ocamllex, BNFC is inflexible and in my case requires an additional AST traversing after parsing. Also, error handling is not very good.

There are three of rest options: Parsec2, Parsec3 and uu-parselib. I've found a number of controversial benchmarks assuming that Parsec2 is faster than Parsec3, or UU is faster, or it's slower.

But what to choose? Does anyone have an experience using uu-parselib? I need the parser for some kind of DSL, need the parses fast enough to not to change it in future.

2
  • 1
    If you are parsing "human-sized" data (i.e. files written by people), any of the mainstream parser combinator libraries should be fine speed-wise, though for you might have to to pay some attention to control backtracking in a parser you write. If you are parsing huge data files then the equation changes somewhat, I'd look for benchmarks at this point and consider what features you can to trade for speed (e.g. source position tracking can be a significant slow down). Commented Jul 19, 2011 at 7:25
  • 1
    Not an answer, but I've used uu-parselib a lot. It's very powerful and has some nice features, like automatic stream correction. My only complaint is that not all of the features are immediately obvious; especially if you're not already familiar with parsers. I've never had a problem with speed, but my input data has mostly been in the kbyte size. Commented Jul 19, 2011 at 8:49

1 Answer 1

7
  1. There is another alternative: polyparse.

  2. After last year's GSoC, parsec3 was optimized and no longer noticeably slower than parsec2

  3. Couple of years ago I've done tests on several grammars (mid-size) and found that performance of happy/alex, parsec2/alex, parsec2 and polyparse is very close. Attoparsec was faster on byte streams, but I needed multi-byte.

My advise: take a look at the way alternatives handle internal and user-defined state and report errors and choose by these criteria.

Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure that Parsec is close to Happy/Alex ? In that case there is no sense to use Happy or BNFC at all. May be there are any benchmarks I may to run by myself?
Regardless of efficiency, using a parser generator like Happy has the advantage that you get errors and warnings about your grammar (like ambiguities).
attoparces-text didn't exist back then, so I didnt test it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.