编译原理作业.docx

上传人:b****6 文档编号:6960277 上传时间:2023-01-13 格式:DOCX 页数:20 大小:18.94KB
下载 相关 举报
编译原理作业.docx_第1页
第1页 / 共20页
编译原理作业.docx_第2页
第2页 / 共20页
编译原理作业.docx_第3页
第3页 / 共20页
编译原理作业.docx_第4页
第4页 / 共20页
编译原理作业.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

编译原理作业.docx

《编译原理作业.docx》由会员分享,可在线阅读,更多相关《编译原理作业.docx(20页珍藏版)》请在冰豆网上搜索。

编译原理作业.docx

编译原理作业

/*识别分界符,运算符,赋值运算符,增减运算符,逻辑与逻辑或,关系运算符,科学计数法,实型常数,整形常数,有符号数,字符串常量,关键字,标示符,输入输出流符号*/

#include

#include

#include

#include

usingnamespacestd;

stringkeyword[15]={"int","include","using","namespace","return","void","char","float","double","unsigned","string","break","continue","do","while"};/*关键字标示符*/

//stringinstruction[3]={"//","/*","*/"};/*注释行符号*/

//分界符

charboundary[9]={',',';','[',']','{','}','(',')','#'};

//运算符

charoperation[5]={'+','-','*','/','%'};

//赋值运算符

charequals='=';

//增减运算符

stringin_decrease[2]={"++","--"};

//逻辑与逻辑或

//stringand_or[2]={"&&","||"};

//关系运算符

//stringrelation[6]={">","<",">=","<=","==","!

="};

inti_point=1;

//

voidanaly_boundary(charA);

voidanaly_operation(charA);

voidanaly_string(charanswer[]);

voidanaly_keyword(charanswer[]);

voidenter_p(inti_pt);

voidanaly(FILE*fp);

//

voidmain()

