For the ANTLR solutions in general, you would find the location of where to put your JOIN and WHERE clauses by analyzing the AST tree that is produced by the (generated) ANTLR parser (this Bart Kiers answer shows an example of a simple tree produced by an ANTLR parser). If you want to discuss an ANTLR-only solution, I recommend starting a new question so that it doesn't get tangled up with the other options listed here.