I have the following bison grammar (as part of a more complex grammar):
classDeclaration : CLASS ID EXTENDS ID LBRACE variableDeclarationList methodDeclarationList RBRACE
;
variableDeclarationList : variableDeclarationList variableDeclaration
| /* empty */
;
variableDeclaration : type ID SEMICOLON
;
type : NATTYPE | ID
;
methodDeclarationList : methodDeclarationList methodDeclaration
| /* empty */
;
methodDeclaration : type ID LPAREN parameterDeclarationList RPAREN variableExpressionBlock
;
which is supposed to describe class declarations which look like this:
class foo extends object
{
nat number;
nat divide(nat aNumber)
{
0;
}
}
or this:
class foo extends object
{
nat divide(nat aNumber)
{
0;
}
}
or this:
class foo extends object
{
}
Problem is that there is ambiguity where variable declarations end and method declarations begin (2 shift/reduce conflicts). For example, the method declaration looks like a variable declaration until it sees the parenthesis.
How can I rewrite this grammar to eliminate this ambiguity?
To clarify: the class body can be empty, the only constraint is that variable declarations come before method declarations if there are any.
Another way to do it is to not even have empty rules, and instead use multiple options, one with the nonterminal and one without.