Pegged, a Parsing Expression Grammar (PEG) generator in D

Philippe Sigaud philippe.sigaud at gmail.com
Sun Mar 11 00:02:35 PST 2012


On Sun, Mar 11, 2012 at 08:51, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> wrote:

> I was thinking of ANTLR-style operators in which you say where the root
> should be and you get to drop unnecessary nodes.
>
> (Post on 2012/02/29 10:19AM GMT-0600.)

Get it, thanks for the ref!

There is an operator to drop unnecessary nodes (':'). Apart from that,
a Pegged grammar is a self-contained entity: it automatically cuts
nodes coming from other grammars, to simplify the tree (it keeps the
matcheds substrings, of course). I plan to code different type of
grammars, to play with the way the deal with nodes coming from outside
rules and grammar composition.

As for the root, the PEG rule is that the first rule in the grammar is
the root. But currently, I deliberately made my grammars unsealed, in
that the user can call any rule inside the grammar to begin parsing:

mixin(grammar("
    A <- B C
    B <- ...
    C <-
"));

A.parse("some input"); // standard way to do it, the parse tree will
be rooted on an 'A' node.
B.parse("some input"); // also possible, the parse tree will be rooted
on a 'B' node.

I'll add what I called closed and sealed grammars. I'll also add named
grammars and such in the days to come. I can add a root note
indication ("unless told otherwise, when asked to parse with grammar
G, begin with rule R).


More information about the Digitalmars-d-announce mailing list