[antlr-interest] Backtracking doesn't work with semantic predicates?

Steve Bennett stevagewp at gmail.com
Wed Feb 27 05:51:44 PST 2008


Hi all,
  I found a rule in my grammar that doesn't work when backtracking is
turned on. Stripping absolutely everything down to the barest
essentials, this is the issue:

grammar bolditalics;
options { output=AST; memoize=true; backtrack=false;}

@members {
  boolean textis(String mw) {
    return input.LT(1).getText().equalsIgnoreCase(mw);
  }
}

bold_and_italics:
     {textis("''") }? => APOSTROPHES
    |{textis("'''") }? => APOSTROPHES
;


APOSTROPHES: '\''+;


This grammar matches the sentence "''" (two apostrophes) ok. Switch
backtracking on and it fails with this exception:

line 1:0 no viable alternative at input ''''

Anyway, I guess the workaround is clear: switch backtracking off
temporarily at that rule. But I thought I would report it.* If this is
a known issue, could someone enlighten me as to the cause?

Steve
* Ok, I only just found the workaround while writing this...:)


More information about the antlr-interest mailing list