Parser generator?

Wouter Verhelst wouter at grep.be
Thu Jul 5 16:17:16 PDT 2012


Jonathan M Davis <jmdavisProg at gmx.com> writes:

> On Wednesday, July 04, 2012 15:32:16 Wouter Verhelst wrote:
>> Jonathan M Davis <jmdavisProg at gmx.com> writes:
>> > On Wednesday, July 04, 2012 14:53:02 Wouter Verhelst wrote:
>> >> Hi folks,
>> >> 
>> >> Does someone know of a parser generator for D?
>> >> 
>> >> If it doesn't exist, I can write a parser by hand, but having it
>> >> generated (at least for my initial permutation) seems like a better
>> >> idea...
>> >> 
>> >> Thanks,
>> > 
>> > https://github.com/PhilippeSigaud/Pegged
>> > https://github.com/PhilippeSigaud/Pegged/wiki
>> 
>> Whoa. That's so perfect, it makes me drool.
>
> Yeah. It's pretty cool. It really shows up D's metaprogramming
> capabilities. 

It's a bit hell to debug it, though. But I finally managed to get a
working parser out of it.

Except now I can't use it, for some reason. Code:

void buildGraph(Output o) {
        void parseToGraph(ParseTree p) {
                writeln(p.ruleName);
        }

        parseToGraph(o.parseTree);
}

void parseconfigs() {
        Output o = ENI.parse(readText("/tmp/ifaces_data"));
        buildGraph(o);
}

produces the following error message:

dmd  -gc -w -unittest -I../../Pegged -c -ofmain.o main.d
ipcfg/parser.d(45): Error: struct pegged.peg.Output(TParseTree) if (isParseTree!(TParseTree)) is used as a type
make: *** [main.o] Error 1

Help?

> The one thing to watch out for though is that such metaprogramming tends to 
> eat up a lot of memory when compiling at this point (primarily because the 
> compiler doesn't manage memory very well at this point - it's approach is very 
> simplistic).

I'll say. My parser does more than just identifiers and comments, but
it's still not that complex. And yet I manage 1GiB+. Whoa.

But that's still acceptable.

> So, that may or may not cause you problems.

Not really.

What does cause problems, though, is that it won't compile with gdc:

gdmd -release  -I../../Pegged -c -ofmain.o main.d
/home/wouter/code/d/Pegged/pegged/grammar.d:128: Error: template pegged.grammar.PEGGED!(ParseTree).PEGGED.parse(ParseLevel pl = ParseLevel.parsing) parse(ParseLevel pl = ParseLevel.parsing) matches more than one template declaration, /home/wouter/code/d/Pegged/pegged/grammar.d(111):parse(ParseLevel pl = ParseLevel.parsing) and /home/wouter/code/d/Pegged/pegged/grammar.d(126):parse(ParseLevel pl = ParseLevel.parsing)
make: *** [main.o] Error 1

GDC 4.6, though, so I'll wait until Iain uploads 4.7 to Debian; if that
doesn't fix it, I'll file bugs where appropriate.

> It should be fixed 
> eventually, but it does sometimes cause problems with this sort of thing. 
> std.regex has similar issues.

Gotcha.

-- 
The volume of a pizza of thickness a and radius z can be described by
the following formula:

pi zz a


More information about the Digitalmars-d-learn mailing list