[antlr-interest] why is second input line not matched?

Bryan Ewbank ewbank at gmail.com
Sat Jan 8 03:35:51 PST 2005


The short answer is that the parser stops when it sees something that
doesn't match, and whitespace after a semicolon is not allowed in the
input grammar.  If you used a loop in your main, you would see a
syntax error on the second attempt to call the "parse" production.

--- --- ---

The (perhaps better) answer is a bit more long-winded:

First, add EOF to the end of the parser production if your intention
is to parse a whole file.

If you now rebuild and run your testcase, you will get a syntax error
because there is whitespace (a newline) after the semicolon on the
first line, and that does not match anything in your grammar.

The easiest way to fix this is to hide whitespace from the parser:
(1) Remove reference to DELIM from the parser
(2) Add the following at the end of the production for DELIM in the lexer:
   { $setType(antlr::Token::SKIP); }
This will cause the lexer to eat all DELIM (whitespace), so the parser
never sees it.

Your ANTLR should now eat your input file.

Hope this helps,
- Bryan


More information about the antlr-interest mailing list