[antlr-interest] Rule parameter troubles
Jim Idle
jimi at temporal-wave.com
Sat Nov 28 11:35:29 PST 2009
When you use the predicate, it is being hoisted into other rules, where it is out of context. So, rather than pass as a parameter, use in a scope at a high enough rule level and then use the scope reference in your predicate.
Also, if you can, allow all syntax through in the parser even if it makes no semantic sense, and reject it with a better error message at a later semantic phase (usually a tree parser). Then you won't need the flags unless allowing such a syntactic construct means an ambiguous grammar of some kind. Basically you are trying to trade "Syntax error at 'wtf'" for "You cannot have more than one 'wtf' constructs with a 'foo'"
Jim
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Andrew Bradnan
Sent: Saturday, November 28, 2009 11:14 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Rule parameter troubles
I was trying to use a rule parameter and can't call another rule with that parameter when I use +. An example will make more sense.
test [bool wtf] returns [int n]:
{$wtf}?=>'wtf' test2[$wtf]+; // attribute is not a token, parameter, or return value: wtf
test [bool wtf] returns [int n]:
{$wtf}?=>'wtf' test2[$wtf]; // works fine
test [bool wtf] returns [int n]:
{$wtf}?=>'wtf' test2[false]+; // works fine as well
test2 [bool wtf2]:
{$wtf2}?=>'foobar';
If I take the gated semantic predicate out of test2, then all the above work.
test2 [bool wtf2]:
'foobar'; // works for all the above
What did I miss?
Thanks
--
/Andrew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091128/89d60cff/attachment.html
More information about the antlr-interest
mailing list