分析两种语法树:
- 每行的语法树
- 整个文件的语法树
每行的语法树:
( function_def ( def def )( id test_loop )( -LRB- -LRB- )( id n )( -RRB- -RRB- )( : : ) )
整个文件的语法树:
代码:
用到的HashTable: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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197//hashtable.cpp
using namespace std;
int operator % (const string & text, int max) {
int code = 0;
for (int i = 0; i < text.size(); ++ i) {
int code1 = text[i];
code1 = code1 << (i * 8 % 24);
code = code ^ code1;
}
return code % max;
}
class NoSuchKeyException {};
template <typename K, typename V>
class HashTable
{
private:
class Entry
{
public:
K key;
V value;
bool isInUse;
Entry() {
isInUse = false;
}
};
Entry * entries;
int capacity;
int count;
void initialize(int capacity2) {
count = 0;
capacity = capacity2;
entries = new Entry[capacity];
}
void assign(const HashTable & map2) {
count = map2.count;
capacity = map2.capacity;
entries = new Entry[capacity];
for (int i = 0; i < capacity; ++ i) {
entries[i] = map2.entries[i];
}
}
public:
HashTable() {
initialize(2);
}
~HashTable() {
delete [] entries;
}
HashTable(const HashTable & map2) {
assign(map2);
}
HashTable & operator = (const HashTable & map2) {
delete [] entries;
assign(map2);
return (*this);
}
void clear() {
delete [] entries;
initialize(2);
}
private:
int hashIndex(const K & key) const {
return key % capacity;
}
int find(const K & key) const {
int index = hashIndex(key);
while (true) {
if (! entries[index].isInUse) {
return index;
}
if (entries[index].key == key) {
return index;
}
index = (index + 1) % capacity;
}
}
void resize(int capacity2) {
Entry * entries0 = entries;
int capacity0 = capacity;
initialize(capacity2);
for (int i = 0; i < capacity0; ++ i) {
if (entries0[i].isInUse) {
put(entries0[i].key, entries0[i].value);
}
}
delete [] entries0;
}
public:
void put(const K & key, const V & value) {
int index = find(key);
entries[index].value = value;
if (entries[index].isInUse) return;
entries[index].isInUse = true;
entries[index].key = key;
++ count;
if (count > capacity / 2) {
resize(capacity * 2);
}
}
V get(const K & key) const {
int index = find(key);
if (! entries[index].isInUse) {
throw NoSuchKeyException();
}
return entries[index].value;
}
bool remove(const K & key) {
int index = find(key);
if (! entries[index].isInUse) return false;
fillNotInUseEntry(index);
-- count;
if (count < capacity / 4) {
resize(capacity / 2);
}
return true;
}
private:
void fillNotInUseEntry(int index) {
int next = index;
while (true) {
next = (next + 1) % capacity;
if (! entries[next].isInUse) {
entries[index].isInUse = false;
return;
}
int index0 = hashIndex(entries[next].key);
if (index < next) {
if (index0 > index &&
index0 <= next) continue;
}
else {
if (index0 > index ||
index0 <= next) continue;
}
entries[index] = entries[next];
index = next;
}
}
public:
bool containsKey(const K & key) const {
int index = find(key);
return (entries[index].isInUse);
}
int size() const {
return count;
}
vector<K> getKeys() const {
vector<K> vec;
for (int i = 0; i < capacity; ++ i) {
if (entries[i].isInUse) {
vec.push_back(entries[i].key);
}
}
return vec;
}
};
1 | //optim.cpp |
1 | //token.cpp |
1 | //tree.cpp |
1 | #test1.py |