Can ANTLR4 java parser handle very large files or can it stream files

后端 未结 1 1940
半阙折子戏
半阙折子戏 2020-12-10 21:18

Is the java parser generated by ANTLR capable of streaming arbitrarily large files?

I tried constructing a Lexer with a UnbufferedCharStream and passed that to the

1条回答
  •  轮回少年
    2020-12-10 21:41

    You could do it like this:

    InputStream is = new FileInputStream(inputFile);//input file is the path to your input file
    ANTLRInputStream input = new ANTLRInputStream(is);
    GeneratedLexer lex = new GeneratedLexer(input);
    lex.setTokenFactory(new CommonTokenFactory(true));
    TokenStream tokens = new UnbufferedTokenStream(lex);
    GeneratedParser parser = new GeneratedParser(tokens);
    parser.setBuildParseTree(false);//!!
    parser.top_level_rule();
    

    And if the file is quite big, forget about listener or visitor - I would be creating object directly in the grammar. Just put them all in some structure (i.e. HashMap, Vector...) and retrieve as needed. This way creating the parse tree (and this is what really takes a lot of memory) is avoided.

    0 讨论(0)
提交回复
热议问题