{

//主函数

FILE*fp0;

fp0=fopen("f:

/program.txt","r+");

if(fp0==NULL)

{

printf("打开文件错误!

\n");

exit(0);

}

analy(fp0);

return;

}

//

voidanaly_boundary(charA)//分析分界符的函数

{

if(A!

=EOF)

{

if(A==','||A==';'||A=='['||A==']'||A=='{'||A=='}'||A=='('||A==')'||A=='#')

{

for(intb=0;b<9;b++)//charboundary[11]={',',';','[',']','{','}','(',')','#'};

{

if(A==boundary[b])

{

cout<<"【"<<"分界符→"<

break;

}

}

}

}

}

//

voidanaly_operation(charA)//分析运算符的函数

{

intd=0;

if(A!

=EOF)

{

if(A=='+'||A=='-'||A=='*'||A=='/'||A=='%')//charoperation[5]={'+','-','*','/','%'}

{

while(d<5)//charoperation[5]={'+','-','*','/','%'};

{

if(A==operation[d])

{

cout<<"【"<<"运算符→"<

break;

}

d++;

}

}

}

}

//

voidanaly_string(charanswer[])//识别字符串常量

{

cout<<"【符号串常量→"<

}

//

voidanaly_keyword(charanswer[])//识别关键字和标示符

{

for(intl=0;l<15;l++)

{

if(answer==keyword[l])

{

cout<<"【关键字→"<

break;

}

}

for(intm=0;m<15;m++)

{

if(answer!

=keyword[l])

{

cout<<"【标示符→"<

break;

}

}

}

//

voidenter_p(inti_pt)//分屏显示操作

{

if(i_pt%21==0)

{

cout<<"NextPage";

system("Pause");

system("cls");

}

}

//

voidanaly(FILE*fp)

{

charnumber[10];//识别数字量

charanswer_0[10];

charanswer_1[10];//识别字符串

//关系运算符stringrelation[6]={">","<",">=","<=","==","!

="};

charanswer_2[100];//识别注释行

charch,ch0,ch1,ch2,ch3,ch4,ch5,ch6;

intk=0,k1=0,k2=0,k3=0,k4=0,k5=0,k6=0;

ch=fgetc(fp);//fgetc()读字符函数,如果遇到文件结束符号,函数返回文件结束标志EOF

while(ch!

=EOF)

{

if(ch=='<'||ch=='>'||ch=='!

')

{

//关系运算符

cout<

";

ch3=ch;

ch=fgetc(fp);

if(ch=='=')

{

//判断<=,>=,==,!

=符号

ch4=ch;

cout<<"【关系运算符→"<

ch3=ch4='0';

ch=fgetc(fp);

enter_p(i_point);

continue;

}

elseif(ch==ch3)

{

cout<<"【输入输出流符→"<

ch3='0';

ch=fgetc(fp);

enter_p(i_point);

continue;

}

else

{

//判断>,<符号

cout<<"【关系运算符→"<

ch3=ch4='0';

enter_p(i_point);

continue;

}

}

if(ch=='=')

{

//判断==和=符号

cout<

";

ch5=ch;

ch=fgetc(fp);

if(ch==ch5)

{

cout<<"【关系运算符→"<

ch5='0';

ch=fgetc(fp);

enter_p(i_point);

continue;

}

else

{

cout<<"【赋值运算符→"<

ch5='0';

enter_p(i_point);

continue;

}

}

if(ch==','||ch==';'||ch=='['||ch==']'||ch=='{'||ch=='}'||ch=='('||ch==')'||ch=='#'){

//判断分界符

cout<

";

analy_boundary(ch);

enter_p(i_point);

}

if(ch=='*'||ch=='/'||ch=='%'){//ch=='+'||ch=='-'||

//判断算术运算符

ch6=ch;

ch=fgetc(fp);

if(ch6=='/')

{

if(ch=='/')

{

//判断//注释

cout<

";

answer_2[k5++]=ch6;

answer_2[k5++]=ch;

ch=fgetc(fp);

while(ch!

='\n')

{

answer_2[k5++]=ch;

ch=fgetc(fp);

}

answer_2[k5]='\0';

cout<<"【注释语句,"<

k5=0;

ch6='0';

enter_p(i_point);

continue;

}

elseif(ch=='*')

{

//判断/*....*/注释

cout<

";

answer_2[k5++]=ch6;

answer_2[k5++]=ch;

ch=fgetc(fp);

while(ch!

='/')

{

answer_2[k5++]=ch;

ch=fgetc(fp);

}

answer_2[k5++]=ch;

answer_2[k5]='\0';

cout<<"【注释语句,"<

k5=0;

ch6='0';

enter_p(i_point);

continue;

}

}

else

{

cout<

";

analy_operation(ch6);

enter_p(i_point);

ch6='0';

continue;

}

}

if(ch=='+'||ch=='-'){

//判断算术运算符或增减运算符

cout<

";

ch0=ch;

ch=fgetc(fp);

if(ch!

=ch0)

{

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

{

do

{

number[k6++]=ch;

ch=fgetc(fp);

}while(ch>='0'&&ch<='9'||ch=='.'||ch=='-'||ch=='e'||ch=='');

number[k6]='\0';

cout<<"【有符号数→"<

enter_p(i_point);

ch0='0';

k6=0;

continue;

}

else

{

analy_operation(ch0);

ch0='0';

k6=0;

enter_p(i_point);

continue;

}

}

else

{

cout<<"【增减运算符→"<

ch0='0';

ch=fgetc(fp);

enter_p(i_point);

continue;

}

}

if(ch=='&'||ch=='|')

{

//判断逻辑与逻辑或

cout<

";

ch1=ch;

ch=fgetc(fp);

ch2=ch;

cout<<"【逻辑与逻辑或→"<

ch1=ch2='0';

ch=fgetc(fp);

enter_p(i_point);

continue;

}

if(ch=='.')//实型常数

{

//实型常数

number[k++]='.';

ch=fgetc(fp);

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

{

number[k++]=ch;

ch=fgetc(fp);

}

number[k]='\0';

k=0;

cout<<"【实型常数→"<

enter_p(i_point);

continue;

}

if(ch>='0'&&ch<='9')//||ch=='.'

{

//判断整形常数和实型常数

cout<

";

do

{

number[k++]=ch;

ch=fgetc(fp);

}while(ch>='0'&&ch<='9');//||ch=='.'

if(ch=='.')//实型常数

{

//实型常数

number[k++]='.';

ch=fgetc(fp);

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

{

number[k++]=ch;

ch=fgetc(fp);

}

number[k]='\0';

k=0;

cout<<"【实型常数→"<

enter_p(i_point);

continue;

}

elseif(ch=='e'||ch=='E')//科学计数法

{

//科学计数法如1e-5

number[k++]=ch;

//ch=fgetc(fp);

//number[k++]=ch;

ch=fgetc(fp);

do

{

number[k++]=ch;

ch=fgetc(fp);

}while(ch>='0'&&ch<='9'||ch=='e'||ch=='E'||ch=='-'||ch=='');

number[k]='\0';

k=0;

cout<<"【科学计数法→"<

enter_p(i_point);

continue;

}

else//整型常数

{

//整型常数

number[k]='\0';

k=0;

cout<<"【整形常数→"<

enter_p(i_point);

continue;

}

}

if(ch=='\"')//识别字符串

{

cout<

";

answer_1[k2++]=ch;//把第一个"

ch=fgetc(fp);

while(ch!

='\"')

{

answer_1[k2++]=ch;//放入字符串

ch=fgetc(fp);

}

answer_1[k2++]='\"';

answer_1[k2]='\0';

k2=0;

analy_string(answer_1);

ch=fgetc(fp);

enter_p(i_point);

continue;

}

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

{

//判断关键字和标示符

cout<

";

answer_0[k1++]=ch;

ch=fgetc(fp);

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

{

answer_0[k1++]=ch;

ch=fgetc(fp);

}

answer_0[k1]='\0';

k1=0;

analy_keyword(answer_0);

enter_p(i_point);

continue;

}

ch=fgetc(fp);

}

}

//

 

#include"stdio.h"//strchr,fgetc,rewind,fseek

#include"iostream.h"

#include"string.h"

#include"stdlib.h"

#definekeyword42

#definefuhao46

#definefenjie13

#defineyunsuan15

charyun[yunsuan]="+-*/=>

\&^|%:

~";

#definefenge12

charfen[fenge]=",.;{}()#[]\"";

char*a[keyword]={"auto","break","case","char","class",

"const","continue","default","delete",

"do","double","else","enum","extern",

"float","for","friend","if","inline",

"int","long","new","operator","private",

"protected","public","register","return",

"short","sizeof","static","struct","switch",

"template","this","typedef","union",

"virtual","void","while","include","char"};

char*b[fuhao]={"+","-","*","/","%","++","--",

">",">=","<","<=","==","!

=","&&","||",

"!

","<<",">>","~","|","^","&","=","+=",

"-=","*=","/=","%=",">>=","<<=","&=","^=",

"|=",",","->",".","[","]","(",")",":

:

","{","}","#",";","\""};

charch;

inti,k;

charbuff[20];

charbuff1[1];

voidoutput(int,char*);

voidzhuyao(FILE*fp){

intj;

ch=fgetc(fp);

while(ch==''||ch=='\n'||ch=='\t')

{ch=fgetc(fp);}

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

buff[0]=ch;

i=1;

ch=fgetc(fp);

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

buff[i]=ch;

i++;

ch=fgetc(fp);//从流中读取字符

}

buff[i]='\0';/*查找保留字*/

fseek(fp,-1,1);

j=0;

while((j

j++;

if(j>=keyword)

output(2,buff);

else

output(1,buff);

}

elseif(ch>='0'&&ch<='9'){

buff[0]=ch;

ch=fgetc(fp);

i=1;

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

buff[i]=ch;

i++;

ch=fgetc(fp);

}

buff1[i]=ch;

ch=fgetc(fp);

if(buff1[i]=='.'&&(ch>='0'&&ch<='9')){

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

i++;

buff[i]=ch;

ch=fgetc(fp);

}

i++;

buff[i]='\0';

}

elsebuff[i]='\0';

output(5,buff);

fseek(fp,-1,1);

}

elseif(strchr(fen,ch)>0)/*分隔符号*/

{

buff[0]=ch;

ch=fgetc(fp);

buff[1]='\0';

fseek(fp,-1,1);

k=0;

if((k

output(3,buff);

}

elseif(strchr(yun,ch)>0)/*运算符号*/

{

buff[0]=ch;

ch=fgetc(fp);

if(buff[0]=='/'&&ch=='*'){

while(ch!

='/'){

ch=fgetc(fp);

}

}

elseif(buff[0]=='/'&&ch=='/'){

while(ch!

='\n'){

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

当前位置:首页 > 总结汇报

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

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