1

If the input to a grako/tatsu generated parser has a syntax error, such as 3 + / 3 to the calc.py examples, one gets a long list of Python calling sequences in addition to the relevant 3 + / 3 ^ I could use try - except constructions but then I lose the relevant part of the error message as well.

I would like to use grako/tatsu to parse grammar rules for a rule compiler and I appreciate the possibility of separating the syntax and semantics in a clean way. The users would be quite annoyed of the excessive error messages. Is there a way for clean error messages?

koskenni
  • 63
  • 5

1 Answers1

2

This should be the same as in any Python program. If you let the exception escape main(), then a stack trace will be printed. Instead, you can write:

try:
   do_parse()
except Exception as e:
  print(str(e))
Apalala
  • 9,017
  • 3
  • 30
  • 48
  • Thanks for the answer. Now I think that I can handle the syntax errors the user makes when writing the rules. The str(e) gives me enough information and I think I can choose the essential information from that string. I still have problems in reporting errors which are detected in my semantic rules. I use the `parse-factored()` of style parsing as in `calc.py`. – koskenni Mar 05 '18 at 17:48
  • 1
    @koskenni You can also spelunk the exception object, which has some interesting information stored in it's attributes. See `tatsu.exceptions` for the definitions. – Apalala Mar 07 '18 at 18:10