summaryrefslogtreecommitdiff
path: root/Homework/cs5300/project-three/parser/Item.java
blob: 1cdb134520dd0db8e36c2fd9341d319490134ab0 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * 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;

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

/**
 * An LR(1) item.
 *
 */
public class Item implements Comparable {

  private Rule rule;
  /**
   * Position of the stack top in the production's RHS.
   */
  private int dot;
  /**
   * The lookahead symbol
   */
  private String a;

  /**
   * Creates a set of items given the rule and lookahead, one item per potential
   * stack position placement. The number of item is the number of symbols on
   * the rhs of the rule plus one.
   *
   * @param rule
   * @param a
   * @return
   */
  static ArrayList<Item> create(Rule rule, String a) {
    ArrayList<Item> ret = new ArrayList<>();
    final ArrayList<String> rhs = rule.getRhs();
    for (int i = 0; i < rhs.size(); ++i) {
      ret.add(new Item(rule, i, a));
    }
    ret.add(new Item(rule, rhs.size(), a));
    return ret;
  }

  public Item(Rule rule, int dot, String a) {
    this.rule = rule;
    this.dot = dot;
    this.a = a;
  }

  public Rule getRule() {
    return rule;
  }

  public int getDot() {
    return dot;
  }

  /**
   * @return the lookahead symbol
   */
  public String getA() {
    return a;
  }

  public String getLookahead() {
    return getA();
  }

  /**
   * Returns the symbol following the stack pointer. Returns null if the pointer
   * is at the end of the rhs list.
   *
   * @return
   */
  public String getNextSymbol() {
    if (dot < rule.getRhs().size()) {
      return rule.getRhs().get(dot);
    }
    return null;
  }

  public String getNextNextSymbol() {
    if (dot < rule.getRhs().size() - 1) {
      return rule.getRhs().get(dot + 1);
    }
    return null;
  }

  public Item advance() {
    return new Item(rule, dot + 1, a);
  }

  @Override
  public int hashCode() {
    int hash = 7;
    hash = 37 * hash + Objects.hashCode(this.rule);
    hash = 37 * hash + this.dot;
    hash = 37 * hash + Objects.hashCode(this.a);
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final Item other = (Item) obj;
    if (!Objects.equals(this.rule, other.rule)) {
      return false;
    }
    if (this.dot != other.dot) {
      return false;
    }
    if (!Objects.equals(this.a, other.a)) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    String ret = "[" + rule.getLhs() + " -> ";
    final ArrayList<String> rhs = rule.getRhs();
    for (int i = 0; i < dot; ++i) {
      ret += rhs.get(i) + " ";
    }
    ret += Character.toString((char) 0x25CF);
    if (dot < rhs.size()) {
      ret += " ";
    }
    for (int i = dot; i < rhs.size(); ++i) {
      ret += rhs.get(i);
      if (i < rhs.size() - 1) {
        ret += " ";
      }
    }
    ret += ", " + a + "]";
    return ret;
  }

  @Override
  public int compareTo(Object o) {
    return toString().compareTo(o.toString());
  }

}