编译原理课设.docx

上传人:b****7 文档编号:23859611 上传时间:2023-05-21 格式:DOCX 页数:26 大小:18.27KB
下载 相关 举报
编译原理课设.docx_第1页
第1页 / 共26页
编译原理课设.docx_第2页
第2页 / 共26页
编译原理课设.docx_第3页
第3页 / 共26页
编译原理课设.docx_第4页
第4页 / 共26页
编译原理课设.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

编译原理课设.docx

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

编译原理课设.docx

编译原理课设

#include

#include

#include

#include

#include

constMAXSIZE=100;

inti;

intj;

//inttop;

FILE*fp;

FILE*fp1;

FILE*fp2;

FILE*fp3;

FILE*fp5;

/****************提取二元式中的有用信息***********************/

chartoken[5];

intk=0;

charsource[20];

charplace[20];

intmove()//滤除二元式文件中无用符号

{

charc;

if((fp1=fopen("text0.txt","rt"))==NULL)//源文件即二元式文件

{

printf("文件不存在\n");

return-1;

}

if((fp=fopen("text1.txt","wt"))==NULL)//目标文件即读取的文件

{

printf("文件不存在\n");

return-1;

}

if((fp2=fopen("text2.txt","wt"))==NULL)//目标文件即读取的文件

{

printf("文件不存在\n");

return-1;

}

c=fgetc(fp1);

//kuohao(s);//读取括号后面的字符

if(c=='(')

c=fgetc(fp1);

while(c!

=EOF)

{

if(c!

='.')//没有遇到逗号

{

token[k]=c;

k++;

}

elseif(c=='.')//遇到了逗号

{

token[k]='\0';//封住字符串进行比较

if(strcmp(token,"id")==0)

{

strcpy(token,"i");

}

elseif(strcmp(token,"num")==0)

{

strcpy(token,"n");

}

elseif(strcmp(token,"float")==0)

{

strcpy(token,"f");

}

elseif(strcmp(token,"int")==0)

{

strcpy(token,"z");

}

fprintf(fp,"%s",token);//将id变为i,将num变为n

for(inti=0;i<5;i++)//清空数组

token[i]='';

k=0;//下标归零

for(;;)//将,后面的内容跨过

{

c=fgetc(fp1);

if((c!

=')')&&(c!

='\n'))

{

fprintf(fp2,"%c",c);

}

if(c=='\n'||c==EOF)//一个二元式结束了

{

c=fgetc(fp1);

break;

}

}

}

elseif(c==EOF)//文件结束

{

fclose(fp1);

break;

}

c=fgetc(fp1);

}

fclose(fp2);

fclose(fp);

}

/********************************************************/

chars1[20];

intp[20];

charT;

charaction[12]={'z','i','f','n','+','*','(',')',',',';','=','#'};

charGoTo[6]={'S','D','A','E','T','F'};

intstat_action[26][12]={-4,-6,-5,0,0,0,0,0,0,0,0,0,

0,-6,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,-8,-7,0,0,

0,2,0,0,0,0,0,0,0,0,0,2,

0,-9,0,0,0,0,0,0,0,0,0,0,

0,-10,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,-11,0,

0,0,1,0,0,0,0,0,0,0,0,1,

0,-12,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,3,0,0,3,

0,0,0,0,0,0,0,4,4,0,0,0,

0,-17,0,-18,0,0,-16,0,0,0,0,0,

0,0,0,0,0,0,0,0,5,5,0,0,

0,0,0,0,-20,0,0,0,0,-19,0,0,

0,0,0,0,8,-21,0,8,0,8,0,0,

0,0,0,0,10,10,0,10,0,10,0,0,

0,-17,0,-18,0,0,-16,0,0,0,0,0,

0,0,0,0,12,12,0,12,0,12,0,0,

0,0,0,0,13,13,0,13,0,13,0,0,

0,6,0,0,0,0,0,0,0,0,0,6,

0,-17,0,-18,0,0,-16,0,0,0,0,0,

0,-17,0,-18,0,0,-16,0,0,0,0,0,

0,0,0,0,-20,0,0,-25,0,0,0,0,

0,0,0,0,7,-21,0,7,0,7,0,0,

0,0,0,0,9,9,0,9,0,9,0,0,

0,0,0,0,11,11,0,11,0,11,0,0,

};

//action表,负数代表移进,正数代表归约,-10代表出错,0表示多余的列

intstat_GoTo[26][6]={1,2,3,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,13,14,15,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,22,14,15,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,23,15,

-1,-1,-1,-1,-1,24,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,

};

//goto表,用0填充多余的行

intsmb_num[14]={1,2,1,2,2,3,4,3,1,3,1,3,1,1};//产生式右部符号个数

