[antlr-interest] issues with setting flags in rule action and used in predicates

An Liu anliu.sh.cn at gmail.com
Wed Jan 4 01:38:39 PST 2012


The following is an example from my grammar:

r1 returns [bool flag1]
@init {}
    :
    subRule1 { $flag1 = true; }
    ;

r2
@init {}
    :
    r1
    { $r1.flag1 }?
    subRule2

The issue is { $flag1 = true; } is only executed when backtracking is 0. If
r2 and r1 are called in a predicate (where backtracking > 0), the action
{$flag1 = true;} won't be executed. Therefore the predicate { $r1.flag1 }?
in r2 will get a wrong value. Is there already a pattern to deal with this
situation?

Thanks in advance.
Liu An


More information about the antlr-interest mailing list