




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、level;param; code; size;/* an alyze.h */#ifndef _ANAL YZE_H_#define _ANAL YZE_H_#defi ne ScopeMaxLevel 20#in elude "TreeNode.h"enum IdKind typeKind , varKind , procKind ;enum TypeKind intType , charType , arrayType recordType , boolType;enum AccessK ind dir, in dir;struct TypeIR;struct Sym
2、bolChai n;TypeIR *idType;IdKi ndkin d;union More struct Acc e ssKind access; level; offset;intintVarAttr;struct intParaint mCha in intProcKi nd);struct ParamChain(SymbolCha inParamChain);struct FieldCha in(charTypeIR * int FieldChai n);struct TypeIR(intTypeK ind union(struct(TypeIR *TypeIR *JArrayAt
3、tr;FieldChai n * More;entry;idName10; uni tType; offset;size; kind;in dexType;eleme ntTypebody;);struct Attribute IRstruct SymbolCha in(intline;charidName10;AttributeIRattrIR;SymbolCha inn ext;*);class an alyze(private:TreeNode* root;intlevel;SymbolCha in * ScopeScopeMaxLevel;SymbolCha in BackupScop
4、eMaxLevel ofstream output;an alyze(ofstream o, TreeNode * r);-an alyze();void Prin tSymbolChai n();bool Fi ndField();bool SearchChai n( char *,i nt,SymbolChain * &);bool FindEntry(char *,bool,SymbolChain * &);bool FindEntry2(char *,SymbolChain * &,IdKind);bool En ter(char *,SymbolChain *
5、 &);void DestroyCha in();void CreateCha in();private:void an alysis(TreeNode *);public:void RecordType(TreeNode * t,SymbolChain * en try); void ArrayType(TreeNode * t, SymbolChain * en try); void nameType(TreeNode * t, SymbolChain * en try); SymbolChai n * NewTy();void TypeProcess(TreeNode * t,
6、SymbolCha in *);void Body(TreeNode * t);void ProcDecPart(TreeNode * t);void VarDecPart(TreeNode * t);void TypeDecPart(TreeNode * t);static void doA nalyze(TreeNode *, ofstream ););#en dif/* parse.h */#ifndef_PARSE_H_#defi ne_PARSE_H_#i nclude"Toke nList.h"#i ncludeHTreeNode.hH class parse
7、private:ofstream * output;Toke nList* head;Toke nList* cur;TreeNode* root;void fieldvarMore(TreeNode * t); TreeNode * fieldvar();void variMore(TreeNode * t);TreeNode * variable();TreeNode * factor();TreeNode * term();TreeNode * Simple_exp();TreeNode * Exp();TreeNode * ActParamMore();TreeNode * ActPa
8、ramList();TreeNode * CallStmRest();TreeNode * ReturnStm();TreeNode * OutputStm();TreeNode * In putStm();TreeNode * LoopStm(); TreeNode * Con ditio nalStm(); TreeNode * ArrayRest();TreeNode * FieldRest(); TreeNodeAssig nmen tRest(); TreeNode * AIICall(); TreeNode * Stm(); TreeNode * StmMore(); TreeNo
9、de * StmList(); TreeNode * ProgramBody(); TreeNode * procBody(); TreeNode * ProcDecPart(); void FidMore(TreeNode * t); void FormList(TreeNode * t); TreeNode * Param();TreeNode * ParamMore(); TreeNode * ParamDecList(); void ParamList(TreeNode * t); TreeNode * ProcDeclarati on(); TreeNode * ProcDec();
10、void varldMore(TreeNode * t); void VarldList(TreeNode * t); TreeNode * VarDecMore(); TreeNode * VarDecList(); TreeNode * VarDeclaratio n(); TreeNode * VarDec();void ldMore(TreeNode * t); void ldList(TreeNode * t); TreeNode * FieldDecMore(); TreeNode * FieldDecList();void RecType(TreeNode * t); void
11、ArrayType(TreeNode * t); void Structure?ype(TreeNode * t); void BaseType(TreeNode * t); void TypeDef(TreeNode * t); void Typeld(TreeNode * t); TreeNode * TypeDecMore(); TreeNode * TypeDecList(); TreeNode * TypeDeclarati on(); TreeNode * TypeDec(); TreeNode * DeclarePart(); TreeNode * DeclarePart2();
12、 TreeNode * ProgramHead(); TreeNode * Program();ofstream * getOutputStream();标识符INST, OP, ASSIGN, DELIMITER, RANGE, POINTER, DOT, ENDFILE, ERROR,Toke nList * getCur();Toke nList * getHead();char * getNextToke nSem();char * getCurre ntToke nSem();void setCur(Toke nList * t);void setOutputStream(ofstr
13、eam * o);void setHead(Toke nList * t);bool read();bool matchSem(c onst char);bool matchLex(LexType I);bool matchNextLex(LexType I);bool matchNextSem(c onst char s);LexType getNextToke nLex();LexType getCurToke nLex();parse(Toke nList * ,ofstream *);parse。;void prin tTree(TreeNode * root);in t getCur
14、re ntToke nLin eNO();public:static TreeNode * doParse(Toke nList *, ofstream *);#en dif/* scan.h */#ifndef _SCAN_H_#defi ne _SCAN_H_#in elude "Toke nList.h"class sea n(public:static Toke nList * doSca n(ifstream "ofstream *);private:ifstream * i nputFile;ofstream * outputFile;sea n();
15、sea n(ifstream *, ofstream *); sca n();ifstream * get In putFile(); ofstream * getOutputFile();void setlnputFile(ifstream *); void setOutputFile(ofstream *); void clearArray(); char getNextChar();LexType lookup(char *); Toke nList * getToke nList(); void prin tToke nList(Toke nList *););#en dif/* To
16、ke n.h7#ifndef _TOKEN_H_#defi ne _TOKEN_H#in clude<iostream> #in clude<fstream> using n amespace std; enum LexType ID,CHARC,字符串RESERVEDWORD, 保留字整型常量符运算符赋值运算符分界符数组下标结构类型成员运算符程序结束标志文件结束标志错误符号class Toke n(public:Toke n();Toke n(i nt, char*,LexType);-Toke n();void prin tToke n(o fstream *);v
17、oid prin tToke n1();void setLex(LexType);void setSem(char *);void setL in e(i nt);int getLi ne();LexType getLex();char * getSem();private:int line;char * sem;LexType lex;);#en dif/*Toke nList.h*/#ifndef_TOKENLIST_H_#defi ne_TOKENLIST_H_#i nclude "Toke n.h" class Toke nList public:Toke nLis
18、t();-Toke nList();void setToke n(Toke n *); void setNext(Toke nList *);void setNumber(i nt);int getNumber(); Toke n * getToke n(); Toke nList * getNext(); void appe nd();private:int nu mber;Toke n * toke n;Toke nList * n ext;););* en um DEC_KIND 具体声明(ArrayK, / 数组#en dif/* Tree no de.h */#ifndef_TREE
19、NODE_H_#defi ne _TREENODE_H_# in elude <stri ng># in elude <iostream># in elude <fstream>using n amespace std;en um NODE_KIND语法树节点PheadK,程序头TypeK,类型声明VarK,变量声明ProcDecK,单个过程声明StmLK,语句序列DecK,声明节点StmtK,语句节点ExpK表达式节点CharK,/ 字符In tegerK, 整型RecordK, / 记录IdK/Type定义的类型);en um STMT_KIND 具体语
20、句(IfK, /if 语句WhileK, /while 语句AssignK, /赋值语句ReadK,WriteK,CallK, ReturnK;enum EXP KIND (OpK,Con stK, IdEK,/read语句/write 语句过程调用语句返回语句具体表达式操作符常整型标识符ArrayEK, / 数组FieldEK, 域CHAREK/单字符);union KIND/记录语法树节点具体类型DEC_KIND dec; 声明STMT_KIND stmt;/ 语句 EXP_KIND exp; 表达式);enum PARAM_TYPE/过程参数属性( valparamtype,/ 值参 v
21、arparamtype/ 变参);enum EXP_OP_TYPE/表达式运算符类型 (LT,EQ, PLUS, MINUS, TIMES, OVER ; struct ArrayAttr/ 数组属性 (int low;/ 上界int up;下届DEC_KIND childType;/ 类型; struct ProcAttr / 过程属性 (PARAM_TYPE paramtype;);struct ExpAttr /表达式属性 (EXP_OP_TYPE op; int val;);struct Attr / 属性 (ArrayAttr arrayattr;ProcAttr procattr;
22、 ExpAttr expattr;);class TreeNode (public:NODE_KIND nodekind;TreeNode *child3;TreeNode *sibling;char * idname3;intlineno;KINDkind;char * type_name;intidnum;intchildnum;public:设置表达式属性*void setAttrExpVal(int i);void setAttrExpOpType(char * s);EXP_OP_TYPE getAttrExpOpType();设置过程属性*void setProcAttrParam
23、Type(PARAM_TYPE d);设置数组属性*void set Array AttrT ype (D EC_KI N D d);void setArrayAttrllp(int u);void setArrayAttrLow(int I);void setLineNo(int I);void setNodeKind(NODE_KIND n);void setTypeName(char * s);void setldName(char *s);void setChild (int i, TreeNode * c);void setChild(TreeNode *);void setSibl
24、ing(TreeNode * t);void setKindOfDec(DEC_KIND);DEC_KIND getKindOfDec();void setKindOfStmt(STMT_KIND);void setKindOfExp(EXP_KIND);TreeNode * getSibling();TreeNode * getChild(int i);intgetldNum();char * getldName(int i);TreeNode();TreeNode();TreeNode(NODE_KIND n);void printTreeNode();void printTreeNode
25、(ofstream * out);#en dif /* zparse.h */#ifndef _ZPARSE_H_ #defi ne _ZPARSE_H_ #in elude "TreeNode.h" #in elude "Toke nList.h" #define Sy_Stack_Max 20 符号栈大小#defi ne OpS_Stack_Max 20 操作符栈大小 #defi ne OpN_Stack_Max 20 操作数栈大小 *vtjmidparen, vt_pointer, vt_dot, vtjt, vt_eq, vt_rmidparen
26、, vt_then, vt_do, vt_plus, vt_minus, vt_mul5 vt_div5 vt_charc, vt_error):class treestack public:TreeNode * & tree; treestack * next; treestack * pre; treestack(TreeNode * & t):tree(t),next(NULL),pre(NULL) );I /* *“*m*zparse 步*root;语法树根节点output; 输出语法LL1Tab®7041;语法分析int表 * . 9 . . 、 /*非终极
27、符集en um VN_VTVN_Program, / 开女台符 VN_ProgramHead, VN_ProgramName, VN_DeclarePart,VN_TypeDec, VN_TypeDeclarati on, VN_TypeDecList, VN_TypeDecMore, VN_Typeld, VN_TypeDef, VN_BaseType, VN_Structuretype, VN_ArrayType, VN_Low, VN_Top, VN_RecType, VN_FieldDecList, VN_FieldDecMore, VNJdList, VN IdMore, VN V
28、arDec, VN V arDeclarati on, VN V arDecList, VN_V arDecMore, VN_VarldList, VN_VarldMore, VN_ProcDec, VN_ProcDeclarati on, 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, V
29、N_Stm, VN_AssCall, VN_Assig nme ntRest, VN_Co nditi on alStm, VN_LoopStm, VN n putStm, VNJ nVar, 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, V
30、N_FieldVarMore, VN_CmpOp, VN_AddOp, VN_MultOp, VN_BaseTypeOfArray, vt_program = 70,/ 为了与 VN 区分 vtjd, vt_type, vt_var, vt_procedure, vt_begi n, vtjnteger, vt_char, vt_array, vt_arraydot, vt_of, vt_record, vtjntc, vt_end, vt_semicolon, vt_comma, vt_rparen, vtjparen, vtjf, vt_while, vt_return, vt_read,
31、 vt_write, vt_else, vt_fi, vt_endwh, vt_assign,*class zparsepublic:treestack * currentTree;treestack * headTree;inttreeNum;voidtree_push(TreeNode * &);voidtree_pop();TreeNode*OptrOpS_Stack_Max;intoptrtop;voidoptr_push(TreeNode *);TreeNode*optr_pop();TreeNode*OpndOpN_Stack_Max;intopndtop;voidopnd
32、_push(TreeNode *);TreeNode* opnd_pop();private:TokenList* head;/TokenList 头节点TokenList* cur; /TokenList当前节点TreeNode* ofstream * 树VN_VTSy_StackSy_Stack_Max;intsy_top;voidsy_push(VN_VT);bool read(); TokenList int char LexTypeVN_VTsy_pop();zparse(TokenList *, ofstream *); zparse();getCur();getCurrentTo
33、kenLineNO(); getCurrentTokenSem(); getCurrentT okenLex();bool matchSem(const char); bool matchLex(LexType I);int convertTokenToVT(); bool matchVT();void ExceptionThrow();TreeNode * parseLL(); void CreateTable(); void predict(int num); int Priosity(TreeNode *);void process1();void process2();void pro
34、cess3();void process4();void process5();void process6();void process7();void process8();void process9();void process10();void processl 1();void processl 2();void processl 3();void processl 4();void processl 5();void processl 6();void processl7();void processl 8();void processl 9();void process20();v
35、oid process21();void process22();void process23();void process24();void process25();void process26();void process27();void process28();void process29();void process30();void process31();void process32();void process33();void process34();void process35();void process36();void process37();void process
36、38();void process39();void process40();void process41();void process42();void process43();void process44();void process45();void process46();void process47();void process48();void process49();void process50();void process51();void process52();void process53();void process54();void process55();void p
37、rocess56();void process57();void process58();void process59();void process60();void process61();void process62();void process63();void process64();void process65();void process66();void process67();void process68();void process69();void process70();void process7 1();void process72();void process73()
38、;void process74();void process75();void process76();void process77();void process78();void process79();void process80();void process81();void process82();void process83();void process84();void process85();void process86();void process87();void process88();void process89();void process90();void proce
39、ss91();void process92();void process93();void process94();void process95();void process96();void process97();void process98();void process99();void process100();void process101();void process102();void process103();void process104();void process105();void process106();void prin tTree(TreeNode * root
40、);public:static TreeNode * doZParse(TokenList * ,ofstream*):b);#en dif/* an alyze.cpp */#in elude “an alyze.h"int aexcepti on;char * exceName;void Except! on Throw(TreeNode * t)aexceptio n = t->li neno;if (t->getldName(O) != NULL)(strcpy(exceName , t->getldName(O);throw(exceName);)void
41、 an alyze:doA nalyze(TreeNode * t, ofstream o) try (an alyze an alyzer(o , t);an alyzer.a nalysis(a nalyzer.root);) catch(i nt excepti on)(cout«Herror in line H«excepti on«en dl;)catch(char * exceName) (cout«",M«exceName«"' is illegal"«endl; )an
42、alyze:a nalyze(ofstream o, TreeNode * r) root = r;output = o;level = -1;)an alyze:a nalyze() if (root != NULL)(delete root;)output.close();)void an alyze:CreateCha in() level+;Scopelevel = NULL;)void an alyze:DestroyCha in()level-;)bool analyze:Enter(char * idname , SymbolChain *en try)(bool prese n
43、t = FindEn try(id name,05e ntry);retur n prese nt;throw(aexcepti on);elsebool analyze:FindEntry(char * idname ,bool flag ,SymbolChain * & entry)int i = level;bool present = true;if (flag = true) (while (i != -1) (present = SearchChain(idnameJ5entry); if (present = false) (return present;)i;)else
44、 (present = SearchChain(idname,i,entry);if (present = false) (return present;)return present;)bool analyze:SearchChain(char * idname Jnt currentLevel,SymbolChain * & entry)(SymbolChain * current;SymbolChain * current2;if (ScopecurrentLevel = NULL) (current = NewTy();ScopecurrentLevel = current;e
45、ntry = current;return true;)else (current = ScopecurrentLevel;while (current != NULL) (if (strcmp(idname , current->idName)current = current->next;)current = NewTy();current2->next = current;entry = current;return true;)void analyze:analysis(TreeNode * t)(TreeNode * p = t->child1;while (
46、p != NULL)(switch (p->nodekind)case TypeK: TypeDecPart(p->childO); break case VarK: VarDecPart(p->childO); break;case ProcDecK: ProcDecPart(p->child0);break;default:ExceptionThrow(p);)p = p->sibling;)p = t->child2;if (p->nodekind = StmLK)(Body(p);)if (level !=-1)(DestroyChain();
47、0)return false;void analyze:TypeDecPart(TreeNode * t)bool present = false; SymbolChain * entry; while (t != NULL)bool analyze:FindEntry2(char * type , SymbolChain * & entry , IdKind idkind)int i = level; SymbolChain * current; while (i>=0)present = Enter(t->idnameO , entry);if (present = t
48、rue) (cout«f,repetition declaration,illegal id '"«t->idnameO«HVn«endl;)else strcpy(entry->idName , t->idname0);TypeProcess(t, entry);)t = t->sibling;)void analyze:TypeProcess(TreeNode *t, SymbolChain * entry)switch (t->getKindOfDec()(case IntegerK: entry-&
49、gt;attrlR.idType->kind = intType;entry->attrlR.idType->size = 1; break;case CharK: entry->attrlR.idType->kind = charType;entry->attrlR.idType->size = 1; break;case IdK:current = Scopei; while (current != NULL) (if (current->attrlR.kind = idkind && strcmp(current->i
50、dName5type) = 0)(entry = current; return true;)i; )return false;)void analyze:nameType(TreeNode *t, SymbolChain * entry)(SymbolChain * entry2;bool presentFindEntry2(t->type_name3entry2,typeKind); if (present = true) (cout«f,undeclaredtype-,H«t->type_name«nIH«endl;)elsenameT
51、ype(t, entry);break;case ArrayK:ArrayType(t,entry); break;case RecordK: RecordType(t,entry); break;SymbolChain * analyze:NewTy() (SymbolChain * s = (SymbolChain*)malloc(sizeof(SymbolChain);s->attrlR.idType = (TypeIR *)malloc(sizeof(TypelR);s->attrlR.kind = typeKind;s->next = NULL;return s:e
52、n try->attrlR = en try2->attrlR;)void analyze:ArrayType(TreeNode * t, SymbolChain * en try)(TypeIR * temp;if (t->attr.arrayattr.low > t->attr.arrayattr.up)( cout«,farrayoverflow-V'«t->type_ name«HH,«e ndl;) else(en try->attrlR.idType->k ind = arrayType
53、;en try->attrlR.idType->size = t->attr.arrayattr.up -t->attr.arrayattr.low ;temp=en try->attrlR.idType->More.ArrayAttr.i ndexType = (TypeIR *)malloc(sizeof(TypeIR);temp->k ind = in tType; temp->size = 1; temp=en try->attrlR.idType->More.ArrayAttr.eleme ntType=(TypeIR *)malloc(sizeof(TypelR);if (t->attr.arrayattr.chil
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 重庆市2025年普通高等学校招生全国统一考试康德调研(四)英语试题及答案
- 《2025员工终止劳动关系合同书》
- 2025年劳动合同指引:中外合资企业
- 机关档案工作规范
- 提升基层治理效能的创新策略与实施路径
- 2025果品类买卖合同书范本
- 西安科技大学《机器人学引论》2023-2024学年第二学期期末试卷
- 珠海艺术职业学院《影视导演艺术》2023-2024学年第一学期期末试卷
- 南京邮电大学通达学院《基础写作(Ⅰ)》2023-2024学年第一学期期末试卷
- 湖南三一工业职业技术学院《精神药理学》2023-2024学年第一学期期末试卷
- 物业项目合作意向书(2024版)
- DLT 572-2021 电力变压器运行规程
- 【有人机与无人机协同作战效能评估探究15000字(论文)】
- GB/T 44014-2024应急避难场所标志
- 医院康复信息系统建设需求
- SL721-2015水利水电工程施工安全管理导则
- 数字贸易学 课件 马述忠 第13-22章 数字贸易综合服务概述- 数字贸易规则构建与WTO新一轮电子商务谈判
- 下肢动静脉溃疡的护理
- 照明维护方案
- 设备管理制度的风险评估与防范方案
- 办公楼装饰工程设计及施工招标文件室内装饰
评论
0/150
提交评论