问题
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;
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
;
回答1:
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.
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).
From page 8:
the path taken by the parser is shown in green
From page 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.
来源:https://stackoverflow.com/questions/14057224/antlrworks-debugging-the-meaning-of-the-different-colors