/// Sunday, November 20

XmlDocument for AST?

I played with Coco/R (lexer/parser generator) last week and was a little disappointed Coco doesn't have a quick way to build an AST like ANTLR. For small utilities, the built-in AST library of ANTLR is sufficient. ANTLR's parser grammar uses a '^' character to indicate the root of the AST node for the production rule. Very simple. To do the same with Coco, you add attributes to each element that becomes part of an AST in your production rules. The grammar file quickly becomes a hybrid of EBNF fules interlaced with C# code. To be fair, ANTLR requires interlacing of C# when creating a grammar of any complexity, but the amount of C# code is much less.

On the other hand, Coco is lightweight and doesn't require a run-time library. The reason for trying it in the first place. Coco is supposedly faster. Parsing does seem faster but I haven't built a complex parser with it.

I went back to ANTLR. Then, this little programmer demon in my head would not succumb to such an easy defeat. I'm now back on Coco for this project. Yep, i'm a flip-flopper. Since .NET doesn't provide a tree collection, I decided to use the XmlDocument for my AST instead of spending a few minutes to create an AST Node. This has many advantages:

I'm not a compiler guy but I see this approach having many advantages (and disadvantages such as performance). Imagine being able to convert from one language to another using XSLT. Or, to simplify the use of AstMacros or NRefactory refactoring? Most of us know how to manipulate XmlDocument and even write XSLT. There is huge potential here.

The idea came from an article I read on the web. Something to do with running multiple languages using common XML ASTs on an XML virtual machine. Interesting read, I wish I saved the link.

Comments:

Keep in mind that _big_ XmlDocument has a lot of waste, memory wise.
Don't think you'll see that when talking AST, but keep it in mind.
The idea is very interesting.
 
Good idea. Have you tried this to build an XML-AST for C# grammar?

If yes, then do you have a clean XSD describing such AST for C# ?

@lex
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?