Re: Help with Grammar Specification

brplummer@gmail.com
Thu, 4 Dec 2008 22:46:49 -0800 (PST)

          From comp.compilers

Related articles
Help with Grammar Specification brplummer@gmail.com (2008-12-04)
Re: Help with Grammar Specification brplummer@gmail.com (2008-12-04)
Re: Help with Grammar Specification bear@sonic.net (Ray Dillinger) (2008-12-05)
Re: Help with Grammar Specification cfc@shell01.TheWorld.com (Chris F Clark) (2008-12-08)
| List of all articles for this month |

From: brplummer@gmail.com
Newsgroups: comp.compilers
Date: Thu, 4 Dec 2008 22:46:49 -0800 (PST)
Organization: Compilers Central
References: 08-12-013
Keywords: LALR, parse
Posted-Date: 05 Dec 2008 10:16:36 EST

On Dec 4, 7:09 pm, brplum...@gmail.com wrote:
>
> What I want is to not require any particular ordering of the parts of
> this production. In other words I'd like to my last example to
> work...while minimizing the number of productions in my grammar.


> [My advice would be to make the parser accept an arbitrary list
> of the things that can occur in an enum, and diagnose multiple
> occurrences yourself. This both makes the grammar simpler, and
> allows you to produce better error messages, "multiple enum
> default not allowed" rather than "syntax error".
>
> This question of syntax where you can allow at most one each of
> an unordered list of possibilities comes up a lot, and this is
> the best solution I've found for LALR parsers.
>
> oneenum : enum '{' enumstuff '}' ;
> enumstuff: /* nothing */
> | enumstuff namespace
> | enumstuff default
> | enumstuff handlerclasses
> | enumstuff values ;
> -John]


Thanks for the reply.


I thought about this some more and asked myself, "where do we see this
kind of language construct in programming?" Answer: The C programming
language. So I looked at a C grammar and found what I was looking
for. Ended up with something very close to what you suggest. Thanks
again for taking the time to read my post and respond!!


Here is what I came up with:


oneenum : enum '{' descriptorlist '}' {
      namespaceHasBeenDefined = defaultHasBeenDefined =
valuesHasBeenDefined = false; // reset for next enum...
};


descriptor
      : namespace {
            if (namespaceHasBeenDefined) {
                  yyerror("namespace has already been defined.");
            }
            namespaceHasBeenDefined = true;
      }
      | default {
            if (defaultHasBeenDefined) {
                  yyerror("default has already been defined.");
            }
            defaultHasBeenDefined = true;
      }
      | handlerclass
      | values {
            if (valuesHasBeenDefined) {
                  yyerror("values has already been defined.");
            }
            valuesHasBeenDefined = true;
      }
      ;


descriptorlist
      : descriptor
      | descriptorlist descriptor
      ;



Post a followup to this message

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