我正在使用 ANTLRWorks 的 Debug模式来测试我的 c 语法。在 ANTLRWorks 中调试对于更好地理解确实非常有用,但我在理解输出树的不同颜色方面遇到了问题。我在我的语法中使用了 backtrack=true
。我认为红色意味着调试器走错了路,而绿色告诉我它走对了路。但是深红色和深绿色呢?
我添加了一张“小树”的图片,它只匹配以下输入:
int test;
如果有必要回答这个问题,这里有 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/14057224/