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
This diagram traces how precedence determines the nesting needed for the AST of 1 + 2 * 3. section 12.1
