ANTLR recognize single character

筅森魡賤 提交于 2019-12-12 04:47:34

问题


I'm pretty sure this isn't possible, but I want to ask just in case.

I have the common ID token definition:

ID: LETTER (LETTER | DIG)*;

The problem is that in the grammar I need to parse, there are some instructions in which you have a single character as operand, like:

a + 4

but

ab + 4

is not possible.

So I can't write a rule like:

sum: (INT | LETTER) ('+' (INT | LETTER))*

Because the lexer will consider 'a' as an ID, due to the higher priority of ID. (And I can't change that priority because it wouldn't recognize single character IDs then)

So I can only use ID instead of LETTER in that rule. It's ugly because there shouldn't be an ID, just a single letter, and I will have to do a second syntactic analysis to check that.

I know that there's nothing to do about it, since the lexer doesn't understand about context. What I'm thinking that maybe there's already built-in ANTLR4 is some kind of way to check the token's length inside the rule. Something like:

sum: (INT | ID{length=1})...

I would also like to know if there are some kind of "token alias" so I can do:

SINGLE_CHAR is alias of => ID

In order to avoid writing "ID" in the rule, since that can be confusing.

PD: I'm not parsing a simple language like this one, this is just a little example. In reality, an ID could also be a string, there are other tokens which can only be a subset of letters, etc... So I think I will have to do that second analysis anyways after parsing the entry to check that syntactically is legal. I'm just curious if something like this exists.


回答1:


Checking the size of an identifier is a semantic problem and should hence be handled in the semantic phase, which usually follows the parsing step. Parse your input with the usual ID rule and check in the constructed parse tree the size of the recognized ids (and act accordingly). Don't try to force this kind of decision into your grammar.



来源:https://stackoverflow.com/questions/45437040/antlr-recognize-single-character

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