编译原理单词识别代码.docx

上传人:b****8 文档编号:23883669 上传时间:2023-05-21 格式:DOCX 页数:18 大小:17.57KB
下载 相关 举报
编译原理单词识别代码.docx_第1页
第1页 / 共18页
编译原理单词识别代码.docx_第2页
第2页 / 共18页
编译原理单词识别代码.docx_第3页
第3页 / 共18页
编译原理单词识别代码.docx_第4页
第4页 / 共18页
编译原理单词识别代码.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

编译原理单词识别代码.docx

《编译原理单词识别代码.docx》由会员分享,可在线阅读,更多相关《编译原理单词识别代码.docx(18页珍藏版)》请在冰豆网上搜索。

编译原理单词识别代码.docx

编译原理单词识别代码

头文件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;

}

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

当前位置:首页 > 高中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1