编译原理的词法分析

编译原理的词法分析

ID:40269451

大小:46.00 KB

页数:4页

时间:2019-07-30

编译原理的词法分析_第1页
编译原理的词法分析_第2页
编译原理的词法分析_第3页
编译原理的词法分析_第4页
资源描述:

《编译原理的词法分析》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、#include"stdio.h"/*定义I/O库所用的某些宏和变量*/#include"string.h"/*定义字符串库函数*/#include"conio.h"/*提供有关屏幕窗口操作函数*/#include"ctype.h"/*分类函数*/charprog[80]={''},token[8];/*存放构成单词符号的字符串*/charch;intsyn,/*存放单词字符的种别码*/n,sum,/*存放整数型单词*/m,p;/*p是缓冲区prog的指针,m是token的指针*/char*rwtab[6]

2、={"begin","if","then","while","do","end"};voidscaner(){m=0;sum=0;for(n=0;n<8;n++)token[n]='';ch=prog[p++];while(ch=='')ch=prog[p++];if(isalpha(ch))/*ch为字母字符*/{while(isalpha(ch)

3、

4、isdigit(ch))/*ch为字母字符或者数字字符*/{token[m++]=ch;ch=prog[p++];}token[m++]='';ch=p

5、rog[p--];syn=10;for(n=0;n<6;n++)if(strcmp(token,rwtab[n])==0)/*字符串的比较*/{syn=n+1;break;}}elseif(isdigit(ch))/*ch是数字字符*/{while(isdigit(ch))/*ch是数字字符*/{sum=sum*10+ch-'0';ch=prog[p++];}ch=prog[p--];syn=11;}elseswitch(ch){case'<':m=0;token[m++]=ch;ch=prog[p++];if

6、(ch=='>'){syn=21;token[m++]=ch;}elseif(ch=='='){syn=22;token[m++]=ch;}else{syn=20;ch=prog[p--];}break;case'>':m=0;token[m++]=ch;ch=prog[p++];if(ch=='='){syn=24;token[m++]=ch;}else{syn=23;ch=prog[p--];}break;case':':m=0;token[m++]=ch;ch=prog[p++];if(ch=='='){

7、syn=18;token[m++]=ch;}else{syn=17;ch=prog[p--];}break;case'+':syn=13;token[0]=ch;break;case'-':syn=14;token[0]=ch;break;case'*':syn=15;token[0]=ch;break;case'/':syn=16;token[0]=ch;break;case'=':syn=25;token[0]=ch;break;case';':syn=26;token[0]=ch;break;case'(

8、':syn=27;token[0]=ch;break;case')':syn=28;token[0]=ch;break;case'#':syn=0;token[0]=ch;break;default:syn=-1;}}main(){printf("Thesignificanceofthefigures:""1.figures1to6saidKeyword""2.figures10and11saidOtherindicators""3.figures13to28saidOperators"

9、);p=0;printf("pleaseinputstring:");do{ch=getchar();prog[p++]=ch;}while(ch!='#');p=0;do{scaner();switch(syn){case11:printf("(%d,%d)",syn,sum);break;case-1:printf("ERROR;");break;default:printf("(%d,%s)",syn,token);}}while(syn!=0);getch();}对源程序begi

10、nx:=9:ifx>9thenx:=2*x+1/3;end#

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。