I'm writing a GLR for fun (again, because I understood a few things since my last try). The parser is working now and I am implementing disambiguation rules. I am handling precedence in a way that seems to work. Now I'm a bit at loss regarding associativity.
Say I have a grammar like this :
E <- E '+' E (rule 1)
E <- E '-' E (rule 2)
E <- '0' (rule 3)
E <- '1' (rule 4)
Where rules 1) and 2) have the same precedence and left associativity.
Without associativity handling, the string '1-1+0' will generate two parse trees:
1 2
/ \ / \
/ \ / \
2 3 4 1
| \ | \
4 4 4 3
Where numbers indicate the rule used for reduction. The correct parse tree is the first one and thus I would like to keep only this one.
I'm wondering how to efficiently detect associativity infringements algorithmically.
One approach I tried was to see that in the first tree, at the top node, rule 2 is LEFT of rule 3 in the list of children of rule 1, whereas in the second tree rule 1 is RIGHT of rule 4 and thus since rules 2 and 1 are LEFT associative I keep only the first tree.
This, however, did not get me very far in more complicated examples. A limitation of this solution is that I can only discard trees based on a comparison with another tree.
Do you think I can find a solution using a refined version of this approach? What is the standard way of doing?