0

Below is a simple ANTLR grammar:

grammar EXAMPLE;

document
            :  Name+ EOF ;

Name
            : 'k'+ ;

The generated antlr parser accepts any string that contains substring 'k'.

However, I want the parser to accept only strings that conform to the grammar. that is, strings that contain characters other than 'k' should trigger an error.

Is there a way to ensure that?

I tried modifying the grammar. And testing with different inputs. But no fix worked.

5
  • Are you saying that the generated parser is not reporting any errors for inputs that don't match the grammar (it should)? Or are you talking about the fact that it still returns a parse tree even when there were errors? Commented Sep 29, 2023 at 16:36
  • Antlr4 lexers throw away unrecognized chars for a given grammar. So the lexer only recognizes sequences of 'k'. By default the lexer will print an error but it doesn't keep track that it had an error. To boot, the parser error count doesn't include lexer errors at all. So if you check the parser error count, all is fine. You need to implement an error listener for the lexer, overriding the error report method. Best to also do the same for the parser in order to be consistent. This is one of the things I don't like how Antlr works. But it is what it is and you can work around the issue. Commented Sep 29, 2023 at 23:48
  • As an alternative you can add a lexer rule at the end of the grammar Fubar: .;. The parser will now keep track of the error. Commented Sep 29, 2023 at 23:52
  • @sepp2k I am talking about error reporting. Commented Oct 4, 2023 at 10:05
  • @kaby76 Thank you for your answer. I tried your last suggestion of adding a lexer rule and it works. Commented Oct 4, 2023 at 10:08

1 Answer 1

0

After reading the comment by Ken and revisiting my code, it turns out that I had the following line in my main function:

lexer.removeErrorListeners()

After deleting that line, now lexer errors are reported.

Thank you all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.