编译原理词法分析和语法分析报告+代码(C语言版) (2).doc
上传人:qw****27 上传时间:2024-09-12 格式:DOC 页数:5 大小:33KB 金币:15 举报 版权申诉
预览加载中,请您耐心等待几秒...

编译原理词法分析和语法分析报告+代码(C语言版) (2).doc

编译原理词法分析和语法分析报告+代码(C语言版)(2).doc

预览

在线预览结束,喜欢就下载吧,查找使用更方便

15 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

词法分析实验目的设计、编制并调试一个词法分析程序,加深对词法分析原理的理解。实验要求词法分析程序的功能:输入:所给文法的源程序字符串。输出:二元组(syn,token或sum)构成的序列。其中:syn为单词种别码;token为存放的单词自身字符串;sum为整型常数。三、词法分析程序的C语言程序源代码:#include<stdio.h>#include<string.h>charprog[80],token[8],ch;intsyn,p,m,n,sum;char*rwtab[6]={"begin","if","then","while","do","end"};scaner();main(){p=0;printf("\npleaseinputastring(endwith'#'):/n");do{scanf("%c",&ch);prog[p++]=ch;}while(ch!='#');p=0;do{scaner();switch(syn){case11:printf("(%-10d%5d)\n",sum,syn);break;case-1:printf("youhaveinputawrongstring\n");getch();exit(0);default:printf("(%-10s%5d)\n",token,syn);break;}}while(syn!=0);getch();}scaner(){sum=0;for(m=0;m<8;m++)token[m++]=NULL;ch=prog[p++];m=0;while((ch=='')||(ch=='\n'))ch=prog[p++];if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))){while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9'))){token[m++]=ch;ch=prog[p++];}p--;syn=10;for(n=0;n<6;n++)if(strcmp(token,rwtab[n])==0){syn=n+1;break;}}elseif((ch>='0')&&(ch<='9')){while((ch>='0')&&(ch<='9')){sum=sum*10+ch-'0';ch=prog[p++];}p--;syn=11;}elseswitch(ch){case'<':token[m++]=ch;ch=prog[p++];if(ch=='='){syn=22;token[m++]=ch;}else{syn=20;p--;}break;case'>':token[m++]=ch;ch=prog[p++];if(ch=='='){syn=24;token[m++]=ch;}else{syn=23;p--;}break;case'+':token[m++]=ch;ch=prog[p++];if(ch=='+'){syn=17;token[m++]=ch;}else{syn=13;p--;}break;case'-':token[m++]=ch;ch=prog[p++];if(ch=='-'){syn=29;token[m++]=ch;}else{syn=14;p--;}break;case'!':ch=prog[p++];if(ch=='='){syn=21;token[m++]=ch;}else{syn=31;p--;}break;case'=':token[m++]=ch;ch=prog[p++];if(ch=='='){syn=25;token[m++]=ch;}else{syn=18;p--;}break;case'*':syn=15;token[m++]=ch;break;case'/':syn=16;token[m++]=ch;break;case'(':syn=27;token[m++]=ch;break;case')':syn=28;token[m++]=ch;break;case'{':syn=5;token[m++]=ch;break;case'}':syn=6;token[m++]=ch;break;case';':syn=26;token[m++]=ch;break;case'\"':sy