Lexer and parser generators using CTFE
Hisayuki Mima
youxkei at gmail.com
Thu May 31 18:42:35 PDT 2012
(2012年05月28日 02:31), d coder wrote:
>
> 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();
> }
>
Hello Puneet,
Thank you for your report. I fixed it. Now CTPG creates a static
function as a parser.
But I'm afraid this fix doesn't help you because I don't understand what
a side effect you said is.
Can you show me some examples which include the side effect?
Thanks,
Hisayuki Mima
More information about the Digitalmars-d
mailing list