I am trying to define a non terminal symbol in a LALR(1) grammar (with CUP parser). It is requested that
the <code> token must appear exactly twice, while the <hour> token must appear at least once.
In the end I came up with this definition:
section ::= hour_l CODE SC hour_l CODE SC hour_l ; hour_l ::= /* epsilon */ | hour_l HOUR SC ;
SC is a separator (semicolon) between tokens and
hour_l is the non terminal symbol for hour's list.
This solution has a problem:
HOUR can be absent, because
epsilon can be reduced from
Is there a clever solution other than specifying all possibilities or using the semantic capabilities of CUP (ie. putting a counter of how many times
HOUR is present in
section)? I'd prefer not to use semantics in order to achieve this; in fact, it seems to me this is syntax related.