class X
{
int A()
{
int B
This topic explains the backtracking error handling strategy.
The following topics are prerequisites to understanding this topic:
It is possible for the Syntax Parsing Engine to parse things incorrectly if the user omits something from the code. Special cases of this can be handled by the synchronizer pair error handling strategy, but it does not make sense to set IsStartOfErrorRecoveryPair on most symbols. The backtracking strategy handles the general case of backing out of a bad parsing decision. The following is an example of part of a document the parser has started analyzing:
In C#:
class X
{
int A()
{
int B
The parser will assume it is within a class “X”, which contains a method “A”, which contains a local variable named “B”. However, here is the full document:
In C#:
class X
{
int A()
{
int B()
{
}
}
When the parsing gets to the “B(“ text, it realizes that something is wrong. Local variable declarations cannot be followed by an open parenthesis. What should have happened was the close brace for method “A” should have been inserted as missing, and “int B” should have been interpreted as the start of a method signature.
The backtracking strategy will break up some small syntax nodes recently created by the parser and see if the tokens in those nodes could have been parsed in a better way if some omitted things had been included by the user. In this above example, this will correctly report that a close brace is missing.
A few things to note about the backtracking error handling strategy:
This strategy can be disabled for a non-terminal symbol by setting its NonTerminalSymbol.PreventBacktracking property to True. Once a reduction occurs to a non-terminal symbol with this value set to True, the syntax node created by that reduction can never be broken up to try a backtracking approach.
Only a small number of syntax nodes will be broken up like this when an error occurs, and if a node is made of up too many tokens, the backtracking strategy will not be used. It would be too slow to re-parse a hundreds or thousands of tokens once they were already parsed into a node structure.
The following topics provide additional information related to this topic.