ollef / Earley

Parsing all context-free grammars using Earley's algorithm in Haskell.
BSD 3-Clause "New" or "Revised" License
361 stars 24 forks source link

Add constraint validation feature #58

Closed amirlb closed 1 year ago

amirlb commented 1 year ago

Hi! I'm getting back into Haskell after a long break, and decided to use Earley as a parser in my project - I've wrote enough recursive descent parsers, both manually and with Parsec writing the boilerplate, and decided to let the computer do the hard work for once :)

Adding here a feature that I didn't have a use for yet, but is a neat property of Earley-type parsers: it's possible to implement arbitrary conditions in the grammar, usually with only a small impact on efficiency. This idea is also referred to by #50, and a real-world use case would be for parsing command lines. Many programs allow arbitrary orders, but require that each flag appears only once. In order to express this as a CFG, you need an exponential number of productions. With constraint validations, it's possible to specify that the valid options at each point are exactly those that didn't appear before.

Other small stuff - In my project I used "any terminal" several times in the grammar, and it feels like a useful addition to the library. In my own tests I found the "matches" predicate useful. So I've added them here too. They don't really belong so we can remove them from the PR or merge separately. The same goes for the unit test file, it's the first time I'm writing unit tests in Haskell and they may not fit here.