/** 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> */
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;
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 \""+
/** 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 */
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)) {
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) {
* </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));
}
}
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),
}
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) {
}
}
+ 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) {