ANTLR 语法 - 解析标签和指令
我正在编写一个使用指令列表代码的解释器。当我尝试编写带标签的指令语法时遇到一些问题。
我想解析这个代码块:
LD 4
ST A
LD A
EQ 4
ST _AUX_1
(* IF *)
LDN _AUX_1
JMPC _label_2
(* THEN *)
LD B
EQ 3
ST _AUX_2
_label_2: (* ELSE *)
_label_1: (* END IF*)
LD TRUE
正如你所看到的,这些标签位于一行中,没有指令,但我必须解析的语言允许“标签+指令”在一行中。因此,存在三种不同类型的指令和标签组合:
- 指令
- 标签:
- 标签: 指令
我可以解析 1 和 2,但我无法解析所有不同的组合。
这是我的 ANTLR 语法的摘录:
program_il : instruction* ;
instruction
: ID':' // label
| ID_INST operand
;
operando
: ID
| CTE_INT
| CTE_BOOL
;
我试图将语法更改为:
instruction : (ID':')? instruction? ;
但我遇到了下一个错误:
As a result, alternative(s) 2 were disabled for that input
[14:43:49] error(201): Analizador.g:131:29: The following alternatives can never be matched: 2
有人可以帮我解决这个问题吗? 先感谢您。
I'm writting an interpreter which uses an Instruction List code. I'm having some problems when I try to write the grammar for instructions with labels.
I'd like to parser this code block:
LD 4
ST A
LD A
EQ 4
ST _AUX_1
(* IF *)
LDN _AUX_1
JMPC _label_2
(* THEN *)
LD B
EQ 3
ST _AUX_2
_label_2: (* ELSE *)
_label_1: (* END IF*)
LD TRUE
As you can see those labels are in a single line without instructions, but the language I'have to parser allows "label + instruction" in a line. So, there are three differents kinds of combinations of instruction and label:
- Instruction
- Label:
- Label: Instruction
I can parse 1 and 2, but I'm not able to parse all of the different combinations.
This is an extract of my ANTLR grammar:
program_il : instruction* ;
instruction
: ID':' // label
| ID_INST operand
;
operando
: ID
| CTE_INT
| CTE_BOOL
;
I tried to change the grammar to:
instruction : (ID':')? instruction? ;
But I've got next error:
As a result, alternative(s) 2 were disabled for that input
[14:43:49] error(201): Analizador.g:131:29: The following alternatives can never be matched: 2
Could someone help me with this problem?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您现在接受
Label:Instruction
,所以语法不明确。解析器无法区分:和
换句话说,您需要让您的指令由某种分隔符分隔。换行符可以实现这一点,如以下演示所示:
现在解析输入:
将生成以下树:
Because you now accept
Label: Instruction
, the grammar is ambiguous. The parser cannot make a distinction between:and
In other words, you need to let your
instruction
s be separated by some sort of delimiter. A line break would do the trick as the following demo demonstrates:Now parsing the input:
will result in the following tree: