-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyGrammar.java
109 lines (85 loc) · 3.05 KB
/
MyGrammar.java
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
import java.util.ArrayList;
import java.util.HashSet;
import computation.contextfreegrammar.ContextFreeGrammar;
import computation.contextfreegrammar.Rule;
import computation.contextfreegrammar.Terminal;
import computation.contextfreegrammar.Variable;
import computation.contextfreegrammar.Word;
public abstract class MyGrammar {
// this version of the grammar reuses rules where possible (fewer rules gives inefficient code a better
// chance of running) plus doesn't use subscript variables
public static ContextFreeGrammar courseworkCNF() {
Variable S = new Variable('S');
Variable E = new Variable('E');
Variable T = new Variable('T');
Variable F = new Variable('F');
Variable A = new Variable('A');
Variable B = new Variable('B');
Variable P = new Variable('P');
Variable M = new Variable('M');
Variable N = new Variable('N');
Variable C = new Variable('C');
HashSet<Variable> variables = new HashSet<>();
variables.add(S);
variables.add(E);
variables.add(T);
variables.add(F);
variables.add(A);
variables.add(B);
variables.add(P);
variables.add(M);
variables.add(N);
variables.add(C);
Terminal plus = new Terminal('+');
Terminal mult = new Terminal('*');
Terminal neg = new Terminal('-');
Terminal one = new Terminal('1');
Terminal zero = new Terminal('0');
Terminal x = new Terminal('x');
HashSet<Terminal> terminals = new HashSet<>();
terminals.add(plus);
terminals.add(mult);
terminals.add(neg);
terminals.add(one);
terminals.add(zero);
terminals.add(x);
ArrayList<Rule> rules = new ArrayList<>();
rules.add(new Rule(S, new Word(E, A)));
rules.add(new Rule(S, new Word(T, B)));
rules.add(new Rule(S, new Word(N, C)));
rules.add(new Rule(S, new Word(one)));
rules.add(new Rule(S, new Word(zero)));
rules.add(new Rule(S, new Word(x)));
rules.add(new Rule(E, new Word(E, A)));
rules.add(new Rule(E, new Word(T, B)));
rules.add(new Rule(E, new Word(N, C)));
rules.add(new Rule(E, new Word(one)));
rules.add(new Rule(E, new Word(zero)));
rules.add(new Rule(E, new Word(x)));
rules.add(new Rule(T, new Word(T, B)));
rules.add(new Rule(T, new Word(N, C)));
rules.add(new Rule(T, new Word(one)));
rules.add(new Rule(T, new Word(zero)));
rules.add(new Rule(T, new Word(x)));
rules.add(new Rule(F, new Word(N, C)));
rules.add(new Rule(F, new Word(one)));
rules.add(new Rule(F, new Word(zero)));
rules.add(new Rule(F, new Word(x)));
rules.add(new Rule(A, new Word(P, T)));
rules.add(new Rule(B, new Word(M, F)));
rules.add(new Rule(N, new Word(neg)));
rules.add(new Rule(P, new Word(plus)));
rules.add(new Rule(M, new Word(mult)));
rules.add(new Rule(C, new Word(one)));
rules.add(new Rule(C, new Word(zero)));
rules.add(new Rule(C, new Word(x)));
ContextFreeGrammar cfg = new ContextFreeGrammar(variables, terminals, rules, S);
return cfg;
}
public static void main(String...args) {
ContextFreeGrammar cfg = courseworkCNF();
System.out.println(cfg);
assert(cfg.isInChomskyNormalForm());
assert(cfg.equals(new ContextFreeGrammar(cfg.getRules())));
}
}