AntLR4 : Build A function

Viewed 2779

I've been trying to build a function: concat('A','B') OR concat('A',9)

Here is a sample grammar I have written :

    LPAREN : '(' ;
    RPAREN : ')' ;
    FUNCTIONNAME : 'CONCAT' ;
    ARGUMENTS : TEXT (',' TEXT)* ;
    TEXT : ('a'..'z' | '0'..'9' | 'A'..'Z')+; 
    allFunction : FUNCTIONNAME LPAREN ARGUMENTS (',' ARGUMENTS)* RPAREN ;

But not able to build a tree properly.

Update1:

Here is the Tree:

  0 null
-- 11 CONCAT
-- 4 (
-- 13 2,5
-- 5 ) 

and the grammar :

allFunction : FUNCTIONNAME LPAREN ARGUMENTS RPAREN;

Update2 :

Grammar:

allfunction : COMMA | FUNCTIONNAME LPAREN ARGUMENTS (COMMA ARGUMENTS)* RPAREN ;

Parsed output:

CONCAT(A,B,C)

[@0,0:5='CONCAT',<8>,1:0]
[@1,6:6='(',<1>,1:6]
[@2,7:11='A,B,C',<9>,1:7]
[@3,12:12=')',<2>,1:12]
[@4,13:14='\n\n',<7>,1:13]
[@5,15:14='<EOF>',<-1>,3:0]

Update3 :

I have been tring to build a function : CONCAT(TEXT,TEXT) -(Input limited to 2 params). This works fine. I have implemented IF function : IF(TEXT,TEXT,TEXT) - This also works fine.

The problem is, I have to modify it to: IF(BOOLEAN,INT,INT) - But with existing grammar for any parameter in IF, it can accept UNSIGNED_INT including the first parameter.

Grammar:

Here is the link: https://ufile.io/undqs or https://files.fm/u/7c44aaee

1 Answers
Related