I'm really stuck with a Shift-Reduce conflict in Goldparser.
I wrote a PHP-like grammar that theoretically should be able to parse the following script:
public $Test = null;
protected $bDemo = true;
function Main()
{
}
private function Run()
{
}
At the top I want to assign the global variables and after that come the function definitions.
To narrow down the problem I reduced my large grammar to the following lines which can reproduce the error. Obviously this is incomplete. The functions have no parameters, no return value and no statements...
"Start Symbol" = <Script>
! ------------------------------------------------- Sets
{Name} = {Letter} + {Alphanumeric} + [_]
! ------------------------------------------------- Terminals
VarName = '$' {Name}*
FuncName = {Name}*
! ------------------------------------------------- Rules
<Variable> ::= VarName
<Value> ::= 'null'
| 'true'
| 'false'
<Modifier> ::= 'private'
| 'protected'
| 'public'
<ModifierOpt> ::= <Modifier>
|
! ---------------
<GlbAssignVar> ::= <ModifierOpt> <Variable> '=' <Value> ';'
<GlobalList> ::= <GlobalList> <GlbAssignVar>
| <GlbAssignVar>
<GlobalListOpt> ::= <GlobalList>
|
! ---------------
<FuncDef> ::= <ModifierOpt> 'function' FuncName '(' ')' '{' '}'
<FuncList> ::= <FuncList> <FuncDef>
| <FuncDef>
<FuncListOpt> ::= <FuncList>
|
! ---------------
<Script> ::= <GlobalListOpt> <FuncListOpt>
When building the LALR Table Goldparser tells me:
"A Shift-Reduce Conflict was fixed 'private', 'protected', 'public' can follow a completed rule and also be shifted. The conflict was resolved by selecting the 'shift' action over the 'reduce'. Be careful, some parts grammar may not be accessible. It is recommended that you attempt to remove all conflicts."
But the fix it applies makes that the grammar does not work correctly. In the above example I get a syntax error at function Main()
where it expects a 'private
', 'protected
' or 'public
' although I declared them as optional.
The error disappears when I remove the <ModifierOpt>
from either the <FuncDef>
definition or from the <GlbAssignVar>
definition.
I have no idea how to solve this. Please help!