首页 > 解决方案 > 使用 ANTLR4 创建不带参数的函数

问题描述

我还是 ANTLR4 的新手,我正在努力实现以下目标

我有由逻辑操作(A='text'或B < 1)和getDataDB组成的业务规则

函数 getDataDB 不带任何参数。该函数将检索一些数据以验证它并返回真或假。

我的语法在下面

   /*
 * 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;

当我从语法生成类时,我没有看到与函数相关的任何内容。

1-如何在语法文件中正确定义函数。

2-我可以在创建类之后将这个函数的代码放在哪里,是否只在动作子句中,有没有办法将类名放在我可以放置实现的语法中

谢谢您的帮助!

标签: functionparsingantlr4business-rules

解决方案


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).


推荐阅读