SNL语言吉大计算机编译原理课程设计.docx
- 文档编号:8748558
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:119
- 大小:36.24KB
SNL语言吉大计算机编译原理课程设计.docx
《SNL语言吉大计算机编译原理课程设计.docx》由会员分享,可在线阅读,更多相关《SNL语言吉大计算机编译原理课程设计.docx(119页珍藏版)》请在冰豆网上搜索。
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*entry;
ParamChain*next;
};
structFieldChain
{
charidName[10];
TypeIR*unitType;
intoffset;
FieldChain*next;
};
structTypeIR
{
intsize;
TypeKindkind;
union
{
struct
{
TypeIR*indexType;
TypeIR*elementType;
}ArrayAttr;
FieldChain*body;
}More;
};
structAttributeIR
{
TypeIR*idType;
IdKindkind;
unionMore
{
struct
{
AccessKindaccess;
intlevel;
intoffset;
}VarAttr;
struct
{
intlevel;
ParamChain*param;
intcode;
intsize;
}ProcKind;
};
};
structSymbolChain
{
intline;
charidName[10];
AttributeIRattrIR;
SymbolChain*next;
};
classanalyze
{
private:
TreeNode*root;
intlevel;
SymbolChain*Scope[ScopeMaxLevel];
SymbolChain*Backup[ScopeMaxLevel];
private:
ofstreamoutput;
analyze(ofstreamo,TreeNode*r);
~analyze();
voidPrintSymbolChain();
boolFindField();
boolSearchChain(char*,int,SymbolChain*&);
boolFindEntry(char*,bool,SymbolChain*&);
boolFindEntry2(char*,SymbolChain*&,IdKind);
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();
voidsetInputFile(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);
intgetLine();
LexTypegetLex();
char*getSem();
private:
intline;
char*sem;
LexTypelex;
};
#endif
/*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;
};
#endif
/*Treenode.h*/
#ifndef_TREENODE_H_
#define_TREENODE_H_
#include
#include
#include
usingnamespacestd;
enumNODE_KIND//语法树节点
{
ProK,//根标志
PheadK,//程序头
TypeK,//类型声明
VarK,//变量声明
ProcDecK,//单个过程声明
StmLK,//语句序列
DecK,//声明节点
StmtK,//语句节点
ExpK//表达式节点
};
//********************************************************
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,//标识符
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;
Attrattr;
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_
#include"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_VarIdList,
VN_VarIdMore,
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_InputStm,
VN_InVar,
VN_OutputStm,
VN_ReturnStm,
VN_CallStmRest,
VN_ActParamList,
VN_ActParamMore,
VN_RelExp,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SNL 语言 计算机 编译 原理 课程设计