永发信息网

急求:编译原理判断文法类型的C语言源代码!!!!!!

答案:2  悬赏:20  手机版
解决时间 2021-11-29 12:56
  • 提问者网友:难遇难求
  • 2021-11-29 06:55
急求:编译原理判断文法类型的C语言源代码!!!!!!
最佳答案
  • 五星知识达人网友:廢物販賣機
  • 2021-11-29 08:34
变量存储每次弹出的栈顶元素
全部回答
  • 1楼网友:神的生死簿
  • 2021-11-29 09:15
#include
#include
#include


char inputString[10];
char stack[10];
int base=0;
int top=1;
char VT[4]={'a','d','b','e'};
char chanShengShi[10];
int firstCharIntex=0;



char pop() ;

int push(char) ;

int search(char temp) ;

int M(char A, char a) ;

void init() ;

int yuCeFenXi() ;
void printStack();
void printinputString();


void main()
{
system("cls");
yuCeFenXi();
system("pause");
}



int yuCeFenXi()
{
char X;
char a;
int i;
int counter=1;

init();
printf("wen fa : \n");
printf("S -> aH \n");
printf("H -> aMd | d \n");
printf("M -> Ab | \n");
printf("A -> aM | e \n");

printf("\ninput string ,'#' is a end sign !!(aaabd#) \n");
scanf("%s",inputString);

push('#');
push('S');

printf("\nCounter-----Stack---------------Input string \n");

while(1)
{
printf(" ");
printf(" %d",counter);
printf(" ");
printStack();
X=pop();
printinputString();

if( search(X)==0 )
{
if(X == '#')
{
printf("success \n");
return 1;
}
else
{
a = inputString[firstCharIntex];
if( M(X,a)==1 )
{
for(i=0;i<10;i++)
{
if( chanShengShi[i]=='$' ) break;
}
i-- ;

while(i>=0)
{
push( chanShengShi[i] );
i-- ;
}
}
else
{
printf(" error(1) !!\n");
return 0;
}
}
}
else
{
if( X==inputString[firstCharIntex] )
{
firstCharIntex++;
}
else
{
printf(" error(2) !! \n");
return 0;
}
}
counter++;
}
}

void init()
{
int i;
for(i=0;i<10;i++)
{
inputString[i]=NULL;
stack[i]=NULL;
chanShengShi[i]=NULL;
}
}

int M(char A, char a)
{
if( A=='S'&& a=='a' ) { strcpy(&chanShengShi[0],"aH$"); return 1; }
if( A=='H'&& a=='a' ) { strcpy(&chanShengShi[0],"aMd$"); return 1; }
if( A=='H'&& a=='d' ) { strcpy(&chanShengShi[0],"d$"); return 1; }
if( A=='M'&& a=='a' ) { strcpy(&chanShengShi[0],"Ab$"); return 1; }
if( A=='M'&& a=='d' ) { strcpy(&chanShengShi[0],"$"); return 1; }
if( A=='M'&& a=='b' ) { strcpy(&chanShengShi[0],"$"); return 1; }
if( A=='M'&& a=='e' ) { strcpy(&chanShengShi[0],"Ab$"); return 1; }
if( A=='A'&& a=='a' ) { strcpy(&chanShengShi[0],"aM$"); return 1; }
if( A=='A'&& a=='e' ) { strcpy(&chanShengShi[0],"e$"); return 1; }
else return 0;
}

char pop()
{
char topChar;
topChar=stack[--top];
return topChar;
}

int push(char ch)
{
if( top>9 )
{
printf(" error : stack overflow ");
return 0;
}
else
{
stack[top]=ch;
top++;
return 1;
}
}

int search(char temp)
{
int i,flag=0;
for(i=0;i<4;i++)
{
if( temp==VT[i] )
{
flag=1;
break;
}
}

if(flag==1) return 1;
else return 0;

}

void printStack()
{
int temp;
for(temp=1;temp {
printf("%c",stack[temp]);
}
}

void printinputString()
{
int temp=firstCharIntex ;
printf(" ");
do{
printf("%c",inputString[temp]);
temp++;
}while(inputString[temp-1]!='#');
printf(" \n");
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