Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to report grammar ambiguity in antlr4

Tags:

antlr

antlr4

According to the antlr4 book (page 159), and using the grammar Ambig.g4, grammar ambiguity can be reported by:

grun Ambig stat -diagnostics

or equivalently, in code form:

parser.removeErrorListeners();
parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);

The grun command reports the ambiguity properly for me, using antlr-4.5.3. But when I use the code form, I dont get the ambiguity report. Here is the command trace:

$ antlr4 Ambig.g4   # see the book's page.159 for the grammar
$ javac Ambig*.java
$ grun Ambig stat -diagnostics < in1.txt # in1.txt is as shown on page.159
    line 1:3 reportAttemptingFullContext d=0 (stat), input='f();'
    line 1:3 reportAmbiguity d=0 (stat): ambigAlts={1, 2}, input='f();'
$ javac TestA_Listener.java
$ java TestA_Listener < in1.txt   # exits silently

The TestA_Listener.java code is the following:

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.*; // for PredictionMode
import java.util.*;
public class TestA_Listener {
    public static void main(String[] args) throws Exception {
        ANTLRInputStream input = new ANTLRInputStream(System.in);
        AmbigLexer lexer = new AmbigLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        AmbigParser parser = new AmbigParser(tokens);
        parser.removeErrorListeners(); // remove ConsoleErrorListener
        parser.addErrorListener(new DiagnosticErrorListener());
        parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
        parser.stat();
    }
}

Can somebody please point out how the above java code should be modified, to print the ambiguity report?

For completeness, here is the code Ambig.g4 :

grammar Ambig;

stat: expr ';'        // expression statement
    | ID '(' ')' ';'  // function call statement
    ;

expr: ID '(' ')'
    | INT
    ;

INT :   [0-9]+ ;
ID  :   [a-zA-Z]+ ;
WS  :   [ \t\r\n]+ -> skip ;

And here is the input file in1.txt :

f();
like image 601
R71 Avatar asked Oct 28 '25 20:10

R71


1 Answers

I waited for several days for other people to post their answers. Finally after several rounds of experimenting, I found an answer:

The following line should be deleted from the above code. Then we get the same ambiguity report as given by grun.

parser.removeErrorListeners(); // remove ConsoleErrorListener
like image 85
R71 Avatar answered Oct 30 '25 15:10

R71



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!