4解析大型C文件需要花费很多时间

4解析大型C文件需要花费很多时间

本文介绍了Antlr 4解析大型C文件需要花费很多时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的C代码文件(> 9000 LoC),并尝试使用以下语法对其进行解析:

I have a large c-code file (>9000 LoC) and attempt to parse it using this grammar:

https://github.com/antlr/grammars- v4/blob/master/c/C.g4

我等待了一个多小时才中止.该机器是具有2GB RAM的Core 2 Duo L9400.最大java vm-heap-size设置为2GB.它不会产生任何解析错误,但是根本无法完成.

I waited for over an hour before aborting. The machine is a Core 2 Duo L9400 with 4GB of ram. Maximum java vm-heap-size is set to 2GB.It does not produce any parse errors, but it simply doesn't finish.

经过一些研究,我将预测模式设置为SLL,这会产生一个在几秒钟内输入时没有可行的选择".

After doing some research, I set the prediction mode to SLL, which produces a"no viable alternative at input" within seconds.

接下来,我将预测模式设置为LL_EXACT_AMBIG_DETECTION并将DiagnosticErrorListener附加到解析器.这产生了很多歧义报告",主要是关于声明/声明说明符.我认为这会迫使解析器非常频繁地回溯,这可能是长时间解析的原因?

Next, I set the prediction mode to LL_EXACT_AMBIG_DETECTION and attached a DiagnosticErrorListener to the parser. This produces a lot of "Ambiguity reports", mainlyconcerning declarations/declaration-specifiers. I assume this forces the parser to backtrack extremely often, which I is probably the explaination for the long parsing time?

除了尝试重写语法之外,我还能做些什么来提高性能?

Is there anything I can do to improve performance other than attempting to rewrite the grammar?

感谢您的帮助;)

推荐答案

首先,请务必注意,ANTLR 4在解析期间不会回溯.

First of all, it's important to note that ANTLR 4 never backtracks during parsing.

您所指的语法不是SLL,这会阻止您以最快的方式使用ANTLR 4.但是,在或实验期间,我们能够识别出一条单独的规则,我们对其进行了更改以制作语法SLL.您可能可以通过关注兴趣的邮件列表获得更改后的语法.我正在参加一场音乐会,因此无法访问它.

The grammar you are referring to is not SLL, which prevents you from using ANTLR 4 in its fastest mode. However, during or experiments we were able to identify a single rule which we altered to make the grammar SLL. You may be able to obtain the altered grammar via the antlr-interest mailing list. I'm at a concert now so I don't have access to it.

这篇关于Antlr 4解析大型C文件需要花费很多时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 03:21