liveBook · Manning

Subexpression

As explained in Building User-Friendly DSLs

An expression contained within another expression as one of its component expressions.

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)
figure

This AST-building diagram shows how the nested subexpression `(3 + 4)` becomes part of the larger expression `1 + 2 * (3 + 4)`. section 12.1

How Building User-Friendly DSLs covers it

How deep it goes
Explains recursive evaluation, operand structure, AST construction, and compositionality without parser implementation details.
Examples
Uses discount of rental price before discount, `1 + 2 * 3`, and `1 + 2 * (3 + 4)`.
What it assumes
Builds from familiar arithmetic operators and PEMDAS while defining operands, arity, and expression composition.
What you can do after
Build it from scratch by nesting operand AST objects and evaluating them recursively according to operator rules.
How it connects
Connects subexpressions to expressions, binary operations, operands, arity, ASTs, order of operations, and compositionality.

Also explained in

Compare how 2 books explain subexpression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.