6.4. Ensuring Proper Precedence

Proper precedence means that some operators in a language should evaluate before others. In regular expression languages, for example, sequence usually has a higher precedence than alternation. In arithmetic, convention gives multiplication and division a higher precedence than addition and subtraction. In Java,

System.out.println(11 + 5 * 2); 

prints "21". Java recognizes that multiplication has higher precedence, so it computes 5 times 2 before adding this product to 11. The idea that multiplication should proceed before addition is a convention and not a law of the universe. The designers of the Smalltalk programming language gave up traditional arithmetic precedence in order to preserve a simple and consistent ...

Get Building Parsers with Java™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.