How to get the AST-tree instead of a list when parsing in Python with ANTLR?
Question: I get simple antlr3 grammar
[MicroXpath](http://www.antlr3.org/grammar/1210113624040/MicroXPath.g) and
build lexer and parser for Python.

Then I wrote a simple test code:

    
    
    import antlr3
    from XPathLexer import XPathLexer
    from XPathParser import XPathParser
    
    def print_level_order(tree, indent):
        print('{0}{1}'.format('   '*indent, tree.text, tree.getType()))
        for child in tree.getChildren():
            print_level_order(child, indent+1)
    
    input = 'descendant::name[class/name[test="x"]="File"]'
    char_stream = antlr3.ANTLRStringStream(input)
    lexer = XPathLexer(char_stream)
    tokens = antlr3.CommonTokenStream(lexer)
    parser = XPathParser(tokens)
    tree = parser.xPath().tree
    
    print_level_order(tree, 0)
    

Result:

    
    
    None
       descendant
       :
       name
       [
       class
       /
       name
       [
       test
       =
       "x"
       ]
       =
       "File"
       ]
    

Where the tree? This is a linear list! What am I doing wrong? Or using ANTLR
can not build a tree?


Answer: Only adding `output=AST;` to the `options{...}` is not enough: you'll have to
tell ANTLR which nodes/tokens to exclude from the AST (if any), and which
nodes/tokens you want to make the root of a (sub) tree. Not doing so results
in a flat tree, as you already observed.

Checkout this Q&A to find out how to create a hierarchy in your tree: [How to
output the AST built using
ANTLR?](http://stackoverflow.com/questions/4931346/how-to-output-the-ast-
built-using-antlr)

