Reducing Pegged ASTs

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 26 00:56:03 PST 2014


On Wednesday, 26 November 2014 at 06:09:12 UTC, Philippe Sigaud 
via Digitalmars-d-learn wrote:
> IIRC there is a free function in Pegged that does it.

What's the name of this function?

> I did not automate it, because every time I cut down severely a 
> parse
> tree, I later regret it because I lost information that way.
>
> Cutting while still retaining original info (who is this node's
> ancestor) is more difficult: you would have to store it 
> somewhere
> anyhow. You cannot create node classes to represent the 
> hierarchy,
> because of loops in the grammar: an Identifier can have many 
> different
> ancestors.
>
> Note also that Pegged produces parse trees (complete parsing
> information), not ASTs. ASTs would indeed be much smaller, but 
> we
> would have to define what are the master nodes in the D grammar.

What do you mean with master nodes?

> If you want to remember the intermediate nodes you cut down, not
> really, since you still need to store them somehow.

I don't quite understand your formulation in English here. Could 
you elaborate?

> I think what's consuming memory right now is that I duplicate 
> the matched strings at each level

What do you mean with duplicate? Doesn't Pegged use string slices 
that reference the original source?

If this problem is related to (im)mutability and If I understand 
you correctly you could use something like

static if (isImmutable!Source)
     node.text = source_text[i..j];
else
     node.text = source_text[i..j].idup;

right? Where in Pegged could this logic be injected?


More information about the Digitalmars-d-learn mailing list