Antlr 4 parsing large c file takes forever

a 夏天 提交于 2019-12-11 09:39:44

问题


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

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.

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

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", mainly concerning 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?

Any help is appreciated ;)


回答1:


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

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.




回答2:


Yep. we converted that from the spec with only a single rule change is a demonstration that you could do it. We will optimize that one soon.



来源:https://stackoverflow.com/questions/19311864/antlr-4-parsing-large-c-file-takes-forever

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!