-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.c
127 lines (101 loc) · 2.01 KB
/
Table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "state.h"
#include "ylib.h"
/*
Table.c
@author octobershiner
@version 0.1
2011 12 02
Description: This file defines the data structure of Token and implements the file accessing of token.
*/
/*
函数功能:初始化符号表
*/
struct Node* initTable(struct Node* h)
{
int i;
for (i = 1; i <= 30; i++)
{
h = add(h,symbol[i-1],i,0,0,0);
}
for(i = 40; i <= 48; i++)
{
h = add(h,keywords[i-40],i,0,0,0);
}
printf("Symbol Table initialized successfully.\n");
return h;
}
/*
函数功能:添加新的节点
*/
struct Node* add(struct Node*h,char * p,int VALUE,int LINE,int BLOCK,int type)
{
struct Node* newNode = NULL;
struct Node* temp = h;
newNode = (struct Node*)malloc(sizeof(struct Node));
if(newNode == NULL)
{
printf("fail in creating symbol table .\n");
exit(0);
}
/*初始化新的节点*/
newNode->value = VALUE;
newNode->line = LINE;
newNode->block_num = BLOCK;
newNode->type = type;
strcpy(newNode->name,p);
newNode->next =NULL;
/*当链表为空的情况*/
if(h ==NULL)
h = newNode;
else
{
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
return h;
}
/*
函数功能:打印序列
*/
void printToken(struct Node* h)
{
struct Node* p = h;
printf("(Attribute , Value, Block)\n");
while(p != NULL)
{
char* c = p->name;
printf("( %d ,",p->value);
while(*c != '\0')
printf("%c",*c++);
printf(" ,%d",p->block_num);
printf(" ,%d",p->type);
printf(")\n");
p = p->next;
}
}
/*
函数功能: 将token序列写入文件
*/
void writeTokenFile(struct Node* h)
{
struct Node* p = h;
FILE * fp = NULL;
if ( ( fp = fopen("token.txt","w+") ) == NULL)
{
printf("YuiToken->Error: Create file fail.\n");
exit(0);
}
while(p != NULL)
{
char* c = p->name;
fprintf(fp,"( %d ,",p->value);
while(*c != '\0')
fprintf(fp,"%c",*c++);
fprintf(fp," )\r\n");
p = p->next;
}
}