为什么ANTLR4需要很长时间才能为String类构建一个解析树

时间:2017-02-22 14:44:24

标签: antlr4

我正在尝试使用以下标准ANTLR设置代码块为Java的String类构建一个解析树。我使用的是antlr4存储库上发布的标准Java8.g4语法(完整代码可在https://github.com/cagdasgerede/whysolong获得)。

ANTLRInputStream input = new ANTLRInputStream(System.in);
Java8Lexer lexer = new Java8Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java8Parser parser = new Java8Parser(tokens);

System.out.println("Building the parse tree...");
long start = System.nanoTime();
ParseTree tree = parser.compilationUnit();
long elapsedNano = System.nanoTime() - start;
long elapsedSec =
    TimeUnit.SECONDS.convert(elapsedNano, TimeUnit.NANOSECONDS);
System.out.println(String.format(
    "Built the parse tree...(took %d seconds)", elapsedSec));

输出结果为:

Building the parse tree...
Built the parse tree...(took 21 seconds)

我试图跑几次,我发现它需要超过20秒。 花费这么长时间是正常的吗?我在设置中做错了吗?有没有办法加快解析树的构建速度?

要在计算机上观察此问题,请执行以下命令:

git clone https://github.com/cagdasgerede/whysolong
cd whysolong
make

0 个答案:

没有答案