1
votes

I've a simple ANTLR4 grammar :

grammar Test;
preprocessing_file: oneline+;

oneline: IDENTIFIER? new_line;

new_line: EOF|CRLF
;

WS: [ \t\f]+ -> channel(2);

CRLF: '\r'? '\n';


IDENTIFIER:  (NONDIGIT | DIGIT )+
; 
  fragment DIGIT: [0-9];
  fragment NONDIGIT: [_a-zA-Z]  ;

I was testing how to use a newline rule that allows the last line not being terminated by a CRLF. I tested the grammar with ANTLR v4.1 and v4.5.3.

An input file of a few lines of text caused ANTLR4 to freeze and OutOfMemoryException after a while. It looks like ANTLR4 entered some infinite loop. Is it a bug with ANTLR4? Did I do anything wrong? Also, if I remove the EOF in new_line rule, everything works fine.

2

2 Answers

3
votes

Just need to add an EOF to the main rule. Without it, the main rule will never terminate - the lexer will continue to produce EOF tokens that are successfully consumed by the new_line rule.

preprocessing_file: oneline+? EOF ;
oneline: IDENTIFIER? new_line ;
new_line: EOF|CRLF ;
0
votes

EOF [end of file] should not be in new_line rule. Just change

oneline: IDENTIFIER? new_line

into

oneline: IDENTIFIER? new_line?

validate the presence of new_line in later phases if needed to validate at all