📄 syntax.h
字号:
#ifndef SYNTAX_H_INCLUDED
#define SYNTAX_H_INCLUDED
#include "GLOBAL.H"
#include "STACK.H"
#include "SYN_LEX.H"
#include "ANALYSE_TABLE.H"
#define STACKSIZE 512
#define TOKENEND -1
#define DEALOK 0
#define STREAMEND -1
int current_token = 0;
FILE *testfp = NULL;
//int program[256] = {2, 7, 13, 1, 7, 15, 3, 7, 14, 11, 7, 25, 7, 37, 7, 45, 12, 18, 13, 14, 11, 1, 7, 45, 12, TOKENEND};
//int tptr = 0;
void next_token()
{
int t;
if (fscanf(testfp, "%d", &t) == EOF)
current_token = TOKENEND;
else
current_token = t;
}
void push_produce(pstack head, int produce_id)
{
struct sign_node *p = NULL;
int sign_count = 0;
int element = 0;
pstack temp = NULL;
new_stack(&temp, 256);
p = sign[produce_id].next;
while(p != NULL)
{
push(temp, p->id);
sign_count++;
p = p -> next;
}
while(sign_count > 0)
{
element = top(temp);
pop(temp);
if (push(head, element) != 0)
error("stack full");
sign_count--;
}
delete_stack(&temp);
}
int syn_error(pstack head)
{
int top_sign = top(head);
printf("***********************Error Syntax*******************\n");
if (top_sign >= tcount)
{
while(follow_set[top_sign][current_token] != 1)
{
next_token();
if(current_token == TOKENEND)
return STREAMEND;
printf("\"%s\" is skipped\n", sign_string[top_sign]);
}
printf("******************************************************\n");
}
else
{
pop(head);
printf("\"%s\" is popped\n", sign_string[top_sign]);
printf("******************************************************\n");
}
return DEALOK;
}
void output_produce(int produce_id)
{
print_single_produce(stdout, produce_id);
}
void syntax()
{
//int current_token = 0;
int top_sign = TOKENEND + 1;
pstack head = NULL;
new_stack(&head, STACKSIZE);
push(head, TOKENEND);
push(head, tcount);
next_token();
do
{
top_sign = top(head);
//current_token = get_current_token();
if (top_sign == TOKENEND || top_sign < tcount)
{
if (top_sign == 0)
pop(head);
else if (top_sign == current_token)
{
if (top_sign > 0)
printf("-------------------->%s\n", sign_string[top_sign]);
pop(head);
next_token();
}
else
if (syn_error(head) == STREAMEND)
break;
}
else
{
if (analyse_table[top_sign-tcount][current_token].next != NULL)
{
int produce_id = 0;
pop(head);
produce_id = analyse_table[top_sign-tcount][current_token].next->id;
push_produce(head, produce_id);
output_produce(produce_id);
}
else
if (syn_error(head) == STREAMEND)
break;
}
} while(top_sign != TOKENEND);
delete_stack(&head);
}
void test_syntax()
{
testfp = fopen("..\\..\\token.txt", "rb+");
if (testfp == NULL)
{
printf("open file fail!\n");
return;
}
syntax();
fclose(testfp);
}
#endif // SYNTAX_H_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -