-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtables.rs
339 lines (335 loc) · 9.74 KB
/
tables.rs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use super::*;
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
pub(crate) enum Field {
Prog,
Block,
ConstDecl,
Const,
VarDecl,
Proc,
Body,
Statement,
LExp,
Exp,
Term,
Factor,
Lop,
Aop,
Mop,
Id,
Integer,
}
pub(crate) static FIELD_FOLLOW_TABLE: Lazy<HashMap<Field, HashSet<Token>>> = Lazy::new(|| {
type Set = HashSet<Token>;
let prog_follow = Set::default();
let proc_follow = FIELD_FIRST_TABLE.get(&Field::Body).unwrap().clone();
let block_follow = {
let src = vec![Token::Semicolon].into_iter().collect::<Set>();
let candidates = vec![prog_follow.clone(), proc_follow.clone()];
candidates
.into_iter()
.fold(src, |acc, x| acc.union(&x).cloned().collect())
};
let const_decl_follow = {
let unions = vec![
FIELD_FIRST_TABLE.get(&Field::VarDecl).unwrap().clone(),
FIELD_FIRST_TABLE.get(&Field::Proc).unwrap().clone(),
FIELD_FIRST_TABLE.get(&Field::Body).unwrap().clone(),
];
unions
.into_iter()
.fold(Set::default(), |acc, x| acc.union(&x).cloned().collect())
};
let const_follow = vec![Token::Semicolon, Token::Comma].into_iter().collect();
let var_decl_follow = {
let unions = vec![
FIELD_FIRST_TABLE.get(&Field::Proc).unwrap().clone(),
FIELD_FIRST_TABLE.get(&Field::Body).unwrap().clone(),
];
unions
.into_iter()
.fold(Set::default(), |acc, x| acc.union(&x).cloned().collect())
};
let body_follow = {
let unions = vec![prog_follow.clone(), proc_follow.clone()];
unions
.into_iter()
.fold(Set::default(), |acc, x| acc.union(&x).cloned().collect())
};
let statement_follow = vec![Token::Semicolon, Token::End, Token::Else]
.into_iter()
.collect::<Set>();
let l_exp_follow = vec![Token::Then, Token::Do].into_iter().collect::<Set>();
let exp_follow = {
let src = vec![Token::ParR, Token::Comma].into_iter().collect::<Set>();
let candidates = vec![
statement_follow.clone(),
l_exp_follow.clone(),
FIELD_FIRST_TABLE.get(&Field::Lop).unwrap().clone(),
];
candidates
.into_iter()
.fold(src, |acc, x| acc.union(&x).cloned().collect())
};
let term_follow = exp_follow
.clone()
.union(FIELD_FIRST_TABLE.get(&Field::Aop).unwrap())
.cloned()
.collect::<Set>();
let factor_follow = term_follow
.clone()
.union(FIELD_FIRST_TABLE.get(&Field::Mop).unwrap())
.cloned()
.collect::<Set>();
let lop_follow = FIELD_FIRST_TABLE.get(&Field::Exp).unwrap().clone();
let aop_follow = FIELD_FIRST_TABLE.get(&Field::Term).unwrap().clone();
let mop_follow = FIELD_FIRST_TABLE.get(&Field::Factor).unwrap().clone();
let id_follow = vec![
Token::Semicolon,
Token::EqSign,
Token::Comma,
Token::ParL,
Token::ParR,
]
.into_iter()
.collect::<Set>()
.union(&factor_follow)
.cloned()
.collect();
let integer_follow = factor_follow
.clone()
.union(&const_follow)
.cloned()
.collect();
vec![
(Field::Prog, prog_follow),
(Field::Block, block_follow),
(Field::ConstDecl, const_decl_follow),
(Field::Const, const_follow),
(Field::VarDecl, var_decl_follow),
(Field::Proc, proc_follow),
(Field::Body, body_follow),
(Field::Statement, statement_follow),
(Field::LExp, l_exp_follow),
(Field::Exp, exp_follow),
(Field::Term, term_follow),
(Field::Factor, factor_follow),
(Field::Lop, lop_follow),
(Field::Aop, aop_follow),
(Field::Mop, mop_follow),
(Field::Id, id_follow),
(Field::Integer, integer_follow),
]
.into_iter()
.collect()
});
pub(crate) static FIELD_FIRST_TABLE: Lazy<HashMap<Field, HashSet<Token>>> = Lazy::new(|| {
type Set = HashSet<Token>;
let id_token: Set = vec![Token::Identifier(Default::default())]
.into_iter()
.collect();
let prog_first: Set = vec![Token::Program].into_iter().collect();
let const_decl_first: Set = vec![Token::Const].into_iter().collect();
let var_decl_first: Set = vec![Token::Var].into_iter().collect();
let proc_first: Set = vec![Token::Procedure].into_iter().collect();
let body_first: Set = vec![Token::Begin].into_iter().collect();
let block_first = {
let candidates = vec![
const_decl_first.clone(),
var_decl_first.clone(),
proc_first.clone(),
];
candidates.into_iter().fold(body_first.clone(), |acc, x| {
acc.union(&x).cloned().collect()
})
};
let const_first = vec![Token::Identifier(Default::default())]
.into_iter()
.collect();
let statement_first = {
let src: Set = vec![
Token::If,
Token::While,
Token::Call,
Token::Read,
Token::Write,
]
.into_iter()
.collect();
let candidates = vec![id_token.clone(), body_first.clone()];
candidates
.into_iter()
.fold(src, |acc, x| acc.union(&x).cloned().collect())
};
let factor_first: Set = vec![
Token::Identifier(Default::default()),
Token::Integer(Default::default()),
Token::ParL,
]
.into_iter()
.collect();
let term_first = factor_first.clone();
let exp_first: Set = term_first
.clone()
.union(&vec![Token::Add, Token::Sub].into_iter().collect())
.cloned()
.collect();
let l_exp_first: Set = exp_first
.clone()
.union(&vec![Token::Odd].into_iter().collect())
.cloned()
.collect();
let lop_first: Set = vec![
Token::Eq,
Token::Lt,
Token::Gt,
Token::Le,
Token::Ge,
Token::Ne,
]
.into_iter()
.collect();
let aop_first: Set = vec![Token::Add, Token::Sub].into_iter().collect();
let mop_first: Set = vec![Token::Mul, Token::Div].into_iter().collect();
vec![
(Field::Prog, prog_first),
(Field::Block, block_first),
(Field::ConstDecl, const_decl_first),
(Field::Const, const_first),
(Field::VarDecl, var_decl_first),
(Field::Proc, proc_first),
(Field::Body, body_first),
(Field::Statement, statement_first),
(Field::LExp, l_exp_first),
(Field::Exp, exp_first),
(Field::Term, term_first),
(Field::Factor, factor_first),
(Field::Lop, lop_first),
(Field::Aop, aop_first),
(Field::Mop, mop_first),
]
.into_iter()
.collect()
});
pub(crate) static TOKEN_FOLLOW_TABLE: Lazy<HashMap<Token, HashSet<Token>>> = Lazy::new(|| {
type Set = HashSet<Token>;
let id_token: Set = vec![Token::Identifier(Default::default())]
.into_iter()
.collect();
let integer_token: Set = vec![Token::Integer(Default::default())]
.into_iter()
.collect();
let end_follow = HashSet::default();
let read_follow: Set = vec![Token::ParL].into_iter().collect();
let write_follow = read_follow.clone();
let aop_follow = FIELD_FIRST_TABLE.get(&Field::Term).unwrap().clone();
let mop_follow = FIELD_FIRST_TABLE.get(&Field::Factor).unwrap().clone();
let lop_follow = FIELD_FIRST_TABLE.get(&Field::Exp).unwrap().clone();
let eqsign_follow = FIELD_FIRST_TABLE
.get(&Field::Exp)
.unwrap()
.clone()
.union(&integer_token)
.cloned()
.collect();
let par_l_follow = FIELD_FIRST_TABLE
.get(&Field::Exp)
.unwrap()
.clone()
.union(&id_token)
.cloned()
.collect();
let comma_follow = id_token
.clone()
.union(FIELD_FIRST_TABLE.get(&Field::Exp).unwrap())
.cloned()
.collect();
let statement_follow: Set = vec![Token::Semicolon, Token::End, Token::Else]
.into_iter()
.collect();
let factor_follow = FIELD_FIRST_TABLE.get(&Field::Mop).unwrap().clone();
let par_r_follow = {
let src: Set = vec![Token::Semicolon].into_iter().collect();
let candidates = vec![factor_follow.clone(), statement_follow.clone()];
candidates
.into_iter()
.fold(src, |acc, x| acc.union(&x).cloned().collect())
};
let semicolon_follow: Set = {
let candidates = vec![
FIELD_FIRST_TABLE.get(&Field::Block).unwrap().clone(),
FIELD_FIRST_TABLE.get(&Field::Statement).unwrap().clone(),
FIELD_FIRST_TABLE.get(&Field::Proc).unwrap().clone(),
];
candidates
.into_iter()
.fold(Set::default(), |acc, x| acc.union(&x).cloned().collect())
};
let id_follow = vec![
Token::Semicolon,
Token::EqSign,
Token::Comma,
Token::ParL,
Token::ParR,
]
.into_iter()
.collect();
let integer_follow = Set::default();
vec![
(
Token::If,
FIELD_FIRST_TABLE.get(&Field::LExp).unwrap().clone(),
),
(
Token::Then,
FIELD_FIRST_TABLE.get(&Field::Statement).unwrap().clone(),
),
(
Token::Else,
FIELD_FIRST_TABLE.get(&Field::Statement).unwrap().clone(),
),
(
Token::While,
FIELD_FIRST_TABLE.get(&Field::LExp).unwrap().clone(),
),
(
Token::Do,
FIELD_FIRST_TABLE.get(&Field::Statement).unwrap().clone(),
),
(Token::Const, id_token.clone()),
(Token::Var, id_token.clone()),
(Token::Procedure, id_token.clone()),
(Token::Program, id_token.clone()),
(
Token::Begin,
FIELD_FIRST_TABLE.get(&Field::Statement).unwrap().clone(),
),
(Token::End, end_follow),
(Token::Call, id_token.clone()),
(Token::Read, read_follow),
(Token::Write, write_follow),
(
Token::Odd,
FIELD_FIRST_TABLE.get(&Field::Exp).unwrap().clone(),
),
(Token::Add, aop_follow.clone()),
(Token::Sub, aop_follow.clone()),
(Token::Mul, mop_follow.clone()),
(Token::Div, mop_follow.clone()),
(Token::Eq, lop_follow.clone()),
(Token::Lt, lop_follow.clone()),
(Token::Gt, lop_follow.clone()),
(Token::Le, lop_follow.clone()),
(Token::Ge, lop_follow.clone()),
(Token::Ne, lop_follow.clone()),
(Token::EqSign, eqsign_follow),
(Token::ParL, par_l_follow),
(Token::ParR, par_r_follow),
(Token::Semicolon, semicolon_follow),
(Token::Comma, comma_follow),
(Token::Identifier(Default::default()), id_follow),
(Token::Integer(Default::default()), integer_follow),
]
.into_iter()
.collect()
});