ANNOUNCE: Grempa 0.1.0, Embedded grammar DSL and LALR parser generator

Hello everyone, I'm pleased to announce the first release of Grempa: A library for expressing programming language grammars in a form similar to BNF, which is extended with the semantic actions to take when a production has been parsed. The grammars are typed and are to be be used with the LALR(1) parser generator, also part of the library, which can generate a parser for the language either at compile time using Template Haskell, producing fast parsers with no initial runtime overhead, or dynamically, which has the initial overhead of generating the parser, but can be used for example when the grammar depends on an input. Here is a small example (from Ex1 in the examples directory) of what a grammar may look like: data E = Plus E E | Times E E | Var | ... expr :: Grammar Char E expr = do rec e <- rule [ Plus <@> e <# '+' <#> t , id <@> t ] t <- rule [ Times <@> t <# '*' <#> f , id <@> f ] f <- rule [ id <@ '(' <#> e <# ')' , Var <@ 'x' ] return e The corresponding BNF grammar is the following: E ::= E + T | T T ::= T * F | F F ::= ( E ) | x Generating a parser from the grammar is simple: parseExpr :: Parser Char E parseExpr = $(mkStaticParser expr [|expr|]) There are a few other examples in the examples directory of the package, most notably a grammar and parser for a simple functional language similar to Haskell. It is possible to generate random input strings and their expected outputs for grammars written using Grempa which makes it possible to test the generated parsers with QuickCheck. The package and documentation (should be up soon) can be found here: http://hackage.haskell.org/package/Grempa-0.1.0 Please get in touch with me if you have any comments, issues, questions, bug reports or would like to contribute to the project. I would love to get some more people involved in the project, as there are many areas of the library that could be improved. Thanks to Daniel Gustafsson and everyone else at Chalmers Uni for valuable input and getting me into Haskell (respectively). Regards, Olle Fredriksson

On Mon, Sep 6, 2010 at 2:45 PM, Olle Fredriksson
expr :: Grammar Char E expr = do rec e <- rule [ Plus <@> e <# '+' <#> t , id <@> t ] t <- rule [ Times <@> t <# '*' <#> f , id <@> f ] f <- rule [ id <@ '(' <#> e <# ')' , Var <@ 'x' ] return e
Looks like Applicative style. This is good, even while I don't really know why we are seeing <@> and <#> instead of <$> and <*>. How does Grempa compare with other parsing libraries/tools, such as Parsec, Attoparsec and Happy, with regard to ease of use and performance? Cheers! =) -- Felipe.

Looks like Applicative style. This is good, even while I don't really know why we are seeing <@> and <#> instead of <$> and <*>.
How does Grempa compare with other parsing libraries/tools, such as Parsec, Attoparsec and Happy, with regard to ease of use and performance?
Cheers! =)
-- Felipe.
Yes, it is indeed in applicative style. The reason for <@> and <#> is that the functions have slightly different constraints to them, compared to the Applicative equivalents. The arguments passed to the function have to be instances of the ToSym class, which converts terminals and non-terminals to symbols. This allows writing the grammars in the way seen above ("Plus <@> e <# '+' <#> t") instead of something like "Plus <@> nont e <# tok '+' <#> nont t". I have yet to do any library comparisons on performance, but it should be faster than monadic combinator style libraries, as it does not have the exponential corner-cases that those parsers can have. It is also easier to write the grammars in Grempa's BNF-like syntax where left-recursion is permitted. Some comparison benchmarks would be interesting though, and I will look into it in the future. I have already done some profiling to eliminate the worst deficiencies of Grempa, but there is definitely room for improvements still. The library does not do lexing for you, which some of the other libraries have support for, which can make it harder to use. A plus for me (compared to Happy) is that you do not have to run an external program every time your grammar has changed, but can instead just recompile your project. I'm glad to see some interest in the project!

Hi Olle,
On Mon, Sep 6, 2010 at 12:45 PM, Olle Fredriksson
There are a few other examples in the examples directory of the package, most notably a grammar and parser for a simple functional language similar to Haskell. It is possible to generate random input strings and their expected outputs for grammars written using Grempa which makes it possible to test the generated parsers with QuickCheck. The package and documentation (should be up soon) can be found here: http://hackage.haskell.org/package/Grempa-0.1.0
Very nice job, I'm interested in using it. I wonder why the documentation hasn't been generated on Hackage yet. (I generated it for myself locally.) Do you plan to set up a repository somewhere? Thanks! Paulo

Hello Paulo,
Glad to see some interest!
I just setup a repository over at github: http://github.com/ollef/Grempa
Fork away!
Kind regards,
Olle
On 8 September 2010 05:05, Paulo Tanimoto
Hi Olle,
On Mon, Sep 6, 2010 at 12:45 PM, Olle Fredriksson
wrote: There are a few other examples in the examples directory of the package, most notably a grammar and parser for a simple functional language similar to Haskell. It is possible to generate random input strings and their expected outputs for grammars written using Grempa which makes it possible to test the generated parsers with QuickCheck. The package and documentation (should be up soon) can be found here: http://hackage.haskell.org/package/Grempa-0.1.0
Very nice job, I'm interested in using it. I wonder why the documentation hasn't been generated on Hackage yet. (I generated it for myself locally.)
Do you plan to set up a repository somewhere?
Thanks!
Paulo
participants (3)
-
Felipe Lessa
-
Olle Fredriksson
-
Paulo Tanimoto