2

I parsed an SQL query using an Antlr 4 grammar. The result of tree.toStringTree() is this: ([] ([845] SELECT ([878 845] ([1473 878 845] ([1129 1473 878 845] ([1700 1129 1473 878 845] col1))) as ([1477 878 845] a)) FROM ([887 845] ([1487 887 845] ([1694 1487 887 845] table1)))))

Antlr documentation tells me this is a LISP style tree. How can I further process a LISP tree?

4

1 回答 1

3

It's friendlier for your eyes if you provide the Parser instance:

SQLiteLexer lexer = new SQLiteLexer(new ANTLRInputStream(sql));
SQLiteParser parser = new SQLiteParser(new CommonTokenStream(lexer));
ParseTree tree = parser.select_stmt();
System.out.println(tree.toStringTree(parser));

As to your question of processing this string: you shouldn't. It is just for displaying the tree. Just as a normal toString() works. You should not parse this string because there is no guarantee it will look the same from version to version.

I've suggested it before, but will do so again: if you want to get a hierarchical structure, just work with ParseTree, it has parent and child references. If that is not what you want, please explain yourself better.

于 2015-04-07T13:34:23.053 回答