Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Once a mismatch is detected, the generated code causes the target language equivalent of the Java Runtime's recognitionException RecognitionException to be thrown or otherwise handled. If you examine a method generated for a parser rule in the Java target, you will see that the rule logic is encapsulated within a try {} catch {} block, which catches RecognitionException:

...

The second thing that happens after reporting the error is that the recover() method is called - it is this method that attempts to resync the token input stream to some place that makes sense within the context of the current parser rule and its parsing point within that rule. In order to do this, it must compute a Follow Set for the current parsing point - note that I am trying to avoid technical definitions and jargon here in favor of comprehensionunderstanding. A follow set is basically a list of all the tokens that would be valid if they were seen at the this point in the parse. The recover() method then basically consumes tokens from the input stream until it locates a token that exists within this set. It may consume no tokens if it feels that there is actually a token missing.

Implementing Your Own Exception Handler

...