收藏 分享(赏)

编写PL-0语言词法分析程序(c).doc

上传人:fmgc7290 文档编号:6830127 上传时间:2019-04-23 格式:DOC 页数:11 大小:44.50KB
下载 相关 举报
编写PL-0语言词法分析程序(c).doc_第1页
第1页 / 共11页
编写PL-0语言词法分析程序(c).doc_第2页
第2页 / 共11页
编写PL-0语言词法分析程序(c).doc_第3页
第3页 / 共11页
编写PL-0语言词法分析程序(c).doc_第4页
第4页 / 共11页
编写PL-0语言词法分析程序(c).doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、编写 PL/0 语言的词法分析程序要求: 1、读入用 PL/0 语言编写的源程序,正确的进行词法分析,并输出二元式序列。 2、若源程序有词法错误,能够给出出错的准确位置。 3、词法代号如下 (+,+); (-,-); (*,*); (/,/); (,(); (),); (,,,); (;,;); (.,.); (#,#); (=,=); (,); (=,b); ( #include #include #include #include #define N 256/每一行的字符数不能超过 256 个 char bufferN; /用作存放一行字符 char word20; /用作存放经过分析单词

2、 char *kword13=“begin“,“call“,“const“,“do“,“end“,“if“,“odd“,“procedure“,“read“,“then“,“var“,“while“,“write“; char ktype13=f,g,h,i,j,k,l,m,n,o,p,q,r; int len;/记录每一行的长度 int count=0;/用来记录行数 void write(char *wstr,char wc,FILE *wout)/将分析结果按照规则写入到文件 fputc(,wout); fputs(wstr,wout); fputc(,wout); fputc(wc,w

3、out); fputc(),wout); int readbuffer(FILE *fp) char ch; len=0; ch=fgetc(fp); while(!feof(fp) ch=fgetc(fp); len+; len-;/用来控制词法分析时行分析中字母的个数 if(feof(fp)/标志文件是否结束 return 0; else return 1; void error(int type) if(type=1) printf(“为无效字符,第%d 行词法出错,标志符不能以数字开头n“,count); else if(type=2) printf(“第%d 行词法出错,赋值符应为:

4、= n “,count); else printf(“为无效字符,第 %d 行词法出错n“,count); void check(char *str,FILE *out);/声明函数,此函数用来分类单词 void fenxi(char *row,FILE *op)/此函数用来对每一行的单词进行语法分析 /printf(“%dn“,count); int k=0;/用作控制临时存放单词的变量 str0 int i=0;/定义两个变量用作控制每一行是否结束, int ferror=0;/用作出错标志 char str020;/临时存放单词的变量 while(i|rowi=|rowi=|rowi=|

5、rowi=|rowi=的运算符后面的=再次被判断 if(rowi=:) i+; if(rowi=) /word0=:; /word1=; /word2=0; strcpy(word,“=“); else error(2);/出错后调用处理函数 i-; if(rowi=) i+; if(rowi=) strcpy(word,“=“); else strcpy(word,“); i-; if(rowi=) if(str1=) write(str,b,out); else write(str,out); if(str0=) if(str1=) write(str,c,out); else write

6、(str,out); if(!strcmp(str,“:=“) write(str,a,out); if(str0=+ | str0=- | str0=* | str0=/ | str0=( | str0=) | str0=, | str0=;| str0=.| str0=# | str0= ) write(str,str0,out); void main() count=1; char scfilename20,rsfilename20;/定义用来存放输入源文件和输出目标文件的名字 printf(“Please input your source file name:“); gets(scf

7、ilename); printf(“Please input your result file name:“); gets(rsfilename); FILE *fp,*op; fp=fopen(scfilename,“r“); op=fopen(rsfilename,“w“); if(fp)/打开文件成功后调用函数对源文件进行词法分析 while(readbuffer(fp) fenxi(buffer,op); count+;/行加一 else/while the file not exist printf(“Your souce file not exist!n“); exit(0); fclose(fp);/close the files fclose(op); printf(“ok!“);/output the mark of end getchar();

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

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报