OutOfMemoryError when parsing incorrect input in ANTLR

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 09:29:58

This generally happens when a lexer contains a rule that can match the empty string. For example, consider the following rule:

WS : (' ' | '\t')*;

This rule can create a WS token containing a total of 0 space and/or tab characters, which means there can be an infinite number of them between any other tokens in your input. During some situations involving invalid input, the error recovery process can be forced into an infinite loop which will buffer tokens until Java runs out of memory.

The first step to solving this situation is examining every lexer rule to make sure this can't happen. The WS should instead be written like this to ensure that at least 1 space and/or tab characters are consumed.

WS : (' ' | '\t')+;

PS: ANTLR 4 performs a static check on the grammar to produce an error (4.0) or warning (4.0.1+) if this occurs.

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