[antlr-interest] help with error recovery

Joe stelmach joestelmach at gmail.com
Wed Feb 17 07:59:24 PST 2010


I'm looking for some help implementing a custom error recovery strategy.

Consider the following grammar which accepts strings of the form
"ab--ab--cd--"..., and generates flat AST's of the form: GROUP["ab"]
GROUP["ab"] GROUP["cd"]...

grammar Test;

options {
  output=AST;
}

tokens {
  GROUP;
}

foo
  : (bar '--')+ -> bar+
  ;

bar
  : (('a' 'b') | ('c' 'd')) -> GROUP[$bar.text]
  ;

Now suppose we feed the parser the input string "ab--ac--cd--".  I
would like the resulting AST to look like: GROUP["ab"] GROUP["cd"]
corresponding to the first "ab" and the last "cd" of the input string.
 In other words, when the parser starts to match a bar rule but fails
(as it will when it encounters the first 'c' token in our example
input,) I'd like to scan past all tokens until the next '--' token,
and then tell the parser to back up to the state it was in just after
encountering the first 'b' token.

I'm able to over-ride what I think to be the appropriate methods of
BaseRecognizer, and I understand how to scan past and consume the
tokens I don't care about, but I'm unsure of how to direct the parser
back to the previous state (or if it's even possible.)

Any help would be appreciated.

 - Joe


More information about the antlr-interest mailing list