-1

I've been playing around with antlr to do a kind of excel formula validation. Antlr looks pretty nice, however, I have some doubts about the way it works.

Imagine I have a grammar that already knows about all kind of tokens needed to perform an excel formula validation (rules references, operations, etc). In this grammar, there is no valid token for currency symbols (€,£, etc), though I have an 'ERROR_CHAR' token that matches anything: ERROR_CHAR: .;

Here's what I want to know about an example input: =€€€+SUM(1,2)

  1. The formula is not valid
  2. All the tokens after €€€ are valid and there are rules for them -> +SUM(1,2)

My parser only knows that is invalid, but don't know about a sequence of ERROR_CHAR, just like €€€, and so, all the input is wrong and all subsequent tokens are caught by the error listener. I assume that this is because, based on my parser rules, I am not saying that ERROR_CHAR could be present anywhere in the input. I don't want to skip those tokens, because I'd like to highlight the position of the error and I am already skipping whitespaces.

Do you have any idea how could I handle this?

Emanuel Pinho
  • 136
  • 1
  • 6

1 Answers1

0

If you just want to highlight the position of an error, let ANTLR detect errors and their positions. It does it quite well. No grammar changes required.

Use ErrorListener to detect errors and handle them.
You can find more information here: Handling errors in ANTLR4

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
  • I understand what you are saying. The problem is that the `ErrorListener` is detecting errors each token after the unrecognizing token. `=€ + SUM(1,2)` -> This works well, just € is fired by `ErrorListener`. `=€€ + SUM(1,2)` -> All tokens are fired with error – Emanuel Pinho Mar 21 '19 at 13:29
  • 1
    In this case you can look at ErrorStrategy to recover from an error. You can find more about it here: https://stackoverflow.com/questions/26675254/antlr-error-strategy-to-skip-tokens-until-rule-matches-again – Pavel Smirnov Mar 21 '19 at 13:41