分享
 
 
 

编译原理词法分析器c++源程序

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

#include<iostream.h>

#include<fstream.h>

#include<stdlib.h>

#include<stdio.h>

#include<string.h>

#include<conio.h>

#include<process.h> /*头文件*/

void init();

char *DchangeB(char *buf);

int search(char *buf,int type,int command);

void intdeal(char *buffer);

void chardeal(char *buffer);

void errordeal(char error,int lineno);

void scanner();

void init()

{ char *key[]={"","auto","break","case","char","const","continue","default","do","double",

"else","enum","extern","float","for","goto","if","int","long","register",

"return","short","signed","sizeof","static","struct","switch","typedef",

"union","unsigned","void","volatile","while"}; /*C语言所有关键字/

char *limit[]={" ","(",")","[","]","->",".","!","++","--","&","~",

"*","/","%","+","-","<<",">>","<","<=",">",">=","==","!=","&&","||",

"=","+=","-=","*=","/=",",",";","{","}","#","_","'"};/*运算、限界符*/

fstream outfile;

int i,j;

char *c;

outfile.open("key.txt",ios::out);

for(i=0;i<32;i++)

outfile<<key[i]<<endl;

outfile.close();

outfile.open("Limit.txt",ios::out);

for(j=0;j<38;j++)

outfile<<limit[j]<<endl;

c="";

outfile<<c;

outfile.close();

outfile.open("bsf.txt",ios::out);

outfile.close();

outfile.open("cs.txt",ios::out);

outfile.close();

outfile.open("output.txt",ios::out);

outfile.close();

}

char *DchangeB(char *buf)

{

int temp[20];

char *binary;

int value=0,i=0,j;

for(i=0;buf[i]!='\0';i++)

value=value*10+(buf[i]-48); /*将字符转化为十进制数*/

if(value==0)

{

binary=new char[2];

binary[0]='0';

binary[1]='\0';

return(binary);

}

i=0;

while(value!=0)

{

temp[i++]=value%2;

value/=2;

}

temp[i]='\0';

binary=new char[i+1];

for(j=0;j<=i-1;j++)

binary[j]=(char)(temp[i-j-1]+48);

binary[i]='\0';

return(binary); /*十进制转化为二进制*/

}

int search(char *buf,int type,int command)

{ int number=0;

fstream outfile;

char ch;

char temp[30];

int i=0;

switch(type)

{

case 1: outfile.open("key.txt",ios::in);break;

case 2: outfile.open("bsf.txt",ios::in);break;

case 3: outfile.open("cs.txt",ios::in);break;

case 4: outfile.open("limit.txt",ios::in);break;

}

outfile.get(ch);

while(ch!=EOF){

while(ch!='\n')

{

temp[i++]=ch;

outfile.get(ch);

}

temp[i]='\0';

i=0;

number++;

if(strcmp(temp,buf)==0)

{

outfile.close();

return number; /*若找到,返回在相应表中的序号*/

}

else

outfile.get(ch);

} //结束外层while循环

if(command==1)

{

outfile.close( );

return 0; /*找不到,当只需查表,返回0,否则还需造表*/

}

switch(type)

{

case 1: outfile.open("key.txt",ios::in);break;

case 2: outfile.open("bsf.txt",ios::in);break;

case 3: outfile.open("cs.txt",ios::in);break;

case 4: outfile.open("limit.txt",ios::in);break;

}

outfile<<buf;

outfile.close();

return number+1;

}

void intdeal(char *buffer){

fstream outfile;

int result;

result=search(buffer,1,1); /*先查关键字表*/

outfile.open("output.txt",ios::app);

if(result!=0)

outfile<<buffer<<result<<endl; /*若找到,写入输出文件*/

else

{

result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/

outfile<<buffer<<result<<endl;

} /*写入输出文件*/

outfile.close();

}

void chardeal(char *buffer)

{ fstream outfile;

int result;

result=search(buffer,1,1); /*先查关键字表*/

outfile.open("output.txt",ios::app);

if(result!=0)

outfile<<buffer<<result<<endl; /*若找到,写入输出文件*/

else

{

result=search(buffer,2,2); /*若找不到,则非关键字,查标识符表,还找不到则造入标识符表*/

outfile<<buffer<<result<<endl;

} /*写入输出文件*/

outfile.close();

}

void errordeal(char error,int lineno)

{ cout<<"\nerror: "<<error<<" ,line"<<lineno;

}

void scanner()

