summaryrefslogtreecommitdiff
path: root/Homework/cs5300/project-three/parser/Rule.java
blob: b251e8420289d5d6d9ce01f1867f79249e5ab978 (plain) (blame)
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
/*
 * Do not modify this file.
 */
package parser;

import java.util.ArrayList;
import java.util.Objects;

/**
 *
 */
public class Rule {

  private int name;
  private final String lhs;
  private ArrayList<String> rhs;

  public Rule(String lhs) {
    this.name = 0;
    this.lhs = lhs;
    rhs = new ArrayList<>();
  }

  public void setName(int name) {
    this.name = name;
  }

  public int getName() {
    return this.name;
  }

  @Override
  public String toString() {
    String ret = "R" + name + " " + lhs + " -> ";
    for (String symbol : getRhs()) {
      ret = ret + symbol + " ";
    }
    return ret;
  }

  @Override
  public int hashCode() {
    int hash = 7;
    hash = 37 * hash + Objects.hashCode(this.lhs);
    hash = 37 * hash + this.name;
    for (int i = 0; i < this.rhs.size(); ++i) {
      hash = 37 * hash + Objects.hashCode(this.rhs.get(i));
    }
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final Rule other = (Rule) obj;
    if (!this.lhs.equals(other.lhs)) {
      return false;
    }
    if (this.name != other.name) {
      return false;
    }
    if (this.rhs.size() != other.rhs.size()) {
      return false;
    }
    for (int i = 0; i < this.rhs.size(); ++i) {
      if (!this.rhs.get(i).equals(other.rhs.get(i))) {
        return false;
      }
    }
    return true;
  }

  public String getLhs() {
    return lhs;
  }

  public ArrayList<String> getRhs() {
    if (rhs.isEmpty()) {
      rhs.add("EPSILON");
    }
    return rhs;
  }

  public void addRhs(String symbol) {
    rhs.add(symbol);
  }

}