1、/* analyze.h */#ifndef _ANALYZE_H_#define _ANALYZE_H_#define ScopeMaxLevel 20#include TreeNode.henum IdKind typeKind , varKind , procKind ;enum TypeKind intType , charType , arrayType , recordType , boolType;enum AccessKind dir , indir ;struct TypeIR;struct SymbolChain;struct ParamChainSymbolChain*e
2、ntry;ParamChain*next;struct FieldChain charidName10;TypeIR*unitType;intoffset;FieldChain*next;struct TypeIRintsize;TypeKindkind;union struct TypeIR*indexType;TypeIR*elementType;ArrayAttr;FieldChain*body;More;struct AttributeIRTypeIR*idType;IdKindkind;union Morestruct AccessKindaccess;intlevel;intoff
3、set;VarAttr;struct intlevel;ParamChain*param;intcode;intsize;ProcKind;struct SymbolChainintline;charidName10;AttributeIRattrIR;SymbolChain *next;class analyzeprivate:TreeNode*root; intlevel;SymbolChain*ScopeScopeMaxLevel;SymbolChain *BackupScopeMaxLevel;private:ofstreamoutput;analyze(ofstream o, Tre
4、eNode * r);analyze();void PrintSymbolChain();bool FindField();bool SearchChain(char *,int,SymbolChain * &);bool FindEntry(char *,bool,SymbolChain * &);bool FindEntry2(char *,SymbolChain * &,IdKind);bool Enter(char *,SymbolChain * &);void DestroyChain();void CreateChain();void analysis(TreeNode *);pu
5、blic:void RecordType(TreeNode * t,SymbolChain * entry);void ArrayType(TreeNode * t , SymbolChain * entry);void nameType(TreeNode * t , SymbolChain * entry);SymbolChain * NewTy();void TypeProcess(TreeNode * t , SymbolChain *);void Body(TreeNode * t);void ProcDecPart(TreeNode * t);void VarDecPart(Tree
6、Node * t);void TypeDecPart(TreeNode * t);static void doAnalyze(TreeNode * , ofstream );#endif/* parse.h */#ifndef _PARSE_H_#define _PARSE_H_#includeTokenList.h#includeTreeNode.hclass parseprivate:ofstream* output;TokenList* head;TokenList* cur;TreeNode* root;void fieldvarMore(TreeNode * t);TreeNode
7、* fieldvar();void variMore(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 * Lo
8、opStm();TreeNode * ConditionalStm();TreeNode * ArrayRest();TreeNode * FieldRest();TreeNode * AssignmentRest();TreeNode * AllCall();TreeNode * Stm();TreeNode * StmMore();TreeNode * StmList();TreeNode * ProgramBody();TreeNode * procBody();TreeNode * ProcDecPart();void FidMore(TreeNode * t);void FormLi
9、st(TreeNode * t);TreeNode * Param();TreeNode * ParamMore();TreeNode * ParamDecList();void ParamList(TreeNode * t);TreeNode * ProcDeclaration();TreeNode * ProcDec();void varIdMore(TreeNode * t);void VarIdList(TreeNode * t);TreeNode * VarDecMore();TreeNode * VarDecList();TreeNode * VarDeclaration();Tr
10、eeNode * VarDec();void IdMore(TreeNode * t);void IdList(TreeNode * t);TreeNode * FieldDecMore();TreeNode * FieldDecList();void RecType(TreeNode * t);void ArrayType(TreeNode * t);void StructureType(TreeNode * t);void BaseType(TreeNode * t);void TypeDef(TreeNode * t);void TypeId(TreeNode * t);TreeNode
11、 * TypeDecMore();TreeNode * TypeDecList();TreeNode * TypeDeclaration();TreeNode * TypeDec();TreeNode * DeclarePart();TreeNode * DeclarePart2();TreeNode * ProgramHead();TreeNode * Program();ofstream * getOutputStream();TokenList * getCur();TokenList * getHead();char * getNextTokenSem();char * getCurr
12、entTokenSem();void setCur(TokenList * t);void setOutputStream(ofstream * o);void setHead(TokenList * t);bool read();bool matchSem(const char);bool matchLex(LexType l);bool matchNextLex(LexType l);bool matchNextSem(const char s);LexType getNextTokenLex();LexType getCurTokenLex();parse(TokenList * ,of
13、stream *);parse();void printTree(TreeNode * root);int getCurrentTokenLineNO();public:static TreeNode * doParse(TokenList * , ofstream *);#endif/* scan.h */#ifndef _SCAN_H_#define _SCAN_H_#include TokenList.hclass scanpublic:static TokenList * doScan(ifstream * ,ofstream *);private:ifstream * inputFi
14、le;ofstream * outputFile;scan();scan(ifstream * , ofstream *);scan();ifstream * getInputFile();ofstream * getOutputFile();void setInputFile(ifstream *);void setOutputFile(ofstream *);void clearArray();char getNextChar();LexType lookup(char *);TokenList * getTokenList();void printTokenList(TokenList
15、*);#endif/* Token.h */#ifndef _TOKEN_H_#define _TOKEN_H_#include#includeusing namespace std;enum LexType ID,/标识符CHARC,/字符串RESERVEDWORD,/保留字INST,/整型常量符OP,/运算符ASSIGN,/赋值运算符DELIMITER,/分界符RANGE,/数组下标POINTER,/结构类型成员运算符DOT,/程序结束标志ENDFILE,/文件结束标志ERROR,/错误符号;class Tokenpublic:Token();Token(int, char*,LexTyp
16、e);Token();voidprintToken(ofstream *);voidprintToken1();voidsetLex(LexType);voidsetSem(char *);voidsetLine(int);intgetLine();LexType getLex();char *getSem();private:intline;char *sem;LexTypelex;#endif/*TokenList.h*/#ifndef _TOKENLIST_H_#define _TOKENLIST_H_#include Token.hclass TokenListpublic:Token
17、List();TokenList();voidsetToken(Token *);voidsetNext(TokenList *);voidsetNumber(int);intgetNumber();Token * getToken();TokenList * getNext();voidappend();private:int number;Token * token;TokenList * next;#endif/* Treenode.h */#ifndef _TREENODE_H_#define _TREENODE_H_#include #include #include using n
18、amespace std;enum NODE_KIND/语法树节点ProK,/根标志PheadK,/程序头TypeK,/类型声明VarK,/变量声明ProcDecK,/单个过程声明StmLK,/语句序列DecK,/声明节点StmtK,/语句节点ExpK/表达式节点;/*enum DEC_KIND/具体声明ArrayK,/数组CharK,/字符IntegerK,/整型RecordK,/记录IdK/Type定义的类型;enum STMT_KIND/具体语句IfK,/if语句WhileK,/while语句AssignK,/赋值语句ReadK,/read语句WriteK,/write语句CallK,/
19、过程调用语句ReturnK/返回语句;enum EXP_KIND/具体表达式OpK,/操作符ConstK,/常整型IdEK,/标识符ArrayEK,/数组FieldEK,/域CHAREK/单字符;union KIND/记录语法树节点具体类型DEC_KINDdec;/声明STMT_KINDstmt;/语句EXP_KINDexp;/表达式;/*enum PARAM_TYPE/过程参数属性valparamtype,/值参varparamtype/变参;enum EXP_OP_TYPE/表达式运算符类型LT,EQ,PLUS,MINUS,TIMES,OVER;struct ArrayAttr/数组属性i
20、nt 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;ExpAttr expattr;class TreeNodepublic:NODE_KINDnodekind;TreeNode *child3;TreeNode *sibling;char * idname3;intlineno;KIN
21、Dkind;char * type_name;Attrattr;intidnum;intchildnum;public:/设置表达式属性*void setAttrExpVal(int i);void setAttrExpOpType(char * s);EXP_OP_TYPE getAttrExpOpType();/设置过程属性*void setProcAttrParamType(PARAM_TYPE d);/设置数组属性*void setArrayAttrType(DEC_KIND d);void setArrayAttrUp(int u);void setArrayAttrLow(int
22、l);void setLineNo(int l);void setNodeKind(NODE_KIND n);void setTypeName(char * s);void setIdName(char *s);void setChild (int i , TreeNode * c);void setChild(TreeNode *);void setSibling(TreeNode * t);voidsetKindOfDec(DEC_KIND);DEC_KINDgetKindOfDec();voidsetKindOfStmt(STMT_KIND);voidsetKindOfExp(EXP_K
23、IND);TreeNode *getSibling();TreeNode *getChild(int i);intgetIdNum();char*getIdName(int i);TreeNode();TreeNode();TreeNode(NODE_KIND n);void printTreeNode();void printTreeNode(ofstream * out);#endif/* zparse.h */#ifndef _ZPARSE_H_#define _ZPARSE_H_#include TreeNode.h#include TokenList.h#define Sy_Stac
24、k_Max20 /符号栈大小#define OpS_Stack_Max20 /操作符栈大小#define OpN_Stack_Max 20 /操作数栈大小/*/*非终极符集*/*enum VN_VTVN_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_R
25、ecType,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,V
26、N_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,VN_OtherRelExp,VN_Exp,VN_OtherTerm,VN_Term,VN_OtherFactor,VN_Factor,VN_Variable,VN_VariMore,VN_
27、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_els
28、e,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;class treestackpublic:TreeNode * & tree;treestack * next;treestack * pre;treestack(TreeNode * & t):tree(t),next(NULL),pre(NULL);/*/*zparse类*/*class zparse
29、public:treestack *currentTree;treestack *headTree;inttreeNum;voidtree_push(TreeNode * &);void tree_pop();TreeNode *OptrOpS_Stack_Max;intoptrtop;voidoptr_push(TreeNode * );TreeNode * optr_pop();TreeNode *OpndOpN_Stack_Max;intopndtop;voidopnd_push(TreeNode * );TreeNode * opnd_pop();private:TokenList*h
30、ead;/TokenList头节点TokenList*cur;/TokenList当前节点TreeNode*root;/语法树根节点ofstream*output;/输出语法树intLL1Table7041;/语法分析表VN_VTSy_StackSy_Stack_Max;intsy_top;voidsy_push(VN_VT);VN_VTsy_pop();zparse(TokenList * , ofstream *);zparse();boolread();TokenList *getCur();intgetCurrentTokenLineNO();char *getCurrentToken
31、Sem();LexTypegetCurrentTokenLex();boolmatchSem(const char);boolmatchLex(LexType l);intconvertTokenToVT();boolmatchVT();voidExceptionThrow();TreeNode *parseLL();voidCreateTable();voidpredict(int num);intPriosity(TreeNode *);void process1();void process2();void process3();void process4();void process5
32、();void process6();void process7();void process8();void process9();void process10();void process11();void process12();void process13();void process14();void process15();void process16();void process17();void process18();void process19();void process20();void process21();void process22();void process
33、23();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 process38();void process39();void process40();void p
34、rocess41();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 process56();void process57();void process58();
35、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 process71();void process72();void process73();void process74();void process75();void proces
36、s76();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 process91();void process92();void process93();void
37、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 printTree(TreeNode * root);public:static TreeNode * doZParse(TokenList *
38、 ,ofstream *);#endif/* analyze.cpp */#include analyze.hint aexception;char * exceName;void ExceptionThrow(TreeNode * t)aexception = t-lineno;if (t-getIdName(0) != NULL)strcpy(exceName , t-getIdName(0);throw(exceName);throw(aexception);void analyze:doAnalyze(TreeNode * t, ofstream o)tryanalyze analyzer(o , t);analyzer.analysis(analyzer.root);catch(int exception)couterror in line exceptionendl;catch(char * exceName)cout