I started using ANTLR today and I've created a basic parser.
After parsing I end up with a tree. To me it seems like this is just a bunch of
s put together in a tree structure of
-nodes. That's not very useful to me. I'd like to have a graph of objects.
To clarify (this is an example, and not my real application): For
I seem to end up with:
What I would find more useful:
What is the most convenient way of going from the first representation to the other? In <a href="http://zentapestry.wordpress.com/2008/02/27/antlr-interpreter-using-visitor-i-2/" rel="nofollow noreferrer">this article</a> the author does something similar to this:
Is this the preferred way?! Can't I instruct ANTLR to generate this boiler-plate code somehow (it will be huge)?
<hr>
<strong>Possibly related questions:</strong>
<ul>
<li><a href="https://stackoverflow.com/questions/2217173/converting-antlr-syntax-tree-into-useful-objects">Converting Antlr syntax tree into useful objects</a> (But I can't see how the answer answers my question.)</li>
</ul>
After parsing I end up with a tree. To me it seems like this is just a bunch of
Code:
String
Code:
Tree
To clarify (this is an example, and not my real application): For
Code:
"5-1+6"
Code:
new String("PLUS")
new String("MINUS")
new String("5")
new String("1")
new String("6")
What I would find more useful:
Code:
new Plus(
new Minus(
new IntegerLiteral(5),
new IntegerLiteral(1)),
new IntegerLiteral(6))
What is the most convenient way of going from the first representation to the other? In <a href="http://zentapestry.wordpress.com/2008/02/27/antlr-interpreter-using-visitor-i-2/" rel="nofollow noreferrer">this article</a> the author does something similar to this:
Code:
public Expression createExpr(CommonTree ast) {
// ...
switch (ast.getType()) {
case SimpleExpressionParser.INT:
return new IntegerLiteral(ast.getText())
case SimpleExpressionParser.PLUS:
return new Plus(createExpr((CommonTree)ast.getChild(0)), // recurse
createExpr((CommonTree)ast.getChild(1))); // recurse
case SimpleExpressionParser.MINUS:
return new Minus(createExpr((CommonTree)ast.getChild(0)), // recurse
createExpr((CommonTree)ast.getChild(1))); // recurse
}
// ...
}
Is this the preferred way?! Can't I instruct ANTLR to generate this boiler-plate code somehow (it will be huge)?
<hr>
<strong>Possibly related questions:</strong>
<ul>
<li><a href="https://stackoverflow.com/questions/2217173/converting-antlr-syntax-tree-into-useful-objects">Converting Antlr syntax tree into useful objects</a> (But I can't see how the answer answers my question.)</li>
</ul>