[m-users.] Lex & Yacc for Mercury

Paul Bone paul at bone.id.au
Sat Dec 31 14:25:49 AEDT 2016


On Sat, Dec 31, 2016 at 11:50:07AM +1100, Zoltan Somogyi wrote:
> 
> On Fri, 30 Dec 2016 11:05:27 -0500, Barthwal <barthwal at protonmail.com> wrote:
> > I want to write a compile and I was wondering, if there are any Lex & Yacc parsers for Mercury or not.
> 
> If by "parsers for Mercury" you mean scanner and parser generators that
> convert specifications of scanners and parsers into Mercury code, the answer
> is yes. I wrote them when I was teaching a compiler class, but they were never
> distributed, because I never got around to documenting them.

There are a couple of tools/libraries of this kind already available.  They
probably arn't as feature complete as lex/flex and yacc/bison but hopefully
if there's something missing you can add it and we'll accept patches :D

We have a lex/flex like library called lex.

https://github.com/Mercury-Language/mercury/tree/master/extras/lex

I'm currently using this within the Plasma compiler.  I found that it would
use a lot of memory, when I restrected it to ASCII or ISO-8859 it fixed the
problem.  The copy I'm using in Plasma is here:
https://github.com/PaulBone/plasma/tree/master/src  I can't find the code I
needed to change.  Let me know if you need it and I'll have a better look.

We also have a yacc/bison tool, called moose for LALR grammars.

https://github.com/Mercury-Language/mercury/tree/master/extras/moose

I haven't tried to use this, I've heard from others that it doesn't do
certain things that yacc/bison do.

For LL grammars there's the parsing_utils library.  This is a library that
you would use to write a recrisve decent parser. 

https://github.com/Mercury-Language/mercury/tree/master/l8brary/parsing_utils.m

This works fairly well, but it handles errors in an arkward way, using
impurity.

I worte my own recursive decent parsing library that I'm using within the
Plasma compiler.  It handles errors differently, requiring a lot more code
but avoiding impurity.
https://github.com/PaulBone/plasma/blob/master/src/parsing.m  It is licensed
under the MIT license.


-- 
Paul Bone
http://paul.bone.id.au


More information about the users mailing list