private static void flattenBag(
     List<org.kframework.kil.Term> flatBag, List<org.kframework.kil.Term> nestedBag) {
   for (org.kframework.kil.Term term : nestedBag) {
     if (term instanceof org.kframework.kil.Bag) {
       org.kframework.kil.Bag bag = (org.kframework.kil.Bag) term;
       KILtoBackendJavaKILTransformer.flattenBag(flatBag, bag.getContents());
     } else {
       flatBag.add(term);
     }
   }
 }
  @Override
  public ASTNode transform(org.kframework.kil.Cell node) throws TransformerException {
    if (node.getContents() instanceof org.kframework.kil.Bag
        || node.getContents() instanceof org.kframework.kil.Cell) {
      List<org.kframework.kil.Term> contents;
      if (node.getContents() instanceof org.kframework.kil.Bag) {
        contents = new ArrayList<org.kframework.kil.Term>();
        KILtoBackendJavaKILTransformer.flattenBag(
            contents, ((org.kframework.kil.Bag) node.getContents()).getContents());
      } else {
        contents = Collections.singletonList(node.getContents());
      }

      Multimap<String, Cell> cells = HashMultimap.create();
      Variable variable = null;
      for (org.kframework.kil.Term term : contents) {
        if (term instanceof org.kframework.kil.Cell) {
          Cell cell = (Cell) term.accept(this);
          cells.put(cell.getLabel(), cell);
        } else if (variable == null
            && term instanceof org.kframework.kil.Variable
            && term.getSort().equals("Bag")) {
          variable = (Variable) term.accept(this);
        } else {
          throw new RuntimeException();
        }
      }

      // TODO(AndreiS): get the multiplicity
      boolean isStar = !cells.isEmpty() && cells.keySet().iterator().next().equals("thread");

      return new Cell<CellCollection>(node.getLabel(), new CellCollection(cells, variable, isStar));
    } else {
      Term content = (Term) node.getContents().accept(this);

      if (content instanceof KItem) {
        return new Cell<KItem>(node.getLabel(), (KItem) content);
      } else if (content instanceof Token) {
        return new Cell<Token>(node.getLabel(), (Token) content);
      } else if (content instanceof KSequence) {
        return new Cell<KSequence>(node.getLabel(), (KSequence) content);
      } else if (content instanceof KList) {
        return new Cell<KList>(node.getLabel(), (KList) content);
      } else if (content instanceof BuiltinList) {
        return new Cell<BuiltinList>(node.getLabel(), (BuiltinList) content);
        //            } else if (content instanceof ListUpdate) {
        //                return new Cell<ListUpdate>(node.getLabel(), (ListUpdate) content);
      } else if (content instanceof BuiltinSet) {
        return new Cell<BuiltinSet>(node.getLabel(), (BuiltinSet) content);
      } else if (content instanceof SetUpdate) {
        return new Cell<SetUpdate>(node.getLabel(), (SetUpdate) content);
      } else if (content instanceof BuiltinMap) {
        return new Cell<BuiltinMap>(node.getLabel(), (BuiltinMap) content);
      } else if (content instanceof MapUpdate) {
        return new Cell<MapUpdate>(node.getLabel(), (MapUpdate) content);
      } else if (content instanceof Variable) {
        return new Cell<Term>(node.getLabel(), content);
      } else {
        throw new RuntimeException();
      }
    }
  }