示例#1
0
  public void compileParser(Trace trace, Strictness strictness) {

    SGrammar sGrammar = new SGrammar(this);
    OldGrammar oldGrammar = null;
    for (Production production : sGrammar.getProductions()) {

      if (oldGrammar == null) {
        oldGrammar = new OldGrammar(this, production.getName(), production);
      }

      OldProduction oldProduction = oldGrammar.getProduction(production.getName(), production);

      for (Alternative alternative : production.getAlternatives()) {

        OldAlternative oldAlternative = oldProduction.addAlternative("", alternative);

        for (Element element : alternative.getElements()) {
          OldElement oldElement;
          if (element instanceof Element.TokenElement) {
            Element.TokenElement tokenElement = (Element.TokenElement) element;
            oldElement =
                oldAlternative.addTokenElement(
                    this,
                    element.getName(),
                    oldGrammar.getToken(tokenElement.getTypeName()),
                    tokenElement);
          } else {
            Element.ProductionElement productionElement = (Element.ProductionElement) element;
            oldElement =
                oldAlternative.addProductionElement(
                    element.getName(),
                    oldGrammar.getProduction(
                        element.getTypeName(), productionElement.getReference()),
                    productionElement);
          }
          sGrammar.addOldElement(element, oldElement);
        }
      }
    }

    oldGrammar.stabilize();
    oldGrammar.computeShortestLengthAndDetectUselessProductions();
    sGrammar.setLRAutomaton(new LRAutomaton(oldGrammar, trace));
    this.simplifiedGrammar = sGrammar;
  }
示例#2
0
文件: MetaK.java 项目: TomGebhardt/k
 public static Term getTerm(Production prod, org.kframework.kil.loader.Context context) {
   if (prod.isSubsort()) {
     final Variable freshVar = Variable.getFreshVar(prod.getItems().get(0).toString());
     if (prod.containsAttribute("klabel")) {
       return KApp.of(KLabelConstant.of(prod.getKLabel(), context), freshVar);
     }
     return freshVar;
   }
   if (prod.isConstant()) {
     String terminal = ((Terminal) prod.getItems().get(0)).getTerminal();
     if (prod.getSort().equals(KSorts.KLABEL)) {
       return KLabelConstant.of(terminal, context);
     } else if (prod.getSort().equals(BoolBuiltin.SORT_NAME)) {
       return BoolBuiltin.kAppOf(terminal);
     } else if (prod.getSort().equals(IntBuiltin.SORT_NAME)) {
       return IntBuiltin.kAppOf(terminal);
     } else if (prod.getSort().equals(StringBuiltin.SORT_NAME)) {
       return StringBuiltin.kAppOf(terminal);
     } else {
       return GenericToken.kAppOf(prod.getSort(), terminal);
     }
   }
   if (prod.isLexical()) {
     return KApp.of(
         KLabelConstant.of("#token", context),
         StringBuiltin.kAppOf(prod.getSort()),
         Variable.getFreshVar("String"));
   }
   TermCons t = new TermCons(prod.getSort(), prod.getCons(), context);
   if (prod.isListDecl()) {
     t.getContents().add(Variable.getFreshVar(((UserList) prod.getItems().get(0)).getSort()));
     t.getContents().add(Variable.getFreshVar(prod.getSort()));
     return t;
   }
   for (ProductionItem item : prod.getItems()) {
     if (item instanceof Sort) {
       t.getContents().add(Variable.getFreshVar(((Sort) item).getName()));
     }
   }
   return t;
 }