ANTLR:通过stringtemplate将修改后的AST转换为Java源代码 [英] ANTLR:Translate the modified AST to java source code by stringtemplate

查看:228
本文介绍了ANTLR:通过stringtemplate将修改后的AST转换为Java源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ANTLR Wiki中的语法Java.g生成词法分析器, Java源文件解析器。然后,使用以下代码生成抽象语法树(AST)

I use the grammar Java.g from the ANTLR wiki produces a lexer and parser for Java source files.Then use the following code to generate an abstract syntax tree (AST).

    ANTLRInputStream input = new ANTLRInputStream(new FileInputStream(fileName));
    JavaLexer lexer = new JavaLexer(input);     // create lexer
    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JavaParser parser = new JavaParser(tokens); // create parser
    JavaParser.javaSource_return r = parser.javaSource();   // parse rule 'javaSource'
    /*RuleReturnScope result = parser.compilationUnit();
    CommonTree t = (CommonTree) result.getTree();*/
    // WALK TREE
    // get the tree from the return structure for rule prog
    CommonTree t = (CommonTree)r.getTree();

然后修改AST。例如,替换文件文件=新文件(文件路径,文件类型);至
S3Object _file = new S3Object(_fileName);通过修改AST节点。之后,我想将此AST转换为Java源代码。我修改JavaTreeParser.g并编写一个stringtemplate并使用以下方法获取Java源代码:

Then modify the AST. For example,replace "File file = new File(filepath, fileType);" to "S3Object _file = new S3Object(_fileName);" by modify the AST node. After this,I want to translate this AST to java source code.I modify the JavaTreeParser.g and write a stringtemplate and use the following method to get the java source code:

    FileReader groupFileR = new FileReader("src/com/googlecode/zcg/templates/JavaTemplate.stg");
    StringTemplateGroup templates = new StringTemplateGroup(groupFileR);
    groupFileR.close();
    // create a stream of tree nodes from AST built by parser
    CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
    // tell it where it can find the token objects
    nodes.setTokenStream(tokens);
    JavaTreeParser walker = new JavaTreeParser(nodes); // create the tree Walker
    walker.setTemplateLib(templates); // where to find templates
    // invoke rule prog, passing in information from parser
    JavaTreeParser.javaSource_return r2 = walker.javaSource();

    // EMIT BYTE CODES
    // get template from return values struct
    StringTemplate output = (StringTemplate)r2.getTemplate(); 
    System.out.println(output.toString()); // render full template

如果我不修改AST,它将获取Java源代码正确,但是在修改AST之后,它没有获得正确的Java源代码(正确修改了AST)。例如,如果我输入以下源代码并转换为AST,则修改文件文件=新File(filepath,fileType);到 S3Object _file = new S3Object(_fileName);:

If I don't modify the AST,it will get the java source code correctly,but after I modify the AST,it doesn't get the right java source code(the AST was modified correctly).For example,if I input the following souce code,and translate to AST,then modify "File file = new File(filepath, fileType);" to "S3Object _file = new S3Object(_fileName);":

public void methodname(String address){
    String filepath = "file";
    int fileType = 3;       
    File file = new File(filepath, fileType);
}

结果如下:

public void methodname( String address)
  { 
     String filepath="file";
     int fileType=3;
     methodname (Stringaddress){Stringfilepath;//it's not  what I wanted
  }

我做错了吗?

推荐答案

不幸的是,我不建议您通过重写源代码来翻译源代码抽象语法树;尝试使用解析树。如果我还记得,ANTLR 3也可以轻松生成它们。
Ter

unfortunately I cannot recommend doing source to source translation by rewriting the abstract syntax trees; try using the parse trees. If I remember ANTLR 3 can also generate those easily. Ter

这篇关于ANTLR:通过stringtemplate将修改后的AST转换为Java源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