[antlr-interest] AST/rewrite rule question

Ashish asengine at gmail.com
Fri Jan 18 15:51:10 PST 2008


Hi,
If the start rule is specified without any rewrite rules all the queries
from the
input are parsed and nodes are created for the select queries as expected.
However if the start rule is modified to use the rewrite rule to create the
AST ie

from
  selectQueries :  (selectQuery)*;
to
  selectQueries :  (sq+=selectQuery)* -> ^(SELECT_QUERIES $sq);

then, only the first query in the file seems to be parsed. The rest seem to
be ignored.
Test file contents/program/grammar snippet  below.

Thanks
Ashish

// run as
java TestTree < input-queries-file

// Input queries
select a;
select x, y, z;
select a, b, c from x, y, z;
select asdf from qwert;
select a from b where x > c;
select a, b, vdef from asdf, qwer where c > x;

// TestTree - Main program snippet
        ANTLRInputStream input = new ANTLRInputStream(System.in);
        SelectTreeLexer lexer = new SelectTreeLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SelectTreeParser parser = new SelectTreeParser(tokens);
        SelectTreeParser.selectQueries_return selectQueries =
           parser.selectQueries();
        Object o = selectQueries.getTree();

        System.out.println("o : " + o.getClass().getName());
        Tree tree = (Tree)o;
        traverseTree(tree, "");
    }
    private static void traverseTree(Tree t, String indent) {
        printTree(t, indent);
        int children = t.getChildCount();
        for(int i=0; i<children; i++) {
            traverseTree(t.getChild(i), indent + "  ");
        }
    }
    private static void printTree(Tree t, String indent) {
        System.out.println(indent + " tree: " + t.toStringTree());
    }

// Grammar contents
grammar SelectTree;

options {
        output=AST;
        ASTLabelType=CommonTree;
}

tokens {
        SELECT_QUERIES;
}


selectQueries
        :       (sq+=selectQuery)* -> ^(SELECT_QUERIES $sq)
        ;

selectQuery
        :       selectClause fromClause? whereClause? SEMICOLON
        ;

selectClause
        :       SELECT selectList  -> ^(SELECT selectList)
        ;

fromClause
        :       FROM fromList  -> ^(FROM fromList)
        ;
whereClause
        :       WHERE conditionClause -> ^(WHERE conditionClause)
        ;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080118/e5dedb2d/attachment.html 


More information about the antlr-interest mailing list