Beispiel #1
0
 protected <S, P, T, D extends Pda<S, P>> S clone(
     S state,
     Pda<S, P> src,
     D target,
     Function<S, T> tokens,
     PdaFactory<D, S, P, T> fact,
     Identity<S> identity) {
   if (state == src.getStart()) return target.getStart();
   if (state == src.getStop()) return target.getStop();
   P push = src.getPush(state);
   if (push != null) return identity.get(fact.createPush(target, tokens.apply(state)));
   P pop = src.getPop(state);
   if (pop != null) return identity.get(fact.createPop(target, tokens.apply(state)));
   return identity.get(fact.createState(target, tokens.apply(state)));
 }
Beispiel #2
0
 protected <S, P, E, T1, T2, D extends Pda<S, P>> void create(
     Cfg<E, T1> cfg,
     D pda,
     S state,
     E ele,
     Iterable<E> followerElements,
     FollowerFunction<E> ff,
     Function<E, T2> tokens,
     PdaFactory<D, S, P, ? super T2> fact,
     Map<E, S> states,
     Map<E, S> stops,
     Multimap<E, E> callers) {
   List<S> followerStates = Lists.newArrayList();
   for (E fol : followerElements) {
     E e;
     if (fol == null) {
       E root = new ProductionUtil().getRoot(cfg, ele);
       if (root == cfg.getRoot()) followerStates.add(pda.getStop());
       for (E c : callers.get(root)) {
         S s = stops.get(c);
         if (s == null) {
           s = fact.createPop(pda, tokens.apply(c));
           stops.put(c, s);
           create(cfg, pda, s, c, ff.getFollowers(c), ff, tokens, fact, states, stops, callers);
         }
         followerStates.add(s);
       }
     } else if ((e = cfg.getCall(fol)) != null) {
       S s = states.get(fol);
       if (s == null) {
         s = fact.createPush(pda, tokens.apply(fol));
         states.put(fol, s);
         create(cfg, pda, s, e, ff.getStarts(e), ff, tokens, fact, states, stops, callers);
       }
       followerStates.add(s);
     } else {
       S s = states.get(fol);
       if (s == null) {
         s = fact.createState(pda, tokens.apply(fol));
         states.put(fol, s);
         create(cfg, pda, s, fol, ff.getFollowers(fol), ff, tokens, fact, states, stops, callers);
       }
       followerStates.add(s);
     }
   }
   fact.setFollowers(pda, state, followerStates);
 }
Beispiel #3
0
 public <S, P, T, D extends Pda<S, P>> D expand(
     Pda<S, P> pda,
     Function<S, Pda<S, P>> expand,
     Function<S, T> tokens,
     PdaFactory<D, S, P, T> fact) {
   D result = fact.create(tokens.apply(pda.getStart()), tokens.apply(pda.getStop()));
   Identity<S> identity = new Identity<S>();
   Map<S, S> idstates = Maps.newIdentityHashMap();
   Multimap<S, S> followers = LinkedHashMultimap.create();
   for (S s_old : nfaUtil.collect(pda)) {
     S s_new = idstates.get(s_old);
     if (s_new == null) {
       Pda<S, P> sub = expand.apply(s_old);
       if (sub != null) {
         S s_start = identity.get(fact.createPush(result, tokens.apply(s_old)));
         S s_stop = identity.get(fact.createPop(result, tokens.apply(s_old)));
         idstates.put(s_old, s_start);
         idstates.put(sub.getStart(), s_start);
         idstates.put(sub.getStop(), s_stop);
         followers.putAll(s_start, sub.getFollowers(sub.getStart()));
         followers.putAll(s_stop, pda.getFollowers(s_old));
         for (S f_old : nfaUtil.collect(sub))
           if (f_old != sub.getStart() && f_old != sub.getStop()) {
             S f_new = idstates.get(f_old);
             if (f_new == null) {
               idstates.put(f_old, f_new = clone(f_old, sub, result, tokens, fact, identity));
               followers.putAll(f_new, pda.getFollowers(f_old));
             }
           }
       } else {
         idstates.put(s_old, s_new = clone(s_old, pda, result, tokens, fact, identity));
         followers.putAll(s_new, pda.getFollowers(s_old));
       }
     }
   }
   for (Map.Entry<S, Collection<S>> entry : followers.asMap().entrySet()) {
     Set<S> f = Sets.newLinkedHashSet();
     for (S s : entry.getValue()) f.add(idstates.get(s));
     fact.setFollowers(result, entry.getKey(), f);
   }
   return result;
 }