{ fstream infile,outfile;

char filename[20];

char ch;

int err=0;

int i=0,line=1;

int count,result,errorno=0;

char array[30];

char *word;

printf("\n please input the file scanner name:");

scanf("%s",filename);

err=1;

infile.open(filename,ios::nocreate|ios::in);

while(! infile)

{

cout<<"cannot open file"<<endl;

printf("please input the file name again:\n");

scanf("%s",filename);

infile.open(filename,ios::nocreate|ios::in);

err++;

if(err==3)

{cout<<"SORROY YOU CAN'T VUEW THE PRGARME\n";

cout<<"TANKE YOU VIEW"<<endl;

exit(0);}

}

infile.get(ch);

while(ch!=EOF)

{ /*按字符依次扫描源程序,直至结束*/

i=0;

if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_'))

{ /*以字母开头*/

while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')||((ch>='0')&&(ch<='9')))

{

array[i++]=ch;

infile.get(ch);

}

word=new char[i+1];

memcpy(word,array,i);

word[i]='\0';

intdeal(word);

if(ch!=EOF)

infile.seekg(-1,ios::cur);

}

else if(ch>='0'&&ch<='9')

{ /*以数字开头*/

while(ch>='0'&&ch<='9')

{

array[i++]=ch;

infile.get(ch);

}

word=new char[i+1];

memcpy(word,array,i);

word[i]='\0';

intdeal(word);

if(ch!=EOF)

infile.seekg(-1,ios::cur);

}

else if((ch==' ')||(ch=='\t'))

; /*消除空格符和水平制表符*/

else if(ch=='\n')

line++; /*消除回车并记录行数*/

else if(ch=='/')

{ /*消除注释*/

infile.get(ch);

if(ch=='=')

{ /*判断是否为‘/=’符号*/

outfile.open("output.txt",ios::noreplace|ios::app);

outfile<<"/=\t\t\t4\t\t\t32\n";

outfile.close();

}

else if(ch!='*')

{ /*若为除号,写入输出文件*/

outfile.open("output.txt",ios::noreplace|ios::app);

outfile<<"/\t\t\t4\t\t\t13\n";

outfile.close();

outfile.seekg(-1,ios::cur);

}

else if(ch=='*')

{ /*若为注释的开始,消除包含在里面的所有字符*/

count=0;

infile.get(ch);

while(count!=2)

{ /*当扫描到‘*’且紧接着下一个字符为‘/’才是注释的结束*/

count=0;

while(ch!='*')

infile.get(ch);

count++;

infile.get(ch);

if(ch=='/')

count++;

else

infile.get(ch);

}

}

}

else if(ch=='"')

{ /*消除包含在双引号中的字符串常量*/

outfile.open("output.txt",ios::noreplace|ios::app);

outfile<<ch<<"\t\t\t4\t\t\t37\n";

outfile.close();

while(ch!='"')

infile.get(ch);

infile<<ch<<"\t\t\t4\t\t\t37\n";

infile.close();

}

else

{ /*首字符为其它字符,即运算限界符或非法字符*/

array[0]=ch;

infile.get(ch); /*再读入下一个字符,判断是否为双字符运算、限界符*/

if(ch!=EOF)

{ /*若该字符非文件结束符*/

array[1]=ch;

word=new char[3];

memcpy(word,array,2);

word[2]='\0';

result=search(word,4,1); /*先检索是否为双字符运算、限界符*/

if(result==0)

{ /*若不是*/

word=new char[2];

memcpy(word,array,1);

word[1]='\0';

result=search(word,4,1); /*检索是否为单字符运算、限界符*/

if(result==0)

{ /*若还不是,则为非法字符*/

errordeal(array[0],line);

errorno++;

infile.seekg(-1,ios::cur);

}

else

{ /*若为单字符运算、限界符,写入输出文件并将扫描文件指针回退一个字符*/

outfile.open("output.txt",ios::noreplace|ios::app);

outfile<<word<<"\t\t\t4\t\t\t"<<result<<"\t"<<endl;

outfile.close();

infile.seekg(-1,ios::cur);

}

}

else

{ /*若为双字符运算、限界符,写入输出文件*/

outfile.open("output.txt",ios::noreplace|ios::app);

outfile<<word<<"\t\t\t4\t\t\t"<<result<<endl;;

outfile.close( );

}

}

else

{ /*若读入的下一个字符为文件结束符*/

word=new char[2];

memcpy(word,array,1);

word[1]='\0';

result=search(word,4,1); /*只考虑是否为单字符运算、限界符*/

if(result==0) /*若不是,转出错处理*/

errordeal(array[0],line);

else

{ /*若是,写输出文件*/

outfile.open("output.txt",ios::noreplace|ios::app);

outfile<<word<<"\t\t\t4\t\t\t"<<result<<"\t"<<endl;

outfile.close();

}

}

}

infile.get(ch);

}

infile.close();

cout<<"\nThere are "<<errorno<<" error(s).\n"; /*报告错误字符个数*/

}

void main()

{ char yn;

do{

init(); /*初始化*/

scanner();/*扫描源程序*/

printf("Are You continue(y/n)\n"); //判断是否继续?

yn=getch();

}while(yn=='y'||yn=='Y');

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有