Build an annotated parse tree from a worked input
An annotated parse tree adds meaning to a grammatical structure. This visualizer builds a concrete parse tree with the same parsing library as the site's grammar tools, then evaluates a fixed syntax-directed definition over that tree. The default arithmetic input already has an answer. Edit it to see the value, node annotations and evaluation order change together, or switch to a declaration example to study inherited type information.
The page is intended for compiler-design exercises about SDDs, S-attributed definitions, L-attributed definitions and semantic-rule dependencies. It is not a generator for arbitrary semantic programs. A syntax-directed definition describes attributes and rules; a translation scheme places executable actions in productions. Here, the displayed print and addType actions have a fixed educational meaning rather than running code supplied by a visitor.
Distinguish values supplied by the lexer from computed attributes
In arithmetic mode, num.lexval holds the integer represented by a numeric token. The lexer supplies that value. F.val, T.val and E.val then carry values computed from children. The grammar separates expressions, terms and factors so multiplication binds more tightly than addition, while parentheses can change the grouping. The top L production prints the expression value; print is displayed as an action, not invented as an inherited value.
Declaration mode works differently. T.type comes from the chosen type keyword. A list receives that type through its inh attribute before the list is visited. The type travels through the nested list nodes, and each addType action associates an identifier occurrence with it. Identifier leaves show their lexical entry names. They do not receive fabricated inherited attributes merely to make the diagram look fuller.
Work the desk-calculator example
For 3 * 5 + 4 n, the tool's library produces the following intermediate values. The final n represents a line ending; it does not mean multiplication by a variable.
| Operation or propagation | Value |
|---|---|
| First numeric token | 3 |
| Second numeric token | 5 |
| Multiplication term | 15 |
| Last numeric token and term | 4 |
| Addition expression | 19 |
| Printed result | 19 |
The generated trace contains 12 rows, including reads of lexical values and the final print action. At each arithmetic node, its children have already provided the values needed by its rule. This is the post-order evaluation pattern for the shown S-attributed definition. Operators and the line-ending terminal have no numeric attributes of their own.
Try the parentheses example, (3+4)*(5+6) n. The same library returns 77. Its tree retains the grouping terminals as well as the intermediate expression, term and factor nodes; this is a concrete parse tree, not the smaller operator-only structure shown by the infix and expression-tree tool.
Walk declaration types from left to right
The loaded declaration int a, b, c gives a: int, b: int and c: int. Its trace contains 10 rows: computing the type, passing it into the list nodes, reading identifier entries and performing their declaration actions. Follow the row dependencies to see why the inherited type must be available before an identifier can be assigned that type.
The textbook-style input int id, id, id is also accepted. It displays three identifier occurrences; repeated spelling is not interpreted as proof of three different variables. This demonstration does not implement C redeclaration rules, scopes, storage allocation, pointer declarators or array declarators. For concrete names, use a, b and c to make the lexical entries easy to distinguish.
The declaration grammar is left-recursive. L-attributed describes which attribute dependencies are allowed; it does not assert that this grammar is LL(1). The tool constructs the full tree before evaluating it with a left-to-right depth-first walk. Compare grammar properties separately with the FIRST and FOLLOW calculator.
Read the numbered nodes and evaluation table
Every node has an ID that also appears in the evaluation rows. The table identifies an attribute or action, its current value, the rule used and its dependencies. A dependency always points to an earlier available value in this demonstration. Lexical rows show supplied token information; inherited rows pass information into a child; synthesized rows collect information from children.
The diagram keeps its text readable rather than shrinking a wide tree into a phone screen. Scroll inside its frame to see other branches. Long values wrap inside nodes, and the evaluation table provides the complete text as another way to inspect them. Nodes without attributes remain visible so the grammar structure is preserved.
Use exact integers within explicit limits
Arithmetic uses exact integers. For example, 9007199254740993 + 7 produces 9007199254741000 without passing the operands through floating-point arithmetic. This is useful when a numeric attribute is larger than the usual exact range of a JavaScript number. Only nonnegative whole-number literals, addition, multiplication and parentheses are supported. Unary minus, subtraction, division, decimals and identifier-valued expressions are rejected.
Inputs are limited to 600 characters. An expression may contain at most 31 tokens before the optional n; literals may have at most 40 digits, and a computed attribute may have at most 256 digits. A declaration supports at most 12 identifier occurrences with names of at most 24 ASCII letters, digits or underscores, beginning with a letter or underscore. These are visualizer limits, not language-standard restrictions.
Keep the working with the result
Copy analysis includes the normalized input, answer, semantic rules, numbered annotations and evaluation trace. Invalid input clears the previous answer rather than leaving old working under a new expression. The analysis runs in the browser and does not execute custom semantic code.
Use the general parse-tree generator for other grammars, the three-address code generator for intermediate-code translation, and the CS theory lab for the wider compiler sequence. For background on the attribute disciplines, see Aho's syntax-directed translation lecture and the NYU declaration-type example.