A subexpression is an expression contained inside a larger expression. It produces a value that the enclosing expression can use as an operand or combine with other values. Expressions with contained subexpressions describe calculations and decisions in a DSL. section 11.6 section 12.1
A binary operation contains two subexpressions: a left operand and a right operand, joined by one operator. Operators can be arithmetical, logical, or comparative. For example, an operation such as “discount of rental price before discount” uses the discount value and the rental-price value as its operands. section 11.6 section 11.1
Subexpressions matter at three stages:
- Representation: an AST object for an operation holds its operator and the AST objects representing its operands. section 11.1
- Evaluation: evaluate contained subexpressions first, then combine their resulting values according to the enclosing operator. section 12.1
- Composition: use expressions with operands to construct larger expression trees from smaller expressions. section 11.5.2
Evaluating nested arithmetic
For `1 + 2 * 3`, `2 * 3` is a subexpression of the whole expression. Under PEMDAS, evaluate it to `6`, replace it with that value, and then evaluate `1 + 6`. Parentheses make their contents a subexpression that is evaluated first: `1 + 2 * (3 + 4)` first reduces `(3 + 4)` to `7`. section 12.1
Evaluation is recursive: a subexpression may itself contain subexpressions, and the process stops at an expression with no contained subexpressions that already yields an expected result value. Replacing an evaluated subexpression with its value preserves the result of the larger expression. section 12.1
The number of subexpressions an expression concept can contain is its arity. Concepts with arity greater than zero enable recursive composition, allowing a small set of expression concepts—such as binary operations—to form ASTs of arbitrary size and capture increasingly detailed business logic. section 11.6 section 11.5.2
Figure 12.8 Building an AST for the expression 1 + 2 * (3 + 4)
This AST-building diagram shows how the nested subexpression `(3 + 4)` becomes part of the larger expression `1 + 2 * (3 + 4)`. section 12.1

