Saturday, February 28, 2009

ANTLR: Calling Java functions from parsed input with a variable number of arguments

I don't remember seeing quite an example like this, so here is one.
This is not exactly rocket science: The point is that you can write code like this:

add(1,2,3,4,5)

And parsing that will call the Java method "add" which looks like this:

public Integer add(Integer...arguments) {
Integer sum = new Integer(0);
for (Integer x : arguments) {
sum+= x;
}
return sum;
}
}


So if you need a simple interface language over Java methods, you could use this approach.

ANTLR parse code (not that beautiful).
The ANTLR Parser Generator.

Good luck with your parser.

0 comments: