summaryrefslogtreecommitdiff
path: root/Homework/cs5300/project-three/parser/Action.java
diff options
context:
space:
mode:
authorElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
committerElizabeth Alexander Hunt <me@liz.coffee>2026-07-02 11:55:17 -0700
commit6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch)
treeed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/cs5300/project-three/parser/Action.java
downloadmisc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.tar.gz
misc-undergrad-6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6.zip
Diffstat (limited to 'Homework/cs5300/project-three/parser/Action.java')
-rw-r--r--Homework/cs5300/project-three/parser/Action.java70
1 files changed, 70 insertions, 0 deletions
diff --git a/Homework/cs5300/project-three/parser/Action.java b/Homework/cs5300/project-three/parser/Action.java
new file mode 100644
index 0000000..040daab
--- /dev/null
+++ b/Homework/cs5300/project-three/parser/Action.java
@@ -0,0 +1,70 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package parser;
+
+/**
+ *
+ * @author edwajohn
+ */
+public class Action {
+
+ enum Act {
+ SHIFT, REDUCE, ACCEPT
+ };
+
+ private final Act act;
+ private final Integer state;
+ private final Rule rule;
+
+ static Action createShift(Integer state) {
+ return new Action(Act.SHIFT, state, null);
+ }
+
+ static Action createReduce(Rule rule) {
+ return new Action(Act.REDUCE, null, rule);
+ }
+
+ static Action createAccept() {
+ return new Action(Act.ACCEPT, null, null);
+ }
+
+ private Action(Act act, Integer state, Rule rule) {
+ this.act = act;
+ this.state = state;
+ this.rule = rule;
+ }
+
+ public boolean isShift() {
+ return act == Act.SHIFT;
+ }
+
+ public boolean isReduce() {
+ return act == Act.REDUCE;
+ }
+
+ public boolean isAccept() {
+ return act == Act.ACCEPT;
+ }
+
+ public Integer getState() {
+ return state;
+ }
+
+ public Rule getRule() {
+ return rule;
+ }
+
+ @Override
+ public String toString() {
+ if (isShift()) {
+ return "S" + state;
+ }
+ if (isReduce()) {
+ return "R" + rule.getName();
+ }
+ return "acc";
+ }
+}