# Re: How to handle operator of undefined associativity

## glen herrmannsfeldt <gah@ugcs.caltech.edu>

Wed, 5 May 2010 20:51:40 +0000 (UTC)

*From comp.compilers*

| List of all articles for this month |

**From: ** | glen herrmannsfeldt <gah@ugcs.caltech.edu> |

**Newsgroups: ** | comp.compilers |

**Date: ** | Wed, 5 May 2010 20:51:40 +0000 (UTC) |

**Organization: ** | California Institute of Technology, Pasadena |

**References: ** | 10-04-073 10-05-011 10-05-020 |

**Keywords: ** | parse |

**Posted-Date: ** | 09 May 2010 12:15:08 EDT |

glen herrmannsfeldt <gah@ugcs.caltech.edu> wrote:

*> BGB / cr88192 <cr88192@hotmail.com> wrote:*

(snip)

*>> but, with a unary operator, there is only a single possible parsing:*

*>> the operator and the expression it operates on.*

*> That is true if you allow only prefix operators, or only*

*> postfix operators, but not if you allow both. Consider C:*

*> *x++ is *(x++) or (*x)++*

*> Where * and ++ are both unary operators.*

(snip)

*> [This isn't associativity, it's precedence. Humph. -John]*

So now I have to actually get out my K&R2...

I know that it always works as *(x++), but why?

It seems that * and ++ are at the same precedence level, but associate

right to left. I haven't actually looked at the table in many years,

yet haven't had much problem with precedence and associativity.

(There is, of course, the well known low precedence of << and >>.)

This could be avoided by not having postfix and prefix

operators at the same precedence level. It seems that

C didn't do that.

(I haven't looked at the C99 precedence table, but I will

presume that they didn't change this.)

-- glen

[I happen to have C99 here, and they did change it. The grammar separates out

postfix-expression and unary-expression, with postfix-expression coming first.

Offhand, I can't think of any plausible examples where the C99 grammar would

parse an expression differently. -John]

Post a followup to this message

Return to the
comp.compilers page.

Search the
comp.compilers archives again.