Syntax Tree OrganizationRepresenting Syntax Tree NodesNode Phrase Types

Node Phrase Types

As mentioned above, the phrase types Nident and Nconst are used to label nodes representing the leaves of the syntax tree. The phrase types Nrefvar and Ndisplay are used to identify the two special node types used to encode variable reference subtrees. All of the other node phrase names defined in the enumeration type nodetype are used to label internal nodes. All of these other node phrase names are listed and described below.

There are several important subgroups of node phrase types. One important group is the group of "list" phrases including Nstmtlist, Ntypelist, Nvarlist and all of the other phrase types whose names end with "list". These nodes are used to represent lists of items in the program. In all cases, such nodes take 2 children. The left child ( child[0] ) of a list node points to the first element of the list (i.e. a statement, type definition, variable definition or whatever element type is appropriate). The right child ( child[1] ) points to the remainder of the list. Its value is either NULL ( = 0 ) or a pointer to another list node of the same type.

Other important groups of phrase types include the statement phrase types (Nasgn, Ncall, Nretn, Nif and Nwhile) the variable phrase types ( Nident, Nselect, and Nsubs) and the expression phrase types ( which includes the Nrefvar phrase type in addition to all the "unaries" and "binaries" mentioned in the table below).

All of the phrase names used in internal tree nodes are described in the list below. This list is organized so that node labels for phrase types occur in roughly the same order as the corresponding rules of the Co grammar in the Revised Report on the Co Programming Language handout.

Node Num. of
Type Children Description
Nprogram 2 Represents an entire program. Child[0] is a (possibly NULL) list of Ntypelist nodes. Child[1] is an Nbody sub-tree.

Nbody

3 Represents the body of a program or function. Child[0] is a (possibly NULL) list of Nvarlist nodes. Child[1] is a (possibly NULL) list of Nfunclist nodes. Child[2] is a list of Nstmtlist nodes.
Ntypelist 2 List header used to build lists of Ntypedefn nodes.

Ntypedefn

2 Used to represent a single type definition. Child[0] will be an Nident node for the name of the type. Child[1] will be an Narray or Nstruct node describing the type itself.

Narray

2 Represents an array type specification. Child[0] is an Nconst specifying the array's size. Child[1] is an Nident node for the type name specified for the elements of the array.

Nstruct

1 Represents a struct specification. Child[0] is a Nfieldlist containing the field specifications for all the components of the structure.

Nfieldlist

2 Used to represent lists of structure field specifications. Child[0] will be an Ndecl.

Ndecl

2 Used to represent variable declarations and structure field specifications. Both children should be of type Nident. Child[0] is the identifier being declared. Child[1] is the type name. Remember that a special symbol table entry is created during initialization to allow uniform treatment of the type integer.

Nvarlist

2 Used to represent lists of variable declarations. Child[0] will be an Ndecl.

Nfunclist

2 Used to represent lists of function definitions. Child[0] will be an Nprocdefn or an Nfuncdefn.

Nprocdefn

3 Used to represent the definition of a void function. Child[0] is an Nident node for the function's name. Child[1] is a ( possibly NULL ) list of Nformallist nodes. Child[2] is an Nbody node for the function's body.

Nfuncdefn

3 Used to represent the definition of a function that returns a value. The use of the children is identical to that of an Nprocdefn.

Nformallist

2 Used to represent lists of formal parameter specifications. Child[0] will be an Nvarparm or an Nvalparm.

Nvarparm

2 Used to represent the specification of a call-by-reference parameter. Child[0] is an Nident node for the formal parameter name. Child[1] is an Nident node for the parameter type.

Nvalparm

2 Used to represent the specification of a call-by-value parameter. The children are similar to those of an Nvarparm node. Child[1] will always be an Nident node for the pseudo-identifier integer.

Nstmtlist

2 Used to represent statement lists. Child[0] will be one of the following five "statement" phase types or another Nstmtlist node.

Nasgn

2 Represents an assignment statement. Child[0] will be a node of type Nrefvar pointing to a subtree that describes the target of the assignment. Child[1] will be a node whose type is classified as an "expression".

Ncall

2 Represents a call statement or a function call expression. Child[0] will be an Nident node for the function's name. Child[1] points to a (possibly NULL) list of Nactuallist nodes.

Nretn

1 Represents a return statement. If an expression was included in the statement, child[0] points to a sub-tree representing the expression. Otherwise, child[0] is NULL.

Nif

3 Represents an if statement. Child[0] points to a sub-tree representing the "boolean" expression. Child[1] points to a list of Nstmtlist nodes that represents the then part. If an else part was included, child[2] points to the list of Nstmtlist nodes representing the else part. Otherwise, child[2] is NULL. Note that the last two children will be list nodes even if only a single statement is included for either the then or else part.

Nwhile

2 Represents a while statement. Child[0] points to a tree representing the loop termination condition. Child[1] points to a statement list representing the loop body. For loops are rewritten to appear as Nwhile subtrees by the parser.
Nactuallist 2 Used to represent lists of actual parameters. Child[0] will be a node of one of the expression phrase types.
Nrefvar 1 Nrefvar nodes are stored using the refvarnode type rather than the internalnode type. They do, however, appear as internal nodes in the tree. They appear as the roots of variable subtrees pointed to by Nasgn nodes and nodes that are expected to point to nodes representing expressions.

In the trees produced by the parser, the baseaddr of such a node will point to either an Nident, Nselect or Nsubs node. After semantic processing, an Nrefvar node will point to a node of some expression phrase type.

Nselect

2 Represents a variable (or expression) formed by selecting a component from some structure variable. Child[0] describes the struct sub-variable. Child[1] is an Nident node for the name of the component being selected.

Nsubs

2 Represents a variable (or expression) formed by subscripting an array variable. Child[0] represents the array sub-variable. Child[1] points to an expression sub-tree for the subscript expression.

unaries

1 The node labels Nnot and Nneg are used to represent expressions formed using the logical not operator (!) and the arithmetic negation operator (unary -). Child[0] points to a sub-tree representing the expression to whose value the operator should be applied.

binaries

2 The node labels Nor, Nand, Nlt, Ngt, Neq, Nle, Nge, Nne, Nplus, Nminus, Ntimes, Ndiv and Nmod are used to represent expressions formed using the logical, relational and arithmetic binary operators. The sub-expressions to whose values the operator should be applied are pointed to by child[0] and child[1].
Nerror 0 Inserted in tree at points where an error was detected in the syntax of a phrase. Actually, the only place that such nodes ever appear is as elements of "lists". So, the only place you need to check for them is when processing statement lists, parameter lists, etc.

Computer Science 434
Department of Computer Science
Williams College

Syntax Tree OrganizationRepresenting Syntax Tree NodesNode Phrase Types