Can not provide for host variables in Antlr3 SQL grammar

余生颓废 提交于 2019-12-11 09:01:58

问题


I have a SQL grammar which do not have support for host variables. I want to provide support for host variables in that but a situation that I have encountered is tricky.

There is SQL Identifier lexer rule in grammar, which supports alphanumeric along with some special characters '@', '#', '$ and '_'.

Host variable's are dependent on language in which SQL is embedded. e.g. COBOL. COBOL identifiers allow additionally hyphen(-) in variable names (Some other differences in them).

So I added LANG_ID additionally in grammar, which is matching COBOL identifiers.

Parser rule for host variable was like :

hostvariable : COLON WS* (ID | LANG_ID);

There was some other rule which was matching expressions. something like that:

expression :  identifier ('+' | '-')  identifier;

identifier here is parser rule which may have ID and some other tokens.

valid input for expressions is ABC+ABC, ABC-ABC, ABC-12.

Now when input is like ABC-12, then it is getting tokenized as COBOL identifier.

Solution which I implemented was that, if its followed by COLON (:) , then only its COBOL identifier. Then I made LANG_ID fragment and created lexer rule as :

HOSTVAR : COLON WS* LANG_ID;

Now problem occurred with JSON_OBJECT SQL function which have syntax like:

JSON_OBJECT('KEY' : VALUE)

There is a rule for JSON_OBJECT which expects something like (character_literal COLON value_expression)

value_expression could also be identifier.

In this case ': VALUE' is tokenized as HOSTVAR and JSON_OBJECT rule is failing.

I am not able to find any solution for the problem. In every approach something getting failed.


回答1:


JSON_OBJECT('KEY' : VALUE) 

syntax is not supported in IBM i COBOL. This can only be provided in the form of

JSON_OBJECT (KEY key VALUE value) 

In the case of COBOL when COLON is followed by something, it should be treated as host variable.



来源:https://stackoverflow.com/questions/51476284/can-not-provide-for-host-variables-in-antlr3-sql-grammar

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