1

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.

Borneo777
  • 139
  • 2
  • 13

1 Answers1

1

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.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288