Reducing Pegged ASTs

Etienne Cimon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Nov 25 15:59:45 PST 2014


On 2014-11-25 10:12, "Nordlöw" wrote:
> Is there a way to (on the fly) reduce Pegged parse results such as

I've made an asn.1 parser using pegged tree map, it's not so complex and 
does the reducing as well.

https://github.com/globecsys/asn1.d

Most of the meat is in asn1/generator/

In short, it's much easier when you put all the info in the same object, 
in this case it's an AEntity: 
https://github.com/globecsys/asn1.d/blob/master/asn1/generator/asntree.d#L239

When the whole tree is done that way, you can easily traverse it and 
move nodes like a linked list.. I've made a helper function here:

https://github.com/globecsys/asn1.d/blob/master/asn1/generator/asntree.d#L10

You can see it being used here:
https://github.com/globecsys/asn1.d/blob/38bd1907498cf69a08604a96394892416f7aa3bd/asn1/generator/asntree.d#L109

and then here:

https://github.com/globecsys/asn1.d/blob/master/asn1/generator/generator.d#L500

Also, the garbage collector really makes it easy to optimize memory 
usage, ie. when you use a node in multiple places and need to re-order 
the tree elements.

I still have a bunch of work to do, and I intend on replacing botan's 
ASN1 functionalities with this and a DER serialization module.

Beware, the pegged structure isn't insanely fast to parse because of the 
recursion limits I implemented very inefficiently because I was too lazy 
to translate the standard asn.1 BNF into PEG.. Also, the bigger 
bottleneck would be error strings.

For a 1-2 months of work (incl. learning ASN.1), I'm very satisfied with 
the technology involved and would recommend intermediate structures with 
traversal helpers.


More information about the Digitalmars-d-learn mailing list