Ok, so for the first time since my career started I have actually developed something using what I learned in school. Well, ok, I mean I learned Java in school, but this is certainly the first time using some so ... academic.

It started with the fact that I need to develop something very similar to http://code.google.com/apis/gdata/docs/2.0/reference.html#PartialResponse
in order to limit large XML/JSON responses down to something manageable inside a set of restful services. Unfortunately Google doesn't have anything available to parse the syntax and nothing else (like XPath/XSLT) can function to select multiple nodes (at different depths) all at once inside a data structure.

So what to do? Write my own language .. no problem! I had been exposed to ANTLR briefly before and I vaguely remembered BNF Grammars from compiler class so I figured what the heck. Here are the results. First I needed a grammar, so I download ANTLRWorks and after some time spent learning the ANTLR sytax and some debugging I wound up with:

grammar Gdl;

options{  
output=AST;  
}

start : fieldlist;  
fieldlist : field (COMMA field)* -> field*;  
field : STRING L fieldlist R -> ^(STRING fieldlist)  
    | STRING ;

STRING : ('a'..'z' | 'A'..'Z')+;  
COMMA : ',' ;  
L   :   '(';  
R   :   ')';  

Kinda hard to explain in a blog post how this works, but if you read all the stuff above after some time it should make sense how this grammar would take something like this:

name,id,othernodes(name),nodes(other,nodes(id))

and parse it into an AST like this:

AST

Now after clicking a few buttons inside ANLTRWorks it will generate the lexer and parser classes in Java (or whatever language you like). I won't post those here, as with all generated code it's not very readable anyway. Plus it's encouragement to try yourself! Your welcome to dump my grammar in there.

Now the last step is to create what's know as a "walker" in compiler speak. A set of functionality that can crawl the parsed material and take action or translate to another representation ... just like a C compiler re-writes your code to assembler. So my walker code though, recursively, "straight forward" is probably too large to post here, but ... the idea is it takes my new filtering language:

name,id,othernodes(name),nodes(other,nodes(id))

and any nested set of simple java objects:

TestNode node = new TestNode("what","the","foo",new TestNode[]{  
                new TestNode("are", "you", "there",new TestNode[]{
                    new TestNode("are", "you", "there"),new TestNode
                                            ("yes", "i","am")
                    })
                ,new TestNode("yes", "i", "am")},new TestNode[]{
                    new TestNode("crap","this","works")
                    }
        );

and produces a JSON representation:

 { 
   name:what,
    id:the,
    othernodes:{
       name:crap
    },
    nodes:{
       other:there,
       nodes:{
          id:you,
          id:i,
       }
       other:am,
       nodes:{
       }
    }
 }

If you do want to look at the code, just send me a tweet @scott2449. You can also take a look at some tree walker info here: http://www.antlr.org/article/1170602723163/treewalkers.html