Rishabh Garg
Rishabh Garg

Reputation: 306

java.lang.IndexOutOfBoundsException thrown in ANTLR4

I was writing my parser and I am getting an awkward error. One of my rules is like the following:

operatorTerm[ReadOptions Options, int priority] returns [Term t]
@init
{
int p = priority;
Term t2 = null;
Term t = null;
CompoundTermTag f = null;
}     
    : {testFY($Options, $priority)}?
      a=op[$Options, p]     {f = $a.tag;}
      b=term[$Options, p]   {$t = $b.t; $t = createTerm(f, $t); }
    | {testFX($Options, $priority)}?
      d=op[$Options, p]     {f = $d.tag;}
      e=term[$Options, p-1] {$t = $e.t; $t = createTerm(f, $t);}
    ;

This will throw the following error an using the alias ANTLR4

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 199, Size: 199
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at org.antlr.v4.automata.ATNSerializer.serialize(ATNSerializer.java:201)
at org.antlr.v4.automata.ATNSerializer.getSerialized(ATNSerializer.java:375)
at org.antlr.v4.codegen.model.SerializedATN.<init>(SerializedATN.java:46)
at org.antlr.v4.codegen.model.Parser.<init>(Parser.java:96)
at org.antlr.v4.codegen.ParserFactory.parser(ParserFactory.java:92)
at org.antlr.v4.codegen.OutputModelController.parser(OutputModelController.java:165)
at org.antlr.v4.codegen.OutputModelController.buildParserOutputModel(OutputModelController.java:114)
at org.antlr.v4.codegen.CodeGenerator.generateParser(CodeGenerator.java:169)
at org.antlr.v4.codegen.CodeGenPipeline.process(CodeGenPipeline.java:73)
at org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:422)
at org.antlr.v4.Tool.process(Tool.java:384)
at org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:343)
at org.antlr.v4.Tool.main(Tool.java:190)

Now if I chope off a few lines from the code this would compile. Making this new rule has thrown this exception. Also, If I chope off a few lines from this rule itself the exception won't be thrown.

Is it because the size of my grammar has become very large ? Unable to figure this error out.

Also, I am experiencing no such error even if I trim off some other independent rule. Is there a limit to the size of a grammar file ?

My grammar file is around 800 lines.

Upvotes: 1

Views: 709

Answers (2)

Oscar Ngxongwana
Oscar Ngxongwana

Reputation: 51

I had the same issue, I tried 4.8 and it worked, hope this helps you too.

Upvotes: 0

Sam Harwell
Sam Harwell

Reputation: 99869

This sounds like a bug in a specific version of the ANTLR 4 tool. You can report bugs to the project issue tracker on GitHub:

https://github.com/antlr/antlr4/issues

Note: I cannot reproduce this issue in the latest source code, which will be available as ANTLR 4.1 released at the end of this month.

Upvotes: 1

Related Questions