[antlr-interest] My recursive tree walker won't

Cameron Ross cross at symboticware.com
Tue Dec 5 19:09:02 PST 2006


I have a simple tree walker that doesn't recurse as I expect.   The tree 
constructed by my parser is as expected.  However, the tree parser only 
reports the highest level PWORD root whereas I expect it to decend and 
report on the nested PWORD node as well.  The expression I'm parsing is 
(a (b c)) and the tree constructed by the parser looks like this:

PWORD
    |
    a -- PWORD
                |
                b -- c

Any help is greatly appreciated.

Cheers,
Cameron.

My client code follows:
------------------------------
    StringReader stringReader = new StringReader("(a (b c))");
    MyLexer lexer = new MyLexer(stringReader);
    MyParser parser = new MyParser(lexer);
    parser.parse();
    AST ast = parser.getAST();
    TestTreeParser treeParser = new TestTreeParser();
    treeParser.start(ast); 

The grammar defining my lexer, parser and tree parser follows:
------------------------------------------------------------------------------

class TestTreeParser extends TreeParser;
options {
    buildAST = true;
}
start: (expr)*;
expr: pword;
pword: #(PWORD (expr)*) {
        System.out.println("pword: " + #pword.toStringList());
    };

class MyParser extends Parser;
options {
    buildAST = true;
    exportVocab = MyVocab;
}
tokens {
    PWORD;
}
parse: (expr)+;
expr: LPAR! (expr | WORD)+ RPAR! {
    if (#expr.getType() == MyVocabTokenTypes.WORD) {
        ## = #([PWORD, "PWORD"], ##);
        System.out.println("WORD: " + #expr.toStringList());
    }
};

class MyLexer extends Lexer;
options {
    exportVocab = MyVocab;
}
LPAR: '(';
RPAR: ')';
WORD: ('a'..'z')+;
WS: ' ' { $setType(Token.SKIP); };



More information about the antlr-interest mailing list