Mats
Mats

Reputation: 1

ANTLR4 - parse a file line-by-line

I try to write a grammar to parse a file line by line.

My grammar looks like this:

grammar simple;

parse: (line NL)* EOF
line    
     : statement1     
     | statement2 
     |            // empty line           
     ;

statement1 : KW1 (INT|FLOAT);
statement2 : KW2 INT;

... 

NL          : '\r'? '\n';
WS          : (' '|'\t')-> skip; // toss out whitespace

If the last line in my input file does not have a newline, I get the following error message:

line xx:37 missing NL at <EOF>

Can somebody please explain, how to write a grammar that actually accepts the last line without newline

Upvotes: 0

Views: 1608

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99879

Simply don't require NL to fall after the last line. This form is efficient, and simplified based on the fact that line can match the empty sequence (essentially the last line will always be the empty line).

// best if `line` can be empty
parse
  : line (NL line)* EOF
  ;

If line was not allowed to be empty, the following rule is efficient and performs the same operation:

// best if `line` cannot be empty
parse
  : (line (NL line)* NL?)? EOF
  ;

The following rule is equivalent for the case where line cannot be empty, but is much less efficient. I'm including it because I frequently see people write rules in this form where it's easy to see that the specific NL token which was made optional is the one following the last line.

// INEFFICIENT!
parse
  : (line NL)* (line NL?)? EOF
  ;

Upvotes: 1

Related Questions