📄 analyse_table.h
字号:
#ifndef ANALYSE_TABLE_H_INCLUDED
#define ANALYSE_TABLE_H_INCLUDED
void init_first_set()
{
int i, j;
for(i = 0; i < scount; i++)
for(j = 0; j < scount; j++)
first_set[i][j] = 0;
for(i = 0; i < tcount; i++)
first_set[i][i] = 1;
}
int iscomplete(int id)
{
int i = 0;
if (id < tcount)
return 1;
for(i = 0; i < pcount; i++)
{
if (id == sign[i].id && cptr[i] != NULL)
return 0;
}
return 1;
}
int or_set(int *dest, int *sorce, int start, int end)
{
int i = 0;
int reval = 0;
for(i = start; i < end; i++)
if (sorce[i] == 1)
{
if (dest[i] == 0)
reval = 1;
dest[i] = 1;
}
return reval;
}
void fill_first_set()
{
int pnum = pcount;
int i = 0;
for(i = 0; i < pcount; i++)
cptr[i] = sign[i].next;
while(pnum != 0)
{
for(i = 0; i < pcount; i++)
{
if(cptr[i] == NULL)
continue;
if(iscomplete(cptr[i]->id) == 0)
continue;
or_set(first_set[sign[i].id], first_set[cptr[i]->id], 1, tcount);
if(first_set[cptr[i]->id][0] == 0)
{
cptr[i] = NULL;
pnum--;
}
else
{
if(cptr[i]->next == NULL)
{
first_set[sign[i].id][0] = 1;
pnum--;
}
cptr[i] = cptr[i]->next;
}
}
}
}
void init_follow_set()
{
int i, j;
for(i = 0; i < scount; i++)
for(j = 0; j < scount; j++)
follow_set[i][j] = 0;
follow_set[tcount][0] = 1;
}
void get_first_set(struct sign_node *ptr, int *arg_set)
{
int i = 0;
for(i = 0; i < scount; i++)
arg_set[i] = 0;
while(ptr != NULL)
{
or_set(arg_set, first_set[ptr->id], 1, tcount);
if (first_set[ptr->id][0] == 0)
return;
ptr = ptr->next;
}
if(ptr == NULL)
{
arg_set[0] = 1;
return;
}
}
void fill_follow_set()
{
int i = 0;
int set_update_flag = 0;
int arg_set[SIZE];
struct sign_node *p = NULL;
for(i = 0; i < pcount; i++)
{
p = sign[i].next;
while(p != NULL)
{
if (p->id < tcount)
{
p = p -> next;
continue;
}
get_first_set(p->next, arg_set);
or_set(follow_set[p->id], arg_set, 1, tcount);
if (arg_set[0] == 1)
follow_set[sign[i].id][p->id] = 1;
p = p -> next;
}
}
set_update_flag = 1;
while(set_update_flag == 1)
{
set_update_flag = 0;
for(i = tcount; i < scount; i++)
{
int j = 0;
for(j = tcount; j < scount; j++)
{
if (follow_set[i][j] == 0)
continue;
set_update_flag = or_set(follow_set[j], follow_set[i], 0, tcount);
}
}
}
}
void print_set(FILE *stream, int array[SIZE][SIZE])
{
int i, j;
//printf("print set\n");
for(i = 0; i < scount; i++)
{
for(j = 0; j < scount; j++)
fprintf(stream, "%d ", array[i][j]);
fprintf(stream, "\n");
}
}
void print_set_string(FILE *stream, int array[SIZE][SIZE], int start, int end)
{
int i = 0;
int j = 0;
//printf("print first set\n");
for(i = start; i < end; i++)
{
fprintf(stream, "%s -> ", sign_string[i]);
for(j = 0; j < tcount; j++)
if (array[i][j] == 1)
fprintf(stream, "%s ", sign_string[j]);
fprintf(stream, "\n");
}
}
void init_analyse_table()
{
int i = 0;
int j = 0;
for(i = 0; i < scount - tcount; i++)
for(j = 0; j < tcount; j++)
{
analyse_table[i][j].next = NULL;
analyse_table[i][j].id = -1;
}
}
void fill_analyse_table()
{
int i = 0;
int j = 0;
int arg_set[SIZE];
analyse_node *p = NULL;
init_analyse_table();
for(i = 0; i < pcount; i++)
{
get_first_set(sign[i].next, arg_set);
for(j = 1; j < tcount; j++)
{
if (arg_set[j] == 0)
continue;
p = (analyse_node *)malloc(sizeof(analyse_node));
p->id = i;
p->next = analyse_table[sign[i].id-tcount][j].next;
analyse_table[sign[i].id-tcount][j].next = p;
}
if (arg_set[0] == 1)
{
for(j = 0; j < tcount; j++)
{
if (follow_set[sign[i].id][j] == 0)
continue;
p = (analyse_node *)malloc(sizeof(analyse_node));
p->id = i;
p->next = analyse_table[sign[i].id-tcount][j].next;
analyse_table[sign[i].id-tcount][j].next = p;
}
}
}
}
void print_analyse_table(FILE *stream)
{
int i = 0;
int j = 0;
analyse_node *p = NULL;
for(i = 0; i < scount - tcount; i++)
{
for(j = 0; j < tcount; j++)
{
p = analyse_table[i][j].next;
if (p == NULL)
fprintf(stream, " e*");
else
{
while(p != NULL)
{
fprintf(stream, "%3d", p->id);
p = p -> next;
}
fprintf(stream, "*");
}
}
fprintf(stream, "\n");
}
}
#endif // ANALYSE_TABLE_H_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -