[antlr-interest] Possible bug with backtrack-generated predicate methods
Bart Kiers
bkiers at gmail.com
Sat Nov 26 00:20:23 PST 2011
Hi Franck,
On Sat, Nov 26, 2011 at 8:54 AM, franck102 <franck102 at yahoo.com> wrote:
> The grammar below won't compile, this looks like a bug to me?
> ...
No bug, syntactic predicates and rule parameters can't be mixed. You can
use rule scopes instead:
---------------------------------
grammar Test;
options {
output=AST;
backtrack=true;
ASTLabelType=CommonTree;
}
program
scope { String x; }
@init { $program::x = null; }
: 'raw'? (ID {$program::x=$ID.text;} -> ID) (rule -> rule)*
| 'raw' ID
;
rule
: 'some' ID {System.out.println("called from: " + $program::x);}
;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
WS : (' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;};
---------------------------------
If you now parse "raw A some B", then "called from: A" will be printed to
your console.
Also, you're trying to pass the tree of `program` as a parameter, but that
tree hasn't been constructed yet, AFAIK (and will therefor be `null`).
That's why my example shows how to use rule scopes with a simple string.
Regards,
Bart.
More information about the antlr-interest
mailing list