Kaleb, Thanks for your response. Although I've read the web page you referenced and the ANTLR book chapter on semantic predicates, I remain with just as many questions as I started out with.
Note that the point of my example is to understand how semantic predicates can be used in this sort of context. I am not interested in a solution to the toy problem that doesn't use semantic predicates. There are obviously simpler ways of solving the toy problem, but the example is meant as a vehicle for understanding how to use semantic predicates to solve more complicated problems. On Oct 16, 2009, at 3:35 PM, Kaleb Pederson wrote: > On Fri, Oct 16, 2009 at 10:39 AM, Robert Wentworth > <b...@wentworth.bz> wrote: >> There appears to be something fundamental that I am not "getting" >> about the way semantic predicates work. In the grammar below, the >> input "cat" is recognized using the rule "cat" but in not recognized >> by either the rule "expr" or the rule "pexpr". Could someone explain >> to me why this is so and what the fix is? > > [... snip ...] >> cat : {input.LT(1).getText().equals("cat")}?=> WORD ; >> >> dog : {input.LT(1).getText().equals("dog")}?=> WORD ; > > You're using a gated semantic predicate. A gated semantic predicate > is used to distinguish between multiple alternatives, but as you have > only one alternative, this isn't what you want. My understanding was that gated predicates are hoisted into rules that indirectly reference them, at which point they should influence the decision in "expr". > See > https://wincent.com/wiki/ANTLR_predicates for more information on > predicates. > >> expr : cat >> | cat WHEN dog >> | dog > > You could do: > > expr: cat (WHEN dog)? | dog; In any event, this doesn't match "cat" either, retaining the old definition of the rule "cat" with the semantic predicate. (And taking out the semantic predicate would change the grammar.) > > You should also be able to use a syntactic predicate: > > expr: (cat WHEN)=> cat WHEN dog > | cat > | dog; Isn't this done implicity, without the explicit syntactic predicate? > > HTH, > > -- > Kaleb Pederson > Twitter - http://twitter.com/kalebpederson > Blog - http://kalebpederson.com > List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to il-antlr-interest@googlegroups.com To unsubscribe from this group, send email to il-antlr-interest+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en -~----------~----~----~----~------~----~------~--~---