简体   繁体   English

AntLR4:构建一个函数

[英]AntLR4 : Build A function

I've been trying to build a function: concat('A','B') OR concat('A',9) 我一直在尝试构建一个函数: 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 : Update1

Here is the Tree: 这是树:

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

and the grammar : 和语法:

allFunction : FUNCTIONNAME LPAREN ARGUMENTS RPAREN;

Update2 : Update2

Grammar: 语法:

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

Parsed output: 解析输出:

CONCAT(A,B,C) 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 : Update3

I have been tring to build a function : CONCAT(TEXT,TEXT) -(Input limited to 2 params). 我一直在努力构建一个函数: CONCAT(TEXT,TEXT) - (输入限制为2个参数)。 This works fine. 这很好用。 I have implemented IF function : IF(TEXT,TEXT,TEXT) - This also works fine. 我已经实现了IF函数: IF(TEXT,TEXT,TEXT) - 这也可以正常工作。

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. 问题是,我必须将其修改为: IF(BOOLEAN,INT,INT) - 但是对于IF中的任何参数的现有语法,它可以接受UNSIGNED_INT,包括第一个参数。

Grammar : 语法

Here is the link: https://ufile.io/undqs or https://files.fm/u/7c44aaee 这是链接: https//ufile.io/undqshttps://files.fm/u/7c44aaee

You should not create a lexer rule ARGUMENTS . 您不应该创建词法分析器规则ARGUMENTS This is something the parse should handle. 这是解析应该处理的事情。 And the parameters should probably not be TEXT tokens, but some sort of expressions so that CONCAT(CONCAT(A, B), C) also works. 参数应该不是TEXT令牌,而是某种表达式,这样CONCAT(CONCAT(A, B), C)也可以工作。

Something like this would be a good start: 这样的事情将是一个良好的开端:

grammar T;

parse
 : expression EOF
 ;

expression
 : expression 'AND' expression
 | expression 'OR' expression
 | function
 | bool
 | TEXT
 | NUMBER
 | TEXT
 | ID
 ;

function
 : ID '(' arguments? ')'
 ;

arguments
 : expression ( ',' expression )*
 ;

bool
 : TRUE
 | FALSE
 ;

TRUE         : 'true';
FALSE        : 'false';
NUMBER       : ( [0-9]* '.' )? [0-9]+;
ID           : [a-zA-Z_] [a-zA-Z0-9_]*;
TEXT         : '\'' ~[\r\n']* '\'';
SPACE        : [ \t\r\n]+ -> skip;

When parsing your input like this, you can simply parse any function that takes any parameter (of any type) an unknown amount of times. 在解析这样的输入时,您可以简单地解析任何带有任何参数(任何类型)未知次数的函数。 Eg it will parse both CONCAT('a','b') and IF(false,1,42) . 例如,它将解析CONCAT('a','b')IF(false,1,42) But note that it will also parse IF(false,1,42,1,1,1,1,1,1,1,1,1,1) . 但请注意,它也将解析IF(false,1,42,1,1,1,1,1,1,1,1,1,1) So after the parsing is finished, you can walk your parse-tree and validate that all the functions have the proper amount of parameters of the correct type. 因此,在解析完成后,您可以遍历解析树并验证所有函数是否具有正确类型的适当数量的参数。

Also, Is there any way to edit parse tree? 另外,有没有办法编辑解析树?

See: How to rewrite Antlr4 Parse Tree manually? 请参阅: 如何手动重写Antlr4 Parse Tree?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM