[antlr-interest] Please help with a grammar issue

Jim Idle jimi at temporal-wave.com
Fri Oct 30 11:25:44 PDT 2009


You don’t want to try and express this in the grammar at all, nor have predicates etc as you will not get good error messages or recovery. 

 

All you need do in fact is code the grammar to accept any number of each option, keep track of how many you find and issue good semantic errors such as “a b block cannot be repeated …” and so on. Something like this:

 

stuff 

scope

{

 int aCount = 0;

 int bCount = 0;

 int cCount = 0;

}

: abc*

  {

    If (cCount == 0) { /* message about having to have a c block */ }

  }

;

 

abc

: a {$stuff::aCount++; }

| b { $styff:bVount++; if ($stuff::bCount > 1) { /* message about b blocks being repeated */ } }

| c {$stuff::cCount++; }

;

 

Generally people try to encode to much of the structure in to the grammar and it is the opposite way around that gives you better error messages and analysis.

 

Jim

 

From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Frank Du
Sent: Friday, October 30, 2009 9:23 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Please help with a grammar issue

 

Dear All,

 

I have a grammar issue and need your help. Let’s say I have tree rules: a, b, c

 

Rule a occurs multiple times, rule b occurs one time or none, and rule c occurs one time.

 

My question is: How to write the grammar, so the rule ordering is arbitrary? 

 

Thank you so much! Have a nice day!

 

Best Regards,

Frank

 



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091030/78335eff/attachment.html 


More information about the antlr-interest mailing list