?? e.cpp
字號(hào):
#include <iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int count=0; /*分解的產(chǎn)生式的個(gè)數(shù)*/
int number; /*所有終結(jié)符和非終結(jié)符的總數(shù)*/
char start; /*開始符號(hào)*/
char termin[50]; /*終結(jié)符號(hào)*/
char non_ter[50]; /*非終結(jié)符號(hào)*/
char v[50]; /*所有符號(hào)*/
char left[50]; /*左部*/
char right[50][50]; /*右部*/
char first[50][50],follow[50][50]; /*各產(chǎn)生式右部的FIRST和左部的FOLLOW集合*/
char first1[50][50]; /*所有單個(gè)符號(hào)的FIRST集合*/
char select[50][50]; /*各單個(gè)產(chǎn)生式的SELECT集合*/
char f[50],F[50]; /*記錄各符號(hào)的FIRST和FOLLOW是否已求過*/
char empty[20]; /*記錄可直接推出^的符號(hào)*/
char TEMP[50]; /*求FOLLOW時(shí)存放某一符號(hào)串的FIRST集合*/
int validity=1; /*表示輸入文法是否有效*/
int ll=1; /*表示輸入文法是否為L(zhǎng)L(1)文法*/
int M[20][20]; /*分析表*/
char choose; /*用戶輸入時(shí)使用*/
char empt[20]; /*求_emp()時(shí)使用*/
char fo[20]; /*求FOLLOW集合時(shí)使用*/
typedef struct
{
char *base, *top;
int stacksize;
}SqStack;
int GA(char intion[]);
void InitStack(SqStack &s,int n); //創(chuàng)建空棧
void Push(SqStack &s,char e); //進(jìn)棧
char GetTop(SqStack &s); //出棧
int son()
{
int i,j=0,length,kh=0;
char oution[100];
char intion[100];
char judge;
SqStack s;
InitStack(s,50);
Push(s,';');
cout<<"輸入要測(cè)試賦值語句,并以“;”結(jié)束 "<<endl;
cin.get(intion,100);
cout<<endl;
if(GA(intion)==0)
{
cout<<"This sentence is illegal!"<<endl;
return 0;
}
for(i=0;i<=99;i++)
{
if(intion[i]=='('||intion[i]==')') kh++;
if(intion[i]==';')
{
length=i-1;
length=length-kh;
break;
}
}
oution[0]=intion[0];
for(i=1;i<=99;i++)//取等號(hào)
{
if(intion[i]==' '||intion[i]==','||intion[i]=='+'||intion[i]=='*')
{
cout<<"error"<<endl;
cout<<"This sentence is illegal!"<<endl;
return 0;
break;
}
if(intion[i]=='=')
{
Push(s,'=');
j=i;
break;
}
oution[i]=intion[i];
}
for(i=j+1;i<=99;i++)
{
if(intion[i]==';') break;
else if(intion[i]=='='||intion[i]==','||intion[i]==' ')
{
cout<<"error"<<endl;
cout<<"This sentence is illegal!"<<endl;
return 0;
break;
}
else if(intion[i]=='+'||intion[i]=='-')
{
judge=GetTop(s);
while(judge=='*'||judge=='/'||judge=='+'||judge=='-')
{
oution[j]=judge;
j++;
s.top--;
judge=GetTop(s);
}
Push(s,intion[i]);
}
else if(intion[i]=='*'||intion[i]=='/')
{
judge=GetTop(s);
while(judge=='*'||judge=='/')
{
oution[j]=judge;
j++;
s.top--;
judge=GetTop(s);
}
Push(s,intion[i]);
}
else if(intion[i]=='(')
{
Push(s,intion[i]);
}
else if(intion[i]==')')
{
judge=GetTop(s);
while(judge=='+'||judge=='-'||judge=='*'||judge=='/')
{
oution[j]=judge;
j++;
s.top--;
judge=GetTop(s);
}
if(judge=='(')
{
s.top--;
}
else Push(s,intion[i]);
}
else
{
oution[j]=intion[i];
j++;
}
}
while(GetTop(s)!=';')
{
if(GetTop(s)!='(')
{
oution[j]=GetTop(s);
s.top--;
j++;
}
else if(GetTop(s)=='(') s.top--;
}
cout<<"the answer is :"<<endl;
for(i=0;i<=length;i++)
{
cout<<oution[i];
}cout<<endl;
return 0;
}
void InitStack(SqStack &s,int n)
{
s.base=(char *)malloc(n*sizeof(int));
if(!s.base)exit(false);
s.stacksize=n;
s.top=s.base;
}
void Push(SqStack &s,char e)
{
*s.top++=e;
}
char GetTop(SqStack &s)
{
char e;
if(s.top==s.base) return false;
e=*(s.top-1);
return e;
}
int GA(char intion[])
{
int i,zkh=0,ykh=0;
if(intion[0]>='0'&&intion[0]<='9')
{
cout<<"error0"<<endl;
return 0;
}
if(intion[0]==' '||intion[0]=='+'||intion[0]=='*')
{
cout<<"error1"<<endl;
return 0;
}
for(i=1;i<=99;i++)
{
if(intion[i]=='(') zkh++;
else if(intion[i]==')') ykh++;
}
if(zkh!=ykh)
{
cout<<"error"<<endl;
return 0;
}
zkh=0;
ykh=0;
for(i=1;i<=99;i++)
{
if(intion[i]=='+'||intion[i]=='-'||intion[i]=='*'||intion[i]=='/')
{
if(intion[i-1]=='='||intion[i-1]=='+'||intion[i-1]=='-'||intion[i-1]=='*'||intion[i-1]=='/'||intion[i-1]=='(')
{
cout<<"error2"<<endl;
return 0;
break;
}
}
else if(intion[i]=='(')
{
zkh++;
if(intion[i-1]=='='||intion[i-1]=='+'||intion[i-1]=='-'||intion[i-1]=='*'||intion[i-1]=='/'||intion[i-1]=='(')
{
}
else
{
cout<<"error3"<<endl;
return 0;
break;
}
}
else if(intion[i]==')')
{
ykh++;
if(zkh<ykh)
{
cout<<"error4"<<endl;
return 0;
break;
}
if(intion[i-1]=='('||intion[i-1]=='='||intion[i-1]=='+'||intion[i-1]=='-'||intion[i-1]=='*'||intion[i-1]=='/')
{
cout<<"error5"<<endl;
return 0;
break;
}
}
else if(intion[i]!=';')
{
if(intion[i-1]==')')
{
cout<<"error6"<<endl;
return 0;
break;
}
}
}
return 1;
}
/*******************************************
判斷一個(gè)字符是否在指定字符串中
********************************************/
int in(char c,char *p)
{
int i;
if(strlen(p)==0)
return(0);
for(i=0;;i++)
{
if(p[i]==c)
return(1); /*若在,返回1*/
if(i==strlen(p))
return(0); /*若不在,返回0*/
}
}
/*******************************************
得到一個(gè)不是非終結(jié)符的符號(hào)
********************************************/
char c()
{
char c='A';
while(in(c,non_ter)==1)
c++;
return(c);
}
/*******************************************
分解含有左遞歸的產(chǎn)生式
********************************************/
void recur(char *point)
{ /*完整的產(chǎn)生式在point[]中*/
int j,m=0,n=3,k;
char temp[20],ch;
ch=c(); /*得到一個(gè)非終結(jié)符*/
k=strlen(non_ter);
non_ter[k]=ch;
non_ter[k+1]='\0';
for(j=0;j<=strlen(point)-1;j++)
{
if(point[n]==point[0])
{ /*如果‘|’后的首符號(hào)和左部相同*/
for(j=n+1;j<=strlen(point)-1;j++)
{
while(point[j]!='|'&&point[j]!='\0')
temp[m++]=point[j++];
left[count]=ch;
memcpy(right[count],temp,m);
right[count][m]=ch;
right[count][m+1]='\0';
m=0;
count++;
if(point[j]=='|')
{
n=j+1;
break;
}
}
}
else
{ /*如果‘|’后的首符號(hào)和左部不同*/
left[count]=ch;
right[count][0]='^';
right[count][1]='\0';
count++;
for(j=n;j<=strlen(point)-1;j++)
{
if(point[j]!='|')
temp[m++]=point[j];
else
{
left[count]=point[0];
memcpy(right[count],temp,m);
right[count][m]=ch;
right[count][m+1]='\0';
printf(" count=%d ",count);
m=0;
count++;
}
}
left[count]=point[0];
memcpy(right[count],temp,m);
right[count][m]=ch;
right[count][m+1]='\0';
count++;
m=0;
}
}
}
/*******************************************
分解不含有左遞歸的產(chǎn)生式
********************************************/
void non_re(char *point)
{
int m=0,j;
char temp[20];
for(j=3;j<=strlen(point)-1;j++)
{
if(point[j]!='|')
temp[m++]=point[j];
else
{
left[count]=point[0];
memcpy(right[count],temp,m);
right[count][m]='\0';
m=0;
count++;
}
}
left[count]=point[0];
memcpy(right[count],temp,m);
right[count][m]='\0';
count++;
m=0;
}
/*******************************************
讀入一個(gè)文法
********************************************/
char grammer(char *t,char *n,char *left,char right[50][50])
{
char vn[50],vt[50];
char s;
char p[50][50];
int i,j,k;
printf("\n請(qǐng)輸入文法的非終結(jié)符號(hào)串:");
scanf("%s",vn);
getchar();
i=strlen(vn);
memcpy(n,vn,i);
n[i]='\0';
printf("請(qǐng)輸入文法的終結(jié)符號(hào)串:");
scanf("%s",vt);
getchar();
i=strlen(vt);
memcpy(t,vt,i);
t[i]='\0';
printf("請(qǐng)輸入文法的開始符號(hào):");
scanf("%c",&s);
getchar();
printf("請(qǐng)輸入文法產(chǎn)生式的條數(shù):");
scanf("%d",&i);
getchar();
for(j=1;j<=i;j++)
{
printf("請(qǐng)輸入文法的第%d條(共%d條)產(chǎn)生式:",j,i);
scanf("%s",p[j-1]);
getchar();
}
for(j=0;j<=i-1;j++)
if(p[j][1]!='-'||p[j][2]!='>')
{ printf("\ninput error!");
validity=0;
return('\0');
} /*檢測(cè)輸入錯(cuò)誤*/
for(k=0;k<=i-1;k++)
{ /*分解輸入的各產(chǎn)生式*/
if(p[k][3]==p[k][0])
recur(p[k]);
else
non_re(p[k]);
}
return(s);
}
/*******************************************
將單個(gè)符號(hào)或符號(hào)串并入另一符號(hào)串
********************************************/
void merge(char *d,char *s,int type)
{ /*d是目標(biāo)符號(hào)串,s是源串,type=1,源串中的‘ ^ ’一并并入目串;
type=2,源串中的‘ ^ ’不并入目串*/
int i,j;
for(i=0;i<=strlen(s)-1;i++)
{
if(type==2&&s[i]=='^')
;
else
{
for(j=0;;j++)
{
if(j<strlen(d)&&s[i]==d[j])
break;
if(j==strlen(d))
{
d[j]=s[i];
d[j+1]='\0';
break;
}
}
}
}
}
/*******************************************
求所有能直接推出^的符號(hào)
********************************************/
void emp(char c)
{ /*即求所有由‘ ^ ’推出的符號(hào)*/
char temp[10];
int i;
for(i=0;i<=count-1;i++)
{
if(right[i][0]==c&&strlen(right[i])==1)
{
temp[0]=left[i];
temp[1]='\0';
merge(empty,temp,1);
emp(left[i]);
}
}
}
/*******************************************
求某一符號(hào)能否推出‘ ^ ’
********************************************/
int _emp(char c)
{ /*若能推出,返回1;否則,返回0*/
int i,j,k,result=1,mark=0;
char temp[20];
temp[0]=c;
temp[1]='\0';
merge(empt,temp,1);
if(in(c,empty)==1)
return(1);
for(i=0;;i++)
{
if(i==count)
return(0);
if(left[i]==c) /*找一個(gè)左部為c的產(chǎn)生式*/
{
j=strlen(right[i]); /*j為右部的長(zhǎng)度*/
if(j==1&&in(right[i][0],empty)==1)
return(1);
else if(j==1&&in(right[i][0],termin)==1)
return(0);
else
{
for(k=0;k<=j-1;k++)
if(in(right[i][k],empt)==1)
mark=1;
if(mark==1)
continue;
else
{
for(k=0;k<=j-1;k++)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -