编译原理课程设计报告C语言词法与语法分析器的实现docWord格式.docx

上传人:b****6 文档编号:17715207 上传时间:2022-12-08 格式:DOCX 页数:4 大小:17.87KB
下载 相关 举报
编译原理课程设计报告C语言词法与语法分析器的实现docWord格式.docx_第1页
第1页 / 共4页
编译原理课程设计报告C语言词法与语法分析器的实现docWord格式.docx_第2页
第2页 / 共4页
编译原理课程设计报告C语言词法与语法分析器的实现docWord格式.docx_第3页
第3页 / 共4页
编译原理课程设计报告C语言词法与语法分析器的实现docWord格式.docx_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

编译原理课程设计报告C语言词法与语法分析器的实现docWord格式.docx

《编译原理课程设计报告C语言词法与语法分析器的实现docWord格式.docx》由会员分享,可在线阅读,更多相关《编译原理课程设计报告C语言词法与语法分析器的实现docWord格式.docx(4页珍藏版)》请在冰豆网上搜索。

编译原理课程设计报告C语言词法与语法分析器的实现docWord格式.docx

}/*判断读取的字符*/staticvoidmatchTokenTypeexpected{iftokenexpected{tokengetToken;

}else{syntaxError“unexpectedtoken-“;

printTokentoken,tokenString;

fprintflisting,““;

}}/*进行语法分析,构建语法树*/TreeNode*declaration_listvoid{TreeNode*tdeclaration;

TreeNode*pt;

whiletokenINT||tokenVOID{TreeNode*qdeclaration;

ifqNULL{iftNULLtpq;

else/*nowpcannotbeNULLeither*/{p-siblingq;

pq;

}}}returnt;

}TreeNode*declarationvoid{TreeNode*tNULL;

switchtoken{caseVOIDcaseINTtnewStmtNodeDecK;

iftokenINTt-typeInteger;

elset-typeVoid;

matchtoken;

switchtoken{caseIDt-attr.namecopyStringtokenString;

t-kind.stmtVarDK;

matchID;

switchtoken{caseLZKHt-kind.stmtVarDK;

t-typeIntArray;

matchLZKH;

matchNUM;

matchRZKH;

matchSEMI;

break;

caseLPARENt-kind.stmtFunDK;

matchLPAREN;

t-child[0]params;

matchRPAREN;

t-child[1]compound_stmt;

defaultmatchSEMI;

break;

}break;

defaultsyntaxError“unexpectedtoken-“;

tokengetToken;

defaultsyntaxError“unexpectedtoken-“;

}/*endcase*/returnt;

}TreeNode*paramsvoid{TreeNode*tNULL;

iftokenVOID{matchtoken;

tnewStmtNodeParamList;

t-child[0]newStmtNodeParamK;

t-child[0]-typeVoid;

}elseiftokenRPARENtNULL;

else{tparam_list;

}returnt;

}TreeNode*param_listvoid{TreeNode*tnewStmtNodeParamList;

inti1;

t-child[0]param;

whiletokenRPAREN{matchDOT;

t-child[i]param;

i;

}TreeNode*paramvoid{TreeNode*tNULL;

matchINT;

tnewStmtNodeParamK;

t-typeInteger;

t-attr.namecopyStringtokenString;

iftokenLZKH{t-typeIntArray;

}TreeNode*compound_stmtvoid{TreeNode*tnewStmtNodeComK;

matchLDKH;

t-child[0]local_declarations;

t-child[1]statement_list;

matchRDKH;

returnt;

}TreeNode*local_declarationsvoid{TreeNode*tnewStmtNodeLocalDecK;

inti0;

whiletokenINT||tokenVOID{t-child[i]declaration;

}TreeNode*statement_listvoid{TreeNode*tnewStmtNodeStmtList;

whiletokenRDKH{t-child[i]statement;

}TreeNode*statementvoid{TreeNode*t;

switchtoken{caseIFtif_stmt;

caseWHILEtwhile_stmt;

caseIDcaseSEMItexpression_stmt;

caseRETURNtreturn_stmt;

caseLDKHtcompound_stmt;

}TreeNode*expression_stmtvoid{TreeNode*tnewStmtNodeExpstmtK;

iftokenSEMImatchSEMI;

else{texpression;

}TreeNode*if_stmtvoid{TreeNode*tnewStmtNodeIfK;

iftNULL{matchIF;

t-child[0]expression;

t-child[1]statement;

iftokenELSE{matchELSE;

iftNULLt-child[2]newStmtNodeElseK;

t-child[2]-child[0]statement;

}}returnt;

}TreeNode*while_stmtvoid{TreeNode*tnewStmtNodeWhileK;

matchWHILE;

iftNULLt-child[0]expression;

iftNULLt-child[1]statement;

}TreeNode*return_stmtvoid{TreeNode*tnewStmtNodeRetK;

iftokenRETURNmatchRETURN;

else{t-child[0]expression;

}TreeNode*expressionvoid{TreeNode*tsimple_exp;

}TreeNode*varvoid{TreeNode*tnewExpNodeIdK;

iftNULLmatchID;

iftokenLZKH{matchtoken;

t-typeArrayUnit;

}TreeNode*simple_expvoid{TreeNode*tadditive_expression;

iftNULL{iftokenLT||tokenLE||tokenMT||tokenME||tokenEQ||tokenNEQ{TreeNode*pnewExpNodeOpK;

ifpNULL{p-attr.optoken;

p-child[0]t;

p-child[1]additive_expression;

tp;

}TreeNode*additive_expressionvoid{TreeNode*tterm;

whiletokenPLUS||tokenMINUS{TreeNode*pnewExpNodeOpK;

p-attr.optoken;

p-child[1]term;

tp;

}TreeNode*termvoid{TreeNode*tfactor;

whiletokenTIMES||tokenOVER{TreeNode*pnewExpNodeOpK;

ifpNULL{p-child[0]t;

p-child[1]factor;

}}returnt;

}TreeNode*factorvoid{TreeNode*tNULL;

switchtoken{caseNUMtnewExpNodeConstK;

iftNULLmatchNUM;

caseIDtvar;

iftokenASSIGN{TreeNode*pnewStmtNodeAssignK;

p-attr.namet-attr.name;

p-child[0]expression;

}iftokenLPAREN{TreeNode*pnewStmtNodeCallK;

p-child[0]args;

caseLPARENmatchLPAREN;

texpression;

}TreeNode*argsvoid{TreeNode*tnewStmtNodeArgList;

iftokenRPAREN{t-child[0]arg_list;

}elsereturnNULL;

}TreeNode*arg_listvoid{TreeNode*tnewStmtNodeArgK;

iftokenRPARENt-child[0]expression;

whiletokenRPAREN{matchDOT;

t-child[i]expression;

}TreeNode*parsevoid{TreeNode*t;

tdeclaration_list;

iftokenENDFILEsyntaxError“Codeendsbeforefile\n“;

}scan.cppinclude“globals.h“include“util.h“include“scan.h“/*对扫描的字符进行匹配判断*/TokenTypegetTokenvoid{/*indexforstoringintotokenString*/inttokenStringIndex0;

/*holdscurrenttokentobereturned*/TokenTypecurrentToken;

/*currentstate-alwaysbeginsatSTART*/StateTypestateSTART;

/*flagtoindicatesavetotokenString*/intsave;

whilestateDONE{intcgetNextChar;

saveTRUE;

switchstate{caseSTARTifisdigitcstateINNUM;

elseifisalphacstateINID;

elseifcstateINEQUAL;

elseifcstateINME;

elseifc||c\t||c\nsaveFALSE;

elseifcstateINNEQ;

elseifc/{ifgetNextChar*{ungetNextChar;

stateDONE;

currentTokenOVER;

}else{saveFALSE;

stateINCOMMENT;

}}else{stateDONE;

switchc{caseEOFsaveFALSE;

currentTokenENDFILE;

casecurrentTokenPLUS;

case-currentTokenMINUS;

case*currentTokenTIMES;

casecurrentTokenLPAREN;

casecurrentTokenRPAREN;

case;

currentTokenSEMI;

case[currentTokenLZKH;

case]currentTokenRZKH;

case{currentTokenLDKH;

case}currentTokenRDKH;

case,currentTokenDOT;

defaultcurrentTokenERROR;

}}break;

caseINCOMMENTsaveFALSE;

ifcEOF{stateDONE;

currentTokenERROR;

}elseifc*{ifgetNextChar/{stateSTART;

}else{ungetNextChar;

caseINNEQstateDONE;

ifccurrentTokenNEQ;

else{ungetNextChar;

saveFALSE;

currentTokenERROR;

caseINEQUALstateDONE;

ifccurrentTokenEQ;

else{/*backupintheinput*/ungetNextChar;

currentTokenASSIGN;

caseINNUMifisdigitc{/*backupintheinput*/ungetNextChar;

saveFALSE;

currentTokenNUM;

caseINIDifisalphac{/*backupintheinput*/ungetNextChar;

currentTokenID;

caseINLEstateDONE;

ifccurrentTokenLE;

currentTokenLT;

caseINMEstateDONE;

ifccurrentTokenME;

currentTokenMT;

caseDONEdefault/*shouldneverhappen*/fprintflisting,“ScannerBugstated\n“,state;

}ifsavebreak;

caseNEQfprintflisting,“\n“;

caseASSIGNfprintflisting,“\n“;

caseDOTfprintflisting,“,\n“;

caseLZKHfprintflisting,“[\n“;

caseRZKHfprintflisting,“]\n“;

caseLDKHfprintflisting,“{\n“;

caseRDKHfprintflisting,“}\n“;

caseLZSfprintflisting,“/*\n“;

caseRZSfprintflisting,“*/\n“;

caseMEfprintflisting,“\n“;

caseLEfprintflisting,“child[i]NULL;

}t-siblingNULL;

t-nodekindStmtK;

t-kind.stmtkind;

t-linenolineno;

}/*FunctionnewExpNodecreatesanewexpressionnodeforsyntaxtreeconstruction*/TreeNode*newExpNodeExpKindkind{TreeNode*tTreeNode*mallocsizeofTreeNode;

inti;

iftNULL{fprintflisting,“Outofmemoryerroratlined\n“,lineno;

}else{fori0;

ichild[i]NULL;

t-nodekindExpK;

t-kind.expkind;

t-typeVoid;

}char*copyStringchar*s{intn;

char*t;

ifsNULL{returnNULL;

}nstrlens1;

tchar*mallocn;

/*其作用是在内存的动态存储区中分配一个长度为n的连续空间.保存tokenstring*/iftNULL{fprintflisting,“Outofmemoryerroratlined\n“,lineno;

}else{strcpyt,s;

/*该函数是字符串拷贝函数,用来将一个字符串复制到一个字符数组中。

*//*例如strcpystr1,“china“;

作用是将”China“这个字符串拷贝到str1数组中*/}returnt;

}staticintindentno0;

defineINDENTindentno2defineUNINDENTindentno-2staticvoidprintSpacesvoid{inti;

fori0;

inodekindStmtK{switchtree-kind.stmt{caseIfKfprintflisting,“If\n“;

caseWhileKfprintflisting,“While\n“;

caseElseKfprintfl

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 电力水利

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1