免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1199 | 回复: 0
打印 上一主题 下一主题

[C] 急等高手帮助c语言词法分析 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-12-18 10:36 |只看该作者 |倒序浏览
我要编一个在TC2.0环境下运行的词法分析器,但许多错误,请有该程序的高手帮一下忙了,给看一下,或这帮本人看一下程序,谢谢。
#include<math.h>;

#include<conio.h>;

#include<stdio.h>;

#include<stdlib.h>;

#include<malloc.h>;

#include<string.h>;

char a[10][20],c[20],d[10][20],f[10][20];

int b[20][20],e[10][20],g[10][20],zh[10][2];

int i,j,k,l,m,n,u,v;

char ch[50],ck[50];

FILE *fp;

void get_wf()

{ int bool;

printf("Hi! please input the functor first grammer:\n";

i=0;

gets(a);

while(strlen(a)) gets(a[++i]);

for(i=0;strlen(a);i++)

{int z,bb=1;

for(z=0;d[z][0];z++)

if(d[z][0]==a[0]){bb=0;break;}

if(bb)d[z][0]=f[z][0]=a[0];

for(j=3;a[j]!='\0';j++)

if(!isupper(a[j])){

bool=1;

for(k=1;c[k];k++)

if(c[k]==a[j]){bool=0;break;}

if(bool)c[k]=a[j];

}

}

if((fp=fopen("wfile","w")!=NULL){fwrite(&a,10*20*sizeof(char),1,fp);

fwrite(&d,10*20*sizeof(char),1,fp);

fwrite(&f,10*20*sizeof(char),1,fp);

fwrite(&c,10*sizeof(char),1,fp);}

}

void firstvt()

{int bool=1,y=0;

for(i=0;strlen(a);i++)

{for(l=0;d[l][0];l++)

if(d[l][0]==a[0]) break;

for(j=3;j<=4;j++)

if(!isupper(a[j])){bool=0;

for(k=1;c[k];k++)

if(c[k]==a[j]){bool=1;break;}

if(bool){

bool=1;

for(m=0;d[l][m];m++)

if(d[l][m]==k){bool=0;break;}

if(bool)d[l][m]=k; }

break;}

if(j==4){

for(n=0;d[n][0];n++)

if(d[n][0]==a[j-1])break;

zh[y][0]=l;zh[y++][1]=n;

}

}

for(y--;y>;=0;y--)

for(m=1;d[zh[y][1]][m];m++)

{bool=1;

for(u=0;d[zh[y][0]];u++)

if(d[zh[y][0]]==d[zh[y][1]][m]){bool=0;break;}

if(bool)d[zh[y][0]]=d[zh[y][1]][m];}

gotoxy(20,wherey());printf("unend symbol FIRSTVT unite is under:\n" );

for(i=0;d[0];i++)

{ gotoxy(20,wherey());

printf("%4c",d[0]);

for(j=1;d[j];j++)

printf("%4c",c[d[j]]);

printf("\n"; }

}

void lastvt()

{int x=0,bool=1,y=0;

for(i=0;strlen(a);i++)

{for(l=0;f[l][0];l++)

if(f[l][0]==a[0]) break;

j=3;while(a[j]!='\0')j++;

x=0;

while(x<=1)

{j--;

if(!isupper(a[j])){bool=0;

for(k=1;c[k];k++)

if(c[k]==a[j]){bool=1;break;}

if(bool){

for(m=0;f[l][m];m++)

if(f[l][m]==k){bool=0;break;}

if(bool)f[l][m]=k; }

break;}

x++;

}

if(x==1){

for(n=0;f[n][0];n++)

if(f[n][0]==a[j+1])break;

zh[y][0]=l;zh[y++][1]=n;

}

}

for(y--;y>;=0;y--)

for(m=1;f[zh[y][1]][m];m++)

{bool=1;

for(u=0;f[zh[y][0]];u++)

if(f[zh[y][0]]==f[zh[y][1]][m]){bool=0;break;}

if(bool)f[zh[y][0]]=f[zh[y][1]][m];}

gotoxy(20,wherey());printf("unend symbol LASTVT unite is under:\n" );

for(i=0;f[0];i++)

{ gotoxy(20,wherey());

printf("%4c",f[0]);

for(j=1;f[j];j++)

printf("%4c",c[f[j]]);

printf("\n"; }

}

void rel_s()

{

for(i=0;strlen(a);i++)

for(j=3;a[j]!='\0';j++)

if(isupper(a[j])){

for(l=0;d[l][0];l++)

if(d[l][0]==a[j]){g[j]=l;break;}

if(g[j]&&e[j-1])

for(m=1;d[g[j]][m];m++)

if(b[e[j-1]][d[g[j]][m]]){ gotoxy(20,wherey());

printf("This grammar is non-functor first grammar!\n";exit(0);}

else b[e[j-1]][d[g[j]][m]]=2;

}

else{

for(k=1;c[k];k++)

if(c[k]==a[j]){e[j]=k;break;}

if(e[j]&&e[j-1])

if(b[e[j-1]][e[j]]){

gotoxy(20,wherey());printf("This grammer is non-functor first grammar!\n";exit(0);}

else b[e[j-1]][e[j]]=1;

if(e[j]&&g[j-1]&&e[j-2])

if(b[e[j-2]][e[j]]){

gotoxy(20,wherey());printf("This grammer is non-functor first grammar!\n";exit(0);}

else b[e[j-2]][e[j]]=1;

if(e[j]&&g[j-1])

for(m=1;f[g[j-1]][m];m++)

if(b[f[g[j-1]][m]][e[j]]){

gotoxy(20,wherey());printf("This grammer is non-functor first grammar!\n";exit(0);}

else b[f[g[j-1]][m]][e[j]]=3;

}

}

void tim0n()

{char cc[10],ce[10];

int x,t=1;

int bl=1,p,q;

gotoxy(20,wherey());printf("lease input the strings you wanna to analyse:" );

gets(ch);

k=0;l=0;

while(ch[k]!='\0')k++;

ch[k]='#';ch[k+1]='\0';

m=0;ck[m]='#';

gotoxy(20,wherey());

printf("Analyse process:\n";

gotoxy(5,wherey());

printf("steps stacks precedence_relation currently_symbol spare_strings actions\n";

bl=1;

for(i=0;ch!='\0';i++)

{gotoxy(5,wherey());

printf("%2d",t++);

gotoxy(27,wherey());

for(u=0;u<=m;u++)

printf("%c",ck);

q=0;

for(p=i+1;ch[p]!='\0';p++)

ce[q++]=ch[p];

ce[q]='\0';

for(k=1;c[k];k++)

if(c[k]==ch)j=k;

n=m;

while(isupper(ck[n])) n--;

for(k=1;c[k];k++)

if(c[k]==ck[n])break;

gotoxy(37,wherey());

switch(b[k][j])

{case 1:printf(" =");break;

case 2:printf(" <");break;

case 3:printf(" >;");

}

gotoxy(45,wherey());

printf("%c",ch);

gotoxy(55,wherey());

printf("%s",ce);

if(b[k][j]==0){ gotoxy(20,wherey());printf("\nThis string is not right!!press any key---\n");bl=0; break;}

else

if(b[k][j]<3){ck[++m]=ch;gotoxy(67,wherey());if(ch[i+1]!='\0')printf("move\n");else

printf("accept\n");}

else {i--; gotoxy(67,wherey()); printf("stipulation\n");

x=0;

do

{n--;

while(isupper(ck[n]))n--;

for(j=0;c[j];j++)

if(c[j]==ck[n])break;

}while(b[j][k]==1);

u=n;

while(m>;u)cc[x++]=ck[++u];

m=n;

for(u=0;strlen(a);u++)

{x=1;

for(v=3;a[v]!='\0';v++)

if(!isupper(a[v])&&a[v]!=cc[v-3])x=0;

if(x){ck[++m]=a[0];l=0;break; }

}

}

} gotoxy(20,wherey());

if(bl) printf("successful!press any key---");

_setcursortype(_NOCURSOR);

while(!kbhit());

}

void main()

{for(i=0;i<10;i++)

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

d[j]=e[j]=f[j]=g[j]=0;

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

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

b[j]=0;

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

c=0;

textbackground(BLUE);

clrscr();

printf("|=-------------------------=[parsing analyzer]=-------------------------=|\n");

printf("|=----------------------------------------------------------------------=|\n");

printf("|=--------------=[team member: tim0n--------------=|\n");

printf("|=--------------=[team member: jeff---------------=|\n");

gotoxy(20,5);

printf("Do you want use new grammar(y/n):");

if(getchar()=='y') {scanf("%c");get_wf();}

else{scanf("%c");

if((fp=fopen("wfile","r"))!=NULL){fread(&a,10*20*sizeof(char),1,fp);

fread(&d,10*20*sizeof(char),1,fp);

fread(&f,10*20*sizeof(char),1,fp);

fread(&c,10*sizeof(char),1,fp);}

gotoxy(20,wherey());printf("Now,you uesd grammar is:\n");

for(i=0;strlen(a);i++)

{ gotoxy(20,wherey());

for(j=0;a[j]!='\0';j++)

printf("%2c",a[j]);

printf("\n");

}

}

for(i=0;strlen(a);i++)

{for(j=1;a[j]!=0;j++)

if(isupper(a[j])&&isupper(a[j-1])){

gotoxy(20,wherey());printf("This grammer is non-functor grammar!\n"); exit(0);}

if(j<=3){ gotoxy(20,wherey());printf("This grammer is non-functor first grammar!\n"); exit(0);}

}

firstvt();

lastvt();

k=1;

while(c[k])k++;

rel_s();

gotoxy(20,wherey());printf("Bingle! The matrix is under:\n");

gotoxy(22,wherey());

for(i=1;i<=k;i++)

printf("%2c",c);

printf("\n");

for(i=1;i<=k;i++)

{ gotoxy(20,wherey());

printf("%2c",c);

for(j=1;j<=k;j++)

switch(b[j])

{case 0:printf(" ");break;

case 1:printf(" =");break;

case 2:printf(" <");break;

case 3:printf(" >;");

}

printf("\n");

}

tim0n();

}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP