📄 syn_lex.h
字号:
#ifndef SYN_LEX_H_INCLUDED
#define SYN_LEX_H_INCLUDED
void error(char *str)
{
printf("Error: %s!\n", str);
exit(1);
}
int load_init()
{
tcount = 0;
scount = 0;
pcount = 0;
strcpy(file, "test.txt");
fp = fopen(file, "rb+");
if (fp == NULL)
{
printf("open file error!\n");
return -1;
}
return 0;
}
void add_string(char * str)
{
int len = 0;
len = strlen(str);
sign_string[scount] = (char*)malloc(len+1);
strcpy(sign_string[scount], str);
sign_string[scount][len] = 0;
}
int get_id(char *str)
{
int i = 0;
for(i = 0; i < scount; i++)
{
if (strcmp(str, sign_string[i]) == 0)
return i;
}
return -1;
}
void load_sign()
{
int state = 0;
char buf[SIZE];
while(1)
{
switch(state)
{
case 0:
fscanf(fp, "%s", buf);
if (strcmp(buf, DEPART) == 0)
state = 1;
else
{
add_string(buf);
tcount++;
scount++;
}
break;
case 1:
fscanf(fp, "%s", buf);
if (strcmp(buf, DEPART) == 0)
state = 2;
else
{
add_string(buf);
scount++;
}
break;
case 2:
return;
}
}
return;
}
void load_produce()
{
int state = 0;
char buf[SIZE];
int id = 0;
struct sign_node *cptr = NULL;
struct sign_node *temp = NULL;
while(1)
{
switch(state)
{
case 0:
fscanf(fp, "%s", buf);
if (strcmp(buf, DEPART) == 0)
return;
id = get_id(buf);
if (id == -1)
{
printf("%d ", count);
strcat(buf, " left");
error(buf);
}
else
{
sign[pcount].id = id;
sign[pcount].next = NULL;
cptr = &sign[pcount];
pcount++;
state = 1;
}
break;
case 1:
fscanf(fp, "%s", buf);
if (strcmp(buf, "-->") == 0)
state = 2;
else
error("-->");
break;
case 2:
fscanf(fp, "%s", buf);
if (strcmp(buf, "\\") == 0)
{
count++;
state = 0;
}
else
{
temp = (struct sign_node *)malloc(sizeof(struct sign_node));
id = get_id(buf);
if (id == -1)
{
strcat(buf, " right");
error(buf);
}
else
{
temp->id = id;
temp->next = NULL;
cptr->next = temp;
cptr = temp;
}
}
break;
}
}
}
void load()
{
load_sign();
load_produce();
fclose(fp);
}
void free_produce(int pos)
{
struct sign_node *p = NULL;
p = sign[pos].next;
while(p != NULL)
{
sign[pos].next = p->next;
free(p);
p = sign[pos].next;
}
}
void print_sign(FILE *stream)
{
int i = 0;
fprintf(stream,"print signs:\n");
for(i = 0; i < scount; i++)
fprintf(stream,"%s\n", sign_string[i]);
}
void print_single_produce(FILE *stream, int num)
{
struct sign_node *p = NULL;
p = &sign[num];
fprintf(stream, "%s -> ", sign_string[p->id]);
p = p -> next;
while(p != NULL)
{
fprintf(stream, "%s ", sign_string[p->id]);
p = p -> next;
}
fprintf(stream, "\n");
}
void print_all_produce(FILE *stream)
{
int i = 0;
fprintf(stream, "print produce:\n");
for(i = 0; i < pcount; i++)
{
print_single_produce(stream, i);
}
}
#endif // SYN_LEX_H_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -