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

查看:39
本文介绍了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))*

因为 lexer 会将 '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天全站免登陆