1.1 代码生成涉及的数据结构
1.1.1 头文件
map<string,string> mp_subprogramToHeadFile;//过程、函数到C程序头文件的映射
map<string,bool> mp_headFileShow;//头文件是否出现的映射
事先将过程、函数及其对应的头文件保存到mp_subprogramToHeadFile中;并将所有的头文件作为键值保存到mp_headFileShow中,对应的属性值均初始化为false。
1.1.2 全局常量
vector<string> v_s_constIdList;//常量标识符
vector<string> v_s_constTypeList;//常量类型,"int","float","char","bool"
vector<string> v_s_constValueList;//常量值,不论是哪种类型,均用string表示
常量的存放顺序按照PASCAL-S程序的定义顺序,且默认语义分析已经完成了常量定义区的常数传播。注意常量是数值类型且是负的,则常量值字符串需要在最前面加上一个负号。
1.1.3 全局变量
vector<string> v_s_variantIdList;//变量标识符
vector<string> v_s_variantTypeList;//变量类型,"int","float","char","bool"
vector< vector<int> > v_ArraySizeList;//各数组变量各维的大小
v_ArrayRangeList的size可以用来指明变量是否为数组,若为数组,数组的维数是多少。
1.1.4 原PASCAL-S主程序对应到C程序的接口声明
string subMainFunctionDeclaration;//原PASCAL主程序对应到C程序的声明
1.1.5 子过程/子函数接口声明
struct subproDef {
string returnType;
//子程序返回值类型,"int","float","char","bool"
string id;
//子程序名称标识符
vector<string> v_paraIdList;
//参数标识符
vector<bool> v_isParaRef;
//参数是否为引用
vector<string> v_paraTypeList;
//参数类型,"int","float","char","bool"
}
;
vector< struct subproDef > v_subproDefList;
//子程序接口列表
子程序接口列表中的第一个子程序永远是main_function
1.1.6 主程序语句列表
vector< pair<string,int> > v_statementList;
每一个pair的first表示语句本身,second表示语句的缩进,即语句前的制表符个数。
1.1.7 子程序
//子程序定义
struct subproDef {
vector<string> constIdList;
//常量标识符
vector<string> constTypeList;
//常量类型,"int","float","char","bool"
vector<string> constValueList;
//常量值,不论是哪种类型,均用string表示
vector<string> variantIdList;
//变量标识符
vector<string> variantTypeList;
//变量类型,"int","float","char","bool"
vector< vector<int> > arraySizeList;
//各数组变量各维的大小
vector< pair<string,int> > statementList;
//pair的first是语句本身,pair的second控制缩进,即语句前的制表符个数
}
;
//子程序定义列表
vector< struct subproDef > subproDefList;
子程序定义中,常量定义、变量定义与全局常量定义和全局变量定义类似;statementList是子程序的语句列表,pari的first是语句本身,pair的second控制缩进,即语句前的制表符个数。
1.1.8 符号表指针
extern _SymbolTable *mainSymbolTable;
_SymbolTable* codeGenerateCurrentSymbolTable;
其中mainSymbolTable是主符号表指针,由语义分析程序提供;而codeGenerateCurrentSymbolTable是用于代码生成时表示当前符号表的指针,用于符号表的定位和重定位。