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
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt::{self, Display, Formatter};
pub struct OpTable{
table: HashMap<(String, String), char>,
ts: HashSet<String>
}
impl OpTable {
pub fn new(ts: HashSet<String>) -> OpTable {
OpTable {
table: HashMap::new(),
ts: ts
}
}
pub fn insert(&mut self, ttuple: &(String, String), ch: char) {
if self.table.contains_key(&ttuple) && self.table[&ttuple] != ch {
println!("The grammar is ambiguous.");
panic!("Ambiguous grammar detected.");
}
self.table.insert(ttuple.clone(), ch);
}
pub fn to_string(&self) -> String{
let mut output:String = "".to_owned();
output = output + " \t";
for j in self.ts.iter() {
output = output + j + "\t";
}
output = output + "\n";
for i in self.ts.iter() {
output = output + i + "\t";
for j in self.ts.iter() {
let ttuple = (i.clone(), j.clone());
if self.table.contains_key(&ttuple) {
output = output + &self.table[&ttuple].to_string() + "\t";
} else {
output = output + " \t";
}
}
output = output + "\n";
}
output
}
}
impl Display for OpTable {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let output = self.to_string();
write!(f, "{}", output)
}
}