资源描述:
《编译原理(PL0编译程序源代码).doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、下载可编辑/*PL/0编译程序(C语言版)*编译和运行环境:*VisualC++6.0*WinXP/7*使用方法:*运行后输入PL/0源程序文件名*回答是否将虚拟机代码写入文件*回答是否将符号表写入文件*执行成功会产生四个文件(词法分析结果.txt符号表.txt虚拟代码.txt源程序和地址.txt)*/#include#include"pl0.h"#include"string"#definestacksize500//解释执行时使用的栈intmain(){boolnxtlev[symnum];printf("请输入源程序文件名:");scanf("%s",fna
2、me);fin=fopen(fname,"r");//以只读方式打开pl0源程序文件cifa=fopen("词法分析结果.txt","w");fa1=fopen("源程序和地址.txt","w");//输出源文件及各行对应的首地址fprintf(fa1,"输入pl0源程序文件名:");fprintf(fa1,"%s",fname);if(fin){printf("是否将虚拟机代码写入文件?(Y/N)");//是否输出虚拟机代码scanf("%s",fname);listswitch=(fname[0]=='y'
3、
4、fname[0]=='Y');printf("是否将符号表写入文
5、件?(Y/N)");//是否输出符号表scanf("%s",fname);tableswitch=(fname[0]=='y'
6、
7、fname[0]=='Y');init();//初始化err=0;cc=cx=ll=0;ch='';if(-1!=getsym()){fa=fopen("虚拟代码.txt","w");fas=fopen("符号表.txt","w");.专业.整理.下载可编辑addset(nxtlev,declbegsys,statbegsys,symnum);nxtlev[period]=true;if(-1==block(0,0,nxtlev)){//调用编译程序fc
8、lose(fa);fclose(fa1);fclose(fas);fclose(fin);return0;}if(sym!=period){error(9);//结尾丢失了句号}if(err!=0){printf("pl0源程序出现错误,退出编译!!!请从第一个错误处开始修改.");fprintf(cifa,"源程序出现错误,请检查!!!");fprintf(fa1,"源程序出现错误,请检查!!!");fprintf(fa,"源程序出现错误,请检查!!!");fprintf(fas,"源程序出现错误,请检查!!!");}fclose(fa);fclose(fa1);fclo
9、se(fas);}fclose(fin);}else{printf("Can'topenfile!");}fclose(cifa);//printf("");return0;}voidinit(){//初始化inti;for(i=0;i<=255;i++)ssym[i]=nul;//设置单字符符号ssym['+']=plus;ssym['-']=minus;ssym['*']=times;ssym['/']=slash;ssym['(']=lparen;.专业.整理.下载可编辑ssym[')']=rparen;ssym['=']=eql;ssym[',']=comma;ss
10、ym['.']=period;ssym['#']=neq;ssym[';']=semicolon;strcpy(&(word[0][0]),"begin");//保留字设置,以字母顺序排列便于折半查找strcpy(&(word[1][0]),"call");strcpy(&(word[2][0]),"const");strcpy(&(word[3][0]),"do");strcpy(&(word[4][0]),"end");strcpy(&(word[5][0]),"if");strcpy(&(word[6][0]),"odd");strcpy(&(word[7][0]),"pro
11、cedure");strcpy(&(word[8][0]),"read");strcpy(&(word[9][0]),"then");strcpy(&(word[10][0]),"var");strcpy(&(word[11][0]),"while");strcpy(&(word[12][0]),"write");wsym[0]=beginsym;//设置保留字类别一字即一类wsym[1]=callsym;wsym[2]=constsym;wsym[3]=dosym;