[antlr-interest] Problem with EBNF

Morten Olav Hansen mortenoh at gmail.com
Sun Dec 5 09:09:15 PST 2010


Hi!

I have a problem with my EBNF I was hoping for a little help with. I
have a block in my grammar that can contain certain keywords
zero-or-one times, and other keywords zero-or-many times. My current
solution is to enable every keyword to be zero-or-many, and then let
my semantic checker deal with the problem. But I was hoping to solve
it already on the grammar side, if possible.

The basic block looks like this:

	:	'region' ID=Identifier? '{'
			(
				psinitial=psinitialDecl?
				finalstate=finalstateDecl?
				pshistory=pshistoryDecl?
				psdeephistory=psdeephistoryDecl?
				states+=stateDecl*
				transitions+=transitionDecl*
			)
		'}' ';'?

And the only solution I have come up with, is to generate every
possible variant of this grammar, which is quite ugly.

What would be nice, would be something like this:

	:	'region' ID=Identifier? '{'
			(
				psinitial=psinitialDecl?
				finalstate=finalstateDecl?
				pshistory=pshistoryDecl?
				psdeephistory=psdeephistoryDecl?
				states+=stateDecl*
				transitions+=transitionDecl*
			)*
		'}' ';'?

(with * at the end). And for every match to one of the zero-or-one
rule, it would take it "away", so it can not be matched again. But
this does not work.

Any suggestions on how to solve this? If I have to end up with every
possible rule combination, then I would probably be better of just
doing it in the semantic checker as I was doing.

Regards,
Morten


More information about the antlr-interest mailing list