SIMPL Semantic Analysis
This section discussed the form of the symbol table, the form of the syntax tree, and syntax tree attributes.
The SIMPL compiler uses a syntax tree produced by the parser to perform
semantic analysis. A central structure in the analysis is the symbol table. The
form of a symbol table entry is as follows:
Field |
Values |
Description |
name |
String |
The name of the variable. |
value |
Integer |
The numeric value of a boolean constant |
ioType |
OUT |
One of three possible functions of the variable. |
dtype |
NUM BOOL |
The data type of an expression node or a variable declaration |
varNum |
Integer |
Each variable is assigned a local variable number |
A syntax tree is built out of several node types. These are listed below:
Node Type |
Children |
Description |
PROG |
p1 p2 p3 p4 |
The top level program node, with name p1, input
variables p2, local variables p3, |
ID |
|
A node representing a variable reference or declaration. |
SPACE |
p1 p2 |
A list of variable declarations, with head p1, and tail p2. |
NULL |
|
The empty list node. |
! |
p1 p2 |
A list of statements, with head p1, and tail p2. |
IF |
p1 p2 p3 |
An if statement node, with test expression p1, then part p2, and else part p3. |
WHILE |
p1 p2 |
A while statement node, with test expression p1, and body p2. |
<- |
p1 p2 |
An assignment statement node, with variable l-value p1, and r-value expression p2. |
> |
p1 p2 |
A greater than operator, with left operand p1, and right operand p2. |
= |
p1 p2 |
An equals operator, with left operand p1, and right operand p2. |
+ |
p1 p2 |
An addition or OR operator, with left operand p1, and right operand p2. |
- |
p1 p2 |
A subtraction operator, with left operand p1, and right operand p2. |
* |
p1 p2 |
A multiplication or AND operator, with left operand p1, and right operand p2. |
/ |
p1 p2 |
A division operator, with left operand p1, and right operand p2. |
NEG |
p1 |
A unary negation or NOT operator with operand p1 |
NUM |
|
A node representing a counting number constant. |
BOOL |
|
A node representing a boolean constant |
During semantic analysis several attributes are calculated, and added to the
nodes of the syntax tree. The table below lists the attributes attached to each
node.
Attribute |
Values |
Description |
Calculation |
name |
String |
The character string representation of a token. |
Supplied by the lexical scanner. |
value |
Integer |
The numeric value of a constant. (0 and 1 used for the boolean constants .F and .T) |
Supplied by the lexical scanner. |
dtype |
NUM BOOL |
The data type of an expression |
Synthesized from the data types of the children |
lineNum |
Integer |
The source line on which a token was scanned. |
Supplied by the lexical scanner. |
symTab |
Symbol Table |
A pointer to the global symbol table. |
Synthesized by the PROG node p1, p2, and p3 children; inherited by the p4 child. |
use |
DECLARE |
The use of a variable name; either declared or referenced. |
Inherited through all children of the PROG node. |
size |
Integer |
The number of leaf nodes in a nodes subtree. |
Synthesized from the nodes children. |
lab1 |
String |
A generated label, used by control nodes. |
Synthesized in the p4 child of the PROG node, using a label generator. |
lab2 |
String |
A generated label, used by control nodes. |
Synthesized in the p4 child of the PROG node, using a label generator. |