The structure of CS-tree representing a statement

Child-sibling trees, and expression trees in general, can be used to represent more complex entities such as statements and control structures.

As an example, we may represent a simple C return statement as follows:


	----------
	|        |
	| return |--->NULL
	|        |
	----------
	    |
	    |
	   \|/
	    V
	   NULL
  
A return statement in which a value is returned may be represented by:

	----------
	|        |
	| return |--->NULL
	|        |
	----------
	    |
	    |
	   \|/
	----*-----
	|        |
	| value  |--->NULL
	|        |
	----------
  
Note that the C statement represented here may be either of: return (value); or return value; At this level, the parentheses are no longer an issue.

A more complex example is a C for loop. In C, a for loop has three operands: an initialization clause, an expression usually used as the conditional test, and an end-of-loop statement executed after iteration. These are followed by a statement (body) which is executed every iteration. We may represent these parts in our CS-tree in different, but consistent, ways.

Here, we have an operand subtree for the initialization clause, the conditional test expression, the end-of-loop statement, and the body.


	----------
	|        |
	|  for   |--->NULL
	|        |
	----------
	    |
	    |
	   \|/
	----*-----    ----------    ----------    ----------    
	|        |    |        |    |        |    |        |    
	|  init  |--->|  test  |--->|  eol   |--->|  body  |
	|        |    |        |    |        |    |        |    
	----------    ----------    ----------    ----------    
  
In practice, we may choose to combine the eol and body parts because the end-of-loop statement must be executed for every iteration of the body anyway:

	----------
	|        |
	|  for   |--->NULL
	|        |
	----------
	    |
	    |
	   \|/
	----*-----    ----------    ----------    
	|        |    |        |    |  body  |    
	|  init  |--->|  test  |--->|   /    |
	|        |    |        |    |  eol   |    
	----------    ----------    ----------    
  

A series of statements may also be combined into a larger tree, usually as siblings:


	----------
	|        |
	| block  |--->NULL
	|        |
	----------
	    |
	    |
	   \|/
	----*-----    ----------    ----------    
	|        |    |        |    |        |    
	| stmt1  |--->| stmt2  |--->| stmt3  |
	|        |    |        |    |        |    
	----------    ----------    ----------    
  
Statements are then executed left-to-right.

Lists, on the other hand, may be executed right-to-left (it depends on the language of course). For example, in C a list is a comma-separated collection that is evaluated from right to left, with the value of the leftmostl element in the list used as the value of the entire list:


	----------
	|        |
	|  list  |--->NULL
	|        |
	----------
	    |
	    |
	   \|/
	----*-----    ----------    ----------    
	|        |    |        |    |        |    
	| expr1  |--->| expr2  |--->| expr3  |
	|        |    |        |    |        |    
	----------    ----------    ----------    
  

This page was last modified .