[antlr-interest] A simple question regarding statement clauses

Lance Gutteridge lance at thinkingworks.com
Mon Nov 21 10:45:25 PST 2005


Sorry if this question has been asked before.
I'm using ANTLR to parse a language that has various statements. A common 
case is when I have several clauses in the statement but I want them to be 
optional, in any order and only one of each.

So for example the report statement can take four margin clauses.
eg. report left margin 20 right margin 40 top margin 30 bottom margin 30 ;
So I can say in ANTLR:
REPORT (MARGIN_CLAUSE)*
where
MARGIN_CLAUSE: (MARGIN! LEFT^ AMOUNT) | (MARGIN! RIGHT^ AMOUNT) | (MARGIN! 
TOP^ AMOUNT ) | (MARGIN! BOTTOM^ AMOUNT) ;

But that would allow
REPORT LEFT MARGIN 20 LEFT MARGIN 20

I could put the clauses in as optional
something like:
REPORT (LEFT_CLAUSE)? (RIGHT_CLAUSE)? (TOP_CLAUSE)? (BOTTOM_CLAUSE)?

That would make sure there is only one of each but it forces them to be in 
that exact order.

I could allow multiples and just take the last specification and have it 
override any previous ones, but that seems somewhat cheesy.

So my question is, what is the easiest way to specify that I want the 
statement to have any or none of these margin clauses, in any order, but no 
more than one of each.

Thanks
Lance




More information about the antlr-interest mailing list