ANTLR: parse NULL as a function name and a parameter

断了今生、忘了曾经 提交于 2019-12-12 02:14:08

问题


I would like to be able to use 'NULL' as both a parameter (the value null) and a function name in my grammar. See this reduced example :

grammar test;

expr
  : value # valueExpr
  | FUNCTION_NAME '(' (expr (',' expr)* )* ')' # functionExpr
  ;


value
  : INT
  | 'NULL'
  ;

FUNCTION_NAME
  : [a-zA-Z] [a-zA-Z0-9]*
  ;


INT: [0-9]+;

Now, trying to parse:

NULL( 1 )

Results in the parse tree failing because it parses NULL as a value, and not a function name.

Ideally, I should even be able to parse NULL(NULL)..

Can you tell me if this is possible, and if yes, how to make this happen?


回答1:


That 'NULL' string in your grammar defines an implicit token type, it's equivalent to adding something along this:

NULL: 'NULL';

At the start of the lexer rules. When a token matches several lexer rules, the first one is used, so in your grammar the implicit rule get priority, and you get a token of type 'NULL'.

A simple solution would be to introduce a parser rule for function names, something like this:

function_name: FUNCTION_NAME | 'NULL';

and then use that in your expr rule. But that seems brittle, if NULL is not intended to be a keyword in your grammar. There are other solution to this, but I'm not quite sure what to advise since I don't know how you expect your grammar to expand.

But another solution could be to rename FUNCTION_NAME to NAME, get rid of the 'NAME' token type, and rewrite expr like that:

expr
  : value # valueExpr
  | NAME '(' (expr (',' expr)* )* ')' # functionExpr
  | {_input.LT(1).getText().equals("NULL")}? NAME # nullExpr
  ;

A semantic predicate takes care of the name comparison here.



来源:https://stackoverflow.com/questions/36618696/antlr-parse-null-as-a-function-name-and-a-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!