make ANSI.clreol()
[sbp.git] / src / edu / berkeley / sbp / Parser.java
index 10768f6..adf24c4 100644 (file)
@@ -9,7 +9,6 @@ import java.util.*;
 
 /** a parser which translates an Input<Token> into a Forest<NodeType> */
 public abstract class Parser<Token, NodeType> {
-
     protected final Table<Token> pt;
 
     /** create a parser to parse the grammar with start symbol <tt>u</tt> */
@@ -38,16 +37,18 @@ public abstract class Parser<Token, NodeType> {
             loc = input.getLocation();
             Token nextToken = input.next();
             GSS.Phase next = gss.new Phase<Token>(current, current, nextToken, loc, input.getLocation(), forest);
-            if (!helpgc) {
-                FileOutputStream fos = new FileOutputStream("out-"+idx+".dot");
-                PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
-                GraphViz gv = new GraphViz();
-                for(Object n : next)
-                    ((Node)n).toGraphViz(gv);
-                gv.dump(p);
-                p.flush();
-                p.close();
-            }
+
+            /*
+            FileOutputStream fos = new FileOutputStream("out-"+idx+".dot");
+            PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
+            GraphViz gv = new GraphViz();
+            for(Object n : current)
+                ((Node)n).toGraphViz(gv);
+            gv.dump(p);
+            p.flush();
+            p.close();
+            */
+
             count = next.size();
             if (current.isDone()) return (Forest<NodeType>)gss.finalResult;
             current = next;
@@ -62,7 +63,7 @@ public abstract class Parser<Token, NodeType> {
         public String toString() {
             StringBuffer sb = new StringBuffer();
             sb.append("parse table");
-            for(State<Token> state : all_states.values()) {
+            for(State<Token> state : all_states) {
                 sb.append("  " + state + "\n");
                 for(Topology<Token> t : state.shifts) {
                     sb.append("      shift  \""+
@@ -75,6 +76,8 @@ public abstract class Parser<Token, NodeType> {
                     sb.append("      reduce \""+
                               new edu.berkeley.sbp.chr.CharTopology((IntegerTopology<Character>)t)+"\" => " +
                               state.reductions.getAll(t) + "\n");
+                for(Sequence s : state.gotoSetNonTerminals.keySet())
+                    sb.append("      goto   "+state.gotoSetNonTerminals.get(s)+" from " + s + "\n");
             }
             return sb.toString();
         }
@@ -105,7 +108,9 @@ public abstract class Parser<Token, NodeType> {
 
         /** used to generate unique values for State.idx */
         private int master_state_idx = 0;
-        HashMap<HashSet<Position>,State<Token>>   all_states    = new HashMap<HashSet<Position>,State<Token>>();
+        HashSet<State<Token>>   all_states    = new HashSet<State<Token>>();
+        HashMap<HashSet<Position>,State<Token>>   doomed_states    = new HashMap<HashSet<Position>,State<Token>>();
+        HashMap<HashSet<Position>,State<Token>>   normal_states    = new HashMap<HashSet<Position>,State<Token>>();
         HashSet<SequenceOrElement>                all_elements  = new HashSet<SequenceOrElement>();
 
         /** construct a parse table for the given grammar */
@@ -122,18 +127,20 @@ public abstract class Parser<Token, NodeType> {
             walk(start0, all_elements);
             for(SequenceOrElement e : all_elements)
                 cache.ys.addAll(e, new Walk.YieldSet(e, cache).walk());
+            for(SequenceOrElement e : all_elements)
+                cache.ys2.addAll(e, new Walk.YieldSet2(e, cache).walk());
             HashSet<Position> hp = new HashSet<Position>();
             reachable(start0, hp);
 
-            this.dead_state = new State<Token>(new HashSet<Position>());
+            this.dead_state = new State<Token>(new HashSet<Position>(), true);
             this.start = new State<Token>(hp);
 
             // for each state, fill in the corresponding "row" of the parse table
-            for(State<Token> state : all_states.values())
+            for(State<Token> state : all_states)
                 for(Position p : state.hs) {
 
                     // the Grammar's designated "last position" is the only accepting state
-                    if (start0.contains(p.owner()) && p.next()==null)
+                    if (start0.contains(p.owner()) && p.next()==null && !state.doomed)
                         state.accept = true;
 
                     if (isRightNullable(p)) {
@@ -155,11 +162,29 @@ public abstract class Parser<Token, NodeType> {
                     if (p.element() != null && p.element() instanceof Atom)
                         state.shifts.addAll(state.gotoSetTerminals.subset(((Atom)p.element()).getTokenTopology()));
                 }
+
             if (top instanceof IntegerTopology)
-                for(State<Token> state : all_states.values()) {
+                for(State<Token> state : all_states) {
                     state.oreductions = state.reductions.optimize(((IntegerTopology)top).functor());
                     state.oshifts = state.shifts.optimize(((IntegerTopology)top).functor());
                 }
+
+            // crude algorithm to assing an ordinal ordering to every position
+            ArrayList<Sequence.Position> al = new ArrayList<Sequence.Position>();
+            for(State s : all_states) {
+                for(Object po : s) {
+                    Sequence.Position p = (Sequence.Position)po;
+                    if (al.contains(p)) continue;
+                    int i=0;
+                    for(; i<al.size(); i++) {
+                        if (p.compareTo(al.get(i), cache) > 0)
+                            break;
+                    }
+                    al.add(i, p);
+                }
+            }
+            for(int i=0; i<al.size(); i++)
+                al.get(i).ord = i;
         }
 
         private boolean isRightNullable(Position p) {
@@ -227,28 +252,29 @@ public abstract class Parser<Token, NodeType> {
              *  </ul>
              */
             public State(HashSet<Position> hs) { this(hs, false); }
-            public boolean special;
-            public State(HashSet<Position> hs, boolean special) {
+            public boolean doomed;
+            public State(HashSet<Position> hs, boolean doomed) {
                 this.hs = hs;
-                this.special = special;
+                this.doomed = doomed;
 
                 // register ourselves in the all_states hash so that no
                 // two states are ever created with an identical position set
-                ((HashMap)all_states).put(hs, this);
-
+                ((HashMap)(doomed ? doomed_states : normal_states)).put(hs, this);
+                ((HashSet)all_states).add(this);
+                
                 for(Position p : hs) {
                     if (!p.isFirst()) continue;
                     for(Sequence s : p.owner().needs()) {
                         if (hs.contains(s.firstp())) continue;
                         HashSet<Position> h2 = new HashSet<Position>();
                         reachable(s.firstp(), h2);
-                        also.add((State<Token>)(all_states.get(h2) == null ? (State)new State<Token>(h2,true) : (State)all_states.get(h2)));
+                        also.add(mkstate(h2, true));
                     }
                     for(Sequence s : p.owner().hates()) {
                         if (hs.contains(s.firstp())) continue;
                         HashSet<Position> h2 = new HashSet<Position>();
                         reachable(s, h2);
-                        also.add((State<Token>)(all_states.get(h2) == null ? (State)new State<Token>(h2,true) : (State)all_states.get(h2)));
+                        also.add(mkstate(h2, true));
                     }
                 }
 
@@ -273,8 +299,7 @@ public abstract class Parser<Token, NodeType> {
                 for(Topology<Token> r : bag0) {
                     HashSet<Position> h = new HashSet<Position>();
                     for(Position p : bag0.getAll(r)) h.add(p);
-                    ((TopologicalBag)gotoSetTerminals).put(r, all_states.get(h) == null
-                                                           ? new State<Token>(h) : all_states.get(h));
+                    ((TopologicalBag)gotoSetTerminals).put(r, mkstate(h, doomed));
                 }
 
                 // Step 2: for every non-Atom element (ie every Element which has a corresponding reduction),
@@ -289,7 +314,8 @@ public abstract class Parser<Token, NodeType> {
                 for(Position p : hs) {
                     Element e = p.element();
                     if (e==null) continue;
-                    for(SequenceOrElement y : cache.ys.getAll(e)) {
+                    for(SequenceOrElement y : cache.ys2.getAll(e)) {
+                        //System.out.println(e + " yields " + y);
                         HashSet<Position> hp = new HashSet<Position>();
                         reachable(p.next(), hp);
                         move.addAll(y, hp);
@@ -297,7 +323,7 @@ public abstract class Parser<Token, NodeType> {
                 }
                 OUTER: for(SequenceOrElement y : move) {
                     HashSet<Position> h = move.getAll(y);
-                    State<Token> s = all_states.get(h) == null ? (State)new State<Token>(h) : (State)all_states.get(h);
+                    State<Token> s = mkstate(h, doomed);
                     // if a reduction is "lame", it should wind up in the dead_state after reducing
                     if (y instanceof Sequence) {
                         for(Position p : hs) {
@@ -316,6 +342,11 @@ public abstract class Parser<Token, NodeType> {
                 }
             }
 
+            private State<Token> mkstate(HashSet<Position> h, boolean b) {
+                if (b) return doomed_states.get(h) == null ? (State)new State<Token>(h,b) : (State)doomed_states.get(h);
+                else   return normal_states.get(h) == null ? (State)new State<Token>(h,b) : (State)normal_states.get(h);
+            }
+
             public String toStringx() {
                 StringBuffer st = new StringBuffer();
                 for(Position p : this) {