问题描述
我正在使用 ANTLRWorks 的调试模式来测试我的 c 语法.在 ANTLRWorks 中进行调试非常有助于更好地理解,但我在理解输出树的不同颜色方面存在问题.我在语法中使用了 backtrack=true
.我认为红色意味着调试器走错了路,而绿色告诉我它走对了.但是深红色和深绿色呢?
I'm using the debugging mode of ANTLRWorks to test my c-grammar. Debugging in ANTLRWorks is really great for better understanding but I have a problem in understanding the different colors of the output tree. I'm using backtrack=true
in my grammar. I thought that the red color means that the debugger goes the wrong way while green tells me that it is has gone the right way. But what about dark red and dark green?
我添加了一张小树"的图片,它只匹配以下输入:
I added a picture of a "small tree" which only match the following input:
int test;
如果有必要回答这个问题,这里是最重要的 4 条规则.
If it's necessary to answer the question, here are the 4 most important rules which are used.
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 的颜色越深越浅,为回溯嵌套级别提供视觉反馈 - 级别越深,颜色越深.
It's not so much about "right" and "wrong" as it as about the parser trying to figure out which rule will match the input. When ANTLR has to backtrack, ANTLRWorks uses red for branches of the parse tree that it considered as possible matches. Green is used for branches the parser actually explored and black for the branch that successfully matched the input. The darker and lighter colors is ANTLRWorks providing visual feedback for nested levels of backtracking - the deeper the level the darker the color.
此答案的主要来源来自ANTLRWorks:An ANTLR Grammar Development Environment Unpublished Draft 由 Bovet(创建 ANTLRWorks)和 Parr(创建 ANTLR)编写.
The primary source of this answer is from ANTLRWorks: An ANTLR Grammar Development Environment Unpublished Draft written by Bovet (created ANTLRWorks) and Parr (created ANTLR).
从第 8 页:
解析器采用的路径以绿色显示
从第 15 页:
当 ANTLR 必须回溯以区分替代产生式时,通常很难调试解析器,因为开发人员必须跟踪解析器何时进行推测以及何时进行推测不是.ANTLRWorks 通过将解析树中的所有推测性解析分支显示为红色来清楚地区分这两种模式.... 第二个子树 [以黑色显示] 是规则 s 中成功匹配的第二个备选方案的解析树.在 ANTLR 必须嵌套回溯的情况下,ANTLRWorks 通过一系列渐变更改颜色,每个回溯嵌套级别一个.
这篇关于ANTLRWorks 调试——不同颜色的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!