编译原理单词识别代码.docx
- 文档编号:23883669
- 上传时间:2023-05-21
- 格式:DOCX
- 页数:18
- 大小:17.57KB
编译原理单词识别代码.docx
《编译原理单词识别代码.docx》由会员分享,可在线阅读,更多相关《编译原理单词识别代码.docx(18页珍藏版)》请在冰豆网上搜索。
编译原理单词识别代码
头文件tou.h:
#definenorw17/*关键字个数*/
#definetxmax100/*名字表容量*/
#definenmax14/*number的最大位数*/
#defineal10/*符号的最大长度*/
#defineamax2047/*地址上界*/
#definelevmax3/*最大允许过程嵌套声明层数[0,lexmax]*/
#definecxmax200/*最多的虚拟机代码数*/
/*符号*/
enumsymbol{
nul,ident,number,plus,minus,
times,slash,oddsym,eql,neq,
lss,leq,gtr,geq,lparen,
rparen,comma,semicolon,period,becomes,
beginsym,endsym,ifsym,thensym,whilesym,
writesym,readsym,dosym,callsym,constsym,
varsym,procsym,exclamate,lbrace,rbrace,elsesym,intsym,returnsym,voidsym
};
#definesymnum32
/*-------------*/
enumobject{
constant,
variable,
procedur,
};
/*--------------*/
enumfct{
lit,opr,lod,sto,cal,inte,jmp,jpc,
};
#definefctnum8
/*--------------*/
structinstruction
{
enumfctf;
intl;
inta;
};
charch;
enumsymbolsym;
charid[al+1];
intn;
intnum;
intcc,ll;
intcx;
charline[81];
chara[al+1];
charword[norw][al];
enumsymbolwsym[norw];
enumsymbolssym[256];
charmnemonic[fctnum][5];
/*------------------------------*/
FILE*fin;
FILE*fout;
charfname[al];
interr;/*错误计数器*/
/*当函数中会发生fatalerror时,返回-1告知调用它的函数,最终退出程序*/
#definegetsymdoif(-1==getsym())return-1
#definegetchdoif(-1==getch())return-1
voiderror(intn);
intgetsym();
intgetch();
voidinit();
CPP文件:
#include"stdio.h"
#include"obj\tou.h"
#include"string.h"
intmain()
{
fin=fopen("try.txt","r");
fout=fopen("2.txt","w");
err=0;
n=0;
cc=cx=ll=0;
ch='';
init();
getsym();
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;
ssym['.']=period;
ssym['#']=neq;
ssym[';']=semicolon;
ssym['!
']=exclamate;
ssym['{']=lbrace;
ssym['}']=rbrace;
/*设置保留字名字,按照字母顺序,便于折半查找*/
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]),"else");
strcpy(&(word[6][0]),"if");
strcpy(&(word[7][0]),"int");
strcpy(&(word[8][0]),"odd");
strcpy(&(word[9][0]),"procedure");
strcpy(&(word[10][0]),"read");
strcpy(&(word[11][0]),"return");
strcpy(&(word[12][0]),"then");
strcpy(&(word[13][0]),"var");
strcpy(&(word[14][0]),"void");
strcpy(&(word[15][0]),"while");
strcpy(&(word[16][0]),"write");
/*设置保留字符号*/
wsym[0]=beginsym;
wsym[1]=callsym;
wsym[2]=constsym;
wsym[3]=dosym;
wsym[4]=endsym;
wsym[5]=elsesym;
wsym[6]=ifsym;
wsym[7]=intsym;
wsym[8]=oddsym;
wsym[9]=procsym;
wsym[10]=readsym;
wsym[11]=returnsym;
wsym[12]=thensym;
wsym[13]=varsym;
wsym[14]=voidsym;
wsym[15]=whilesym;
wsym[16]=writesym;
}
voiderror(intn)
{
charspace[81];
memset(space,32,81);printf("-------%c\n",ch);
space[cc-1]=0;//出错时当前符号已经读完,所以cc-1
printf("****%s!
%d\n",space,n);
err++;
}
intgetch()//每次调用此函数结束后CC会加一,然后下次调用时回和LL比较,所以这就是每次读一行,然后再读下一行
{
if((!
feof(fin))==0)
{
fprintf(fout,"%d",n);
fputs("",fout);
if(ch=='}')
{
fprintf(fout,"%s","(rbrase)");
}
}
if(cc==ll)
{
n++;
if(feof(fin))
{
printf("programincomplete");
return-1;
}
ll=0;
cc=0;
ch='';
while(ch!
=10)
{
if(EOF==fscanf(fin,"%c",&ch))
{
line[ll]=0;
break;
}
line[ll]=ch;
ll++;
}
}
ch=line[cc];
cc++;
return0;
}
/*词法分析,获取一个符号
*/
intgetsym()
{
inti,j,k;
while(ch==''||ch==10||ch==9)
{
getchdo;
}
while(!
feof(fin))
{
while(ch==''||ch==10||ch==9)
{
getchdo;
}
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
{
k=0;
do{
if(k { a[k]=ch; k++; } getchdo; }while((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')); a[k]=0; strcpy(id,a); i=0; j=norw-1; do{ k=(i+j)/2; if(strcmp(id,word[k])<=0) { j=k-1; } if(strcmp(id,word[k])>=0) { i=k+1; } }while(i<=j); if(i-1>j) { sym=wsym[k]; fprintf(fout,"%d",n); fputs("",fout); fputc('(',fout); fputs(word[k],fout); fputs("sym",fout); fputs(")\n",fout); } else { sym=ident; fprintf(fout,"%d",n); fputs("",fout); fputs("(ident,",fout); fputs(a,fout); fputs(")\n",fout); } } else { if(ch>='0'&&ch<='9') { k=0; num=0; sym=number; fprintf(fout,"%d",n); fputs("",fout); fputs("(number)\n",fout); do{ num=10*num+ch-'0'; k++; getchdo; }while(ch>='0'&&ch<='9');/*获取数字的值*/ k--; if(k>nmax) { error(30); } } else { if(ch=='>')/*检测赋值符号*/ { getchdo; if(ch=='=') {getchdo; sym=becomes; fprintf(fout,"%d",n); fputs("",fout); fputs("(geq)\n",fout); } else { sym=nul; fprintf(fout,"%d",n); fputs("",fout); fputs("(gtr)\n",fout);/*不能识别的符号*/ } } else { if(ch=='<')/*检测小于或小于等于符号*/ { getchdo; if(ch=='=') {getchdo; sym=leq; fprintf(fout,"%d",n); fputs("",fout); fputs("(leq)\n",fout); } else { sym=lss; fprintf(fout,"%d",n); fputs("",fout); fputs("(lss)\n",fout); } } else { if(ch=='! ')/*检测大于或大于等于符号*/ { getchdo; if(ch=='=') {getchdo; sym=neq; fprintf(fout,"%d",n); fputs("",fout); fputs("(neq)\n",fout); } else { sym=exclamate; fprintf(fout,"%d",n); fputs("",fout); fputs("(exclamate)\n",fout); } } } } } } if(ch=='(') { getchdo; fprintf(fout,"%d",n); fputs("",fout); fputs("(lparen)\n",fout); } else { if(ch==')') {getchdo; fprintf(fout,"%d",n); fputs("",fout); fputs("(rparen)\n",fout); } } if(ch=='+') { getchdo; fprintf(fout,"%d",n); fputs("",fout); fputs("(plus)\n",fout); } else { if(ch=='-') {getchdo; fprintf(fout,"%d",n); fputs("",fout); fputs("(minus)\n",fout); } else { if(ch=='*') { getchdo; fprintf(fout,"%d",n); fputs("",fout); fputs("(times)\n",fout); } else { if(ch=='/') { getchdo; fprintf(fout,"%d",n); fputs("",fout); fputs("(slash)\n",fout); } } } } if(ch=='{') { getchdo; fprintf(fout,"%d",n); fputs("",fout); fputs("(lbrace)\n",fout); } else { if(ch=='}') { getchdo; fprintf(fout,"%d",n); fputs("",fout); fputs("(rbrase)\n",fout); } } if(ch=='=') { getchdo; if(ch=='=') { getchdo; sym=eql; fprintf(fout,"%d",n); fputs("",fout); fputs("(eql)\n",fout); } else { sym=becomes; fprintf(fout,"%d",n); fputs("",fout); fputs("(becomes)\n",fout); } } if(ch==';') {getchdo; sym=semicolon; fprintf(fout,"%d",n); fputs("",fout); fputs("(semicolon)\n",fout); } else { if(ch==',') { getchdo; sym=comma; fprintf(fout,"%d",n); fputs("",fout); fputs("(comma)\n",fout); } } if(ch=='/') { getchdo; if(ch=='*') { getchdo; while(ch! ='*'&&feof(fin)! =1) { getchdo; } if(ch=='/') { fprintf(fout,"%d",n); fputs("",fout); fputs("(注释)\n",fout); } } } else { sym=ssym[ch];/*当符号不满足上述条件时,全部按照单字符号处理*/ } } return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 单词 识别 代码
![提示](https://static.bdocx.com/images/bang_tan.gif)