summary patch for Nov->Jan work
[sbp.git] / src / edu / berkeley / sbp / GSS.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Parser.Table.*;
7 import edu.berkeley.sbp.Sequence.Position;
8 import java.io.*;
9 import java.util.*;
10 import java.lang.reflect.*;
11
12 /** implements Tomita's Graph Structured Stack */
13 class GSS {
14
15     Input input;
16     public GSS(Input input) { this.input = input; }
17     public Input getInput() { return input; }
18
19     // FIXME: right now, these are the performance bottleneck
20     HashMapBag<Integer,Sequence>       performed       = new HashMapBag<Integer,Sequence>();
21
22     /** FIXME */
23     Forest.Many finalResult;
24
25     /** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
26     class Phase<Tok> implements Invokable<State, Result, Object>, IntegerMappable, GraphViz.ToGraphViz, Iterable<Node> {
27
28         public ArrayList<Reduction> reductionQueue = new ArrayList<Reduction>();
29
30         public void invoke(State st, Result result, Object o) {
31             //shifts++;
32             good |= next.newNode(result, st, false);
33         }
34
35         /** the token immediately after this phase */
36         final Tok token;
37         final int pos;
38
39         public IntPairMap<Node> hash;  /* ALLOC */
40         private boolean good;
41          Phase next = null;
42         private Phase prev;
43         private Input.Location location;
44         private Input.Location nextLocation;
45         private Input.Location prevLocation;
46         
47         private Forest forest;
48
49         public Phase(Phase prev, Phase previous, Tok token, Input.Location location,
50                      Input.Location nextLocation, Forest forest) throws ParseFailed {
51             this.prevLocation = prev==null ? location : prev.getLocation();
52             this.prev = prev;
53             this.forest = forest;
54             this.pos = previous==null ? 0 : previous.pos+1;
55             this.token = token;
56             this.location = location;
57             this.nextLocation = nextLocation;
58             performed.clear();
59             hash = new IntPairMap<Node>();
60             good = false;
61             finalResult = null;
62             if (prev != null) prev.shift(this, forest);
63         }
64       
65         public boolean isDone() throws ParseFailed {
66             if (token != null) return false;
67             if (token==null && finalResult==null)
68                 ParseFailed.error("unexpected end of file", this);
69             return true;
70         }
71
72         public Input.Location getPrevLocation() { return prevLocation; }
73         public Input.Location getLocation() { return location; }
74         public Input.Region   getRegion() { return getPrevLocation().createRegion(getLocation()); }
75         public Input.Location getNextLocation() { return nextLocation; }
76
77         /** perform all shift operations, adding promoted nodes to <tt>next</tt> */
78         public void shift(Phase next, Forest result) throws ParseFailed {
79             // this massively improves GC performance
80             if (prev!=null) prev.hash = null;
81             this.next = next;
82             for(Node n : hash.values()) {
83                 if (token == null && n.state().isAccepting()) {
84                     if (finalResult==null) finalResult = new Forest.Many();
85                     for(Result r : n)
86                         finalResult.merge(r.getForest());
87                 }
88                 if (token == null) continue;
89                 n.state().invokeShifts(token, this, new Result(result, n, null), null);
90             }
91             if (!good && token!=null) ParseFailed.error("unexpected character", this);
92             if (token==null && finalResult==null) ParseFailed.error("unexpected end of file", this);
93         }
94
95         /** perform all reduction operations */
96         public void reduce() throws ParseFailed {
97             Reduction last = null;
98             while(!reductionQueue.isEmpty()) {
99                 Reduction r = null;
100
101                 // ugly
102                 OUTER: for(int i=0; i<reductionQueue.size(); i++) {
103                     for(int j=0; j<reductionQueue.size(); j++) {
104                         if (i==j) continue;
105                         if (reductionQueue.get(i).compareTo(reductionQueue.get(j)) > 0)
106                             continue OUTER;
107                     }
108                     r = reductionQueue.get(i);
109                     reductionQueue.remove(r);
110                     break;
111                 }
112
113                 /*
114                 if (last == null) last = r;
115                 else if (r.compareTo(last) > 0) last = r;
116                 else if (r.compareTo(last) < 0) {
117                     if (r.targetPhase() != null)
118                         System.out.println("err " + last.compareTo(r) + " " + last.targetPhase().pos() +
119                                            " "    + r.targetPhase().pos() + " " + pos);
120                 }
121                 */
122
123                 r.perform();
124             }
125         }
126
127         public void newNodeFromReduction(Result result, State state, boolean fromEmptyReduction, Position reduction) {
128             int pos = result.phase().pos;
129             Sequence owner = reduction.owner();
130             for(Sequence s : owner.hates)
131                 if (performed.contains(pos, s))
132                     return;
133             for(Sequence s : owner.needs)
134                 if (!performed.contains(pos, s))
135                     return;
136             if (owner.needed_or_hated && !performed.contains(pos, owner))
137                 performed.add(pos, owner);
138             if (state!=null)
139                 newNode(result, state, fromEmptyReduction);
140         }
141
142         /** add a new node (merging with existing nodes if possible)
143          *  @param parent             the parent of the new node
144          *  @param result             the SPPF result corresponding to the new node
145          *  @param state              the state that the new node is in
146          *  @param fromEmptyReduction true iff this node is being created as a result of a reduction of length zero (see GRMLR paper)
147          *  @param start              the earliest part of the input contributing to this node (used to make merging decisions)
148          */
149         public boolean newNode(Result result, State state, boolean fromEmptyReduction) {
150             Node p = hash.get(state, result.phase());
151             if (p != null) { p.merge(result); return true; }
152             do {
153                 if (token != null && state.canShift(token)) break;
154                 if (state.isAccepting()) break;
155                 if (token==null) break;
156                 if (!state.canReduce(token)) return false;
157             } while(false);
158             new Node(Phase.this, result, state, fromEmptyReduction);  // ALLOC
159             return true;
160         }
161
162         public int toInt() { return pos+1; }
163         public int size() { return hash==null ? 0 : hash.size(); }
164         public int pos() { return pos; }
165         public Tok getToken() { return token; }
166         public Iterator<Node> iterator() { return hash.iterator(); }
167         public GSS getGSS() { return GSS.this; }
168
169         // GraphViz //////////////////////////////////////////////////////////////////////////////
170
171         public GraphViz.Node toGraphViz(GraphViz gv) {
172             if (gv.hasNode(this)) return gv.createNode(this);
173             GraphViz.Group g = gv.createGroup(this);
174             g.label = "Phase " + pos;
175             g.color = "gray";
176             g.cluster = true;
177             return g;
178         }
179         public boolean isTransparent() { return false; }
180         public boolean isHidden() { return false; }
181
182     }
183
184 }