Translator using Antlr4
Asked Answered
E

1

7

I want to create a translator from SQL to XQuery.
I want to parse SQL and generate an intermediate structure and then use it to generate the XQuery query.
(Note- I want to use an intermediate representation because i'm looking forward to translating SQL to other Query languages in future)
but I don't know exactly how to produce a translator once the grammar has been defined. I want to use ANTLR and have indeed already created the grammar. I'm stuck at the moment with the grammar file and proceeding to build the translator as I don't know exactly the next step in producing one is.

Endres answered 19/9, 2016 at 11:39 Comment(0)
H
5

This depends on what you want to do and in what language you want to do it. Now you have your grammar you should be looking into what language you wish to develop a parser for that language in. Antlr4 has runtimes for Java,javascript and python.

So you can compile your grammar to one of those output languages:

Java -jar yourgrammar.g4 
Java -jar -Dlanguage=Python2 yourgrammar.g4 
Java -jar -Dlanguage=Python3 yourgrammar.g4 

Note

Your really should read the ANTLR documentation for how to proceed using ANTLR or a full tutorial including the book written by terence parr.

But once you have compiled your grammar you can then begin to construct your translator by filling in a listener or visitor method that will produce whatever you decide when elements of your grammar are encountered. By default you will have an empty listener named "yourGrammarListener" where your grammar is the name of your .g4 file with listener appended to the end. This file will have many blank methods for any of your rules you defined.

Whichever language you choose as a target therefore defines whether you just should extend this file or implement your functionality into the generated listener.

Once you fill in any of the methods you should be able to run your application after you make sure your project contains links to whichever ANTLR runtime is required for your application, again the default is the Java runtime and the most popular so usually this means including the ANTLR.jar file into your Java project.

So in short:

  1. Write your grammar

  2. Compile your grammar

  3. Fill in any listener/visitor methods.

  4. In your Java file or whichever target language, define a visitor/tree and pass it the file from an argument like this (depending on language, see documentation for python alternative):

    private void main(String[] args) {
    // Get our lexer
    yourGrammarLexer lexer = new yourGrammarLexer(new ANTLRInputStream(args[0]));
    
    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    
    // Pass the tokens to the parser
    yourGrammarParser parser = new yourGrammarParser(tokens);
    
    // Specify our entry point
    yourGrammarContext yourGrammarContext = yourGrammarRule.drinkSentence();
    
    // Walk it and attach our listener
    ParseTreeWalker walker = new ParseTreeWalker();
    yourGrammarListener listener = new yourGrammarListener();
    walker.walk(listener, yourGrammarContext);
     }
    

Once you have this basic application taking an input file and producing output you wish to have it output into your intermediate format only. Another parser will be required to read your intermediate code and translate it finally into the complete and final language.

One final note is also to consider whether your specific language needs to incorporate the parsing of separate files, i.e files linked from within your current file in the form of includes etc as you would have to develop a parser in its own class and call a new instance for each file that is then linked.

Anyway hope this helps in someway and good luck on your project!

Horeb answered 19/9, 2016 at 12:1 Comment(7)
Hi @Dean219. Thanks for your answer and for the first link too it was useful. the book I've already download it, and I'm reading bit by bit. My target language is java, I've already compile the grammar and get the generated files (listener and visitor) I'm confused at that point (how to use listener/visitor)Endres
Basically the listener or visitor classes contain methods generated from ur grammar, each method is called whenever a part of your language is parsed that matches one of your rules. So say you have an import rule when antlr reads your file and finds an import statement the listener/visitor method contained in the java file is called and you can then handle whatever it is you would like to do with that encountered textHoreb
Thanks again @Dean219. life after parsing is such a onfusing area, I really don't how to begin in it, I couldn't find some simple examples that could simplified the task for me. Sometimes I am so slow in understanding new stuff without examplesEndres
Hey don't worry actually if u wanna discuss it more in depth we could private msg and I could try and answer your questions more directly?Horeb
That would be awesome. Thanks for your time @Dean219Endres
Hi @Dean219 how we could private msg in StackOverflow, sorry for the silly question but I didn't find a way to send you a messageEndres
Ok i looked into it and apparently SO doesent have a private message system on the site :/ the only way is with email "All registered users already have an email address associated with their account. Add "Enable e-mail from other users" to user prefs. For those who have the option enabled, "E-mail this user" link appears on their user profile."Horeb

© 2022 - 2024 — McMap. All rights reserved.