Search code examples
functionparsingantlr4business-rules

Using ANTLR4 to create functions with no argument


I am still new to ANTLR4 and I am trying to achieve the following

I have business rules which consist of logical operation (A= 'text' or B < 1) and getDataDB

the function getDataDB does not take any argument. the function will retrieve some data to validate it and return either true or false.

my grammar is below

   /*
 * Test grammar
 */

grammar FunctionRule;

parse: expr EOF
    ; 


expr
 : expr binop expr                  #logicalExpression
 | lhs=VARIABLE compop rhs=VARIABLE #variableExpression
 | lhs=VARIABLE compop rhs=STRING   #stringExpression
 | lhs=VARIABLE compop rhs=NUMBER   #numberExpression
 | TRUE                             #booleanTrue
 | FALSE                            #booleanFalse
 | function                         #functionExpression
 | VARIABLE                         #booleanVariable
 | LEFTPAREN expr RIGHTPAREN        #enclosedExpression
 ;

binop : AND | OR
 ;

compop: EQUAL | LT | GT | LTE | GTE | NE
      ;

function  : ID {System.out.println("HELLLL");};



TRUE:       'true' | 'TRUE'  ;
FALSE:      'false' | 'FALSE';
STRING:     '"'   ~([\t\n\r]| '"')* '"'
     ;
ID : [getDataDB];
LEFTPAREN:  '(';
RIGHTPAREN: ')';
EQUAL     : '=' | 'EQ';
LT        : '<' | 'LT';
GT        : '>' | 'GT';
LTE       : '<=' | 'LE';
GTE       : '>=' | 'GE';
NE        : '!=' | 'NE';
AND       : 'AND' | '&' | 'and';
OR        : 'OR' | 'or' | '|';
VARIABLE  : [a-zA-Z]+[a-zA-Z0-9_.-]*;
NUMBER  : [0-9]+ ('.'[0-9]+)?;
SPACE     : [ \t\r\n] -> skip;

When I generate classes from the grammar, i did not see anything related to the function.

1-how do I define a function correctly in the grammar file.

2- where i can put the code for this function after creating the classes, is it only in the action clause, is there is a way to put the class name in the grammar where i can put the implementation

Thanks for the help!


Solution

  • ID : [getDataDB];
    

    This means that ID matches a single letter that could be either one of g, e, t, D, a or B. What you likely wanted is ID: 'getDataDB'; which matches the string getDataDB. Note that calling this ID is highly misleading.

    where i can put the code for this function

    Are you writing an interpreter using a visitor? Then you'd put the code into the visitFunction method or rather in a getDataDB method that you call from visitFunction if the function name was equal to getDataDB (right now that would always be the case, but I'm assuming you eventually want to introduce more than one function).

    Alternatively you could also structure your grammar slightly differently like this (removing the ID rule):

    function : 'getDataDB' # GetDataDB
             | 'otherFunction' # OtherFunction
             ;
    

    Then you could define the functions in visitGetDataDB and visitOtherFunction respectively.

    All that's assuming that you want function names to be keywords (which implies that there can't be user-definable functions). If you don't, you should not have separate tokens for function names, so zero-argument functions and variables become indistinguishable syntactically (unless you add a requirement to add () for functions, but it doesn't look like that's what you want). So you should just have one rule that could be either a variable or a zero-argument function and then check whether the given identifier is the name of a function in visitVariableOrNullaryFunction (which maybe you'd just call visitVariable for brevity).