Writing a language parser in D

div0 div0 at users.sourceforge.net
Tue Sep 15 11:49:53 PDT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bill Baxter wrote:
> On Mon, Sep 14, 2009 at 2:46 PM, div0 <div0 at users.sourceforge.net> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Justin Johansson wrote:
>>> Can D people please recommend suitable tools for generating a parser (in D) for an LL(1) grammar.  There's bound to be much better parser generator tools available nowadays, since my last foray into this area 10+ years ago with YACC.  I've heard of tools like bison, SableCC etc but apart from the names know nothing about them.
>>>
>>> (Note.  This question is not about writing a parser for D.  It is about writing a parser in D for another language which has an LL(1) grammar).
>>>
>>> Thanks in advance for all help.
>>>
>>> -- Justin Johansson
>>>
>> I've ported boost::spirit to d. No idea if it does what you want,
>> but I've written some fairly complicated grammars with it.
>>
>> It's not a tool though, you just define your grammar directly in code.
>> Which is either a plus or a minus depending on your point of view.
>>
>> Quick intro:
>>
>> http://www.boost.org/doc/libs/1_36_0/libs/spirit/classic/index.html
>>
>> And D implementation:
>>
>> http://www.sstk.co.uk/spiritd.php
> 
> I'm not seeing the powershell script or test app in that .zip file.
> I don't really need it, I was just curious what the syntax looked like
> without any operator overloading.
> 
> --bb

Their both in the top level bit of the zip, build.ps1 & test0.d
I've gone for template factory functions at the moment,
it's quick and dirty:

> rT	values = rT.create(
>         or(
>             or(
>                 or(
>                     or(parseReal, boolVal[&_outer.gotBool]),
>                     parseInt
>                 ),
>                 stringVal[&_outer.gotString]
>             ),
>             arrayValues
>         ));
> 
> rT	fieldName = rT.create(
>         lexemeD[
>             seq(
>                 or(alphaP, chP!(chT)('_')),
>                 star(or(alnumP, chP!(chT)('_')))
>             )
>         ]);
> 
> rT	field = rT.create(
>         seq(
>             seq(fieldName[&_outer.gotFieldName], chP!(chT)(':')),
>             values
>         ));

One day I may write a something to generate the grammar from a string
but I've got way too much other stuff to do at the mo, so that's a low
priority.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFKr+HRT9LetA9XoXwRAppuAJ4n+0i/BCW4pVI3CPpBXXEadDlF8wCdG8RH
gp+7369u/3k9hkE2E/vxapg=
=ZYF3
-----END PGP SIGNATURE-----



More information about the Digitalmars-d mailing list