Lexer and parser generators using CTFE

d coder dlang.coder at gmail.com
Sun May 27 10:31:21 PDT 2012


>
>
> Generally a parser generated by other tool and accepting tokens returns
> the abstract syntax tree, but it return the evaluated value in the example.
> In other words, it does lexical analysis, parsing and (type) converting at
> a time.
> If you want simply abstract syntax tree, it may be a little pain to use
> ctpg.
>

 Hello Youkei

I am trying to use CTPG for compile time parsing for a DSL I am working on.
I have tried the examples you created in the examples directory.

I would like the parser to effect some side effects. For this purpose, I
tried including the parser mixin into a class, but I got a strange error
saying:

Error: need 'this' to access member parse

I have copied the code I am trying to compile at the end of the email. Let
me know what I could be doing wrong here.

Regards
- Puneet


import ctpg;
import std.array: join;
import std.conv: to;

class Foo
{
  int result;
  mixin(generateParsers(q{
        int root = mulExp $;

        int mulExp =
          primary !"*" mulExp >> (lhs, rhs){ return lhs * rhs; }
        / primary;

        int primary = !"(" mulExp !")" / [0-9]+ >> join >> to!int;
      }));

  void frop() {
    result = parse!root("5*8");
  }
}


void main(){
  Foo foo = new Foo();
  foo.frop();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120527/f9b2c97f/attachment.html>


More information about the Digitalmars-d mailing list