title: Parsing Newlines, EOF as End-of-Statement Marker with ANTLR3
body: <p>My question is in regards to running the following grammar in ANTLRWorks:</p>
  
  <pre><code>INT :('0'..'9')+;
  SEMICOLON: ';';
  NEWLINE: ('\r\n'|'\n'|'\r');
  STMTEND: (SEMICOLON (NEWLINE)*|NEWLINE+);
  
  statement
      : STMTEND
      | INT STMTEND
      ;
  
  program: statement+;
  </code></pre>
  
  <p>I get the following results with the following input (with <em>program</em> as the start rule), regardless of which newline <em>NL</em> (CR/LF/CRLF) or integer I choose:</p>
  
  <p>"; <em>NL</em>" or "32; <em>NL</em>" parses without error.
  ";" or "45;" (without newlines) result in EarlyExitException.
  "<em>NL</em>" by itself parses without error.
  "456 <em>NL</em>", without the semicolon, results in MismatchedTokenException.</p>
  
  <p>What I want is for a statement to be terminated by a newline, semicolon, or semicolon followed by newline, and I want the parser to eat as many contiguous newlines as it can on a termination, so "; <em>NL NL NL NL</em>" is just one termination, not four or five. Also, I would like the end-of-file case to be a valid termination as well, but I don't know how to do that yet.</p>
  
  <p>So what's wrong with this, and how can I make this terminate nicely at EOF? I'm completely new to all of parsing, ANTLR, and EBNF, and I haven't found much material to read on it at a level somewhere in between the simple calculator example and the reference (I have The Definitive ANTLR Reference, but it really <em>is</em> a reference, with a quick start in the front which I haven't yet got to run outside of ANTLRWorks), so any reading suggestions (besides Wirth's 1977 ACM paper) would be helpful too. Thanks!</p>