日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java的语法树,JAVA语言语法树.doc

發布時間:2025/4/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。