How to write grammar for the below criterion – Grammar for ANTLR4 – Custom Expressions

I have a simple expression that I am writing and I am looking for a way to write grammar for such expressions so that ANTLR can generate the lexer and parser using this file.

My expressions don’t have any assignments. They are just a bunch of operations on some pre existing fields. They don’t need to be evaluated.

I have a bunch of pre defined functions ( such as SUM, MEAN, SUBSTR which the backend understands ) and these functions are applied on some existing fields.

The operators I need are :- + , – , * , /
Brackets : ( , ) for opening and closing.
Functions ( keywords ) : SUM, MEAN, MAX SUBSTR.

Examples :-

  1. ( A + B ) , this can also be SUM(A,B)
  2. (MEAN(A, B, C) + MAX( X, MIN(Y,Z)) + 2)/4
  3. SUBSTR("TEST1",0,6)

The expression can extend to multi line.

Here is the basic version that I’ve written.

grammar ExpressionGrammar;

parse: (expr)+ EOF
    ;

expr: expr / expr
    | expr * expr  
    | expr + expr
    | expr - expr
    | NUM
    | function
    ;

function : ID '(' arguments? ')'

arguments: expr ( ',' expr)*

/* Tokens */ 

OPEN_PAR : '(' ;
CLOSE_PAR : ')' ;

NUM : '0' | '-'?[1-9][0-9]*;
ID : [a-zA-Z_] [a-zA-Z]*;
COMMENT: '//' ~[\r\n]* -> skip;
WS: [ \t\n]+ -> skip;

Eventually I would also have to run some validations on the expression typed by the user. If I input a string inside a MAX() function which only accepts numbers, I should be able to know the line/position where the error is at and notify the user. I believe this comes during the parsing phase ? But just putting it out there, in case there are any inputs and if this grammar can help me identify that.

97 thoughts on “How to write grammar for the below criterion – Grammar for ANTLR4 – Custom Expressions”