[antlr-interest] Unexpected behavior - Error?

John B. Brodie jbb at acm.org
Tue Mar 9 06:19:02 PST 2010


Greetings!

On Tue, 2010-03-09 at 14:43 +0100, Christoph Schinko wrote:
> Hi!
> 
> Using the grammar below we experience unexpected behavior. The parser 
> accepts the following input line:
> 
> <<<<<<< .mine
> 
> without throwing an error - which it should - according to the grammar. 
> The grammar has been reduced to
> a toy example reproducing the error. Any thoughts on that?

ANTLR is simply doing what you told it to do!

> grammar JScriptDoc;
> 
> options
> {
>      output=AST;
>      backtrack=true;
>      memoize=true;
> }
> 
> source
>      : (statement)*
>      ;

so your source is permitted to be zero (or more) statements.

your input: "<<<<<<< .mine" contains zero statements.

ANTLR is cool with that and does nothing (as you have instructed it to
do).

you probably want to add EOF to your source rule in order to instruct
ANTLR to consume all of the input it is given. I also suspect that
source is really One (or more) statements -- an empty input can be
discovered without invoking the parser/lexer... So I think you probably
want your source rule to be this:

source : statement+ EOF! ; // does not include EOF in the AST...

> 
> statement
>      : statementEmpty
>      | statementBlock
>      ;
> 
> statementBlock
> 
>      : '{' (statement )* '}'
>      ;
> 
> statementEmpty
>      : ';'
>      ;
> 
....rest snipped....

Hope this helps
   -jbb




More information about the antlr-interest mailing list