SNL语言吉大计算机编译原理课程设计.docx
《SNL语言吉大计算机编译原理课程设计.docx》由会员分享,可在线阅读,更多相关《SNL语言吉大计算机编译原理课程设计.docx(97页珍藏版)》请在冰豆网上搜索。
SNL语言吉大计算机编译原理课程设计
/*analyze.h*/
#ifndef_ANALYZE_H_
#define_ANALYZE_H_
#defineScopeMaxLevel20
#include"TreeNode.h"
enumIdKind{typeKind,varKind,procKind};
enumTypeKind{intType,charType,arrayType
recordType,boolType};
enumAccessKind{dir,indir};
structTypeIR;
structSymbolChain;
structParamChain
{
SymbolChain
ParamChain
};
structFieldChain
{
char
TypeIR*
int
FieldChain
};
structTypeIR
{
int
TypeKind
union
{
struct
{
TypeIR*indexType;
TypeIR*elementType;
}ArrayAttr;
FieldChain*body;
}More;
};
structAttributeIR
{
private:
ofstreamoutput;
analyze(ofstreamo,TreeNode*r);
〜analyze();
voidPrintSymbolChain();
boolFindField();
boolSearchChain(char*,int,SymbolChain*&);
boolFindEntry(char*,bool,SymbolChain*&);
boolFindEntry2(char*,SymbolChain*&,ldKind);
boolEnter(char*,SymbolChain*&);
voidDestroyChain();
voidCreateChain();
voidanalysis(TreeNode*);
public:
voidRecordType(TreeNode*t,SymbolChain*entry);
voidArrayType(TreeNode*t,SymbolChain*entry);
voidnameType(TreeNode*t,SymbolChain*entry);
SymbolChain*NewTy();
voidTypeProcess(TreeNode*t,SymbolChain*);
voidBody(TreeNode*t);
voidProcDecPart(TreeNode*t);
voidVarDecPart(TreeNode*t);
voidTypeDecPart(TreeNode*t);
staticvoiddoAnalyze(TreeNode*,ofstream);
};
#endif
/*parse.h*/
#ifndef_PARSE_H_
#define_PARSE_H_#include"TokenList.h"
#include"TreeNode.h"classparse{
private:
ofstream*output;
TokenList*head;
TokenList*cur;
TreeNode*root;
voidfieldvarMore(TreeNode*t);TreeNode*fieldvar();
voidvariMore(TreeNode*t);
TreeNode*variable();
TreeNode*factor();
TreeNode*term();
TreeNode*Simple_exp();
TreeNode*Exp();
TreeNode*ActParamMore();
TreeNode*ActParamList();
TreeNode*CallStmRest();
TreeNode*ReturnStm();
TreeNode*OutputStm();
TreeNode*InputStm();
TreeNode*LoopStm();TreeNode*ConditionalStm();TreeNode*ArrayRest();
TreeNode*FieldRest();TreeNode*AssignmentRest();
TreeNode*AllCall();
TreeNode*Stm();
TreeNode*StmMore();
TreeNode*StmList();
TreeNode*ProgramBody();TreeNode*procBody();
TreeNode*ProcDecPart();voidFidMore(TreeNode*t);voidFormList(TreeNode*t);TreeNode*Param();
TreeNode*ParamMore();TreeNode*ParamDecList();
voidParamList(TreeNode*t);TreeNode*ProcDeclaration();TreeNode*ProcDec();
voidvarIdMore(TreeNode*t);voidVarIdList(TreeNode*t);TreeNode*VarDecMore();TreeNode*VarDecList();TreeNode*VarDeclaration();TreeNode*VarDec();
voidIdMore(TreeNode*t);voidIdList(TreeNode*t);TreeNode*FieldDecMore();TreeNode*FieldDecList();
voidRecType(TreeNode*t);voidArrayType(TreeNode*t);voidStructureType(TreeNode*t);voidBaseType(TreeNode*t);voidTypeDef(TreeNode*t);voidTypeId(TreeNode*t);TreeNode*TypeDecMore();TreeNode*TypeDecList();TreeNode*TypeDeclaration();TreeNode*TypeDec();TreeNode*DeclarePart();TreeNode*DeclarePart2();TreeNode*ProgramHead();TreeNode*Program();
ofstream*getOutputStream();
TokenList*getCur();
TokenList*getHead();
char*getNextTokenSem();
char*getCurrentTokenSem();
voidsetCur(TokenList*t);
voidsetOutputStream(ofstream*o);
voidsetHead(TokenList*t);
boolread();
boolmatchSem(constchar[]);
boolmatchLex(LexTypel);
boolmatchNextLex(LexTypel);
boolmatchNextSem(constchars[]);
LexTypegetNextTokenLex();
LexTypegetCurTokenLex();
parse(TokenList*,ofstream*);
~parse();
voidprintTree(TreeNode*root);
intgetCurrentTokenLineNO();
public:
staticTreeNode*doParse(TokenList*,ofstream*);};
#endif
/*scan.h*/
#ifndef_SCAN_H_
#define_SCAN_H_
#include"TokenList.h"
classscan
{
public:
staticTokenList*doScan(ifstream*,ofstream*);
private:
ifstream*inputFile;
ofstream*outputFile;
scan();
scan(ifstream*,ofstream*);~scan();
ifstream*getInputFile();ofstream*getOutputFile();voidsetlnputFile(ifstream*);voidsetOutputFile(ofstream*);voidclearArray();
chargetNextChar();
LexTypelookup(char*);TokenList*getTokenList();voidprintTokenList(TokenList*);
};
#endif
/*Token.h*/
#ifndef_TOKEN_H_
#define_TOKEN_H_
#include
#include
usingnamespacestd;
enumLexType{
ID,〃标识符
CHARC,〃字符串
RESERVEDWORD,//保留字
INST,〃整型常量符
OP,〃运算符
ASSIGN,〃赋值运算符
DELIMITER,〃分界符
RANGE,〃数组下标
POINTER,〃结构类型成员运算符
DOT,〃程序结束标志
ENDFILE,〃文件结束标志
ERROR,〃错误符号
};
classToken
{
public:
Token();
Token(int,char*,LexType);
~Token();
voidprintToken(ofstream*);
voidprintToken1();
voidsetLex(LexType);
voidsetSem(char*);
voidsetLine(int);
/*TokenList.h*/
#ifndef_TOKENLIST_H_
#define_TOKENLIST_H_
#include"Token.h"classTokenList{public:
TokenList();
~TokenList();
voidsetToken(Token*);voidsetNext(TokenList*);voidsetNumber(int);
intgetNumber();Token*getToken();TokenList*getNext();voidappend();
private:
intnumber;
Token*token;
TokenList*next;
};
};
//*********************************************
***********enumDEC_KIND〃具体声明
{
ArrayK,//数组
CharK,〃字符
IntegerK,//整型
RecordK,//记录
IdK//Type定义的类型
};
enumSTMT_KIND〃具体语句
{
IfK,//if语句
WhileK,//while语句
AssignK,//赋值语句
ReadK,
//read语句
WriteK,
//write语句
CallK,
//过程调用语句
ReturnK
//返回语句
};
enumEXP_KIND
//具体表达式
{
OpK,
〃操作符
ConstK,
//常整型
IdEK,
//标识符
#endif
/*Treenode.h*/
#ifndef_TREENODE_H_
#define_TREENODE_H_
#inelude
#inelude
#inelude
usingnamespacestd;
enumNODE_KIND〃语法树节点
{
ArrayEK,〃数组
FieldEK,//域
CHAREK〃单字符
};
unionKIND//记录语法树节点具体类型{
DEC_KINDdec;//声明
STMT_KINDstmt;//语句
EXP_KINDexp;//表达式
};
enumPARAM_TYPE//过程参数属性{
valparamtype,//值参varparamtype//变参
};
enumEXP_OP_TYPE//表达式运算符类型{
LT,
EQ,PLUS,MINUS,TIMES,OVER};
structArrayAttr//数组属性
{
intlow;//上界
intup;//下届
DEC_KINDchildType;//类型};
structProcAttr//过程属性
{
PARAM_TYPEparamtype;
};
structExpAttr//表达式属性
{
EXP_OP_TYPEop;intval;
};
structAttr//属性
{
ArrayAttrarrayattr;
ProcAttrprocattr;ExpAttrexpattr;
};
classTreeNode
{public:
NODE_KINDnodekind;
TreeNode*child[3];
TreeNode*sibling;
char*idname[3];
intlineno;
KINDkind;
char*type_name;
intidnum;
intchildnum;
public:
//设置表达式属性
**********************************************
*******
voidsetAttrExpVal(inti);
voidsetAttrExpOpType(char*s);
EXP_OP_TYPEgetAttrExpOpType();
//设置过程属性
**********************************************
*********
voidsetProcAttrParamType(PARAM_TYPEd);
//设置数组属性
**********************************************
*********
voidsetArrayAttrType(DEC_KINDd);
voidsetArrayAttrUp(intu);
voidsetArrayAttrLow(intl);
voidsetLineNo(intl);
voidsetNodeKind(NODE_KINDn);
voidsetTypeName(char*s);
voidsetIdName(char*s);
voidsetChild(inti,TreeNode*c);
voidsetChild(TreeNode*);
voidsetSibling(TreeNode*t);
voidsetKindOfDec(DEC_KIND);
DEC_KINDgetKindOfDec();
voidsetKindOfStmt(STMT_KIND);
voidsetKindOfExp(EXP_KIND);
TreeNode*getSibling();
TreeNode*getChild(inti);
intgetIdNum();
char*getIdName(inti);
~TreeNode();
TreeNode();
TreeNode(NODE_KINDn);
voidprintTreeNode();
voidprintTreeNode(ofstream*out);
};
#endif
/*zparse.h*/
#ifndef_ZPARSE_H_#define_ZPARSE_H_#inelude"TreeNode.h"#include"TokenList.h"#defineSy_Stack_Max20//符号栈大小
#defineOpS_Stack_Max20〃操作符栈大小#defineOpN_Stack_Max20〃操作数栈大小
〃*********************************************
*********
************************
〃*********************************************
*********
enumVN_VT{
VN_Program,〃开始符VN_ProgramHead,VN_ProgramName,VN_DeclarePart,
VN_TypeDec,VN_TypeDeclaration,VN_TypeDecList,VN_TypeDecMore,VN_TypeId,VN_TypeDef,VN_BaseType,VN_Structuretype,VN_ArrayType,VN_Low,VN_Top,
VN_RecType,
VN_FieldDecList,VN_FieldDecMore,VN_IdList,VN_IdMore,VN_VarDec,VN_VarDeclaration,VN_VarDecList,VN_VarDecMore,VN_VarldList,VN_VarldMore,VN_ProcDec,
VN_ProcDeclaration,
VN_ProcDecMore,VN_ProcName,VN_ParamList,VN_ParamDecList,VN_ParamMore,VN_Param,VN_FormList,VN_FidMore,VN_ProcDecPart,VN_ProcBody,VN_ProgramBody,VN_StmList,VN_StmMore,VN_Stm,VN_AssCall,VN_AssignmentRest,VN_ConditionalStm,VN_LoopStm,VN」nputStm,VN_InVar,VN_OutputStm,VN_ReturnStm,VN_CallStmRest,VN_ActParamList,VN_ActParamMore,VN_RelExp,VN_OtherRelExp,VN_Exp,VN_OtherTerm,VN_Term,VN_OtherFactor,VN_Factor,VN_Variable,VN_VariMore,VN_FieldVar,VN_FieldVarMore,VN_CmpOp,VN_AddOp,VN_MultOp,VN_BaseTypeOfArray,vt_program=70,//为了与VN区分vt_id,vt_type,vt_var,vt_procedure,vt_begin,
vt_integer,vt_char,vt_array,vt_arraydot,vt_of,vt_record,vt_intc,vt_end,vt_semicolon,vt_comma,vt_rparen,vt_lparen,vt_if,vt_while,vt_return,vt_read,vt_write,vt_else,vt_fi,vt_endwh,vt_assign,vt_lmidparen,vt_pointer,vt_dot,vt_lt,vt_eq,vt_rmidparen,vt_then,vt_do,vt_plus,vt_minus,vt_mul,vt_div,vt_charc,vt_error
};
classtreestack{public:
TreeNode*&tree;treestack*next;treestack*pre;treestack(TreeNode*&t):
tree(t),next(NULL),pre(NULL){}
};
//*********************************************
*******
//******************zparse类
**************************
//*********************************************
*******
classzparse{
public:
treestack
*currentTree;
treestack
*headTree;
int
treeNum;
void
tree_push(TreeNode*&);
void
tree_pop();
TreeNode
*Optr[OpS_Stack_Max];
int
optrtop;
void
optr_push(TreeNode*);
TreeNode
*optr_pop();
TreeNode
*Opnd[OpN_Stack_Max];
int
opndtop;
void
opnd_push(TreeNode*);
TreeNode
*opnd_pop()