6.1. The Role of Grammar Transformation

Many simple languages, including most data languages, avoid the subtleties of grammar design by not introducing operators. Operators such as “+” and “-” bring up the question of whether

11 + 5 * 2

should equal 32 or 21. Different languages, notably Smalltalk and Java, have different answers to this question, depending on the precedence of operators. Another question that arithmetic encounters is whether

25 – 16 - 9

equals 0 or 18. Here the answer depends on the associativity of the “-” operator, which determines whether the rightmost or leftmost operator applies first. The grammar for your language determines the associativity and precedence of operators in your language.

In addition to establishing ...

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.