Ejemplo n.º 1
0
 /**
  * This methos takes a list of productions with the same kLabel, and finds the maximum arity. This
  * is needed to avoid situations where there might be more productions with different arities
  * belonging to same label, for example:
  *
  * <p>syntax Foo ::= Bar "*" Bar [klabel(Foo)] syntax Foo ::= Bar "*" [klabel(Foo)]
  *
  * @param productions
  * @return
  */
 private int getMaxArityForProductions(List<Production> productions) {
   int max = productions.get(0).getArity();
   if (productions.size() > 1) {
     for (Production production : productions.subList(1, productions.size())) {
       if (production.getArity() > max) {
         max = production.getArity();
       }
     }
   }
   return max;
 }
Ejemplo n.º 2
0
  private static String getSortAndFunctionDeclarations(
      Definition definition, Set<Variable> variables) {
    Set<Sort> sorts = new HashSet<>();
    List<Production> functions = new ArrayList<>();
    for (Production production : definition.context().productions) {
      String smtlib = production.getAttribute(Attribute.SMTLIB_KEY);
      if (smtlib != null && !SMTLIB_BUILTIN_FUNCTIONS.contains(smtlib) && !smtlib.startsWith("(")) {
        functions.add(production);
        sorts.add(Sort.of(production.getSort()));
        for (int i = 0; i < production.getArity(); ++i) {
          sorts.add(Sort.of(production.getChildSort(i)));
        }
      }
    }
    for (Variable variable : variables) {
      sorts.add(variable.sort());
    }

    if (!Sets.intersection(sorts, RESERVED_Z3_SORTS).isEmpty()) {
      throw new UnsupportedOperationException("do not use sorts " + RESERVED_Z3_SORTS);
    }

    StringBuilder sb = new StringBuilder();

    for (Sort sort : Sets.difference(sorts, SMTLIB_BUILTIN_SORTS)) {
      sb.append("(declare-sort ");
      sb.append(sort);
      sb.append(")\n");
    }

    for (Production production : functions) {
      sb.append("(declare-fun ");
      sb.append(production.getAttribute(Attribute.SMTLIB_KEY));
      sb.append(" (");
      List<String> childrenSorts = new ArrayList<>();
      for (int i = 0; i < production.getArity(); ++i) {
        childrenSorts.add(getSortName(production.getChildNode(i)));
      }
      Joiner.on(" ").appendTo(sb, childrenSorts);
      sb.append(") ");
      sb.append(getSortName(production));
      sb.append(")\n");
    }

    return sb.toString();
  }
Ejemplo n.º 3
0
 public int arity() {
   return production.getArity();
 }