The abstract syntax tree (AST)
In computer science, an AST, or just syntax tree, is a tree representation of the abstract (simplified) syntactic structure of the source code. Each node of the tree denotes a construct of the programming language under consideration. In our expression evaluator, the nodes are numeric values (IEEE 754 floating points), binary operators, unary operators, trigonometric functions, and a variable.
The syntax is abstract in the sense that it does not represent every detail that appears in the real syntax. For instance, grouping parentheses is implicit in the tree structure, and AST data structure discards parentheses. Before we model the AST, let us see some expressions and its AST representations:
// AST for 5*10 Exp e = new BinaryExp( new NumericConstant(5), new NumericConstant(10), OPERATOR.MUL);
The preceding example uses two node types, that is, NumericConstant
, BinaryExp
. Even the simplest expression creates...