Actually, your approach is wrong here. What you want to aim at in the parser is to accept all the syntax that can possibly be good but if it is not, can be rejected using semantic checks later. This is because a good semantic error such as "not able to use 'v2' when you utilize 'b'" is much more useful to your user than "Unexpected token 'v2' at....". Doing this will also simplify your grammar and usually make it a lot faster. So, just allow either of the options, set a flag and print the error message if it is incorrect. Then you have implemented only the syntax you wish in a much cleaner way. If you are producing an AST then it is usually easier to check for these things in a special semantic pass of the AST.
Jim From: antlr-interest-boun...@antlr.org [mailto:antlr-interest-boun...@antlr.org] On Behalf Of Naveen Chawla Sent: Tuesday, October 06, 2009 4:03 PM To: antlr-inter...@antlr.org Subject: Re: [antlr-interest] Help writing a particular semantic predicate Ok, I've solved my particular problem using token lookaheads, but I was wondering if there is a way of doing non-terminal symbol lookaheads in semantic predicates? 2009/10/6 Naveen Chawla <naveen.c...@googlemail.com> grammar x; x: a b; a: 'i' a? | c? 'j'; // Question, how would I write the semantic predicates: if this "a" is followed by 'v1' choose the first alternative, if not choose the second alternative c: 'i'; b: 'v1' | 'v2'; --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address