[antlr-interest] Interpreter built-in functions: code in grammar or use function table?

Michael Cooper tillerman35 at yahoo.com
Sun Oct 28 12:17:07 PDT 2012


I need the usual set of built-in functions for my (java-based) interpreter.  Stuff like string manipulation functions, math functions, date functions, etc.

I'm on a fence about how to implement them. 

On the one hand, I could do it with two simple grammar rules.  One for method invocations and another for function calls.  Then all I would have to 
do is maintain a function table with a list of built-in functions.  I already maintain a function table for functions in the input, so it wouldn't be much of 
a stretch to adapt that to built-in functions as well.

The other alternative is to define each built-in function in the grammar itself.  E.g. a rule for "println" and another for "substr" and another for "randbetween" etc. etc.
The advantage to that approach is that they appear in the railroad diagrams in Eclipse.  Those are pretty handy for documentation.  I suppose I could write a
little helper function to output function definitions to ANTLR-compatible grammar rules. 

Any thoughts?


More information about the antlr-interest mailing list