java的语法树,JAVA语言语法树.doc
JAVA语言语法树
软件课程设计
题目:Eclipse ast实现语法树
班级:
学号:
姓名:
指导教师:
时间:
一、课程设计要求:
用Eclipse AST分析java程序所形成的抽象语法树,并通过graphviz画出所对应的图。
二、开发环境:
操作系统:windows 7
开发工具:MyEclipse 8.x Graphviz2.28.0
四、具体代码:
1、获取根节点:
public class LxyAst {
private ASTParser astParser = ASTParser.newParser(AST.JLS3);
static String javaFilePath = "D:\\Users\\Administrator\\Workspaces\\MyEclipse 8.x\\LxyAst\\src\\lxy\\ast\\Test.java";
public CompilationUnit getCompilationUnit(String javaFilePath) throws Exception {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(javaFilePath));
byte[] input = new byte[bufferedInputStream.available()];
bufferedInputStream.read(input);
bufferedInputStream.close();
this.astParser.setSource(new String(input).toCharArray());
CompilationUnit result = (CompilationUnit) (this.astParser.createAST(null));
return result;
}
2、将节点储存到txt中:
public void writetotext(String str, boolean i) {
try {
FileWriter fw = new FileWriter("E:\\t5.txt", i);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(str);
bw.newLine();
bw.flush();
bw.close();
} catch (IOException e) {
System.out.print(e);
}
}
3、用graphviz画图:
public void Draw() {
Runtime rt = Runtime.getRuntime();
Process p = null;
try {
String s = "D:\\Program Files\\Graphviz 2.28\\bin\\dot E:\\t5.txt -Tpng -o result.png";
p = rt.exec(s);
} catch (Exception e) {
}
}
4、显示结果:
public void open() {
try {
Process p = Runtime.getRuntime().exec("cmd /c cd D:\\Users\\Administrator\\Workspaces\\MyEclipse 8.x\\LxyAst");
Process p2 = Runtime.getRuntime().exec("cmd /c start result.png");
} catch (Exception e) {
}
}
5、遍历语法树:
public void writexly(ASTNode node, String context) {
String wnode, nodename;
Loanode xqnode = new Loanode();
LxyAst xq = new LxyAst();
if (node.getParent() == null) {
xqnode.setMid(id);
xqnode.setPid(-1)
总结
以上是生活随笔为你收集整理的java的语法树,JAVA语言语法树.doc的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: php json对象取数据类型,PHP如
- 下一篇: java取geosever数据,终于搞定