charsmb_left[14]={'P','S','S','D','D','D','A','E','E','T','T','F','F','F'};//产生式左部符号

chara;

charh;

charb;

chary;

char*x;

intflag=1;//用来判断语法最终是否正确的

typedefstruct{

chardata[MAXSIZE];

inttop;

}SeqStacksmb;

typedefstruct{

intdata[100];

inttop;

}SeqStackstat;

typedefstruct{

char*data[MAXSIZE];

inttop;

}SeqStackplace;

SeqStackstat*stat_seq;//定义一个指向顺序栈的指针变量

SeqStacksmb*smb_seq;

SeqStackplace*place_seq;

SeqStackplace*mid;

SeqStackstat*Init_SeqStackstat();

intEmpty(SeqStackstat*s);

intPush(SeqStackstat*s,inty);

intPop(SeqStackstat*s);

SeqStacksmb*Init_SeqStacksmb();

intEmpty(SeqStacksmb*s);

intPush(SeqStacksmb*s,charx);

intPop(SeqStacksmb*s);

intPush(SeqStackplace*s,char*y);

intPop(SeqStackplace*s);

intsearchaction(charn);

intsearchgoto(chart);

voiderror();

intanalys();

SeqStackstat*Init_SeqStackstat()//初始化一个状态栈

