I'm currently working on a project which that require me to generate an ANTLR grammar on the fly because the generated language depends on user input. Hence I generate the ANTLR grammar in code, and generate a lexer and parser from it.
My goal is to have an input program that is written in the language of the generated grammar (it is actually created through genetic algorithms, but that's not relevant here), and to ultimately have an AST representing the program. However, currently I'm only able to generate a ParseTree object, and this is not sufficient for my program.
Does anybody know how to use the ANTLR API to generate an object representing the AST? (For example an antlr.collections.AST object). I'll append a piece of code here, but the best way to test it is to run the Eclipse project that resides in https://snowdrop.googlecode.com/svn/trunk/src/ANTLRTest/
public class GEQuorra extends GEModel {
Grammar grammar;
private org.antlr.tool.Grammar lexer;
private org.antlr.tool.Grammar parser;
private String startRule;
private String ignoreTokens;
public GEQuorra(IntegrationTest.Grammar g) {
grammar = new Grammar(g.getBnfGrammar());
setGrammar(grammar);
try {
ignoreTokens = "WS";
startRule = "agentProgram";
parser = new org.antlr.tool.Grammar(g.getAntlrGrammar());
@SuppressWarnings("rawtypes")
List leftRecursiveRules = parser.checkAllRulesForLeftRecursion();
if (leftRecursiveRules.size() > 0) {
throw new Exception("Grammar is left recursive");
}
String lexerGrammarText = parser.getLexerGrammar();
lexer = new org.antlr.tool.Grammar();
lexer.importTokenVocabulary(parser);
lexer.setFileName(parser.getFileName());
lexer.setGrammarContent(lexerGrammarText);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public double getFitness(CandidateProgram program) {
try {
GECandidateProgram gecp = (GECandidateProgram) program;
System.out.println("Parsing:" + gecp.getSourceCode());
CharStream input = new ANTLRStringStream(gecp.getSourceCode());
Interpreter lexEngine = new Interpreter(lexer, input);
FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
StringTokenizer tk = new StringTokenizer(ignoreTokens, " ");
while (tk.hasMoreTokens()) {
String tokenName = tk.nextToken();
tokens.setTokenTypeChannel(lexer.getTokenType(tokenName), 99);
}
Interpreter parseEngine = new Interpreter(parser, tokens);
ParseTree t;
t = parseEngine.parse(startRule);
return 1.0 / t.toStringTree().length();
} catch (Exception e) {
// Something failed, return very big fitness, making it unfavorable
return Double.MAX_VALUE;
}
}
Where t.toStringTree() contains the ParseTree.