Hi,
I'm using the LL(*) Parser Framework and implementing the grammar. But I've been struggling with removing ambiguities.
The following is the simple version of my grammar code.
statement.Production = assignmentStatement | invocation;
assignmentStatement.Production = variable + @assign + expression + @semicolon;
invocation.Production = variable + @openParen + paramAssignmentList.Optional() + @closeParen + @semicolon;
variable.Production = @identifier | multiElementVariable;
multiElementVariable.Production = @identifier + (subscriptList | (@dot + @identifier)).OneOrMore();
subscriptList.Production = @openSquareBrace + expression + (@comma + expression).ZeroOrMore() + @closeSquareBrace;
paramAssignmentList.Production = expression + (@comma + expression).ZeroOrMore();
Although I did not show in the above code to keep it concise, I'm using Core (built-in) Tree Construction to build an AST.
I've already created a Can-Match callback for the multiElementVariable:
private bool CanMatchMultiElementVariable(IParserState state) {
if (state.TokenReader.AreNext(TokenId.Identifier, TokenId.OpenSquareBrace)) {
return true;
}
if (state.TokenReader.AreNext(TokenId.Identifier, TokenId.Dot)) {
return true;
}
return false;
}
The problem is that an assignmenStatement and invocation both start with a variable so I need to create a Can-Match callback for the assignmentStatement or invocation. But a variable can be complex when it is a multiElementVariable.
When the variable is a multiElementVariable, an assignmentStatement can be like this:
x.y[1].z = 1;
And an invocation can be like this:
x.y[1].z();
I tried some approaches to create an appropriate Can-Match callback but nothing could solve the problem.
I'd appreciate it if you could give me advice.