Operator Precedence

Almost every ProgrammingLanguage supporting InfixNotation for expressions has extensive operator precedence levels. This is required to disambiguate expressions: does a + b * c mean (a + b) * c or a + (b * c)? And does it mean the same thing as c * b + a?

Here are the precedence tables of some well-known languages:

CeeLanguage (from highest to lowest):

PascalLanguage (from highest to lowest):

SmalltalkLanguage (highest to lowest):

All other ambiguity can is resolved by using left-to-right associativity. Thus, "1+1*2" would be parsed as "(1+1)*2", and "1<<2+3" as "(1<<2)+3".

LispLanguage gets along just fine without OperatorPrecedence. Well, OK, I suppose you could consider some macro characters to be unary operators with high precedence. :) -- DanMuller

ForthLanguage also does without precedence. Expressions strictly using PrefixNotation or PostfixNotation don't need it. (This is not strictly true... some words are IMMEDIATE and either parse the word(s) ahead of them or do some control flow magic at compile time.) -- IanOsgood


PerlSix is going to be even more of a precedence hell; see http://www.ozonehouse.com/mark/blog/code/PeriodicTable.html.


See IssuesForLanguageDesigners

CategoryMath


EditText of this page (last edited February 24, 2006) or FindPage with title or text search