繁体   English   中英

如何使用Monticore生成的PrettyPrinter将AST从ASTEmbeddedAutomaton打印到文本文件?

[英]How can I print the AST from ASTEmbeddedAutomaton to a text file using the generated PrettyPrinter from Monticore?

我已经使MontiCore生成了带有Configure pom.xml中说明的选项的PrettyPrinter类, 以为MontiCore语言生成PrettyPrinter

如何生成MontiArcAutomaton(类型:ASTEmbeddedAutomaton)的格式化文本输出?

向MontiCore团队询问之后,以下解决方案是一种很好的方法:我编写了一个帮助程序类,用于漂亮地打印嵌入式自动机ASTNode(参见清单1)。 该帮助程序类可以与以下代码行一起使用:

System.out.println(PrettyPrintFactory.getInstance().printNode(autMoore));

清单1:

import mc.ast.ASTCNode;
import mc.ast.PrettyPrinter;
import mc.helper.IndentPrinter;
import mc.maautomaton._prettyprint.MontiArcAutomatonConcretePrettyPrinter;

/**
* Prints the AST-Content to a file
**/
public class PrettyPrintFactory {

  private static PrettyPrintFactory instance;
  private PrettyPrinter pretty;

  public static PrettyPrintFactory getInstance() {
    if (instance == null) {
      instance = new PrettyPrintFactory();
    }
    return instance;
  }

  private PrettyPrintFactory() {
         pretty = new PrettyPrinter();
         pretty.addConcretePrettyPrinter(new MontiArcAutomatonConcretePrettyPrinter());
  }

  /** prints the syntax of the astNode to a String */
  public String printNode(ASTCNode astNode) {
         IndentPrinter ip = new IndentPrinter();
         pretty.prettyPrint(astNode, ip);
         return ip.getContent();
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM