By default, both the off-line and inline parsers generated by the ParseLR
suite stop parsing as soon as they encounter an input token that is not
recognised for the current point in the grammar. An error message is returned to
the application employing the parser, and the parse exits with a boolean failure
code false
being returned from the Parse()
method of
the parser. Some details of the position in the input token stream are available
together with a message giving the symptoms observed within the parser that
apply to the error.
It is possible to set up the parser so that it attempts to resume execution after an error has been encountered. In this mode, when an error is encountered the parser shifts and reduces rules without consuming input tokens until it encounters the special token error as the next expected input token. It then discards input tokens until it finds a token that matches a nominated terminal token. At this point it attempts to resume the parse from the subsequent token.
In order to indicate in a grammar where to resume parsing, we need to use the
special token error
followed by a terminal token, somewhere within a grammar
rule as in the following example:
AnOuterRule : SomeNonTerminal SomeOtherNonTerminal error SEMICOLON;
SomeNonTerminal : x y EXPECTEDTERMINAL z;
SomeOtherNonTerminal : a b c OTHERTERMINAL;
Imagine that we have just reduced y
in SomeNonTerminal
and the next input token is not EXPECTEDNONTERMINAL
. With error
recovery enabled, the parser will skip over the remainder of the rule, reduce to
part way through AnOuterRule
, and skip over SomeOtherNonTerminal
until it encounters the error
token. At this point it advances to the
terminal token SEMICOLON
, and skips tokens on the input stream until
a SEMICOLON
is encountered. This constitutes a match, so the parser resumes
executing a shift operation over the SEMICOLON
in the usual way.
By default the parser engine does not include this error resumption behaviour, as it
does cause a slight degradation in parser performance on each state transition. In
order to make the parser recognise the error
token and exhibit the
behaviour described above, the feature must be explicitly enabled.
For inline parsers, the ParserFactory<InlineParserClass>.InitializeFromGrammar(...)
method takes a boolean argument that explicitly enables this feature.
For off-line parsers, the ParseLR.exe
application has a command line argument
(-r
) that turns this feature on.
If you create a grammar containing the error token, but fail to enable the flags
as described above, the error token will be treated as an unrecognised non-terminal
symbol. Either the ParseLR.exe
program will fail with error messages, or
the ParserFactory<InlineParserClass>.InitializeFromGrammar(...)
function will fail to create a
parser at runtime.