From 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Thu, 2 Jul 2026 11:55:17 -0700 Subject: Init --- Homework/cs5300/project-three/parser/Rule.java | 93 ++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Homework/cs5300/project-three/parser/Rule.java (limited to 'Homework/cs5300/project-three/parser/Rule.java') diff --git a/Homework/cs5300/project-three/parser/Rule.java b/Homework/cs5300/project-three/parser/Rule.java new file mode 100644 index 0000000..b251e84 --- /dev/null +++ b/Homework/cs5300/project-three/parser/Rule.java @@ -0,0 +1,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 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 getRhs() { + if (rhs.isEmpty()) { + rhs.add("EPSILON"); + } + return rhs; + } + + public void addRhs(String symbol) { + rhs.add(symbol); + } + +} -- cgit v1.3