help-bison
[Top][All Lists]
Advanced

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

Simplifying a "recursive, zero-or more" - grammar


From: Frans Englich
Subject: Simplifying a "recursive, zero-or more" - grammar
Date: Sun, 19 Feb 2006 22:04:09 +0000
User-agent: KMail/1.8.50

Hi everyone,

I'm pondering about how to write something in the cleanest/most efficient way. 
Yes, I know, it's basic stuff, but neveretheless. Look at this grammar:

------------------------------------------------------
FirstProlog: /* empty */
| FirstPrologPart FirstPrologPart
| FirstPrologPart

FirstPrologPart: DefaultNamespaceDecl
| Setter
| NamespaceDecl
| Foo
| Bar
------------------------------------------------------

In other words, this grammar (intentionally) allows zero or more 
DefaultNAmespaceDecl, Setter or NamespaceDecl in arbitrary order. For 
example: "Setter, Setter, NamespaceDecl", or simply "<empty>".

Here's another version, which as far as I can tell, achieves the same:

------------------------------------------------------
FirstProlog: /* empty */
| FirstProlog DefaultNamespaceDecl
| FirstProlog Setter
| FirstProlog NamespaceDecl
| FirstProlog Foo
| FirstProlog Bar
------------------------------------------------------

The second version is perhaps more compact while on the other hand being 
repetitive. I wonder, what is the best way to write the above? What is 
fastest? What is most beautiful? What do you prefer and why?

Also, is it possible to with Bison grammar specify that an terminal may appear 
in any order among other terminals, but it may not occur more than once? For 
example, in the above grammar, that "NamespaceDecl" can only appear once(be 
it after two Setters or between a DefaultNamespaceDecl and a Setter, for 
example). Of course, if such things aren't possible I can always add business 
logic and error out if a violation is detected, but it would be nice to push 
it down in the grammar.


Cheers,

                Frans




reply via email to

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