CS 451-651 Homework #7 and #8
HW #7 is Due Wednesday, Oct 28
(due to hour exam)
HW #8 is due the following Monday, Nov 2
(to get us back on track)
When both of these assignments are done, your compiler will support the
use of scalar variables, and have type checking.
HW #7: Semantic Analysis
Implement the Semantic Analysis pass. This includes:
- Tree walk ("analyze()") in all Nodes. (I believe we agreed
that analyze should return a Type for ExprNodes, and void for
StmtNodes.)
- A nameTable, to hold declared names and look them up.
(Associated with currentFunction, which is itself anchored in Main.)
- A mechanism for handling built-in [scalar] types: "int",
also "boolean" and "float".
- logic to allocate variables to memory slots as their declarations
are encountered.
- recall that Semantic Analysis of a variable reference is
responsible for discovering its associated Declaration, and thus for
determining its type (Type).
HW #8: Scalar Variables
Building on #7, provide the ability to use scalar variables in
expressions and assignments. This includes:
- ability to load a variable when referenced "normally", i.e. as
part of a general expression.
- ability to store a value into a variable, when referenced
(specially) from assignments and also from declarations (for init).
We'll do arrays next week.
If you want to
change the order of the subtasks, that's fine ... as long as you
complete about 50% of the assignment by the HW #7 due date.
I'll provide some files that may
be helpful. Watch this space (notice will be sent
when new one appear)
Here's a collection,
which
includes my code for FunctionInfo and NameTable support...some
ideas might be helpful.
Boolean:
Here's some of what's needed to provide a boolean type, which will be
helpful in testing
the semantic analysis type-checking code. (for instance, it
will allow you to write "apple = true * pie;", which should probably
produce an
error message.)
In Scanner.jflex:
"true" { System.out.println(" Scan: '"+yytext()+"'"); return sf.newSymbol("TRUE", sym.BOOL_LIT, "1"); }
"false" { System.out.println(" Scan: '"+yytext()+"'"); return sf.newSymbol("FALSE", sym.BOOL_LIT, "0"); }
Remember, in the calls to newSymbol, the SECOND arg is the symbol that
the parser cares about (the one that appears in the grammar
rules); the third arg, if present, is the value associated with
that symbol (i.e., the value accessed via the :label in the {: action
code :}.
In Parser.cup, do something like this:
factor ::= NUMBER:n
{:
RESULT= new LitNode(n, "int");
System.out.println("factor ::= number: " + RESULT);
:}
| BOOL_LIT:bl
{:
// (our scanner returns value as 1 or 0 )
RESULT= new LitNode(bl, "boolean");
System.out.println("factor ::= boolean literal: " + RESULT);
:}
(Note: you can probably send the constructor the reference to
"Main.theIntType" -- just make sure theIntType has been created by the
time the parser runs!
OR, equally valid, just have a different class for IntLit and
BooleanLit Nodes.) (I was betting that most of the processing
would be the same, so I could easily combine them)