liveBook · Manning

Expression AST

As explained in Building User-Friendly DSLs

An abstract syntax tree representing an expression and its nested expression structure.

An expression AST is an abstract syntax tree that represents only an expression and its nested subexpressions. It can refer to data attributes without containing them: the attributes remain part of the surrounding DSL content. Expression nodes such as Attribute Reference, Number, and Binary Operation encode the expression itself. section 11.1 section 12.1

Binary Operation nodes form the structural skeleton of an expression AST. Each node records an operator and links to left and right operands, allowing an outer operation to contain a nested operation as one operand. Attribute references and numeric values supply the values on which those operations act. section 11.1 section 12.1

Build an expression AST so its shape follows evaluation order:

  • Identify values, attribute references, and operations in the expression. section 11.1
  • Use operator precedence to nest the operation that must evaluate first inside the operation that uses its result. section 12.1 section 12.5
  • Use associativity when adjacent operators have equal precedence: left-associative operators group from the left, and right-associative operators group from the right. section 12.5
  • Add virtual show-only parentheses when a projection could make different nested ASTs appear identical or contradict expected evaluation order. section 12.5

Discounted rental price

The expression “rental price before discount − (discount of rental price before discount)” becomes two nested Binary Operation nodes. The outer subtraction references rental price before discount on the left; its right operand is the nested operation that calculates the discount. The referenced attributes are targets of the expression AST rather than contained nodes. section 11.1

Precedence in 1 + 2 * 3

Build multiplication as the nested operation because it has higher precedence than addition. The resulting tree already mirrors recursive evaluation, so generated run-time evaluation code need not reconstruct the intended order from separate expression parts. section 12.1 section 12.5

Figure 12.5 Building an AST for the expression 1 + 2 * 3
figure

This diagram traces how precedence determines the nesting needed for the AST of 1 + 2 * 3. section 12.1

How Building User-Friendly DSLs covers it

How deep it goes
Explains node containment, operands, precedence, associativity, shorthand notation, and projection ambiguity, without detailing generated code implementation.
Examples
Uses rental-price discount calculation plus 1 + 2 * 3 and 1 + 2 * (3 + 4) to demonstrate construction.
What you can do after
Build it from scratch by nesting expression nodes according to precedence and associativity, then disambiguate projections with show-only parentheses.
How it connects
Connects abstract syntax trees with Attribute Reference, Binary Operation, operator precedence, associativity, parentheses, projections, and code generation.

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.