ANTLR识别单个字符 [英] ANTLR recognize single character

查看:145
本文介绍了ANTLR识别单个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很确定这是不可能的,但是我想以防万一.

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

我有通用ID令牌定义:

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

但是

ab + 4

是不可能的.

所以我不能写这样的规则:

So I can't write a rule like:

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

由于词法分析器会将"a"作为ID,因为ID的优先级较高. (而且我无法更改该优先级,因为那时它无法识别单个字符ID)

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)

所以我只能在该规则中使用ID而不是LETTER.这很丑陋,因为不应该有一个ID,只有一个字母,我将不得不进行第二次语法分析以确认这一点.

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.

我知道这无关紧要,因为词法分析器不了解上下文.我在想,也许已经内置了ANTLR4,这是一种检查规则中令牌长度的方法.像这样:

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

为了避免在规则中写入"ID",因为这可能会造成混淆.

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

PD:我不是在解析像这样的简单语言,这只是一个小例子.实际上,ID也可以是字符串,还有其他标记只能是字母的一个子集,依此类推……因此,我认为我必须在解析条目以语法上检查合法性后再进行第二次分析.我很好奇是否存在这样的东西.

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.

推荐答案

检查标识符的大小是一个语义问题,因此应在语义阶段进行处理,该阶段通常在解析步骤之后进行.使用通常的ID规则分析您的输入,并在构造的分析树中检入识别出的ID的大小(并采取相应措施).不要试图将这种决定强加于语法中.

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.

这篇关于ANTLR识别单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