-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRule.java
More file actions
68 lines (61 loc) · 1.49 KB
/
Rule.java
File metadata and controls
68 lines (61 loc) · 1.49 KB
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
public class Rule {
public Token[] rule;
public Rule(Token[] rule) {
this.rule = rule;
}
public Rule() {
this.rule = new Token[0];
}
public static Rule CreateEplsilonRule() {
return new Rule(new Token[] {new Terminal(Terminal.TerminalType.EPSILON)});
}
public void AddItem(Token rule) {
Token[] newRule = new Token[this.rule.length + 1];
for (int i = 0; i < this.rule.length; i++) {
newRule[i] = this.rule[i];
}
newRule[newRule.length-1] = rule;
this.rule = newRule;
}
// public void ReplaceItem(Token replace, Token[] with) {
// int i = 0;
// boolean found = false;
// while (i < rule.length) {
// if (rule[i].name.equals(replace.name)) {
// found = true;
// break;
// }
// i++;
// }
// Token[] newRule = new Token[rule.length + with.length];
// if (found) {
// for (int j = 0; j < i; j++) {
// newRule[j] = rule[j];
// }
// for (int j = 0; j < with.length; j++) {
// newRule[j+i] = with[j];
// }
// for (int j = i+1; j < rule.length; j++) {
// newRule[j+with.length] = rule[j];
// }
// }
// }
public String toString() {
StringBuilder sb = new StringBuilder();
for (Token token : rule) {
sb.append(token.toString());
sb.append(" ");
}
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Rule)) return false;
Rule r = (Rule)o;
if (r.rule.length != rule.length) return false;
for (int i = 0; i < rule.length; i++) {
if (rule[i] != r.rule[i]) return false;
}
return true;
}
}