Beispiel #4
0
 public <S, P, R, D extends Pda<S, P>> D filterEdges(
     Pda<S, P> pda, Traverser<? super Pda<S, P>, S, R> traverser, PdaFactory<D, S, P, S> factory) {
   HashStack<TraversalItem<S, R>> trace = new HashStack<TraversalItem<S, R>>();
   R previous = traverser.enter(pda, pda.getStart(), null);
   if (previous == null)
     return factory == null ? null : factory.create(pda.getStart(), pda.getStop());
   Map<S, Integer> distances = new NfaUtil().distanceToFinalStateMap(pda);
   MappedComparator<S, Integer> distanceComp = new MappedComparator<S, Integer>(distances);
   trace.push(newItem(pda, distanceComp, distances, pda.getStart(), previous));
   Multimap<S, S> edges = LinkedHashMultimap.create();
   HashSet<S> states = Sets.newLinkedHashSet();
   HashSet<Pair<S, R>> success = Sets.newLinkedHashSet();
   states.add(pda.getStart());
   states.add(pda.getStop());
   ROOT:
   while (!trace.isEmpty()) {
     TraversalItem<S, R> current = trace.peek();
     while (current.followers.hasNext()) {
       S next = current.followers.next();
       R item = traverser.enter(pda, next, current.data);
       if (item != null) {
         if ((next == pda.getStop() && traverser.isSolution(item))
             || success.contains(Tuples.create(next, item))) {
           S s = null;
           for (TraversalItem<S, R> i : trace) {
             if (s != null) edges.put(s, i.state);
             states.add(i.state);
             success.add(Tuples.create(i.state, i.data));
             s = i.state;
           }
           edges.put(s, next);
         } else {
           if (trace.push(newItem(pda, distanceComp, distances, next, item))) continue ROOT;
         }
       }
     }
     trace.pop();
   }
   if (factory == null) return null;
   D result = factory.create(pda.getStart(), pda.getStop());
   Map<S, S> old2new = Maps.newLinkedHashMap();
   old2new.put(pda.getStart(), result.getStart());
   old2new.put(pda.getStop(), result.getStop());
   for (S old : states) {
     if (old == pda.getStart() || old == pda.getStop()) continue;
     else if (pda.getPop(old) != null) old2new.put(old, factory.createPop(result, old));
     else if (pda.getPush(old) != null) old2new.put(old, factory.createPush(result, old));
     else old2new.put(old, factory.createState(result, old));
   }
   for (S old : states) {
     List<S> followers = Lists.newArrayList();
     for (S f : edges.get(old)) followers.add(old2new.get(f));
     factory.setFollowers(result, old2new.get(old), followers);
   }
   return result;
 }
Beispiel #5
0
 public <S, P, E, T1, T2, D extends Pda<S, P>> D create(
     Cfg<E, T1> cfg,
     FollowerFunction<E> ff,
     Function<E, T2> element2token,
     PdaFactory<D, S, P, ? super T2> fact) {
   D pda = fact.create(null, null);
   Map<E, S> states = Maps.newLinkedHashMap();
   Map<E, S> stops = Maps.newLinkedHashMap();
   Multimap<E, E> callers = new CfgUtil().getCallers(cfg);
   create(
       cfg,
       pda,
       pda.getStart(),
       cfg.getRoot(),
       ff.getStarts(cfg.getRoot()),
       ff,
       element2token,
       fact,
       states,
       stops,
       callers);
   return pda;
 }