TP");strcpy(T[0][4],"E->TP");strcpy(T[1][0],"P->" />
搜档网
当前位置:搜档网 › 编译原理词法分析去语法分析器C语言代码

编译原理词法分析去语法分析器C语言代码

#include
#include
#include
int i=0,j=0;
char R[6],S[5],T[5][6][30];
void message()
{
strcpy(R,"+*()i#");
strcpy(S,"EPTQF");
for(i=0;i<5;i++)
for(j=0;j<6;j++)
strcpy(T[i][j],"");
strcpy(T[0][2],"E->TP");
strcpy(T[0][4],"E->TP");
strcpy(T[1][0],"P->+TP");
strcpy(T[1][3],"P->EMPTY");
strcpy(T[1][5],"P->EMPTY");
strcpy(T[2][2],"T->FQ");
strcpy(T[2][4],"T->FQ");
strcpy(T[3][0],"Q->EMPTY");
strcpy(T[3][1],"Q->*FQ");
strcpy(T[3][3],"Q->EMPTY");
strcpy(T[3][5],"Q->EMPTY");
strcpy(T[4][2],"F->(E)");
strcpy(T[4][4],"F->i");
}
typedef struct
{
char *base;
char *top;
int max;
}SqStack;

int InitStack(SqStack &s)
{
s.base=(char *)malloc(100*sizeof(char *));
//*s.base=*s.top;
s.top=s.base;
s.max=200;
return 1;
}
int Push(SqStack &s,char c)
{
*(s.top++)=c;//()?
return 1;
}
int Pop(SqStack &s,char &c)//&c
{
c=*--s.top;//--deweizhi
return 1;
}
char Get(SqStack &s)
{
char c;
c=*(s.top-1);//--
return c;
}

int StackEmpty(SqStack &s)
{
if(s.base==s.top)
return 1;
else
return 0;
}
int len(SqStack s)
{
SqStack t;
t=s;
int i=0;
while(t.top!=t.base)
{
i++;
t.top--;
}
return i;
}





void Get2(SqStack s,int flag)//&
{
int a=0;
char ch[300],ch2[300];
i=0;
SqStack T;
InitStack(T);
T=s;
while(!StackEmpty(T))
{
Pop(T,ch[i++]);a++;
}
ch[i]='\0';
i--;
for(int j=0;jch2[j]=ch[i--];
ch2[j]='\0';
j=0;
//printf("%s\t",ch);
if(flag==1)//%-s---hehe
printf("%10s\t",ch);//return ch2;
else
printf("%-10s\t",ch2);//return ch;
}

void intoStack(SqStack &s1,char input[])//&,&
{

char input2[100];
Push(s1,'#');
gets(input);
while(input[i]!='\0')
i++;
i--;//j=0;
while(i>=0)
{
input2[j]=input[i];
i--;j++;
}
input2[j]='\0';//-1
i=0;//这一步不可少..
while(input2[i]!='\0')//'/0'xiecuole
{
Push(s1,input2[i]);
i++;
}
}

void operate(SqStack &s1,SqStack &s2)//??&,&
{
int n=0;//步骤号
int x=-1,y=-1;
char e,c1,c2;
char c[30]="";//产生式...
char t[30],t2[30];
Push(s2,'#');
Push(s2,'E');//符号串...E,F...
printf("步骤号\t符号栈\t\t 输入串\t产生式\n");
while(len(s1)>1||len(s2)>1)
{
printf(" %d\t",n++);//.................
Get2(s2,2);
Get2(s1,1);
printf("%s\n",c);

x=-1;y=-1;
c1=Get(s1);
c2=Get(s2);
/*if(Get(s1)=='#'&&len(s1)>1)
{
printf("错误3:输入的字符串不对,出现了#!\n");//0000000000000000000000
exit(0);
}
*/

if(Get(s1)=='#')
{
if(Get(s2)=='#')
{
strcpy(c,"");
Pop(s1,e);
if(len(s2)>1)
{Pop(s2,e);continue;}
else
{printf("错误5: 输入的字符串过长,符号栈没有字符与它匹配了..\n");exit(0);}
}
else
{
Pop(s2,e);
for(i=0;i<5;i++)
{
if(S[i]==e)
{x=i;break;}
}
if(strcmp(T[x][5],""))
strcpy(c,T[x][

5]);
else
strcpy(c,"");////待定...
continue;
}
}
for(i=0;i<6;i++)
{if(R[i]==c1)
{y=i;break;}
}
for(i=0;i<5;i++)
{if(S[i]==c2)
{x=i;break;}
}
if(y==-1)
{
printf("错误2:输入的字符串不对!\n");
exit(0);
}
if(x==-1)//一般不会...hehe
{
if(c1=c2)
{
Pop(s1,e);
Pop(s2,e);
strcpy(c,"");//
continue;
}
else
{
printf("错误4:输入的字符串不匹配...!\n");
exit(0);
}
}

if(strcmp(T[x][y],""))
{
strcpy(c,T[x][y]);
strcpy(t,T[x][y]);//*t?
for(i=0;t[i]!='\0';i++);//注意了,是\0不是/0!!
for(i--,j=0;i>=0;j++,i--)
t2[j]=t[i];
t2[j]='\0';
Pop(s2,e);
if(t2[0]=='Y')
continue;
else
{for(i=0;t2[i]!='>';i++)
Push(s2,t2[i]);
}
}
else//一般不会...
{
printf("错误1:输入的字符串不匹配!\n");
exit(0);
}
}
printf(" %d\t",n++);//.................
Get2(s2,2);
Get2(s1,1);
printf("%s\n",c);
}
void main()
{
char input[100];
SqStack s1,s2;
InitStack(s1);//!!
InitStack(s2);

printf("请输入输入串:\n\t");
intoStack(s1,input);//输入串写入了...
message();////
//printf("%d\n",len(s1));
//将字符串进行翻译....//
printf("\t ----------------------------------\n");


operate(s1,s2);
printf("(成功! )\n");

}

相关主题