预览加载中,请您耐心等待几秒...
在线预览结束,喜欢就下载吧,查找使用更方便
如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
编译原理实验一词法分析输入:source.c输出:token[40]tokenstring[40][30]以下样例程序已经能够识别:变量、数、赋值号、分号要求修改代码使其能够识别:变量、数、赋值号、分号、加号、减号、乘号、除号自动机:字母数其他INID字母数INNUMDONESTART数其它INSEMIINASSIGN:=;+INADDDD样例程序#include<stdio.h>#include<ctype.h>FILE*source;/*源程序文件句柄*/typedefenum{PLUS,MINUS,TIMES,OVER,LPAREN,RPAREN,SEMI,ASSIGN,NUM,ID,DOLLAR}tokentype;/*记号*/typedefenum{START,INASSIGN,INCOMMENT,INNUM,INID,INSEMI,INDOLLAR,DONE}statetype;/*状态*/tokentypetoken[40];/*存记号*/chartokenstring[40][30];/*存记号串*/intwordindex=0;/*以上两个数组的索引*/intback=0;/*0不回退字符,1回退字符*/intc;/*存获得的字符*/intgetnextchar();/*获得下一个字符*/intgettoken();/*获得下一个记号*/main(){inti;source=fopen(".\\source.c","r");while(gettoken()!=0){wordindex++;}for(i=0;i<=wordindex;i++)printf("word:%s\n",tokenstring[i]);fclose(source);}intgetnextchar(){intc=fgetc(source);if((c=='\n')||(c=='\t')||(c==''))/*忽略无效字符*/c=fgetc(source);returnc;}intgettoken(){inttokenStringIndex=0;intsave;statetypestate=START;while(state!=DONE){if(back==0)c=getnextchar();save=1;switch(state){caseSTART:if(isdigit(c))state=INNUM;elseif(isalpha(c))state=INID;elseif(c==':'){state=INASSIGN;back=0;}elseif(c==';')state=INSEMI;elseif(c=='$'){state=INDOLLAR;back=1;/*回退一个字符*/}break;caseINASSIGN:state=DONE;if(c=='=')token[wordindex]=ASSIGN;break;caseINSEMI:back=0;save=0;state=DONE;token[wordindex]=SEMI;break;caseINDOLLAR:state=DONE;token[wordindex]=DOLLAR;save=0;break;caseINNUM:if(!isdigit(c)){back=1;/*回退一个字符*/save=0;state=DONE;token[wordindex]=NUM;}break;caseINID:if((!isalpha(c))&&(!isdigit(c))){back=1;/*回退一个字符*/save=0;state=DONE;token[wordindex]=ID;}break;}if(save==1)tokenstring[wordindex][tokenStringIndex++]=(char)c;if(state==DONE){tokenstring[wordindex][tokenStringIndex]='\0';}}if(c=='$')return0;elsereturn1;}样例source.c文件x12:=100;y34:=200;z56:=x12$ASCII码参考字符ASCII码字符ASCII码a97149b250c351x120空格32y121\t9z122\n10A65,44B66;59C67:58X88=61Y89Z90调试时,