CS 325 Project Part III
You are to write a semantic analyzer and code generator for
SIMPL. You will incorporate this with
your parser to produce Jasmin assembly
language for the JVM. You should write a
driver program which calls the parser, then runs the semantic analyzer and the
code generator on the syntax tree. To
design your semantic analyzer you must first design an attribute grammar for
the syntax tree.
The semantic analyzer should build the symbol table and calculate the node attributes described below. Semantic errors that are possible in SIMPL are redefining a variable, using an undeclared variable, and an incorrect data type. To handle these errors, print an error message and abort.
Symbol Table
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. |
ioType |
OUT |
One of three possible functions of the variable. |
dtype |
NUM |
One of two possible data types of a variable. |
varNum |
Integer |
Each variable is assigned a local variable number |
Node Attributes
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. |
Supplied by the lexical scanner. |
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. |
dtype |
NUM |
The data type of an expression node, or a variable
declaration. |
Synthesized from the data types of the children
nodes, or supplied by the lexical scanner, respectively. |
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. |
The target machine for the SIMPL compiler is the JVM. Code is generated from
the syntax tree, using the templates given below. (Note: the BANNER is a
comment string identifying the compiler and version.)
Node
Type |
Template |
PROG |
; BANNER |
PROG |
; BANNER |
ID |
ldc "name" |
ID |
ldc "name" |
ID |
ldc "name" |
ID |
ldc "name" |
ID |
sipush 0 |
ID |
bipush 0 |
ID |
iload varNum |
SPACE |
p1.generateCode |
NULL |
|
! |
p1.generateCode |
IF |
p1.generateCode |
WHILE |
lab1: |
<- |
p2.generateCode |
> |
p1.generateCode |
= |
p1.generateCode |
+ |
p1.generateCode |
+ |
p1.generateCode |
- |
p1.generateCode |
* |
p1.generateCode |
* |
p1.generateCode |
/ |
p1.generateCode |
NEG |
p1.generateCode ineg |
NEG |
bipush 1 |
NUM |
sipush name |
BOOL |
bipush value |