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

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

  •  16-07-2023
  •  | 
  •  

Domanda

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.

È stato utile?

Soluzione

Just add EOF to the end of your start rule.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top