当使用ANTLR时,我一直看到的一个问题是,当在数组中测试大量数据时,会出现最大递归深度错误。
产生错误的文件的内容看起来像:
const unsigned char foo[] = {
99,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,
0,64,0,0,0,115,248,1,0,0,100,0,90,0,100,91,
90,1,100,92,90,2,101,2,101,1,23,0,90,3,100,4,
100,5,132,0,90,4,100,6,100,7,132,0,90,5,100,8,
100,9,132,0,90,6,100,10,100,11,132,0,90,7,100,12,
100,13,132,0,90,8,100,14,100,15,132,0,90,9,100,16,
100,17,132,0,90,10,100,18,100,19,132,0,90,11,100,20,
100,21,132,0,90,12,100,93,100,23,100,24,132,1,90,13,
101,14,101,13,106,15,131,1,90,16,100,25,106,17,100,26,
100,27,131,2,100,28,23,0,90,18,101,19,106,20,101,18,
100,27,131,2,90,21,100,29,90,22,100,30,90,23,100,31,
103,1,90,24,100,32,103,1,90,25,101,25,4,0,90,26,
90,27,100,94,100,33,100,34,156,1,100,35,100,36,132,3,
90,28,100,37,100,38,132,0,90,29,100,39,100,40,132,0,
90,30,100,41,100,42,132,0,90,31,100,43,100,44,132,0,
90,32,100,45,100,46,132,0,90,33,100,47,100,48,132,0,
90,34,100,95,100,49,100,50,132,1,90,35,100,96,100,51,
100,52,132,1,90,36,100,97,100,54,100,55,132,1,90,37,
...
};
单个文件包含一个数组,其中包含2432行数据,没有其他数据。解析数组时输出的错误为:
File "../antlr4/tree/Tree.py", line 147, in walk
self.walk(listener, child)
File "../antlr4/tree/Tree.py", line 147, in walk
self.walk(listener, child)
File "../antlr4/tree/Tree.py", line 145, in walk
self.enterRule(listener, t)
File "../antlr4/tree/Tree.py", line 159, in enterRule
ctx.enterRule(listener)
File "../Parser.py", line 14461, in enterRule
listener.enterInitializerlist(self)
RuntimeError: maximum recursion depth exceeded
ANTLR版本=4.8
有人知道为什么会发生这种情况吗?或者有什么建议/解决方法吗?我正在为ANTLR4使用Python绑定。
我正在使用语法文件:
https://github.com/antlr/grammars-v4/blob/master/c/C.g4
然后我做:
java -cp ./etc/antlr-4.8-complete.jar org.antlr.v4.Tool -Dlanguage=Python2 -o ./src/autogen ./grammars/C.g4 -visitor
它产生了以下文件,我使用
src/autogen/grammars/C.tokens
src/autogen/grammars/CLexer.py
src/autogen/grammars/CLexer.tokens
src/autogen/grammars/CListener.py
src/autogen/grammars/CParser.py
src/autogen/grammars/CVisitor.py
对于偶然发现这个问题的人来说:答案是"隐藏的";在kaby76:提供的评论中
initializerList
是递归的,而不是EBNF。这就是它爆炸的原因。这样做:
initializerList : designation? initializer ( ',' designation? initializer)* ;