package edu.berkeley.sbp;
import edu.berkeley.sbp.*;
import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.Parser.Table.*;
import edu.berkeley.sbp.Sequence.Position;
-import edu.berkeley.sbp.Parser.Table.State;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
public Forest.Ref finalResult;
/** corresponds to a positions <i>between tokens</i> the input stream; same as Tomita's U_i's */
- public class Phase implements Invokable<State, Forest, GSS.Phase.Node>, IntegerMappable {
+ public class Phase<Tok> implements Invokable<State, Forest, Phase<Tok>.Node>, IntegerMappable {
+
+ public void invoke(State st, Forest result, Node n) {
+ good |= next.newNode(n, result, st, false);
+ }
/** the token immediately after this phase */
- final Token token;
+ final Tok token;
private final int pos;
private Forest forest;
- public Phase(Phase prev, Parser parser, Phase previous, Token token, Token.Location location, Forest forest) {
+ public Phase(Phase prev, Parser parser, Phase previous, Tok token, Token.Location location, Forest forest) {
this.prev = prev;
this.forest = forest;
this.parser = parser;
class Reset extends RuntimeException { }
- public void invoke(State st, Forest result, Node n) {
- good |= next.newNode(n, result, st, false);
- }
-
/** perform all shift operations, adding promoted nodes to <tt>next</tt> */
public void shift(Phase next, Forest result) throws ParseFailed {
// this massively improves GC performance
private boolean allqueued = false;
/** what state this node is in */
- public final State state;
+ public final Parser.Table<Tok>.State<Tok> state;
/** which Phase this Node belongs to (node that Node is also a non-static inner class of Phase) */
public Phase phase() { return Phase.this; }
}
}
- public void reduce(Position r, int pos, GSS.Phase target, Forest[] holder) {
+ public void reduce(Position r, int pos, Phase target, Forest[] holder) {
Forest old = holder[pos];
holder[pos] = this.pending();
if (pos==0) {
System.arraycopy(holder, 0, r.holder, 0, holder.length);
for(int i=0; i<r.pos; i++) if (r.holder[i]==null) throw new Error("realbad");
Forest rex = r.rewrite(target.getLocation());
- for(GSS.Phase.Node child : this.parents()) child.finish(r, rex, target, holder);
+ for(Node child : this.parents()) child.finish(r, rex, target, holder);
} else {
- for(GSS.Phase.Node child : this.parents()) child.reduce(r, pos-1, target, holder);
+ for(Node child : this.parents()) child.reduce(r, pos-1, target, holder);
}
holder[pos] = old;
}
- public void finish(Position r, Forest result, GSS.Phase target, Forest[] holder) {
- State state0 = state.gotoSetNonTerminals.get(r.owner());
+ public void finish(Position r, Forest result, Phase<Tok> target, Forest[] holder) {
+ Parser.Table<Tok>.State<Tok> state0 = state.gotoSetNonTerminals.get(r.owner());
if (result==null) throw new Error();
if (state0!=null)
target.newNode(this, result, state0, r.pos<=0, r);
import java.util.*;
/** a parser which translates streams of Tokens of type T into a Forest<R> */
-public abstract class Parser<T extends Token, R> {
+public abstract class Parser<Tok, Result> {
private final Table pt;
/** create a parser to parse the grammar with start symbol <tt>u</tt> */
- protected Parser(Union u, Topology<T> top) { this.pt = new Table(u, top); }
- protected Parser(Table pt) { this.pt = pt; }
+ protected Parser(Union u, Topology<Tok> top) { this.pt = new Table<Tok>(u, top); }
+ protected Parser(Table pt) { this.pt = pt; }
/** implement this method to create the output forest corresponding to a lone shifted input token */
- public abstract Forest<R> shiftedToken(T t, Token.Location loc);
+ public abstract Forest<Result> shiftToken(Tok t, Token.Location loc);
/** parse <tt>input</tt>, using the table <tt>pt</tt> to drive the parser */
- public Forest<R> parse(Token.Stream<T> input) throws IOException, ParseFailed {
+ public Forest<Result> parse(Token.Stream<Tok> input) throws IOException, ParseFailed {
GSS gss = new GSS();
Token.Location loc = input.getLocation();
- GSS.Phase current = gss.new Phase(null, this, null, input.next(1, 0, 0), loc, null);
+ GSS.Phase current = gss.new Phase<Tok>(null, this, null, input.next(1, 0, 0), loc, null);
current.newNode(null, Forest.leaf(null, null), pt.start, true);
int count = 1;
for(;;) {
loc = input.getLocation();
current.reduce();
- Forest forest = current.token==null ? null : shiftedToken((T)current.token, loc);
- GSS.Phase next = gss.new Phase(current, this, current, input.next(count, gss.resets, gss.waits), loc, forest);
+ Forest forest = current.token==null ? null : shiftToken((Tok)current.token, loc);
+ GSS.Phase next = gss.new Phase<Tok>(current, this, current, input.next(count, gss.resets, gss.waits), loc, forest);
count = next.size();
- if (current.isDone()) return (Forest<R>)gss.finalResult;
+ if (current.isDone()) return (Forest<Result>)gss.finalResult;
current = next;
}
}
// Table //////////////////////////////////////////////////////////////////////////////
/** an SLR(1) parse table which may contain conflicts */
- static class Table extends Walk.Cache {
+ static class Table<Tok> extends Walk.Cache {
public final Walk.Cache cache = this;
}
/** the start state */
- public final State start;
+ public final State<Tok> start;
/** used to generate unique values for State.idx */
private int master_state_idx = 0;
cache.eof.put(start0, true);
// construct the set of states
- HashMap<HashSet<Position>,State> all_states = new HashMap<HashSet<Position>,State>();
- HashSet<Element> all_elements = new HashSet<Element>();
+ HashMap<HashSet<Position>,State<Tok>> all_states = new HashMap<HashSet<Position>,State<Tok>>();
+ HashSet<Element> all_elements = new HashSet<Element>();
walk(start0, all_elements);
for(Element e : all_elements)
cache.ys.addAll(e, new Walk.YieldSet(e, cache).walk());
HashSet<Position> hp = new HashSet<Position>();
reachable(start0, hp);
- this.start = new State(hp, all_states, all_elements);
+ this.start = new State<Tok>(hp, all_states, all_elements);
// for each state, fill in the corresponding "row" of the parse table
- for(State state : all_states.values())
+ for(State<Tok> state : all_states.values())
for(Position p : state.hs) {
// the Grammar's designated "last position" is the only accepting state
if (p.element() != null && p.element() instanceof Atom)
state.shifts.addAll(state.gotoSetTerminals.subset(((Atom)p.element())));
}
- for(State state : all_states.values()) {
+ for(State<Tok> state : all_states.values()) {
state.oreductions = state.reductions.optimize();
state.oshifts = state.shifts.optimize();
}
/** a single state in the LR table and the transitions possible from it */
- public class State implements Comparable<Table.State>, IntegerMappable, Iterable<Position> {
+ public class State<Tok> implements Comparable<State<Tok>>, IntegerMappable, Iterable<Position> {
public final int idx = master_state_idx++;
private final HashSet<Position> hs;
- public transient HashMap<Element,State> gotoSetNonTerminals = new HashMap<Element,State>();
- private transient TopologicalBag<Token,State> gotoSetTerminals = new TopologicalBag<Token,State>();
+ public transient HashMap<Element,State<Tok>> gotoSetNonTerminals = new HashMap<Element,State<Tok>>();
+ private transient TopologicalBag<Tok,State<Tok>> gotoSetTerminals = new TopologicalBag<Tok,State<Tok>>();
- private TopologicalBag<Token,Position> reductions = new TopologicalBag<Token,Position>();
+ private TopologicalBag<Tok,Position> reductions = new TopologicalBag<Tok,Position>();
private HashSet<Position> eofReductions = new HashSet<Position>();
- private TopologicalBag<Token,State> shifts = new TopologicalBag<Token,State>();
+ private TopologicalBag<Tok,State<Tok>> shifts = new TopologicalBag<Tok,State<Tok>>();
private boolean accept = false;
- private VisitableMap<Token,State> oshifts = null;
- private VisitableMap<Token,Position> oreductions = null;
+ private VisitableMap<Tok,State<Tok>> oshifts = null;
+ private VisitableMap<Tok,Position> oreductions = null;
// Interface Methods //////////////////////////////////////////////////////////////////////////////
boolean isAccepting() { return accept; }
public Iterator<Position> iterator() { return hs.iterator(); }
- boolean canShift(Token t) { return oshifts.contains(t); }
- <B,C> void invokeShifts(Token t, Invokable<State,B,C> irbc, B b, C c) {
+ boolean canShift(Tok t) { return oshifts.contains(t); }
+ <B,C> void invokeShifts(Tok t, Invokable<State<Tok>,B,C> irbc, B b, C c) {
oshifts.invoke(t, irbc, b, c);
}
- boolean canReduce(Token t) { return t==null ? eofReductions.size()>0 : oreductions.contains(t); }
- <B,C> void invokeReductions(Token t, Invokable<Position,B,C> irbc, B b, C c) {
+ boolean canReduce(Tok t) { return t==null ? eofReductions.size()>0 : oreductions.contains(t); }
+ <B,C> void invokeReductions(Tok t, Invokable<Position,B,C> irbc, B b, C c) {
if (t==null) for(Position r : eofReductions) irbc.invoke(r, b, c);
else oreductions.invoke(t, irbc, b, c);
}
* </ul>
*/
public State(HashSet<Position> hs,
- HashMap<HashSet<Position>,State> all_states,
+ HashMap<HashSet<Position>,State<Tok>> all_states,
HashSet<Element> all_elements) {
this.hs = hs;
// of _new_ positions (positions after shifting). These mappings are
// collectively known as the _closure_
- TopologicalBag<Token,Position> bag0 = new TopologicalBag<Token,Position>();
+ TopologicalBag<Tok,Position> bag0 = new TopologicalBag<Tok,Position>();
for(Position position : hs) {
if (position.isLast() || !(position.element() instanceof Atom)) continue;
Atom a = (Atom)position.element();
// set, add that character set to the goto table (with the State corresponding to the
// computed next-position set).
- for(Topology<Token> r : bag0) {
+ for(Topology<Tok> r : bag0) {
HashSet<Position> h = new HashSet<Position>();
for(Position p : bag0.getAll(r)) h.add(p);
- gotoSetTerminals.put(r, all_states.get(h) == null ? new State(h, all_states, all_elements) : all_states.get(h));
+ gotoSetTerminals.put(r, all_states.get(h) == null ? new State<Tok>(h, all_states, all_elements) : all_states.get(h));
}
// Step 2: for every non-Atom element (ie every Element which has a corresponding reduction),
}
for(Element y : move) {
HashSet<Position> h = move.getAll(y);
- State s = all_states.get(h) == null ? new State(h, all_states, all_elements) : all_states.get(h);
+ State<Tok> s = all_states.get(h) == null ? new State<Tok>(h, all_states, all_elements) : all_states.get(h);
gotoSetNonTerminals.put(y, s);
}
}
return ret.toString();
}
- public int compareTo(Table.State s) { return idx==s.idx ? 0 : idx < s.idx ? -1 : 1; }
+ public int compareTo(State<Tok> s) { return idx==s.idx ? 0 : idx < s.idx ? -1 : 1; }
public int toInt() { return idx; }
}
}