www.pudn.com > 106330.rar > 106330.C


#include 
#include 
#include 
#include 
char origin[10][20]={"E->E+T|T","T->T*F|F","F->(E)|i"}; 
char dest[10][20]; 
char first[10][20]; 
char follow[10][20]; 
char kw[5],ch1[5],chs[5]; 
int step,flagsy,nd,no=3; 
char sourse[10]="i*i+i"; 
char symb[10]; 
struct TableX{ 
	char a; 
	char b[10]; 
	struct TableX *nextT; 
	}; 
struct LLX{ 
	char Nword[5]; 
	struct TableX *table; 
	struct LLX *nextL; 
	}; 
struct LLX *Table; 
struct LLX *currentL; 
struct TableX *currentT; 
 
void Getkw(char *so) 
{int i; 
 for(i=0;i<20;i++) 
 { 
  if(so[i]!='-') 
    kw[i]=so[i]; 
  else 
  {kw[i]='\0'; i=20;} 
 } 
} 
 
void creatTable() 
{int i,j,k,t,flagb; 
 char temp[5]; 
 Table=(struct LLX *)malloc(sizeof(struct LLX)); 
 currentL=Table; 
 for(i=0;i<5;i++) 
 { 
  currentL->nextL=(struct LLX *)malloc(sizeof(struct LLX)); 
  currentL=currentL->nextL; 
  currentL->table=(struct TableX *)malloc(sizeof(struct TableX)); 
  currentT=currentL->table; 
  Getkw(dest[i]); 
  strcpy(currentL->Nword,kw); 
  for(j=0;jnextT=(struct TableX *)malloc(sizeof(struct TableX)); 
     currentT=currentT->nextT; 
     currentT->a=follow[i][k]; 
     strcpy(currentT->b,"@"); 
    } 
   else 
   { 
    currentT->nextT=(struct TableX *)malloc(sizeof(struct TableX)); 
    currentT=currentT->nextT; 
    flagb=0; 
    for(k=0;k'||dest[i][k]=='|') 
     { k++; 
      if(first[i][j]==dest[i][k]) 
      { t=0; 
       currentT->a=first[i][j]; 
       while(dest[i][k]!='|'&&dest[i][k]!='\0') 
	temp[t++]=dest[i][k++]; 
       temp[t]='\0'; 
       strcpy(currentT->b,temp); 
       flagb=1;   k=20; 
    }}} 
    if(flagb==0) 
    { 
     for(k=0;k'||dest[i][k]=='|') 
      { k++; 
       if(isupper(dest[i][k])) 
       { t=0; 
	currentT->a=first[i][j]; 
	while(dest[i][k]!='|'&&dest[i][k]!='\0') 
	 temp[t++]=dest[i][k++]; 
	temp[t]='\0'; 
	strcpy(currentT->b,temp); k=20; 
  }}}}}} 
  currentT->nextT=NULL; 
 } 
 currentL->nextL=NULL; 
} 
 
void Egrammar() 
{ int i,j,n,t,k; 
 int n1,n2,flag,flaga; 
 nd=0; 
 for(i=0;i'||origin[i][j]=='|') 
   { k=0; 
    while(origin[i][j+1+k]==kw[k]) 
       k++; 
    if(k==t) 
    {flag=1; j=20;} 
   } 
  } 
  if(flag==0) 
   strcpy(dest[nd++],origin[i]); 
  else 
  { 
   for(j=0;j<=t+1;j++) 
    dest[nd][j]=origin[i][j]; 
   n1=t+2; 
   strcpy(dest[nd+1],kw); 
   strcat(dest[nd+1],"'->"); 
   n2=t+3; 
   for(j=t+2;j<20;j++) 
   { k=0; 
    while(origin[i][j+k]==kw[k]) 
     k++; 
    if(k==t) 
      flaga=1; 
    else 
      flaga=0; 
    if(flaga==0) 
    { 
     while(origin[i][j]!='|'&&origin[i][j]!='\0') 
      dest[nd][n1++]=origin[i][j++]; 
     for(k=0;k'||dest[i][j]=='|') 
    if(!(isupper(dest[i][j+1]))) 
    {j++; t=0; 
     while(first[i][t]!='\0') 
      t++; 
     first[i][t]=dest[i][j]; 
    } 
 } 
 for(i=0;i'||dest[i][j]=='|') 
   {j++; f=1; 
    while(f==1) 
    { 
     if(isupper(dest[i][j])) 
     { 
      temp[0]=dest[i][j]; 
      if(dest[i][j+1]=='\'') 
      { j++; 
       temp[1]='\''; 
       temp[2]='\0'; 
      } 
      else 
       temp[1]='\0'; 
      j++; t=0; 
      Getkw(dest[t++]); 
      while(strcmp(kw,temp)!=0) 
	Getkw(dest[t++]); 
      t--; 
      itoa(t,ch,10); 
      t=0; 
      while(first[i][t]!='\0') 
	t++; 
      first[i][t]=ch[0]; 
     } 
     for(t=0;t<20;t++) 
     { 
      if(first[i][t]=='@') 
      {j++; f=1; } 
      else  f=0; 
 }}}}} 
 for(i=0;i\n"); 
 printf("<%d\t%s\t\t%s\n",step,symb,sourse); 
 chs[0]=symb[flagsy]; 
 ch1[0]=sourse[0]; 
} 
 
void Strcat() 
{int len,i,t; 
 char r[10],m; 
 if(kw[0]=='@') 
 {symb[flagsy--]='\0'; 
  return; 
 } 
 t=strlen(kw); 
 for(i=0;inextL; 
 while(currentL !=NULL) 
 { 
  if(strcmp(currentL->Nword,chs)!=0) 
   currentL=currentL->nextL; 
  else 
  {currentT=currentL->table->nextT; 
   while(currentT !=NULL) 
   { 
    if(currentT->a !=m) 
     currentT=currentT->nextT; 
    else if(currentT->a==m) 
    { 
     if(symb[flagsy]=='\'') 
      symb[flagsy--]='\0'; 
     strcpy(kw,currentT->b); 
     return(1); 
    } 
   } 
   symb[++flagsy]='\0'; 
   strcpy(kw,ch1); return(1); 
  } 
 } 
 return(0); 
} 
 
void changso() 
{int i,t; 
 t=strlen(sourse); 
 for(i=0;i%s\n",step,symb,sourse,currentL->Nword,kw); 
    Getchs(); 
 }}} 
} 
 
void main() 
{int i; 
 clrscr(); Egrammar(); 
 firsta(); cfollow();  followb(); 
 printf(" The first and follow is:\n"); 
 for(i=0;inextL!=NULL) 
 {currentL=currentL->nextL; 
  currentT=currentL->table->nextT; 
  printf("<%s\t",currentL->Nword); 
  while(currentT!=NULL) 
  {printf("%c,%s->%-2s\t",currentT->a,currentL->Nword,currentT->b); 
   currentT=currentT->nextT; 
  } 
  printf("\n"); 
 } 
 getch(); currentL=Table; 
 init(); done(); 
 getch(); 
}