liveBook · Manning

Abstract syntax tree

As explained in Building User-Friendly DSLs

A structured representation of language content in which nodes represent syntactic constructs independently of surface notation.

An abstract syntax tree (AST) represents DSL content as structured data rather than its written or visual notation. Concrete syntax is reduced by annotating visible cues and extracting objects, settings, and relations. When reference relations are disregarded, the resulting object-relation diagram can be laid out as a tree; that tree-shaped abstract syntax is an AST. section 2.3 section 2.2

An AST separates what content means structurally from how an editor displays it. A projectional editor starts from the AST, reconstructs concrete syntax for display, and lets domain experts edit the structure directly. This makes the AST the central representation for editing and later processing DSL content. section 2.0 section 12.0

Rental record type

A Rental record type becomes a Record Type object containing Data Attribute objects. Each attribute has settings such as name and type. Initial values become separate objects: a Number holds a numeric value, while an Attribute Reference holds a reference to another attribute. The containment structure forms the tree; the attribute reference is an additional non-tree relation. section 2.2 section 2.3

In JavaScript, encode each AST object as an object with concept and settings properties. Store simple settings and containment values directly; use arrays for multivalued settings. Encode a reference relation as a reference object whose ref property points to its target. Navigate containment through settings and references through .ref, or visit the tree with recursive depth-first traversal. section 3.4 section 3.2.1

Figure 2.18 The extracted ORD (without the reference relation) laid out as a tree
figure

This tree layout makes the extracted Rental structure visible while deliberately omitting its reference relation, clarifying why an AST is tree-shaped only under that condition. section 2.2

ASTs also support change. When a DSL renames or removes concepts or properties, a migration transforms existing ASTs to the new structure, commonly with a depth-first traversal. ASTs can also represent expressions, where evaluation order shapes the tree even when the projected notation looks like a linear formula. section 10.3.2 section 12.0

A textual DSL reaches an AST differently: a parser processes text into a parse tree that may already be an AST or may require transformation. Once produced, the AST can feed language services such as content assist, constraint checking, syntax highlighting, and type information. section 15.1 section 1.2

How Building User-Friendly DSLs covers it

How deep it goes
Defines extraction and tree shape, then implements JavaScript encoding, navigation, recursive traversal, and migrations; defers parsing mechanics.
Examples
Builds a Rental record AST with attributes, numeric initial values, and an attribute-reference initial value.
What it assumes
Teaches AST representation from visible DSL content without assuming advanced DSL or compiler knowledge.
What you can do after
Apply it via JavaScript to encode, inspect, modify, traverse, and migrate DSL content represented as ASTs.
How it connects
Connects concrete syntax, object-relation diagrams, projectional editing, reference relations, depth-first traversal, parsing, and expressions.

Also explained in

Compare how 2 books explain abstract syntax tree →

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.