{

SeqStackstat*s;

s=newSeqStackstat;

if(!

s)

{

printf("空间不足!

");

returnNULL;

}

else

{

s->top=-1;

returns;

}

}

intEmpty(SeqStackstat*s)//判断栈空

{

if(-1==s->top)

{

return1;

}

else

{

return0;

}

}

intPush(SeqStackstat*s,inty)//入栈

{

if(s->top==MAXSIZE-1)//栈满,不能入栈返回错误代码0

return0;

else

{

s->top++;//栈顶指针向上移动

s->data[s->top]=y;//将x置入新的栈顶

return1;//入栈成功返回成功代码1

}

}

intPop(SeqStackstat*s)//出栈

{

if(Empty(s))

{

return0;

}

else

{

y=s->data[s->top];//保存栈顶元素值

s->top--;//栈顶指针向下移动

return1;//栈顶元素存入x,返回成功代码1

}

}

SeqStacksmb*Init_SeqStacksmb()//初始化一个符号栈

{

SeqStacksmb*s;

s=newSeqStacksmb;

if(!

s)

{

printf("空间不足!

");

returnNULL;

}

else

{

s->top=-1;

returns;

}

}

intEmpty(SeqStacksmb*s)//判断栈空

{

if(-1==s->top)

{

return1;

}

else

{

return0;

}

}

intPush(SeqStacksmb*s,chary)//入栈

{

if(s->top==MAXSIZE-1)//栈满,不能入栈返回错误代码0

return0;

else

{

s->top++;//栈顶指针向上移动

s->data[s->top]=y;//将x置入新的栈顶

return1;//入栈成功返回成功代码1

}

}

intPop(SeqStacksmb*s)//出栈

{

if(Empty(s))

{

return0;

}

else

{

y=s->data[s->top];//保存栈顶元素值

s->top--;//栈顶指针向下移动

return1;//栈顶元素存入x,返回成功代码1

}

}

SeqStackplace*Init_SeqStackplace()//初始化一个place栈

{

SeqStackplace*s;

s=newSeqStackplace;

if(!

s)

{

printf("空间不足!

");

returnNULL;

}

else

{

s->top=-1;

returns;

}

}

intEmpty(SeqStackplace*s)//判断栈空

{

if(s->top==-1)

{

return1;

}

else

{

return0;

}

}

intPush(SeqStackplace*place,char*x)//入栈

{

if(place->top==MAXSIZE-1)//栈满,不能入栈返回错误代码0

return0;

else

{

place->top++;

//s->data++;//栈顶指针向上移动

place->data[place->top]=x;//将x置入新的栈顶

return1;//入栈成功返回成功代码1

}

}

intPop(SeqStackplace*s)//出栈

{

if(Empty(s))

{

return0;

}

else

{

x=s->data[s->top];//保存栈顶元素值

s->top--;//栈顶指针向下移动

return1;//栈顶元素存入x,返回成功代码1

}

}

voidprintstat(SeqStackstat*s)

{

intp[20];

intr,c1;

intb1=0;

for(r=s->top;r>=0;r--)

{p[b1]=s->data[r];b1++;}

for(c1=b1-1;c1>=0;c1--)

printf("%d",p[c1]);

}

voidprintsmb(SeqStacksmb*s)

{

chars1[20];

inta2,b2,c2;

b2=0;

for(a2=s->top;a2>=0;a2--)

{s1[b2]=s->data[a2];b2++;}

for(c2=b2-1;c2>=0;c2--)

printf("%c",s1[c2]);

}

intsearchgoto(charn)//查找GOTO表

{

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

{

if(n==GoTo[i])

{

returni;

break;

}

}

if(i==6)

{

return-1;

}

}

intsearchaction(chart)//查找ACTION表

{

for(j=0;i<12;j++)

{

if(t==action[j])

{

returnj;

break;

}

}

if(i==12)

return-1;

}

voiderror()

{printf("shibai\n");}

voidPrint(SeqStackplace*s)

{

inttopl;

intn=0;

inti=0;

topl=s->top;

while(topl>=i)

{

printf("%s",s->data[i]);

i++;

}

}

char*p0,*p1,*p2,*p3;

charsq0,sq1,sq2,sq3;

voidemit6()

{

Pop(place_seq);

p0=x;

Pop(place_seq);

p1=x;

Pop(place_seq);

p2=x;

Pop(place_seq);

p3=x;

Push(place_seq,p3);

Push(place_seq,p2);

Push(place_seq,p1);

Pop(smb_seq);

sq0=y;

Pop(smb_seq);

sq1=y;

Pop(smb_seq);

sq2=y;

Pop(smb_seq);

sq3=y;

Push(smb_seq,sq3);

Push(smb_seq,sq2);

Push(smb_seq,sq1);

Push(smb_seq,sq0);

}

voidemit7()

{

Pop(place_seq);

p1=x;

Pop(place_seq);

p2=x;

Pop(place_seq);

p3=x;

Push(place_seq,p3);

Push(place_seq,p2);

Push(place_seq,p1);

Pop(smb_seq);

sq1=y;

Pop(smb_seq);

sq2=y;

Pop(smb_seq);

sq3=y;

Push(smb_seq,sq3);

Push(smb_seq,sq2);

Push(smb_seq,sq1);

}

voidprintemit()

{

if(sq2=='=')

{

printf("(=,%s,_,T1)",p1);

printf("(=,T1,_,T2)");

}

else

printf("(%c,%s,%s,area)\n",sq2,p3,p1);

}

inti1,j2,m,n,number,top,z;

voidguiyue()

{

printstat(stat_seq);

printf("\t\t\t");

printsmb(smb_seq);

printf("\t\t\t");

printf("%c\t\t",a);

Print(place_seq);

printf("\t\t\t");

number=smb_num[k];

for(i=0;i

{

Pop(smb_seq);

}

Push(smb_seq,smb_left[k]);

for(i=0;i

{

Pop(stat_seq);

}

char*m1;

for(i=0;i

{

Pop(place_seq);

if(i==0)

m1=x;

elseif(i==1)

m1=x;

else

m1=x;

}

Push(place_seq,m1);

top=stat_seq->top;

z=stat_seq->data[top];

m=searchgoto(smb_left[k]);

n=stat_GoTo[z][m];

Push(stat_seq,n);

top=stat_seq->top;

printf("归约\n");

}

intanalys()

{

if((fp=fopen("text1.txt","r"))==NULL)//打开二元式的文件

{

printf("openerror!

");

return-1;

}

if((fp2=fopen("text2.txt","r"))==NULL)//打开二元式的文件

{

printf("openerror!

");

return-1;

}

printf("状态栈\t\t\t符号栈\t\t\t输入串\t\t语义栈\t\t\t动作说明\t\t\t\t四元式\n");

Push(stat_seq,0);

Push(smb_seq,'#');

Push(place_seq,"_");

a=fgetc(fp);

b=fgetc(fp2);

top=smb_seq->top;

while(a!

=EOF)

{

do

{

i1=stat_seq->data[top];

j2=searchaction(a);

k=stat_action[i1][j2];

if(k<0)//移进

{

printstat(stat_seq);

printf("\t\t\t");

printsmb(smb_seq);

printf("\t\t\t");

printf("%c\t\t",a);

Print(place_seq);

printf("\t\t\t");

k=0-k;

Push(stat_seq,k);

Push(smb_seq,a);

if(b=='_')

{

Push(place_seq,"_");

}

elseif(a=='i'&&b=='0')

{

Push(place_seq,"area");}

elseif(a=='i'&&b=='1')

Push(place_seq,"r");

elseif(a=='n'&&b=='0')

Push(place_seq,"0");

elseif(a=='n'&&b=='0')

Push(place_seq,"1");

elseif(a=='n'&&b=='0')

Push(place_seq,"2");

top=stat

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

当前位置:首页 > PPT模板 > 自然景观

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

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