function - 使用 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-我可以在创建类之后将这个函数的代码放在哪里,是否只在动作子句中,有没有办法将类名放在我可以放置实现的语法中
谢谢您的帮助!
解决方案
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).
推荐阅读
- javascript - 为什么我的表单在按 Enter 或使用条形码扫描仪时提交了两次?
- css - 需要在父级中设置高度以使拉伸内容内部可滚动
- java - 线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:-1 在 java.lang.String.substring(String.java:1960)
- sql - 如何在 Oracle SQL 中将常用日期合并为 1 行
- html - 有没有办法将导航栏中的 li 下方的下拉菜单居中?
- c# - c#在并发任务执行之间添加延迟
- html - 如何将html表格分成两部分?
- docker - 既然我们在配置服务时有这个选项,那么 Kubernetes 部署端口配置有什么用?
- android - Android 应用程序不使用 firebase 启动意图
- google-apps-script - 谷歌表格中的getLastRow问题