[antlr-interest] Help writing a particular semantic predicate

Jim Idle jimi at temporal-wave.com
Wed Oct 7 07:36:18 PDT 2009


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-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Naveen Chawla
Sent: Tuesday, October 06, 2009 4:03 PM
To: antlr-interest at 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.chwl at 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';

 



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091007/3b9c5729/attachment.html 


More information about the antlr-interest mailing list