liveBook · Manning

Ast-building process

As explained in Building User-Friendly DSLs

A procedure for constructing an expression abstract syntax tree by representing operands, parentheses, and operations in evaluation order.

Build an expression abstract syntax tree by representing operands, parentheses, and operations in the order dictated by evaluation. The resulting tree mirrors evaluation, simplifying runtime code generation. section 12.1

Follow the evaluation order while constructing the tree:

  • Represent each operation as a Binary Operation with its operator and left and right operands. section 12.1
  • Use shorthand AST notation to encode labels, values, containment, and parentheses while building expressions such as 1 + 2 * 3. section 12.1

1 + 2 * 3

Apply PEMDAS by constructing the multiplication subtree for 2 * 3 before attaching it as the right operand of the addition operation. section 12.1

Figure 12.9 The AST-building process recapped using shorthand notation
figure

Review the complete AST-building process in shorthand notation. section 12.1

How Building User-Friendly DSLs covers it

How deep it goes
Works through shorthand AST construction for arithmetic and parentheses, stopping short of detailed parser implementation.
Examples
Uses 1 + 2 * 3 and 1 + 2 * (3 + 4) to demonstrate evaluation order and parentheses.
What it assumes
Assumes familiarity with PEMDAS and expression evaluation while teaching shorthand notation for AST objects.
What you can do after
Build it from scratch by translating evaluation order into a shorthand expression AST.
How it connects
Connects abstract syntax trees with order of operations, binary operations, operands, parentheses, and code generation.

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.