Source code: org/sablecc/sablecc/LR0Collection.java
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * This file is part of SableCC. *
3 * See the file "LICENSE" for copyright information and the *
4 * terms and conditions for copying, distribution and *
5 * modification of SableCC. *
6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7
8 package org.sablecc.sablecc;
9
10 import java.util.*;
11 import com.sun.java.util.collections.*;
12 import java.util.Vector;
13
14 final class LR0Collection
15 {
16 private final Vector sets = new Vector(0);
17 private final TreeMap setIndices = new TreeMap();
18 private final Vector GOTO = new Vector(0);
19 final Vector names = new Vector(0);
20
21 LR0Collection(LR0ItemSet set)
22 {
23 add(set, -1, null);
24
25 for(int i = 0; i < sets.size(); i++)
26 {
27 System.out.print(".");
28 Symbol[] symbols = Symbol.symbols();
29
30 for(int j = 0; j < symbols.length; j++)
31 {
32 addGoto(i, symbols[j], Grammar.GOTO(set(i), symbols[j]));
33 }
34 }
35 System.out.println();
36 }
37
38 private int add(LR0ItemSet set, int from, Symbol symbol)
39 {
40 Integer result = set(set);
41
42 if(result == null)
43 {
44 result = new Integer(sets.size());
45
46 setIndices.put(set, result);
47 sets.addElement(set);
48 GOTO.addElement(new TreeMap());
49 if(from == -1)
50 {
51 names.addElement(" ");
52 }
53 else
54 {
55 names.addElement(names.elementAt(from) + "" + symbol + " ");
56 }
57 }
58
59 return result.intValue();
60 }
61
62 private static LR0ItemSet empty = new LR0ItemSet();
63
64 public static void reinit()
65 {
66 empty = new LR0ItemSet();
67 }
68
69 private void addGoto(int from, Symbol symbol, LR0ItemSet to)
70 {
71 if(!to.equals(empty))
72 {
73 ((TreeMap) GOTO.elementAt(from)).put(symbol, new Integer(add(to, from, symbol)));
74 }
75 }
76
77 private Integer set(LR0ItemSet set)
78 {
79 return (Integer) setIndices.get(set);
80 }
81
82 private LR0ItemSet set(int index)
83 {
84 return (LR0ItemSet) sets.elementAt(index);
85 }
86
87 LR0ItemSet[] sets()
88 {
89 LR0ItemSet[] result = new LR0ItemSet[sets.size()];
90 sets.copyInto(result);
91
92 return result;
93 }
94
95 Integer GOTO(int set, Symbol symbol)
96 {
97 return (Integer) ((TreeMap) GOTO.elementAt(set)).get(symbol);
98 }
99
100 public String toString()
101 {
102 StringBuffer result = new StringBuffer();
103
104 result.append("{[LR0ItemCollection]" + System.getProperty("line.separator"));
105 LR0ItemSet[] sets = sets();
106 Symbol[] symbols = Symbol.symbols();
107
108 for(int i = 0; i < sets.length; i++)
109 {
110 result.append(i + ":" + Grammar.CLOSURE(sets[i]));
111 result.append(System.getProperty("line.separator"));
112
113 for(int j = 0; j < symbols.length; j++)
114 {
115 if(GOTO(i, symbols[j]) != null)
116 {
117 result.append("[");
118 result.append(symbols[j]);
119 result.append(":");
120 result.append(GOTO(i, symbols[j]));
121 result.append("]");
122 }
123 }
124
125 result.append(System.getProperty("line.separator"));
126 }
127
128 result.append("}");
129 return result.toString();
130 }
131 }
132
133