关于antlr:ANTLRWorks调试——不同颜色的含义?

ANTLRWorks debugging - the meaning of the different colors?

我正在使用 ANTLRWorks 的调试模式来测试我的 c 语法。在 ANTLRWorks 中进行调试非常有助于更好地理解,但我在理解输出树的不同颜色时遇到了问题。我在我的语法中使用了 backtrack=true 。我认为红色意味着调试器走错了路,而绿色告诉我它走对了。但是深红色和深绿色呢?

我添加了一张只匹配以下输入的"小树"图片:

1
int test;

enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
start
: declaration*
;

declaration
: functionDefinition
| dataDeclaration //also used for Function Declaration
| assemblerDefinition
;


functionDefinition
: declarationSpecifier* declarator Equals Default Semi
| declarationSpecifier* declarator Equals Delete Semi
| declarationSpecifier* declarator functionBody
;

dataDeclaration
:declarationSpecifier* declarator initializer? (Comma declarator initializer?)* Semi
;


与其说是"正确"和"错误",不如说是解析器试图找出与输入匹配的规则。当 ANTLR 必须回溯时,ANTLRWorks 将红色用于它认为可能匹配的分析树分支。绿色用于解析器实际探索的分支,黑色用于成功匹配输入的分支。较深和较浅的颜色是 ANTLRWorks 为嵌套级别的回溯提供视觉反馈 - 级别越深颜色越深。

这个答案的主要来源来自 ANTLRWorks:An ANTLR Grammar Development Environment Unpublished Draft,由 Bovet(创建 ANTLRWorks)和 Parr(创建 ANTLR)编写。

从第 8 页开始:

the path taken by the parser is shown in green

从第 15 页开始:

When ANTLR must backtrack to distinguish between alternative productions, it is usually difficult
to debug the parser because developers must track when the parser is speculating and when it is
not. ANTLRWorks clearly distinguishes between the two modes by showing all speculative parsing branches in the parse tree in red. ... The second subtree [shown in black] is the parse tree for the second alternative in rule s that matches successfully. In situations where ANTLR must nest the backtrack, ANTLRWorks changes the color through a series of gradations, one for each backtracking nesting level.