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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! Earley Parsing for context-free-grammars.
#![cfg_attr(doctest, doc = "````no_test")] // highlight, but don't run the test (rust/issues/63193)
//! ```
//! let mut g = CFG::new("EXP");
//!
//! g.add_rule("EXP", vec![nt("EXP"), tr('-'), nt("EXP")]);
//! g.add_rule("EXP", vec![nt("TERM")]);
//!
//! g.add_rule("TERM", vec![nt("TERM"), tr('/'), nt("TERM")]);
//! g.add_rule("TERM", vec![nt("FACTOR")]);
//!
//! g.add_rule("FACTOR", vec![tr('('), nt("EXP"), tr(')')]);
//! for a in '0'..='9' {
//! g.add_rule("FACTOR", vec![tr(a)]);
//! }
//!
//! assert!(parse("5--5", &g).is_none());
//! assert!(parse("5-5", &g).is_some());
//!
//! let result = parse("(5-5)/(2-3/4)", &g);
//! assert!(result.is_some());
//! println!("{:#?}", PrettyPrint(&result.unwrap().collapse()));
//! // TERM(FACTOR('(', EXP('5', '-', '5'), ')'), '/', FACTOR('(', EXP('2', '-', TERM('3', '/', '4')), ')'))
//! ````
use std::cmp;
use std::collections::{BTreeSet, HashMap, VecDeque};
use std::rc::Rc;
pub type Terminal = char;
pub type NonTerminal = String;
/// A sequence of `Symbol`s forms the right-hand-side of a CFG production.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Symbol {
Terminal(Terminal),
NonTerminal(NonTerminal),
}
impl Symbol {
fn strval(&self) -> String {
match self {
Symbol::Terminal(ref c) => c.to_string(),
Symbol::NonTerminal(ref s) => s.clone(),
}
}
}
/// Convenience function for creating a nonterminal `Symbol`
pub fn nt(x: impl Into<NonTerminal>) -> Symbol {
Symbol::NonTerminal(x.into())
}
/// Convenience function for creating a terminal `Symbol`
pub fn tr(x: Terminal) -> Symbol {
Symbol::Terminal(x)
}
/// A struct holding production rules for a CFG.
pub struct CFG {
start: NonTerminal,
rule_map: HashMap<NonTerminal, Vec<Vec<Symbol>>>,
dummy: Vec<Vec<Symbol>>,
}
impl CFG {
/// Initialize the CFG with the starting symbol.
pub fn new(start: impl Into<NonTerminal>) -> Self {
Self {
start: start.into(),
rule_map: HashMap::new(),
dummy: Vec::new(),
}
}
pub fn add_rule(&mut self, lhs: impl Into<NonTerminal>, rhs: Vec<Symbol>) {
let lhs: NonTerminal = lhs.into();
self.rule_map
.entry(lhs)
.or_insert_with(|| Vec::new())
.push(rhs)
}
pub fn rules(&self, lhs: &NonTerminal) -> &[Vec<Symbol>] {
self.rule_map.get(lhs).unwrap_or(&self.dummy).as_slice()
}
}
#[derive(Debug, Clone)]
pub struct ASTNode {
sym: Symbol,
children: Vec<ASTNode>,
}
impl ASTNode {
pub fn string_value(&self) -> String {
match self.sym {
Symbol::Terminal(ref c) => c.to_string(),
Symbol::NonTerminal(ref s) => s.clone(),
}
}
pub fn symbol(&self) -> &Symbol {
&self.sym
}
pub fn is_terminal(&self) -> bool {
match self.sym {
Symbol::Terminal(_) => true,
Symbol::NonTerminal(_) => false,
}
}
pub fn children(&self) -> &[ASTNode] {
self.children.as_slice()
}
/// Collapse the parse tree.
/// Returns a tree where no nonterminal node has a single child.
pub fn collapse(mut self) -> ASTNode {
if self.children.is_empty() || self.is_terminal() {
self
} else if self.children.len() == 1 {
return self.children.pop().unwrap().collapse();
} else {
self.children = self.children.into_iter().map(|c| c.collapse()).collect();
self
}
}
}
#[derive(Debug, Clone)]
struct EarleyState {
lhs: NonTerminal,
rhs: Vec<Symbol>,
rhs_idx: usize,
start_idx: usize,
left_parent: Option<Rc<EarleyState>>, // These are intern IDs
right_parent: Option<Rc<EarleyState>>,
}
impl EarleyState {
fn done(&self) -> bool {
self.rhs_idx == self.rhs.len()
}
fn new(lhs: NonTerminal, rhs: Vec<Symbol>, start_idx: usize) -> Self {
Self {
lhs,
rhs,
start_idx,
rhs_idx: 0,
left_parent: None,
right_parent: None,
}
}
fn advance(&self) -> Self {
Self {
lhs: self.lhs.clone(),
rhs: self.rhs.clone(),
rhs_idx: self.rhs_idx + 1,
start_idx: self.start_idx,
left_parent: None,
right_parent: None,
}
}
fn next_sym(&self) -> Symbol {
self.rhs[self.rhs_idx].clone()
}
}
impl std::fmt::Display for EarleyState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ->", self.lhs)?;
for i in 0..self.rhs.len() {
if i == self.rhs_idx {
write!(f, " .")?;
}
write!(f, " {}", self.rhs[i].strval())?;
}
write!(f, " {}", self.start_idx)
}
}
impl cmp::Ord for EarleyState {
fn cmp(&self, other: &EarleyState) -> cmp::Ordering {
// Actual compare
(self.start_idx + self.rhs_idx)
.cmp(&(other.start_idx + other.rhs_idx))
// by done
.then_with(|| self.done().cmp(&other.done()))
// doesn't matter, but needs to be consistent
.then_with(|| self.lhs.cmp(&other.lhs))
.then_with(|| self.rhs.cmp(&other.rhs))
.then_with(|| self.rhs_idx.cmp(&other.rhs_idx))
.then_with(|| self.start_idx.cmp(&other.start_idx))
}
}
impl cmp::PartialOrd for EarleyState {
fn partial_cmp(&self, other: &EarleyState) -> Option<cmp::Ordering> {
Some(cmp::Ord::cmp(self, other))
}
}
impl cmp::PartialEq for EarleyState {
fn eq(&self, other: &EarleyState) -> bool {
self.cmp(other) == cmp::Ordering::Equal
}
}
impl cmp::Eq for EarleyState {}
/// Perform Earley parsing on the input using the given CFG.
pub fn parse(input: &str, grammar: &CFG) -> Option<ASTNode> {
let mut mem = vec![BTreeSet::new(); input.len() + 1];
for rhs in grammar.rules(&grammar.start) {
mem[0].insert(Rc::new(EarleyState::new(
grammar.start.clone(),
rhs.clone(),
0,
)));
}
for i in 0..=input.len() {
let mut q = mem[i].iter().map(|s| s.clone()).collect::<VecDeque<_>>();
while let Some(curr_state) = q.pop_front() {
if !curr_state.done() {
match curr_state.next_sym() {
Symbol::NonTerminal(ref nt) => {
// predict
for rhs in grammar.rules(nt) {
let mut new_state =
Rc::new(EarleyState::new(nt.clone(), rhs.clone(), i));
if !mem[i].contains(&new_state) {
Rc::get_mut(&mut new_state).unwrap().left_parent =
Some(Rc::clone(&curr_state));
mem[i].insert(Rc::clone(&new_state));
q.push_back(new_state);
}
}
}
Symbol::Terminal(t) => {
// Scan
if i < input.len() && t == input.as_bytes()[i] as char {
let mut new_state = Rc::new(curr_state.advance());
if !mem[i + 1].contains(&new_state) {
Rc::get_mut(&mut new_state).unwrap().left_parent =
Some(Rc::clone(&curr_state));
mem[i + 1].insert(new_state);
}
}
}
}
} else {
// Complete
let iterlist = mem[curr_state.start_idx]
.iter()
.map(|s| Rc::clone(s))
.collect::<Vec<_>>();
for state in iterlist.into_iter() {
if !state.done()
&& state.next_sym() == Symbol::NonTerminal(curr_state.lhs.clone())
{
let mut new_state = Rc::new(state.advance());
if !mem[i].contains(&new_state) {
Rc::get_mut(&mut new_state).unwrap().left_parent =
Some(Rc::clone(&state));
Rc::get_mut(&mut new_state).unwrap().right_parent =
Some(Rc::clone(&curr_state));
mem[i].insert(Rc::clone(&new_state));
q.push_back(new_state);
}
}
}
}
}
}
fn generate_parse_tree(state: Rc<EarleyState>) -> ASTNode {
let mut iter = Rc::clone(&state);
let mut children = Vec::new();
for i in (0..state.rhs.len()).rev() {
match state.rhs[i] {
Symbol::NonTerminal(_) => children.insert(
0,
generate_parse_tree(Rc::clone(iter.right_parent.as_ref().unwrap())),
),
Symbol::Terminal(tt) => children.insert(
0,
ASTNode {
sym: tr(tt),
children: Vec::new(),
},
),
}
iter = Rc::clone(iter.left_parent.as_ref().unwrap());
}
return ASTNode {
sym: nt(state.lhs.clone()),
children,
};
}
mem[input.len()]
.iter()
.filter(|&s| s.lhs == grammar.start && s.start_idx == 0 && s.done())
.nth(0)
.map(|state| generate_parse_tree(Rc::clone(state)))
}
/// A struct with a pretty `Debug` impl for `ASTNode`s.
pub struct PrettyPrint<'a>(pub &'a ASTNode);
impl<'a> std::fmt::Debug for PrettyPrint<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0.sym {
Symbol::Terminal(c) => write!(f, "'{}'", c),
Symbol::NonTerminal(ref s) => {
let mut tup = f.debug_tuple(s);
for child in self.0.children() {
tup.field(&PrettyPrint(child));
}
tup.finish()
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_arith() {
let mut g = CFG::new("EXP");
g.add_rule("EXP", vec![nt("EXP"), tr('-'), nt("EXP")]);
g.add_rule("EXP", vec![nt("TERM")]);
g.add_rule("TERM", vec![nt("TERM"), tr('/'), nt("TERM")]);
g.add_rule("TERM", vec![nt("FACTOR")]);
g.add_rule("FACTOR", vec![tr('('), nt("EXP"), tr(')')]);
for a in '0'..='9' {
g.add_rule("FACTOR", vec![tr(a)]);
}
assert!(parse("5--5", &g).is_none());
assert!(parse("5-5", &g).is_some());
let result = parse("(5-5)/(2-3/4)", &g);
assert!(result.is_some());
println!("{:?}", PrettyPrint(&result.unwrap().collapse()));
}
}