1
votes

Context

I am trying to parse some SQL stored in an application configuration using the Java classes generated from the plsql antlr4 grammar files.

In this context I'd like to know how to use correctly those classes and to leverage grun (org.antlr.v4.gui.TestRig) for some testing.

I found some elements here but it might be outdated as this previous post

  • refers only to one .g4 grammar file whereas I found there is one for the Lexer and one for the Parser in the repository currently.
  • I tried similar things without success for grun

Use of antlr4 plsql generated classes (Java)

On one hand I created a maven java project using org.antlr:antlr4-maven-plugin:4.7.2, it generates the lexer, parser, listener and visitor classes from the garmmar files PlSqlLexer.g4 and PlSqlParser.g4 downloaded from antlr4 github repository

Now I'd need a hint how to use correctly the generated classes

@Test
public void testPlSqlGrammar() throws IOException {
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("testfile.sql");
    CharStream s = CharStreams.fromStream(is);
    CaseChangingCharStream upper = new CaseChangingCharStream(s, true);
    Lexer lexer = new PlSqlLexer(upper);
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    PlSqlParser parser = new PlSqlParser(tokenStream);

    PlSqlParserListener listener = new PlSqlParserListener() {
        //here is my listener code
        //(...)
    }

    ParseTreeWalker walker = new ParseTreeWalker();
    parser.addParseListener(listener);
    parser.sql_script(); // <-- here I called this method which trigger a processing
}

Question1 What is the proper method to trigger the processing of the whole InputStream, is it as per the above snippet calling sql_script() method ?

Note sql_script is the top level element of the grammar

plsql grammar and org.antlr.v4.gui.TestRig

On the other hand I wanted to do some testing using the org.antlr.v4.gui.TestRig tool from antlr4 getting-started doc

I installed antlr-4.7.2 on Windows as per per the instructions and generated the lexer and parser from the grammars as follows

>antlr4 PlSqlLexer.g4
>java org.antlr.v4.Tool PlSqlLexer.g4
>antlr4 PlSqlParser.g4
>java org.antlr.v4.Tool PlSqlParser.g4

I added PlSqlBaseLexer.java and PlSqlBaseParser.java in the folder to compile the generated classes

> javac Pl*.java

Then I looked for the right parameter to launch grun with

>grep -i grammar *.g4
PlSqlLexer.g4:lexer grammar PlSqlLexer;
PlSqlParser.g4:parser grammar PlSqlParser;

However the 2 above grammar names (PlSqlLexer, PlSqlParser) did not work and only using "PlSql" triggered the tool execution via either below commands

>grun PlSql r -tree
>grun PlSql r -gui

but they (both consistently) returned the following error (illustrated for -gui flavor)

>java org.antlr.v4.gui.TestRig PlSql r -gui
SELECT FIELD1 FROM TABLE1;
^Z
No method for rule r or it has arguments

Question2 What is the correct grammar name to be used ? is it PlSql ?

Question3 Any advise how to use successfully grun script with this antlr4 plsql grammar ?

1

1 Answers

1
votes

What is the proper method to trigger the processing of the whole InputStream, is it as per the above snippet calling sql_script() method ?

Yes, to parse the input according to the sql_script rule, you invoke the sql_script method on the parser object.

What is the correct grammar name to be used ? is it PlSql ?

Yes.

Any advise how to use successfully grun script with this antlr4 plsql grammar ?

You need to specify the name of the rule that you want to invoke. In your invocations you passed r as the rule name, but the grammar does not define a rule named r. That's what the error message is trying to tell you. If you want to invoke the sql_script rule, you'll need to pass that instead of r, so grun PlSql sql_script -tree or grun PlSql sql_script -gui.