[antlr-interest] Re: Syntactic predicate confusion
lgcraymer
lgc at mail1.jpl.nasa.gov
Thu Feb 12 17:32:21 PST 2004
--- In antlr-interest at yahoogroups.com, Christopher Nebel <c.nebel at a...> wrote:
> Apparently I don't quite "get" syntactic predicates. Does the
> predicate imply that the attached alternative will fire *only* if the
> predicate is true, or it only apply the predicate if the lookahead is
> otherwise ambiguous? It appears to be the former, which seems lame.
> Here's my situation:
>
> x: a | b
> a: c | b d
Well, actually neither is quite correct. Using your example (below)
>
> (The real thing is far more complicated, but this is the basic
> problem.) Naturally, I get an ambiguity warning here -- if it sees the
> stuff predicted by "b", it doesn't know whether to follow x-> a-> b e
> or just x-> b. So, I put in a predicate:
>
> x: (b d)=> a | b
> a: c | b d
ANTLR reasons as follows
1.) Could it be a (normal alternative testing precedes synpred testing)?
2.) If yes, does it match (b d)?
3.) If and only if yes, it is an a; ignore all other alternatives.
>
> This doesn't work, however: if I give it something c-like, then it
> can't parse it -- it just completely ignores the a-> c production. If
> I add a duplicate of x-> a without the predicate, I just get more
> ambiguity warnings. What to do? I don't want to break up the "a"
> rule, since the real thing is rather complicated, not to mention
> self-referential. I suppose I could come up with a syntactic predicate
> for the duplicate x-> a rule, but it's going to be sort of large.
> Suggestions?
If you can identify a minimal synpred, you can do something like adding a rule a_prime to be used just for the synpred:
a_prime
:
c | b B
;
and change your rule to
x :
(a_prime)=> a
| b
;
As long as a_prime is not otherwise referenced, you won't have any problems. One of the nice features of LL parsing is that you can
have "duplicate" versions of rules--unlike most LR parsers, if you expect an a in an LL parser, you only look for a. That is useful for
attaching different sets of actions to the same syntactic phrase to get context-dependent semantics.
--Loring
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list