Search code examples
c#parsinggold-parser

How to find a wrong character in my grammar


I started working on the gold parser and tried to adopt its syntax to . This is a fragment code. I'm interested in what this particular part does, and if I can see which of the characters in the text which i entered with textbox is wrong with my grammar?

private void TokenErrorEvent(LALRParser parser, TokenErrorEventArgs args)
{
    string message = "Token error with input: '"+args.Token.ToString()+"'";

}

private void ParseErrorEvent(LALRParser parser, ParseErrorEventArgs args)
{
    string message = "Parse error caused by token: '"+args.UnexpectedToken.ToString()+"'";

}

Solution

  • Gold parser uses events to communicate with the host application. You posted handlers for two of them, which are:

    • OnTokenError... unrecognizable input. cannot do anything about this except fixing the source.
    • OnParseError... encountered a token it cannot parse. You can decide how to proceed with the ContinueMode: supply a replacement token, ignore, or stop.

    According to documentation, source location information is available with these properties:

    args.UnexpectedToken.Location.LineNr
    

    and

    args.UnexpectedToken.Location.ColumnNr
    

    They are both zero-based, so if you split your source into an array of lines, you can directly use LineNr as index, and then the SubString function to point to the first character of the unexpected token literal.