I am trying to construct a translator that could convert PL/SQL code to Java using Antlr 4 and StringTemplate 4. I have the grammar of PL/SQl and have already build a parser for PL/SQL but i have no idea how to approach the problem further. I found many articles of language translation using antlr and stringtemplate but they all use ANTLR 3 or ANTLR 2 . So is there any difference when using Antlr 4 to translate along with Stringtemplate as the parser for PL/SQL using Antlr 4 had some differences than Antlr3
I am completely new to Programming language translation and don't know if there is any better way to approach the problem.
ANTLR prior to v4 had intrinsic support for StringTemplate (you could specify that your grammar output is ST). As of v4, this support seems to be dropped.
One option is to use Listener or Visitor interfaces to manually construct your templates. Visitor is likely more useful in this context.
The other option I'm currently investigating is assigning ParseTree (result from parsing) as a parameter to the template. I use custom ModelAdapter for ParserRuleContext so I can access sub-contexts from the templates.
Example: I'm assuming you are using grammar for PL/SQL. Then you could have a template group like:
You would also need ModelAdapter for ParserRuleContext (this is just an example of the only method in it):
Now you can do the following:
Hope this helps!