我是使用 antlr 4 的新手,以前从未写过语法。语法似乎适用于短样本,但我有一些较长的示例将在一夜之间运行而不会完成。我将 TestRig 与我的输入文件一起使用。我使用了 JVisualVM 并观察了大部分时间都花在这里的代码:
Org.antlr.v4.runtime.atn.ArrayPredictionContext.equals(Object) (89.8%)
Org.antlr.v4.runtime.atn.SingletonPredictionContext.equals(Object) (7.4%)
Org.antlr.v4.runtime.misc.DoubleKeyMap.put(Object,Object,Object) (1.8%)
Org.antlr.v4.runtime.misc.DoubleKeyMap.get(Object, Object) (0.9%)
该问题似乎与以下格式的陈述量有关。
reservedWord = quotedstring
在其中的 12 到 72 个之间的某个地方出现了问题
我给了jvm 2G的内存,它使用的内存不到1G。堆增长和缩小,所以我不相信有内存泄漏。
关于如何调试这样的东西的任何建议?
我看不到附加文件的方法,堆栈跟踪是 422 行,大部分重复 1278 和 1325。我不了解 TestRig,所以我不知道它在做什么。如果我能找到它,将获得 antlr-4.0-complete.jar 的源代码。
at org.antlr.v4.runtime.atn.PredictionContext.hashCode(PredictionContext.java:121)
at org.antlr.v4.runtime.atn.SingletonPredictionContext.equals(SingletonPredictionContext.java:97)
at java.util.HashMap.getEntry(HashMap.java:349)
at java.util.LinkedHashMap.get(LinkedHashMap.java:280)
at org.antlr.v4.runtime.misc.DoubleKeyMap.get(DoubleKeyMap.java:60)
at org.antlr.v4.runtime.atn.PredictionContext.mergeSingletons(PredictionContext.java:204)
at org.antlr.v4.runtime.atn.PredictionContext.merge(PredictionContext.java:138)
at org.antlr.v4.runtime.atn.ATNConfigSet.add(ATNConfigSet.java:354)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closure_(ParserATNSimulator.java:1293)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1278)
<repeat a lot 1278 and 1325>
at org.antlr.v4.runtime.atn.ParserATNSimulator.closure_(ParserATNSimulator.java:1325)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1278)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closure_(ParserATNSimulator.java:1325)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1278)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1261)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closure_(ParserATNSimulator.java:1325)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1278)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1261)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closure_(ParserATNSimulator.java:1325)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1278)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closure_(ParserATNSimulator.java:1325)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1278)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1261)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closure_(ParserATNSimulator.java:1325)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closureCheckingStopState(ParserATNSimulator.java:1278)
at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1216)
at org.antlr.v4.runtime.atn.ParserATNSimulator.computeReachSet(ParserATNSimulator.java:988)
at org.antlr.v4.runtime.atn.ParserATNSimulator.execATNWithFullContext(ParserATNSimulator.java:801)
at org.antlr.v4.runtime.atn.ParserATNSimulator.execATN(ParserATNSimulator.java:701)
at org.antlr.v4.runtime.atn.ParserATNSimulator.predictATN(ParserATNSimulator.java:389)
at org.antlr.v4.runtime.atn.ParserATNSimulator.adaptivePredict(ParserATNSimulator.java:346)
at ValidatingPolicyParser.showstatement(ValidatingPolicyParser.java:1023)
at ValidatingPolicyParser.statement(ValidatingPolicyParser.java:685)
at ValidatingPolicyParser.policy(ValidatingPolicyParser.java:246)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.antlr.v4.runtime.misc.TestRig.process(TestRig.java:247)
at org.antlr.v4.runtime.misc.TestRig.process(TestRig.java:209)
at org.antlr.v4.runtime.misc.TestRig.main(TestRig.java:142)
我开始研究精简的语法以用作测试用例。这个练习确实帮助我识别了我从语法中删除的一些奇怪的东西。这解决了我精简语法的问题,但是当我恢复完整的语法时,性能问题又回来了。
在我看来,增加简单比较的出现次数会非线性地增加处理时间,这真的很奇怪。'TOKEN = "SOMESTRING" OR TOKEN = "SOMEOTHERSTING"' 的几个实例处理相对较快,但如果我有 75 个这样的比较,处理时间就会变得太多。
我无法确定 antlr4 源是否在某处可用。很高兴单步执行代码以查看发生了什么。
我想我会重复这个过程,看看它会产生什么。