How to define an "everything else" parser rule to catch illegal extra

StackOverflow https://stackoverflow.com/questions/23505893

  •  16-07-2023
  •  | 
  •  

문제

So I have my parser up and running. It works great. Now I find that, it parses only the "legal" part of the file and totally ignores the illegal extra.

Here is an example:

grammar myrule;

myrule: a_rule+;

a_rule: Open_Brace  Lower_Case_Char+  Close_Brace;

Open_Brace: '{';

Lower_Case_Char: 'a'..'z';

Close_Brace: '}';

when I feed it with "{ abc }", it parses great as expected.

But when I feed with "{ abc } extra extra", it happily ends without reporting the illegal part, "extra extra".

How to catch the illegal part?

I am using ANTLR v3.4.

도움이 되었습니까?

해결책

Just add EOF to the end of your start rule.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top