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数据,终于搞定