The Wayback Machine - https://web.archive.org/web/20261125231635/https://www.geeksforgeeks.org/compiler-design/parse-tree-and-syntax-tree/

Parse Tree and Syntax Tree

Last Updated : 20 Nov, 2026

A Parse Tree and a Syntax Tree are tree structures used in compiler design to represent the grammatical and syntactic structure of an input.

Parse Tree

A Parse Tree represents how an input string is derived from the start symbol of a Context-Free Grammar (CFG) using its production rules.

For a CFG, G = (V, \Sigma, P, S), a Parse Tree satisfies the following properties:

  • The root node is labeled with the start symbol S.
  • Internal nodes represent non-terminals.
  • Leaf nodes represent terminals or the empty string (\varepsilon).
  • If a production rule A \rightarrow X_1 X_2 \ldots X_n is applied, the children of node A are labeled X_1, X_2, \ldots, X_n, in the same order.
  • Reading the leaf nodes from left to right produces the input string.

Syntax Tree

A Syntax Tree, commonly called an Abstract Syntax Tree (AST), represents the essential syntactic structure of a program without including unnecessary grammar details.

  • Operators and language constructs are represented as internal nodes.
  • Identifiers, constants, and literals are generally represented as leaf nodes.
  • It provides a compact representation of expressions and statements.
  • It is used in semantic analysis, type checking, optimization, and code generation.

Example of Parse Tree and Syntax Tree

Consider the following arithmetic expression:

3 * 4 + 5

Assume the following grammar:

E → E + T | T
T → T * F | F
F → digit

For Parse Tree

The Parse Tree represents the complete derivation of the expression using grammar production rules.

  • The root node is E.
  • Apply E → E + T to represent the addition operation.
  • Apply E → T and T → T * F to derive the multiplication expression.
  • Apply T → F and F → digit to derive the operands 3, 4, and 5.
frame_1000002537

For Syntax Tree

The Syntax Tree removes intermediate grammar nodes and represents the operators and operands directly.

  • The root node is +, representing addition.
  • Its left child is *, representing multiplication.
  • The children of * are 3 and 4.
  • The right child of + is 5.
frame_1000002538

The tree structure represents operator precedence, with multiplication nested under addition.

Parse Tree vs. Syntax Tree

Parse Tree

Syntax Tree

Represents the complete grammatical structure.

Represents the essential syntactic structure.

Contains terminals and non-terminals.

Contains operators, operands, and language constructs.

Follows grammar production rules.

Does not explicitly represent every grammar production.

More detailed and generally larger.

More compact and abstract.

Used to represent grammatical derivation.

Used in semantic analysis, optimization, and code generation.

Comment

Explore