3

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?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
user3898179
  • 211
  • 5
  • 13

1 Answers1

4

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.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • Hi @Bart - Thanks for the insights. An extremely basic question, do the numbers in the Tree String represent token numbers assigned by the Lexer? – user3898179 Apr 07 '15 at 17:08
  • @user3898179, no problem. If by "number" you mean the `int` that denotes the token-type, then yes, that is assigned by the lexer. – Bart Kiers Apr 07 '15 at 17:57