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 :-
- ( A + B ) , this can also be SUM(A,B)
- (MEAN(A, B, C) + MAX( X, MIN(Y,Z)) + 2)/4
- 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: '//' ~[
]* -> skip;
WS: [
]+ -> 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.
question from:
https://stackoverflow.com/questions/65914097/how-to-write-grammar-for-the-below-criterion-grammar-for-antlr4-custom-expre 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…