I was told there is a algorithm that always resolves ambiguity for grammars that have issues with precedence and associativity. I know ambiguity in general is undecidable, so I only want to resolve those two sources of ambiguity (precedence and associativity).
As far as I know from reading this course, the heuristic I have developed are the following:
higher precedence = appear lower in the tree always (so they are never produced by any rule bellow a symbol that is higher up). A different way to understand this is; higher precedence operators are evaluated first, so they appear lower in the parse/computation tree.
right-associativity = when two operators of the same type fight for an argument if its right associative then the op on the right wins. Thus, this means that immediate recursion of the same production rule generating that op must always appear on the right (and that symbol/op can never be produced on the other side/left).
however, these rules must be incomplete or slightly wrong because when I was trying to resolve a “tricky” grammar I got wrong answers or couldn’t make sense of the right solution.
For example, imagine we have this:
<exp> ::= 0 | 1 | b<exp> | <exp>a | <exp>m<exp>
and we want to enforce:
Right associativity for
m and the following precedence inequality is the one we want: $ b >_p m >_p a$ which means b has higher precedence than m has higher precedence than a.
the solution given was:
<exp> ::= <no a m> | <not m>m<no a> | <exp>a <no a> ::= <no a m> | <no a m>m<no a> <not m> ::= <no a m> | <exp>a <no a m> ::= b<no a m> | 0 | 1
However, according to the “rules” I came up this must be wrong. But since its the instructor solutions to its safer to assume I am wrong and need to refine my understanding.
The reason I think its wrong is that the first rule there is a
<not m> to the left of
m. That means we can generate an
a has lower precedence than
m, so according to my rule it must never appear bellow the parse tree of
m, but it does. So it seems I do not understand precedence properly or the algorithm to enforce it.
My understanding is that precedence means that if you have higher precedence then you bind tighter than other operators, so you steal the arguments from others. I was also told that means that you appear lower in the parse tree, which intuitively makes sense. But that’s too vague for me to really unambiguously and formally define a way to resolve precedence in any grammar (assuming I only want to resolve precedence and associativity only and solving that “solves the grammar”).
So is my understanding correct? Can someone help me make my algorithms for resolving these issue more rigorous (and ideally bullet proof)?
Note, I am aware that resolving ambiguity is undecidable in general, but I was told precedence and associativity is not undecidable assuming thats all we want. So thats what Im looking for.