gpt4 book ai didi

debugging - ANTLRWorks 调试 - 不同颜色的含义?

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:09 25 4
gpt4 key购买 nike

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

我添加了一张“小树”的图片,它仅与以下输入匹配:

int test;

enter image description here

如果有必要回答这个问题,这里有 4 个最重要的规则。

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.

关于debugging - ANTLRWorks 调试 - 不同颜色的含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com