#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define PROJECT_SET_SIZE20
#define PROJECT_ID_POS 0
#define GRAMMER_ID_POS 1
#define GRAMMER_START_CHAR_POS 5
#define BFCHAR_POS 2
#define AFCHAR_POS 3
#define PROJECT_LEN_POS 4
#define ID 10000
char st[20][30];
int r;
struct grammer
{
char **g;
char vt[127];
char vn[27];
char s;
int line;
};
typedef struct
{
int id;
char ch;
}SElemType;
struct gprjt
{
char **gp;
char s;
int line;
};
typedef struct prjset
{
int id;
struct prjset *next;
char prjt[PROJECT_SET_SIZE+1];
char pointafter[PROJECT_SET_SIZE+1];
struct prjset *actorgo[PROJECT_SET_SIZE];
char pointbefore;
}prjset,*pprjset;
struct head
{
prjset *I;
prjset *tail;
int size;
};
char DOT = '~';
grammer g;
gprjt project;
head root;
void Input();
int OpenFile(char *s);
void CreateProjectSet();
void Closure(prjset *prjset);
int go(int rk, prjset *prjset);
void PrintPojectSet();
int IsInSet(char *s, char ch);
void JoinSet(char *s, char ch);
void main()
{
for(;;){
r=1;
cout<<"请输入文法规则(以#作为结尾):"<<endl;
for(;;){
string z;
cin>>z;
int m=0;
int w=0;
for(;m
if(z[m]=='#'){w=1;break; }
st[r][m]=z[m];
}
st[r][m]='@';
r=r+1;
if(w==1) break;
}
st[0][0]=st[1][0];st[0][1]='-';st[0][2]='>';st[0][3]=st[1][0];st[0][4]='@';
int count;
int i;
int j;
count = r;
g.line=count;
g.g=(char **)malloc(sizeof(char *)*g.line);
for(i=0; i
{
g.g[i]=(char *)malloc(21);
g.g[i][0] = i;
g.g[i][1] = 0;
}
for(int x=0;x
{
j = 2;
for(inty=0;st[x][y]!='@';y++)
{
if(st[x][y]=='-'&& st[x][y+1]=='>'){y=y+1;continue;}
else
{
charch=st[x][y];
g.g[x][j] =ch;
g.g[x][1]++;
j++;
}
}
}
g.vn[0] = g.vt[0] = 0;
for(i=0; i
{
for(j=2; j<=g.g[i][1]+1;j++)
{
if(g.g[i][j]>='A'&& g.g[i][j]<='Z')
JoinSet(g.vn,g.g[i][j]);
else
JoinSet(g.vt,g.g[i][j]);
}
}
g.s = g.vn[1];
count = 0;
for(i=0; i
count += g.g[i][1];
if((project.gp=(char **)malloc(sizeof(char*)*count)) == NULL)exit(1);
int k;
int pointpos;
int m;
int n;
k = 0;
for(i=0; i
{
pointpos =GRAMMER_START_CHAR_POS + 1;
for(j=0; j
{
if((project.gp[k]=(char*)malloc(6+g.g[i][1])) ==NULL)exit(1);
project.gp[k][PROJECT_ID_POS]= k;
project.gp[k][GRAMMER_ID_POS]= i;
project.gp[k][PROJECT_LEN_POS]= g.g[i][1]+1;
project.gp[k][GRAMMER_START_CHAR_POS]= g.g[i][2];
n = 3;
for(m=GRAMMER_START_CHAR_POS+1;m
{
project.gp[k][m]= g.g[i][n];
n++;
}
project.gp[k][pointpos]= DOT;
for(m=pointpos+1;m<=project.gp[k][PROJECT_LEN_POS]+PROJECT_LEN_POS; m++)
{
project.gp[k][m]= g.g[i][n];
n++;
}
if(pointpos==project.gp[k][PROJECT_LEN_POS]+PROJECT_LEN_POS)
{
project.gp[k][AFCHAR_POS]= '\0';
}
else
{
project.gp[k][AFCHAR_POS]= project.gp[k][pointpos+1];
}
if(pointpos==GRAMMER_START_CHAR_POS+1)
{