0

I have about 15 java source files and for every file I use this method

 private ClassDescriptor processClass(Path file) {
    try {
        String classContent = FileUtils.readFile(file, StandardCharsets.UTF_8);
        //First, we construct the lexer:
        Java9Lexer java9Lexer = new Java9Lexer(CharStreams.fromString(classContent));
        //Then, we instantiate the parser:
        CommonTokenStream tokens = new CommonTokenStream(java9Lexer);
        Java9Parser parser = new Java9Parser(tokens);
        //ParseTree tree = parser.compilationUnit();
        ParserRuleContext context = parser.compilationUnit();
        ParseTreeWalker walker = new ParseTreeWalker();
        EntityListener listener = new EntityListener(this.getLog());
        walker.walk(listener, context);
        return listener.getDescriptor();
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
}

Everything works fine but not very slowly, but rather slowly. 15 files about 10 seconds. Maybe I do something wrong. Are there any ways when multiple sources are parsed?

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
  • One issue may be that the Java9.g4 grammar is not optimized to be time efficient. I have investigated parsing 50k different Java source files. Some parse quickly, some very slowly. The largest factor in parse time is the tree size. You either can optimize the Java9 grammar to produce a smaller tree, or use Parr's Java grammar which may not accept all you input files but is still pretty good. – kaby76 Jun 12 '20 at 15:22
  • @kaby76 Thank you very much for your comment. Could you say if I use antlr4 rightly for parsing multiple sources? I am asking as these are my first steps in antlr4. – Pavel_K Jun 12 '20 at 15:59
  • Try Parr's grammar first with your code and see if that fixes your problem. https://github.com/antlr/grammars-v4/tree/master/java/java . There may be other optimizations you could try--you are creating a fresh parser for each file. – kaby76 Jun 12 '20 at 18:56
  • Check out https://stackoverflow.com/questions/48700598/reuse-antlr-objects-for-a-new-input-string-c-runtime . It may or may not help. – kaby76 Jun 12 '20 at 18:59

0 Answers0