Custom error messages

Feb 22, 2009 at 9:51 PM
I am trying to figure out how to implement custom error messages in my grammar.  In YACC, it would look like this:

Identifier
    : IDENTIFIER
    | Identifier '.' IDENTIFIER
    | Indetifier '.' error { CallHdlr("expected identifier", @3; }
    ;

I noticed in the C# Grammar the ErrorRule and the SyntaxError terminal.

statement.ErrorRule = SyntaxError + semi; //skip all until semicolon

How do you define the custom error message that will be displayed when the ErrorRule is used?
Coordinator
Feb 23, 2009 at 4:18 PM
The ErrorRule facility is primarily for error recovery, not for error reporting, to give parser a hint how to recover from error to continue parsing for (possibly) more errors. It tells parser "skip all until next semicolon", so it is quite coarse-grained in nature. You should try overriding Grammar.GetSyntaxErrorMessage method. It receives compilerContext as parameter, and from it you can get Parser object, and inspect its internal state (stack, current symbol, etc).