Goldie Parsing System v0.4 Released - Now for D2

Nick Sabalausky a at a.a
Fri Apr 15 00:50:30 PDT 2011


"Andrej Mitrovic" <andrej.mitrovich at gmail.com> wrote in message 
news:mailman.3528.1302836832.4748.digitalmars-d-announce at puremagic.com...
> So could your library be used to implement an alternative for HTOD? Or
> more simply put, could I use this to do (simple) transformations of C
> code?

C code is admittedly a bit tricky because it uses a preprocessor. But in 
general, yes, Goldie can be used transform source. The way it would work is 
like this:

1. Define a grammar for the "input" language.

There's an ANSI C grammar here, but I haven't looked at it, so I don't know 
how good it is:
http://www.devincook.com/goldparser/grammars/index.htm

An introduction to the grammar description langauge is here:
http://www.semitwist.com/goldie/Start/Grammar/

2. Use Goldie to parse the input. Details here: 
http://www.semitwist.com/goldie/Start/HowToUse/

3. Once Goldie's parsed the input, it will give you a parse tree (it'll be 
structured based on the grammar you used). You can then walk the tree and do 
whatever you want with it. I don't recommend actually modifying the parse 
tree that Goldie gives you in-place, since the interface isn't really 
designed for that right now (though you may still be able to make it work). 
But you can walk it and either build up your own tree structure, or convert 
it to text however you want, etc.

Actually, you can even take a look at what the parse tree you'll get back 
will look like before writing any code: Use the included Goldie parse ( 
http://www.semitwist.com/goldie/Tools/Parse/ ) to parse a file according to 
whatever grammar you want to use. It'll save the parse tree to JSON. Then 
you can inspect the parse tree with this: ( 
http://www.semitwist.com/goldie/Tools/JsonViewer/ ). But try to use just a 
small sample file: Parse trees tend to get very big, very fast and 
JsonViewer is written in C# and can't handle large files very well.

Since you're talking about C, you'll probably want to run your original C 
code through the "preprocess-only" option of a real C compiler. (I *think* 
DMC will do that.) Then parse the resulting "preprocessed C" files with 
Goldie. (Although if your goal is an HTOD-like tool, maybe you would need to 
deal with the original un-preprocessed source directly. If Golde's grammar 
langauge doesn't seem quite up to the task, it probably wouldn't bee too 
hard to just manually make a basic C preprocessor.)

Right now, the grammar description format isn't really very good as 
describing preprocessors (a limitation Goldie inherited from GOLD Parser 
Builder). But fixing that limitation is one of the things on my TODO list 
for Goldie.

If you do try this, I'd love to hear how it works out :) Even if you 
encounter any problems, it would be very helpful for me to know. Haven't 
gotten a whole lot of feedback yet.





More information about the Digitalmars-d-announce mailing list