프로그램 사용/antlr2011. 11. 2. 21:59
학교에 있을때도 잘 안다루던 java라서 import 하나하나 해주고
exception 처리해주는게 익숙하지가 않아서 힘들다 ㅠ.ㅠ

import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;

public class antlrtest{

/**
 * @param args
 */

public static void printTree(CommonTree t, int indent) {
if (t != null) {
StringBuffer sb = new StringBuffer(indent);
for (int i = 0; i < indent; i++)
sb = sb.append("   ");
for (int i = 0; i < t.getChildCount(); i++) {
if(t.getChild(i).toString().matches(".*Manager"))
{
System.out.println(sb.toString() + t.getChild(i).toString());
printTree((CommonTree) t.getChild(i), indent + 1);
}
}
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
JavaLexer theLexer = new JavaLexer(new ANTLRFileStream(args[0]));
CommonTokenStream theTokenStream = new CommonTokenStream(theLexer);
JavaParser Parser = new JavaParser(theTokenStream);
CommonTree tree = (CommonTree) Parser.compilationUnit().getTree();
printTree(tree,1);
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(tree);
// System.out.println(st);
} catch (IOException | RecognitionException inException) {
inException.printStackTrace();
}
}

[링크 : http://www.antlr.org/wiki/display/ANTLR3/Tree+construction]
[링크 : http://www.antlr.org/wiki/display/ANTLR3/Interfacing+AST+with+Java]
[링크 : http://antlr.1301665.n2.nabble.com/Inconsistent-Parse-Results-td3018499.html]
[링크 : http://stackoverflow.com/questions/4931346/how-to-output-the-ast-built-using-antlr]

'프로그램 사용 > antlr' 카테고리의 다른 글

antlr을 이용한 구문분석(parsing)하기  (0) 2012.02.23
ANTLR IDE 2.1.2 for ANTLR 3.0 above  (0) 2011.11.02
antlrworks  (0) 2011.11.02
antlr + eclipse & java  (0) 2011.10.30
antlr / pccts package  (0) 2011.10.24
Posted by 구차니