电子科技大学-计算机学院-编译原理实验-语法分析.doc

上传人:豆**** 文档编号:27113877 上传时间:2022-07-22 格式:DOC 页数:127 大小:205.50KB
返回 下载 相关 举报
电子科技大学-计算机学院-编译原理实验-语法分析.doc_第1页
第1页 / 共127页
电子科技大学-计算机学院-编译原理实验-语法分析.doc_第2页
第2页 / 共127页
点击查看更多>>
资源描述

《电子科技大学-计算机学院-编译原理实验-语法分析.doc》由会员分享,可在线阅读,更多相关《电子科技大学-计算机学院-编译原理实验-语法分析.doc(127页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-date电子科技大学-计算机学院-编译原理实验-语法分析#include/ SyntaxAnalyzer.cpp : 定义控制台应用程序的入口点。/#include #include #include #define MAX_COUNT 1024#define SIGN_UNDEFINED_ERR 1#define SIGN_REDEFINED_ERR 2#define SI

2、GN_EXECUTE_ERR 3#define NO_SIGN_ERR 4#define SIGN_RESERVE_ERR 5#define NO_PARA_ERR 6/*types是支持类型的集合*/typedef enum integer types;/*记录变量信息的结构体*/typedef struct char vname17;char vproc17;bool vkind;types vtype;int vlev;int vadr; varRecord;/*记录过程信息的结构体*/typedef structchar pname17;types ptype;int plev;int

3、 varNum;int fadr;int ladr;int parameter;bool parameterIsDefined; proRecord;/*文法产生式如下*A:程序A-BB:分程序B-begin C;M endC:说明与句表C-DCC-;DC|D:说明语句D-E|JE:变量说明E-integer FF:变量F-GG:标识符G-HGG-HG|IG|H:字母H-a|.|z|A|.|ZI:数字I-0|1|.|9J:函数说明J-integer function G(K);LK:参数K-FL:函数体L-begin C;M endM:执行语句表M-NMM-;NM|N:执行语句N-O|P|Q|

4、WO:读语句O-read(F)P:写语句P-write(F)Q:赋值语句Q-F:=RR:算术表达式R-SRR-SR|S:项S-TSS-*TS|T:因子T-F|U|ZU:常数U-VV:无符号整数V-IVV-IV|W:条件语句W-if X then N else NX:条件表达式X-RYRY:关系运算符Y-|=|=|Z:函数调用Z-G(R)*/void A();void B();void C();void C_();void D();void E();void F();void G();void J();void K();void L();void M();void M_();void N();v

5、oid O();void P();void Q();void R();void R_();void S();void S_();void T();void U();void W();void X();void Y();void Z();/*初始化函数:从输入文件读取数据,建立各个文件,初始化全局变量*/bool init(int argc, char* argv);/*结束处理函数,将var和pro数组中的元素输出到相应文件,填充输出文件*/bool final();/*错误处理函数,参数分别为行号、错误码和错误符号*/bool error(int lineNum, int errNum, c

6、onst char* sign);/*获得所处目录路径,包括最后斜杠,或者为空*/void getPath(char* in, char* out);/*获得文件名,不包括扩展*/void getFilename(char* in, char* out);/*获得下一符号,true表示已到队尾,false表示还未到队尾*/bool nextToken();/*获得当前符号的下一字符,true表示已到0*/bool nextChar();/*判断变量是否已存在*/bool isVarExisted(char* vname, char* vproc, bool vkind);/*判断过程是否已存在

7、,参数为过程名*/bool isProExisted(char* vname);/*获得下一符号,指针不变*/int getNextToken();char inputMAX_COUNT17;/存放输入文件所有符号的数组int kindMAX_COUNT;int inputCount;/输入符号的数量int pToken;/指向当前输入符号int pChar;/指向当前输入符号中的当前字符varRecord currentVar;/存放当前变量的信息proRecord currentPro;/存放当前过程的信息int lineNum;/当前行号varRecord varMAX_COUNT;/存

8、放变量名表项数组proRecord proMAX_COUNT;/存放过程名表项数组int varCount;/变量的数量int proCount;/过程的数量FILE* inFile;/输入文件句柄FILE* outFile;/输出文件句柄FILE* errFile;/错误文件句柄FILE* varFile;/变量文件句柄FILE* proFile;/过程文件句柄/*主函数*/int main(int argc, char* argv)if (init(argc, argv)A();final();return 0;bool init(int argc, char* argv)if (argc

9、 != 2)return false;elsechar* inFilename = argv1;char outFilenameMAX_COUNT = ;char errFilenameMAX_COUNT = ;char varFilenameMAX_COUNT = ;char proFilenameMAX_COUNT = ;char filenameMAX_COUNT = ;char pathMAX_COUNT = ;/获得文件名(不包括扩展名)和路径getFilename(inFilename, filename);getPath(inFilename, path);/生成输出文件全部路径

10、strcat(outFilename, path);/strcat(outFilename, );strcat(outFilename, filename);strcat(outFilename, .dys);/生成错误文件全部路径strcat(errFilename, path);/strcat(errFilename, );strcat(errFilename, filename);strcat(errFilename, .err);/生成变量文件全部路径strcat(varFilename, path);/strcat(varFilename, );strcat(varFilename,

11、 filename);strcat(varFilename, .var);/生成过程文件全部路径strcat(proFilename, path);/strcat(proFilename, );strcat(proFilename, filename);strcat(proFilename, .pro);/打开文件句柄if (inFile = fopen(inFilename, r) & (outFile = fopen(outFilename, w) & (errFile = fopen(errFilename, w) & (varFile = fopen(varFilename, w) &

12、 (proFile = fopen(proFilename, w)/初始化单词指针、字符指针、行号、层次inputCount = 0;pToken = 0;pChar = 0;lineNum = 1;/当前行号/level = 0;/当前层次/varCountInPro = 0;strcpy(currentPro.pname, );currentPro.plev = 0;currentPro.varNum = 0;currentPro.parameter = -1;varCount = 0;proCount = 0;/读取输入文件内容,初始化input数组while (!feof(inFile

13、)char stringOfLineMAX_COUNT;if (fgets(stringOfLine, MAX_COUNT, inFile)char lineString20 = ;strncpy(lineString, stringOfLine, 19);char* kindString = strrchr(lineString, );kindinputCount = atoi(kindString+1);char string17 = ;strncpy(string, stringOfLine, 16);char* lastString = strrchr(string, );strcpy

14、(inputinputCount, lastString + 1);inputCount+;return true;elsefclose(inFile);fclose(outFile);fclose(errFile);fclose(varFile);fclose(proFile);return false;bool final()for (int i = 0; i varCount; i+)int vkind = vari.vkind ? 1 : 0;char* vtype = (vari.vtype = integer) ? integer : ;fprintf(varFile, %16s

15、%16s %d %s %d %dn, vari.vname, vari.vproc, vkind, vtype, vari.vlev, vari.vadr);for (int i = 0; i proCount; i+)char* ptype = (proi.ptype = integer) ? integer : ;fprintf(proFile, %16s %s %d %d %dn, proi.pname, ptype, proi.plev, proi.fadr, proi.ladr);if (fseek(inFile, 0, 0) = 0)while (!feof(inFile)fput

16、c(fgetc(inFile), outFile);bool val;val = fclose(inFile);val = fclose(outFile);val = fclose(errFile);val = fclose(varFile);val = fclose(proFile);return val;bool error(int errNum,const char* symbol)char* errInfo;switch (errNum)case SIGN_UNDEFINED_ERR:fprintf(errFile, *LINE:%d %s符号无定义n, lineNum, inputp

17、Token);break;case SIGN_REDEFINED_ERR:fprintf(errFile, *LINE:%d %s符号重定义n, lineNum, inputpToken);break;case SIGN_EXECUTE_ERR:fprintf(errFile, *LINE:%d %s处不能匹配执行语句n, lineNum, inputpToken);break;case NO_SIGN_ERR:fprintf(errFile, *LINE:%d %s处缺少%sn, lineNum,inputpToken, symbol);break;case SIGN_RESERVE_ERR

18、:errInfo = 以保留字开头;break;case NO_PARA_ERR:fprintf(errFile, *LINE:%d 缺少形参%s的声明n, lineNum, symbol);break;default:errInfo = 未知错误;return true;void getPath(char* in, char* out)char* name;name = strrchr(in, );if (name != NULL)strncpy(out, in, strlen(in) - strlen(name) + 1);elsestrcpy(out, );void getFilenam

19、e(char* in, char* out)char* fullName;char* extension;fullName = strrchr(in, );extension = strrchr(in, .);if (fullName != NULL)strncpy(out, fullName + 1, strlen(fullName) - 1 - strlen(extension);elsestrncpy(out, in, strlen(in) - strlen(extension);bool nextToken()pToken+;pChar = 0;if (strcmp(inputpTok

20、en, EOF) = 0)return true;while (strcmp(inputpToken, EOLN) = 0)pToken+;lineNum+;return false;bool nextChar()if (inputpTokenpChar = 0)/nextToken();return true;pChar+;return false;bool isVarExisted(char* vname, char* vproc, bool vkind)for (int i = 0; i varCount; i+)if (strcmp(vname, vari.vname) = 0) &

21、(strcmp(vproc, vari.vproc) = 0) & (vari.vkind = vkind)return true;for (int i = 0; i proCount; i+)if (strcmp(vname, proi.pname) = 0)return true;return false;bool isProExisted(char* vname)for (int i = 0; i varCount; i+)if (strcmp(vname, vari.vname) = 0)return true;for (int i = 0; i proCount; i+)if (st

22、rcmp(vname, proi.pname) = 0)return true;return false;int getNextToken()int pNextToken = pToken + 1;while (strcmp(inputpNextToken, EOLN) = 0)pNextToken+;return pNextToken;void A()B();void B()if (strcmp(inputpToken, begin) = 0)nextToken();elseerror(NO_SIGN_ERR,begin);if (strcmp(inputpToken, integer)!=

23、0)nextToken();C();if (strcmp(inputpToken, ;) = 0)nextToken();elseerror(NO_SIGN_ERR, ;);if (strcmp(inputpToken, integer) != 0) & (strcmp(inputpToken, read) != 0) & (strcmp(inputpToken, write) != 0) & (kindpToken!=10)nextToken();M();if (strcmp(inputpToken, end) = 0)nextToken();elseerror(NO_SIGN_ERR, e

24、nd);void C()D();C_();void C_()if (strcmp(inputpToken, ;) = 0 & strcmp(inputgetNextToken(), integer) = 0)nextToken();D();C_();elseif (strcmp(inputpToken, integer) = 0)error(NO_SIGN_ERR, ;);D();C_();void D()if (strcmp(inputpToken + 1, function) = 0)J();elseE();void E()if (strcmp(inputpToken, integer)

25、= 0)nextToken();elseerror(NO_SIGN_ERR, integer);/if (kindpToken != 10)/nextToken();/strcpy(currentVar.vname, inputpToken);strcpy(currentVar.vproc, currentPro.pname);if (pToken = currentPro.parameter)currentVar.vkind = true;currentPro.parameterIsDefined = true;elsecurrentVar.vkind = false;currentVar.

26、vtype = integer;currentVar.vlev = currentPro.plev;currentVar.vadr = varCount;if (isVarExisted(inputpToken, currentPro.pname, currentVar.vkind)/如果存在变量error(SIGN_REDEFINED_ERR,NULL);else if (currentPro.varNum = 0)/如果当前过程中变量数为0,则当前变量是当前过程的第一个变量currentPro.fadr = currentVar.vadr;currentPro.ladr = current

27、Var.vadr;/过程中最后一个变量在变量表中的位置currentPro.varNum+;/过程中变量数+varvarCount = currentVar;/当前变量存入var数组varCount+;/变量数+F();void F()G();void G()if (kindpToken = 10)nextToken();/*char* keywords = begin, end, integer, if, then, else, function, read, write ;int keywordLength = 5, 3, 7, 2, 4, 4, 8, 4, 5 ;for (int i =

28、 0; i 9; i+)if (strncmp(inputpToken, keywordsi, keywordLengthi) = 0)error(SIGN_RESERVE_ERR);H();G_();nextToken();*/void J()proRecord proBackup = currentPro;/备份当前过程,在匹配完G过程后恢复if (strcmp(inputpToken, integer) = 0)nextToken();elseerror(NO_SIGN_ERR, integer);if (strcmp(inputpToken, function) != 0)nextTo

29、ken();if (strcmp(inputpToken, function) = 0)nextToken();elseerror(NO_SIGN_ERR, function);if (kindpToken != 10)nextToken();strcpy(currentPro.pname, inputpToken);currentPro.ptype = integer;currentPro.plev+;currentPro.varNum = 0;currentPro.parameterIsDefined = false;if (isProExisted(inputpToken)error(S

30、IGN_REDEFINED_ERR,NULL);G();if (strcmp(inputpToken, () = 0)nextToken();elseerror(NO_SIGN_ERR, ();if (kindpToken != 10)nextToken();/*strcpy(currentVar.vname, inputpToken);strcpy(currentVar.vproc, currentPro.pname);currentVar.vkind = true;currentVar.vtype = integer;currentVar.vlev = currentPro.plev;cu

31、rrentVar.vadr = varCount;*/currentPro.parameter = pToken;K();/*if (currentPro.varNum = 0)currentPro.fadr = currentVar.vadr;currentPro.ladr = currentVar.vadr;currentPro.varNum+;varvarCount = currentVar;varCount+;*/if (strcmp(inputpToken, ) = 0)nextToken();elseerror(NO_SIGN_ERR, );if (strcmp(inputpTok

32、en, ;) != 0)nextToken();if (strcmp(inputpToken, ;) = 0)nextToken();elseerror(NO_SIGN_ERR, ;);if (strcmp(inputpToken, begin) != 0)nextToken();L();currentPro = proBackup;/匹配完G过程后恢复原过程void K()F();void L()if (strcmp(inputpToken, begin) = 0)nextToken();elseerror(NO_SIGN_ERR, begin);if (strcmp(inputpToken

33、, integer) != 0)nextToken();C();if (!currentPro.parameterIsDefined)error(NO_PARA_ERR, inputcurrentPro.parameter);proproCount = currentPro;/在这里而不是在J()函数最后把currentPro加入pro数组是因为M中可能会使用当前过程(递归)proCount+;if (strcmp(inputpToken, ;) = 0)nextToken();elseerror(NO_SIGN_ERR, ;);if (strcmp(inputpToken, integer)

34、 != 0) & (strcmp(inputpToken, read) != 0) & (strcmp(inputpToken, write) != 0) & (kindpToken != 10)nextToken();M();if (strcmp(inputpToken, end) = 0)nextToken();elseerror(NO_SIGN_ERR, end);if (strcmp(inputpToken, ;) != 0) & (strcmp(inputpToken, end) != 0)nextToken();/*if (strcmp(inputpToken, begin) =

35、0)nextToken();C();proproCount = currentPro;/在这里而不是在J()函数最后把currentPro加入pro数组是因为M中可能会使用当前过程(递归)proCount+;if (strcmp(inputpToken, ;) = 0)nextToken();M();if (strcmp(inputpToken, end) = 0)nextToken();elseerror(SIGN_MATCH_ERR);elseerror(SIGN_MATCH_ERR);elseerror(SIGN_MATCH_ERR);*/void M()N();M_();void M_

36、()if (strcmp(inputpToken, ;) = 0)nextToken();N();M_();else/if (strcmp(inputpToken, read) = 0) | (strcmp(inputpToken, write) = 0)|(strcmp(inputpToken, if) = 0)|(kindpToken = 10)if (strcmp(inputpToken, end) != 0) & (strcmp(inputpToken,EOF) !=0 )error(NO_SIGN_ERR, ;);N();M_();void N()if (strcmp(inputpT

37、oken, read) = 0)O();else if (strcmp(inputpToken, write) = 0)P();else if (strcmp(inputpToken, if) = 0)W();else if (kindpToken = 10)Q();elseerror(SIGN_EXECUTE_ERR, NULL);nextToken();void O()if (strcmp(inputpToken, read) = 0)nextToken();elseerror(NO_SIGN_ERR, read);if (strcmp(inputpToken, () != 0)nextToken();if (strcmp(inputpToken, () = 0)nextToken();elseerror(NO_SIGN_ERR, ();if (kindpToken != 10)nextToken();if (!isVarExisted(inputpToke

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