[antlr-interest] Using tree grammar for second pass to check function signatures

Felix Dorner felix_do at web.de
Mon Apr 21 15:21:45 PDT 2008


Marko Simovic wrote:
> hi all,
>
> i was wondering about using the tree grammar to validate parameter 
> counts on some functions in my grammar. the rule in the parser is:
>
> functionCall : functionName LParen parameterList RParen -> ^(Function 
> functionName parameterList)
>
> in this case, functionName is any string token.
>
> now, if i have a function called SUM that takes two parameters i'd 
> like to ensure that the params supplied are correct. to do this 
> currently i use (in parser):
>
> functionCall : functionName LParen parameterList RParen -> ^(Function 
> functionName parameterList)
> |
>                     tfunc='sum' LParen parameter Comma parameter 
> RParen -> ^(Function $tfunc parameter+);
>
> is it possible to check this in a tree grammar instead? 
If you just want to check if the number of arguments are correct, then 
you could do:

expr: ^(CALL name parameters*) {aMap.get(name)  ==  
$CALL.childrenCount() - 1}

where aMap would be a map from function names to the number of 
arguments. Syntax above might not be correct, but I hope the idea is 
clear. You'll probably not
want to list all your functions literally in your grammar, like you do 
above.

Felix




More information about the antlr-interest mailing list