C2008 Abstract Syntax Trees - CSCI310 – Spring 2008

There are three basic types of trees you need to deal with:

Each of these also has subtypes. The following describes these subtypes.

Statement trees

      If -----------> Next
  |   |    |
Test Then Else(op) 

Test will be an Expression tree. Then and Else will be Statement trees. Else is optional (may be NULL). Next points to the next Statement tree or is NULL.

      Return -----> Next
        |
       Exp

Exp is an Expression tree. Exp is optional (may be NULL). Next points to the next Statement tree or is NULL.

      While -----> Next
      |  |
    Test Body

Test is an Expression tree. Body is a Statement tree. Next points to the next Statement tree or is NULL.

      For -------------> Next
   |   |    |   |
 Init Test Iter Body

Init, Test, Iter are Expression trees (which could be NULL). Body is a Statement tree. Next points to the next Statement tree or is NULL.

      Do -------------> Next
    |    |   
  Test  Body

Test is an Expression tree (which could be NULL). Body is a Statement tree. Next points to the next Statement tree or is NULL.

       Compound -----> Next
      |        |
    LocalDecls Statement-list

LocalDecls is the first of a series of Declaration trees. It may be NULL. Statement-list is the first of a series of Statement trees. It may be NULL. Next points to the next Statement tree or is NULL.

      Expression -----> Next
         |  
       Exp 

Exp is an Expression tree. Next points to the next Statement tree or is NULL. This structure is optional -- you could use an Expression tree directly instead.

Expression trees

   Operator --> Next
     |  |
  LOper ROper 

Operator is a structure that includes the type of operation (!, INC, DEC, =,+,-,*,/, and relational operators). LOper and ROper are Expression trees. ROper is NULL for unary operators.Next points to the next Expression tree (used when defining arglists) or may be NULL.

Const --> Next

Const is a structure containing a value. Next points to the next Expression tree (used when defining arglists) or may be NULL.

     Var --> Next
      |
     subscript

Var is a structure that includes a name. Subscript is an Expression tree. It may be NULL (if the ID is not an array). Next points to the next Expression tree (used when defining arglists) or may be NULL.

    Call --> Next 
    |   |
   var Arglist

Call is a structure. Var is a var tree node for the function name. Arglist is the first in a series of expression trees. It may be NULL. Next points to the next Expression tree (used when defining arglists) or may be NULL.

Declaration Trees

      VarDecl -> Next

VarDecl is a structure that includes a name, type, and size. If the variable represented is not an array, size is -1. Otherwise, size is the size of the array. Next points to the next Declaration tree or is NULL.

     FunDecl -> Next
     |     |
   Params Body

FunDecl is a structure that includes a name and type. Params points to the first in a series of Param trees. Params may be NULL. Body is a Compound Statement tree. Next points to the next Declaration tree or is NULL.

     Param -> Next

Param is a structure that includes a name, type, and a flag as to whether the param is a single variable or array. Next points to the next Param tree or is NULL.