Question 1 | Question 2 | Question 3
The following productions need to be added to the base grammar:
typedef → TYPEDEF TUPLE ID ID DOT type → ID varDeclList → varDeclList typedef
The first production permits typedefs to be used with tuple types. The second production permits a declaration that uses a new type name. The third production permits a typedef to occur anywhere that a local variable declaration can occur.
Each symbol table entry should include "kind" (variable, function, tuple,or typedef) information, as well as "type" information. For a typedef, the "type" information is the built-in type or tuple type for which the typedef defines a new name. (Note: It is also possible to let the "type" field be a typedef name, but that complicates type checking. Note also that if the "kind" field is not needed for type checking, then it is OK just to have a flag that says whether or not this is a typedef name.)
To process typedef T xxx:
Processing a variable/function/parameter declaration T xxx is the same as processing typedef T xxx except that when xxx is added to the symbol table, its kind is var/function/parameter (as appropriate) instead of "typedef".
To process a use of name xxx in a statement:
Here is the symbol table after processing the given declarations:
Name | Kind | Type |
---|---|---|
MonthDayYear | tuple | - |
date | typedef | tuple MonthDayYear |
today | variable | tuple MonthDayYear |
dollars | typedef | integer |
salary | variable | integer |
moreDollars | typedef | integer |
md | variable | integer |
d | variable | integer |
If the name-analysis phase handles uses of names as described above, then no changes need to be made to the type-checking phase.
If the name-analysis phase processes a use of a name in a statement by simply doing a global lookup and linking the use to the corresponding symbol-table entry, then the type-checking phase must ensure that a typedef name is not used where a variable, parameter, or function name is expected. That simply involves checking the "kind" field of the symbol-table entry linked to each ID node, and giving an error message if the kind is "typedef".
Last Updated: 4/1/2024 © 2024 CS 536