~ubuntu-branches/ubuntu/vivid/sablecc/vivid

« back to all changes in this revision

Viewing changes to src/org/sablecc/sablecc/LR1ItemSet.java

  • Committer: Bazaar Package Importer
  • Author(s): Etienne M. Gagnon
  • Date: 2004-01-25 13:28:01 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040125132801-0so74ui506b30b1q
Tags: 2.18.2-1
* New upstream release.
* Keep the org.sablecc.ant.taskdef.Sablecc ant task of the upstream
  package, closes: #213020.
* Improve man page.
* Update Standards-Version to 3.6.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
final class LR1ItemSet implements Cloneable, Comparable
14
14
{
15
 
    private final TreeMap items;
16
 
    private int hashCode = 0;
17
 
 
18
 
    LR1ItemSet()
19
 
    {
20
 
        this.items = new TreeMap();
21
 
    }
22
 
 
23
 
    private LR1ItemSet(LR1ItemSet set)
24
 
    {
25
 
        this.items = (TreeMap) set.items.clone();
26
 
        this.hashCode = set.hashCode;
27
 
    }
28
 
 
29
 
    void set(LR1Item item)
30
 
    {
31
 
        if(items.put(item, item) == null)
32
 
        {
33
 
            hashCode += item.hashCode();
34
 
            modified_ = true;
35
 
        }
36
 
    }
37
 
 
38
 
    boolean get(LR1Item item)
39
 
    {
40
 
        return items.get(item) != null;
41
 
    }
42
 
 
43
 
 
44
 
    LR1Item[] items_;
45
 
    boolean modified_ = true;
46
 
 
47
 
    private void computeArray()
48
 
    {
49
 
        Vector itemVector = new Vector(0);
50
 
 
51
 
        for(Iterator e = items.keySet().iterator(); e.hasNext();)
52
 
        {
53
 
            itemVector.addElement(e.next());
54
 
        }
55
 
 
56
 
        items_ = new LR1Item[itemVector.size()];
57
 
        itemVector.copyInto(items_);
58
 
        modified_ = false;
59
 
    }
60
 
 
61
 
    LR1Item[] items()
62
 
    {
63
 
        if(modified_)
64
 
        {
65
 
            computeArray();
66
 
        }
67
 
 
68
 
        return items_;
69
 
    }
70
 
 
71
 
    public String toString()
72
 
    {
73
 
        String nl = System.getProperty("line.separator");
74
 
 
75
 
        StringBuffer result = new StringBuffer();
76
 
        result.append("{" + nl + "\t");
77
 
 
78
 
        Production[] productions = Production.productions();
79
 
        Symbol[] terminals = Symbol.terminals();
80
 
        boolean comma = false;
81
 
        for(int i = 0; i < productions.length; i++)
82
 
        {
83
 
            int rightsideLength = productions[i].rightside().length;
84
 
 
85
 
            for(int j = 0; j <= rightsideLength; j++)
86
 
            {
87
 
                LR0Item lr0Item = new LR0Item(productions[i].index, j);
88
 
 
89
 
                for(int k = 0; k < terminals.length; k++)
90
 
                {
91
 
                    LR1Item item = new LR1Item(lr0Item, terminals[k].index);
92
 
                    if(get(item))
93
 
                    {
94
 
                        if(comma)
95
 
                        {
96
 
                            result.append("," + nl + "\t");
97
 
                        }
98
 
                        else
99
 
                        {
100
 
                            comma = true;
101
 
                        }
102
 
 
103
 
                        result.append(item);
104
 
                    }
105
 
                }
106
 
            }
107
 
        }
108
 
 
109
 
        result.append(nl + "}");
110
 
        return result.toString();
111
 
    }
112
 
 
113
 
    public String toString(Symbol lookahead)
114
 
    {
115
 
        String nl = System.getProperty("line.separator");
116
 
 
117
 
        LR1Item[] items = items();
118
 
        int length = items.length;
119
 
 
120
 
        TreeSet strings = new TreeSet();
121
 
 
122
 
        for(int i = 0; i < length; i++)
123
 
        {
124
 
            String s = items[i].toString(lookahead);
125
 
 
126
 
            if(s != null)
127
 
            {
128
 
                strings.add(s);
129
 
            }
130
 
        }
131
 
 
132
 
        StringBuffer result = new StringBuffer();
133
 
        result.append("{");
134
 
 
135
 
        boolean colon = false;
136
 
        for(Iterator i = strings.iterator(); i.hasNext(); )
137
 
        {
138
 
            if(colon)
139
 
            {
140
 
                result.append(",");
141
 
                result.append(nl);
 
15
  private final TreeMap items;
 
16
  private int hashCode = 0;
 
17
 
 
18
  LR1ItemSet()
 
19
  {
 
20
    this.items = new TreeMap();
 
21
  }
 
22
 
 
23
  private LR1ItemSet(LR1ItemSet set
 
24
                      )
 
25
  {
 
26
    this.items = (TreeMap) set.items.clone();
 
27
    this.hashCode = set.hashCode;
 
28
  }
 
29
 
 
30
  void set
 
31
    (LR1Item item)
 
32
  {
 
33
    if(items.put(item, item) == null)
 
34
    {
 
35
      hashCode += item.hashCode();
 
36
      modified_ = true;
 
37
    }
 
38
  }
 
39
 
 
40
  boolean get
 
41
    (LR1Item item)
 
42
  {
 
43
    return items.get(item) != null;
 
44
  }
 
45
 
 
46
  LR1Item[] items_;
 
47
  boolean modified_ = true;
 
48
 
 
49
  private void computeArray()
 
50
  {
 
51
    Vector itemVector = new Vector(0);
 
52
 
 
53
    for(Iterator e = items.keySet().iterator(); e.hasNext();)
 
54
    {
 
55
      itemVector.addElement(e.next());
 
56
    }
 
57
 
 
58
    items_ = new LR1Item[itemVector.size()];
 
59
    itemVector.copyInto(items_);
 
60
    modified_ = false;
 
61
  }
 
62
 
 
63
  LR1Item[] items()
 
64
  {
 
65
    if(modified_)
 
66
    {
 
67
      computeArray();
 
68
    }
 
69
 
 
70
    return items_;
 
71
  }
 
72
 
 
73
  public String toString()
 
74
  {
 
75
    String nl = System.getProperty("line.separator");
 
76
 
 
77
    StringBuffer result = new StringBuffer();
 
78
    result.append("{" + nl + "\t");
 
79
 
 
80
    Production[] productions = Production.productions();
 
81
    Symbol[] terminals = Symbol.terminals();
 
82
    boolean comma = false;
 
83
    for(int i = 0; i < productions.length; i++)
 
84
    {
 
85
      int rightsideLength = productions[i].rightside().length;
 
86
 
 
87
      for(int j = 0; j <= rightsideLength; j++)
 
88
      {
 
89
        LR0Item lr0Item = new LR0Item(productions[i].index, j);
 
90
 
 
91
        for(int k = 0; k < terminals.length; k++)
 
92
        {
 
93
          LR1Item item = new LR1Item(lr0Item, terminals[k].index);
 
94
          if(get
 
95
              (item))
 
96
          {
 
97
            if(comma)
 
98
            {
 
99
              result.append("," + nl + "\t");
142
100
            }
143
101
            else
144
102
            {
145
 
                colon = true;
146
 
                result.append(nl);
147
 
            }
148
 
 
149
 
            result.append("\t");
150
 
            result.append(i.next());
151
 
        }
152
 
 
153
 
        result.append(nl);
154
 
        result.append("}");
155
 
 
156
 
        return result.toString();
157
 
    }
158
 
 
159
 
    public Object clone()
160
 
    {
161
 
        return new LR1ItemSet(this);
162
 
    }
163
 
 
164
 
    public boolean equals(Object obj)
165
 
    {
166
 
        if((obj == null) ||
167
 
            (obj.getClass() != this.getClass()))
168
 
        {
169
 
            return false;
170
 
        }
171
 
 
172
 
        LR1ItemSet set = (LR1ItemSet) obj;
173
 
 
174
 
        if(set.items.size() != items.size())
175
 
        {
176
 
            return false;
177
 
        }
178
 
 
179
 
        for(Iterator e = items.keySet().iterator(); e.hasNext();)
180
 
        {
181
 
            if(!set.get((LR1Item) e.next()))
182
 
            {
183
 
                return false;
184
 
            }
185
 
        }
186
 
 
187
 
        return true;
188
 
    }
189
 
 
190
 
    public int hashCode()
191
 
    {
192
 
        return hashCode;
193
 
    }
194
 
 
195
 
    public int compareTo(Object object)
196
 
    {
197
 
        LR1ItemSet set = (LR1ItemSet) object;
198
 
 
199
 
        int result = items.size() - set.items.size();
200
 
 
201
 
        if(result == 0)
202
 
        {
203
 
            Iterator e = items.keySet().iterator();
204
 
            Iterator f = set.items.keySet().iterator();
205
 
 
206
 
            while(e.hasNext() && f.hasNext() && (result == 0))
207
 
            {
208
 
                result = ((LR1Item) e.next()).compareTo(f.next());
209
 
            }
210
 
 
211
 
            if(result == 0)
212
 
            {
213
 
                if(e.hasNext())
214
 
                {
215
 
                    return 1;
216
 
                }
217
 
 
218
 
                if(f.hasNext())
219
 
                {
220
 
                    return -1;
221
 
                }
222
 
            }
223
 
        }
224
 
 
225
 
        return result;
226
 
    }
 
103
              comma = true;
 
104
            }
 
105
 
 
106
            result.append(item);
 
107
          }
 
108
        }
 
109
      }
 
110
    }
 
111
 
 
112
    result.append(nl + "}");
 
113
    return result.toString();
 
114
  }
 
115
 
 
116
  public String toString(Symbol lookahead)
 
117
  {
 
118
    String nl = System.getProperty("line.separator");
 
119
 
 
120
    LR1Item[] items = items();
 
121
    int length = items.length;
 
122
 
 
123
    TreeSet strings = new TreeSet();
 
124
 
 
125
    for(int i = 0; i < length; i++)
 
126
    {
 
127
      String s = items[i].toString(lookahead);
 
128
 
 
129
      if(s != null)
 
130
      {
 
131
        strings.add(s);
 
132
      }
 
133
    }
 
134
 
 
135
    StringBuffer result = new StringBuffer();
 
136
    result.append("{");
 
137
 
 
138
    boolean colon = false;
 
139
    for(Iterator i = strings.iterator(); i.hasNext(); )
 
140
    {
 
141
      if(colon)
 
142
      {
 
143
        result.append(",");
 
144
        result.append(nl);
 
145
      }
 
146
      else
 
147
      {
 
148
        colon = true;
 
149
        result.append(nl);
 
150
      }
 
151
 
 
152
      result.append("\t");
 
153
      result.append(i.next());
 
154
    }
 
155
 
 
156
    result.append(nl);
 
157
    result.append("}");
 
158
 
 
159
    return result.toString();
 
160
  }
 
161
 
 
162
  public Object clone()
 
163
  {
 
164
    return new LR1ItemSet(this);
 
165
  }
 
166
 
 
167
  public boolean equals(Object obj)
 
168
  {
 
169
    if((obj == null) ||
 
170
        (obj.getClass() != this.getClass()))
 
171
    {
 
172
      return false;
 
173
    }
 
174
 
 
175
    LR1ItemSet set
 
176
      = (LR1ItemSet) obj;
 
177
 
 
178
    if(set.items.size() != items.size())
 
179
    {
 
180
      return false;
 
181
    }
 
182
 
 
183
    for(Iterator e = items.keySet().iterator(); e.hasNext();)
 
184
    {
 
185
      if(!set.get((LR1Item) e.next()))
 
186
      {
 
187
        return false;
 
188
      }
 
189
    }
 
190
 
 
191
    return true;
 
192
  }
 
193
 
 
194
  public int hashCode()
 
195
  {
 
196
    return hashCode;
 
197
  }
 
198
 
 
199
  public int compareTo(Object object)
 
200
  {
 
201
    LR1ItemSet set
 
202
      = (LR1ItemSet) object;
 
203
 
 
204
    int result = items.size() - set.items.size();
 
205
 
 
206
    if(result == 0)
 
207
    {
 
208
      Iterator e = items.keySet().iterator();
 
209
      Iterator f = set.items.keySet().iterator();
 
210
 
 
211
      while(e.hasNext() && f.hasNext() && (result == 0))
 
212
      {
 
213
        result = ((LR1Item) e.next()).compareTo(f.next());
 
214
      }
 
215
 
 
216
      if(result == 0)
 
217
      {
 
218
        if(e.hasNext())
 
219
        {
 
220
          return 1;
 
221
        }
 
222
 
 
223
        if(f.hasNext())
 
224
        {
 
225
          return -1;
 
226
        }
 
227
      }
 
228
    }
 
229
 
 
230
    return result;
 
231
  }
227
232
}
228
233