Keep on trying
Keep on trying

Reputation: 3

antlr4 handling incomplete rule match because of parse error in visitor

I'm new to antlr4 and I'm trying to make good use of antlr's ability to recover from parser errors and proceed. I find that it can proceed to visit the parse tree even when there has been a parse error and it will match a rule but sometimes not all of the rule elements are there. This causes a problem in my visitor code because my code is expecting all elements of the rule match to be there and it throws an exception.

Two options I'm thinking of:

1) after parsing, check parser.getNumberOfSyntaxErrors() > 1 and don't proceed to visit the parse tree if so. This will stop an exception being thrown, but does not give the user as good feedback as possible. antlr4 does recover nicely from errors and can get to the next independent section of what I'm trying to parse so this is stronger than what I'd like.

2) I can wrap each self.visit() in something that will catch an exception and react accordingly. I think this would work.

But, I'm wondering if there is something in ctx or otherwise that would tell me that what's below it in the parse tree is an incomplete match?

In case it is relevant, I'm using python with antlr 4.

Upvotes: 0

Views: 987

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53337

As you have seen ANTLR4 tries to re-synchronize the input stream to the rule structure once an error was encountered. This is usually done by trying to detect a single missing token or a single additional token. Everything else usually leads to error nodes all the way to the end of the input.

Of course, if the input cannot be parsed successfully the parse tree will be incomplete, at least from the point of the error, which might be much earlier than where the actual error is located. This happens because of variable lookahead, which may consume much of the input to find a prediction early in the parsing process (and hence this can fail early).

In fact I recommend to follow path 1). Once you got a syntax error there's not much in the parse tree you can use. It's totally up to the grammar structure what part will be parsed successfully (don't assume it would always be everything up to the error position, as I just explained).

Upvotes: 1

Related Questions