[antlr-interest] Newbie needs guidance on passing paramters to functions

Sebastian Kaliszewski Sebastian.Kaliszewski at softax.com.pl
Thu Feb 16 11:04:09 PST 2006


Dan Spaven wrote:
> My problem is that despite my best efforts i still have no idea on how 
> to pass the parameters to these functions from calls that occur later on 
> in the code.
> 
> my questions are:
> 
> Can this be done by just using a grammar file or will i need to build a 
> treeparser?
> and depending on the answer to the above can anybody give me any details 
> on a recommended solution?

In simple cases of code generator for simple grammars tree parser is non 
needed. If there is lots of syntactic shugar and stuff in your language, 
tree parser is a nice addition(*) allowing you to simplify your code 
generation code reducing code repetition (3 x code on one sentence, thats 
fun;)).

Anyway, what you need here is thing known as symbol table (in typical 
language it is in fact a hierarchy of dictionaries, for each scope one such 
dictionary) i.e. thing which maps your symbol names (i.e. names of 
variables, functions, types, classes, constant, enumerations, whatever) to 
types or values they represent. So if you have in your code a following 
construct:

float myFun(int a, float b) {
   return a+b;
}

Then you'd load the following entry (mapping) into your symbol table:

"myFun" -> kind: FUNCTION
            type: "float"
            parameters: "int", "float"
            subscope: (
              "a" -> kind: VARIABLE
                     type: "int";
              "b" -> kind: VARIABLE
                     type: "float";
            );


Where subscope contains symbol table for the scope of myFun function body.

Of course, you have to encode the symbol table structure into programming 
language you're using to write your compiler. In statically typed OO 
languages like Java, C++ or C# in case of such rather simple symbol 
tables(**) just use language's own dictionary type (std::map in C++) and 
entries could be members of some class heirarchy with common root. In Python 
you could do the same or take advantage of some dynamic typing and/or 
functional programming features.


rgds
Sebastian Kaliszewski
-

*) Especially with Antlr 3 this will be even better option, as tree 
generation code will be simpler and more readable, but


More information about the antlr-interest mailing list