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
use crate::analysis::{non_productive_non_terminals, unreachable_non_terminals};
use crate::errors::*;
use crate::{detect_left_recursions, left_factor, Cfg};
pub fn check_and_transform_grammar(cfg: &Cfg) -> Result<Cfg> {
let non_productive = non_productive_non_terminals(cfg);
if !non_productive.is_empty() {
let non_productive_string = non_productive
.iter()
.map(|nt| nt.to_string())
.collect::<Vec<String>>()
.join(", ");
return Err(format!(
"Grammar contains non-productive non-terminals:\n{}",
non_productive_string
)
.into());
}
let unreachable = unreachable_non_terminals(cfg);
if !unreachable.is_empty() {
let unreachable_string = unreachable
.iter()
.map(|nt| nt.to_string())
.collect::<Vec<String>>()
.join(", ");
return Err(format!(
"Grammar contains unreachable non-terminals:\n{}",
unreachable_string
)
.into());
}
let left_recursions = detect_left_recursions(cfg);
if !left_recursions.is_empty() {
let left_recursions_string = left_recursions
.iter()
.map(|n| {
n.iter()
.map(|s| format!("{}", s))
.collect::<Vec<String>>()
.join(" => ")
})
.collect::<Vec<String>>()
.join(", ");
return Err(format!(
"Grammar contains left_recursions:\n{}",
left_recursions_string
)
.into());
}
Ok(left_factor(cfg))
}