ANTLR V4 + Java8 Grammar & # 8594; OutOfMemoryException

I am trying to use ANTLR V4 with public Java 8 grammar - https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4

I generated class files and tried to parse the Java 8 JRE , but somehow with java.text.SimpleDateFormat.java it worked with:

 java.lang.OutOfMemoryError: GC overhead limit exceeded 

It also crashes when I try to parse just one file.

Can this be somehow solved? Obviously ANTLR V4 cannot handle files with more than 2000 LOC ? Is this a correct guess?

What i have done so far:

  • Changing the allocated memory on the JVM in a few steps from 256 MB to 4 GB - it then changes to

    java.lang.OutOfMemoryError: Java heap space

  • To make sure there is no syntax problem with the input file
    First, I deleted the first half of the file β†’ , the parsing looked fine , and then disabled this action and deleted the second half of the file strong> β†’ the parsing looks fine

+5
source share
1 answer

It seems that the grammar in this repository is based on what I wrote. The grammar is based on certain functions that are only available in my β€œoptimized” ANTLR 4 plug in order to work well. In addition to using this release, you need to do the following two things to maximize performance:

  • Use a two-step parsing strategy. Assuming your start rule is called compilationUnit , it might look like this:

     CompilationUnitContext compilationUnit; try { // Stage 1: High-speed parsing for correct documents parser.setErrorHandler(new BailErrorStrategy()); parser.getInterpreter().setPredictionMode(PredictionMode.SLL); parser.getInterpreter().tail_call_preserves_sll = false; compilationUnit = parser.compilationUnit(); } catch (ParseCancellationException e) { // Stage 2: High-accuracy fallback parsing for complex and/or erroneous documents // TODO: reset your input stream parser.setErrorHandler(new DefaultErrorStrategy()); parser.getInterpreter().setPredictionMode(PredictionMode.LL); parser.getInterpreter().tail_call_preserves_sll = false; parser.getInterpreter().enable_global_context_dfa = true; compilationUnit = parser.compilationUnit(); } 
  • Include the global DFA context (I included it in the previous block of code so you can't skip it)

     parser.getInterpreter().enable_global_context_dfa = true; 
+8
source

Source: https://habr.com/ru/post/1232810/


All Articles