help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Match at least one?


From: John Levine
Subject: Re: Match at least one?
Date: 5 Apr 2011 13:54:42 -0000

>I'm guessing that I can't do it with the grammar itself and that I'll
>have to do some post processing once msg is matched.

You can in principle do it by factoring out the list into different
rules that track which of the mandatory items you've seen so far.
Here it is with two mandatory items A and B and optional item X:

---- snip ----
%token MSG34 TERMINATOR
%token A B X
%%
msg: MSG34 blocks TERMINATOR  ;

xl: /* empty */ | xl X ;

al: xl A | al A | al X ;

bl: xl B | bl B | bl X ;

abl: al B | bl A | abl A | abl B | abl X ;

blocks: abl ;

%%
---- snip ----

To do three items, you need to add more rules cl, acl, bcl, and abcl.

BUT don't do that.  Your users will be much happier if your grammar just
builds a list and semantic code checks to see whether all the items
are present.  That allows you to produce useful errors like "required
block BLK7 not present" rather than a mystifying "syntax error".

R's,
John





reply via email to

[Prev in Thread] Current Thread [Next in Thread]