More parser theory
Suppose I have an expression grammar in which the multiplication operator *
and the addition operator +
have their usual relative priority (namely, *
binds more tightly, i.e. is evaluated first). Then suppose I – perhaps unwisely – introduce a prefix operator, which I'll call PFX
for want of a better name, which has intermediate priority between the two, so that
PFX a + b
behaves like(PFX a) + b
, i.e. thePFX
is evaluated firstPFX a * b
behaves likePFX (a * b)
, i.e. thePFX
is evaluated second.
PFX
appears on the right of another operator. Specifically, what would you imagine happens to this expression:a * PFX b + cin which you can't process the operators in priority order (
*
then PFX
then +
) because the PFX
necessarily has to happen before the *
.In what order should the parser process those operators?
PFX then * then + to give (a * (PFX b)) + c
5 (50.0%)
+ then PFX then * to give a * (PFX (b + c))
0 (0.0%)
PFX then + then * to give a * ((PFX b) + c)
0 (0.0%)
None! Report a parse error and demand some disambiguating parentheses.
4 (40.0%)
Low-priority prefix operators misparsed my grandparents, you insensitive clod
1 (10.0%)
https://groups.google.com/forum/#!topic/scala-sips/ARVf1RLDw9U
Although we know that PFX a * b = PFX(a*b), we don't know that * is commutative (ie a * b = b *a ) so we cannot assume that PFX(a*b)=PFX(b*a)
or that a * PFX b = PFX b * a.
Re: https://groups.google.com/forum/#!topic/scala-sips/ARVf1RLDw9U
Sorry about the URL title.
I was trying to add that https://groups.google.com/forum/#!topic/scala-sips/ARVf1RLDw9U *might* be discussing a similar question, but got stuck trying to copy-n-paste on a tablet (I'm a heavy middle+right mouse button user) and failed to clean up properly when giving up.
no subject