How to best implement "optional" in YACC? (Kent J Quirk)
Wed, 14 Dec 1994 20:36:15 GMT

          From comp.compilers

Related articles
How to best implement "optional" in YACC? (1994-12-14)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (Kent J Quirk)
Keywords: yacc, question, comment
Organization: The World Public Access UNIX, Brookline, MA
Date: Wed, 14 Dec 1994 20:36:15 GMT

I'm creating a YACC grammar to read a file type that has lots of optional
elements in it, but the elements have an order.

The spec looks something like this:

test: foo bar? bazz? buzz yark? yutz

where the stuff with ? may or may not be present. Sometimes I get like 5
or 6 optional items in a row. Is there a way to do this without
explicitly defining all the permutations?

Right now, I've chickened out and done something like:

test: foo optionalstuff buzz moreoptions yutz

optionalstuff: /* possibly empty */
| optionalstuff oneoption

oneoption: bar
| bazz

Are there better ways to handle this?

[Try this:
test: foo optbar optbazz buzz optyark yutz ;
optbar: bar | /*empty*/ ;
optbazz: bazz | /*empty*/ ;
optyark: yark | /*empty*/ ;

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.