Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to report errors from ANTLR 4 Visitor?

Tags:

c#

antlr

antlr4

I created a grammar for boolean expressions and now I'm trying to implement visitor for evaluating it.

It is told that there is no need to overcomplicate grammar lexer and parser rules with semantic analysis because it is much better to provide meaningful error messages from the Visitor.

So I'm trying to check type consistency, date correctness etc in the Visitor. And the surprise I get is there is no way (at least I don't see it) to report an error from the Visitor other than throwing exceptions. And if I throw an exception I won't be able to proceed with expression validation and detect all errors at once. In addition I have to catch somehow all parsing exception types (how should I know them?). All in all, exception throwing doesn't seem to be correct solution.

Could you give me a direction how it is planned to report errors in expression semantics during Visitor traversal?

like image 309
Sasha Avatar asked Sep 03 '15 16:09

Sasha


1 Answers

Since you define the visitor, you can create and pass it an object it will report errors to.

Simple example:

public interface IErrorReporter
{
    void ReportError(ParserRuleContext context, string error);
}
public class ValidationVisitor : YourLanguageBaseVisitor<Whatever>
{
    private readonly IErrorReporter _errorReporter;

    public ValidationVisitor(IErrorReporter errorReporter)
    {
        _errorReporter = errorReporter;
    }

    public override Whatever VisitSomeNode(YourLanguageParser.SomeNodeContext context)
    {
        if (context.GetText() != "expected")
            _errorReporter.ReportError(context, "Invalid text");

        return Visit(context.someSubNode());
    }
}

Then validate like this:

var parseTree = DoTheParsingStuff();

// Implement that one, store the errors in a list
var errorReporter = new SimpleErrorReporter();  

new ValidationVisitor(errorReporter).Visit(parseTree);

if (errorReporter.Errors.Count > 0)
{
    // Display errors
}

The ParserRuleContext can be useful to locate where the error occurred (line/column etc), but otherwise you may implement whatever fits your error reporting needs.


Side note: If you plan on having many visitors, then building an AST and then validating based on that may be beneficial in the long run. You need to decide whether it's worth it.

like image 104
Lucas Trzesniewski Avatar answered Oct 17 '22 00:10

Lucas Trzesniewski