Borneo777
Borneo777

Reputation: 181

Strange behaviour of ANTLR3

Why does grammar presented in this answer https://stackoverflow.com/a/1932664/5613768 accept expression like this : 2(38) ?? I know why 12*(5-6) is accepted and why 12*(5-6 is not accepted but I can't explain this behaviour.

Upvotes: 1

Views: 31

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170308

It doesn't accept the entire input. It stops parsing after the 2 because the eval rule:

eval
    :    additionExp
    ;

matches 2 as a additionExp and then stops since the rest of the input cannot be matched.

If you "anchor" the eval rule so that it must consume the entire token stream like this:

eval
    :    additionExp EOF
    ;

you will see an error on your console.

Upvotes: 1

Related Questions