summaryrefslogtreecommitdiff
path: root/Homework/cs5300/project-three/parser/Action.java
blob: 040daab77613d3a50f29bdb247ea833e3ecd9acd (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
/*
 * 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";
  }
}