sclass LCLongerProds { new TreeMap> productions; Map> files; } static LCLongerProds lcSimplifyToLongerProductions(int nLiterals, L pairs, Map> files) { new LCLongerProds lc; for i over pairs: lc.productions.put(nLiterals+i, pairToList(pairs.get(i))); lc.files = mapValues cloneList(files); bool change; do { ping(); change = false; new MultiSet counts; for (L l : concatLists(values(lc.productions), values(lc.files))) for (int i : l) if (i >= nLiterals) counts.add(i); for (int i : cloneList(counts.asSet())) if (counts.get(i) == 1) { L unrolled = assertNotNull(lc.productions.get(i)); print("Unrolling " + i + " => " + unrolled); lc.productions.remove(i); counts.remove(i); for (L l : concatLists(values(lc.productions), values(lc.files))) replaceSublist(l, ll(i), unrolled); } } while change; ret lc; }