3

I am using Antlr4 to tokenize and parse a legacy language, my end goal is to generate C# code and execute this code in a runtime environment that simulates the physical hardware that executes the legacy language.

The simulator and code generation is working very well.

I would like to generate specific error messages or warning messages from the listener, is this possible?

For example the language allows a line to end or not end in a semi-colon, I would like to issue a warning if a line is missing a semi-colon.

I have many more similar scenarios, the language and physical hardware have a number of bizarre characteristics.

Thanks, Gregg

1 Answers1

1

The likely better approach would be to issue the warnings from the parser. In the construction of the parser, there are two methods:

parser.addErrorListener(new YourErrorListener());
parser.setErrorHandler(new YourParserErrorStrategy());

The error listener allows custom error messages. The error strategy controls how the parser deals with specific errors: re-sychronizing with the token stream or effectively inserting 'missing' tokens.

Community
  • 1
  • 1
GRosenberg
  • 5,843
  • 2
  • 19
  • 23
  • Your suggesting that I demand a ";" at the end of the line. If the line is missing the semi-colon I would add the semi-colon and issue a custom error message. I will give this a try. – Gregg Swanson Jul 17 '15 at 21:10
  • If having the semicolon (or other 'bizarre' circumstance token) present makes subsequent analysis of the parse tree easier/more regular, make it non-optional (guess that is what you meant by demand) and add it when missing. Can go the other way as well: skipping over extra/unexpected tokens dependent on whether the ErrorStrategy determines they are significant or not -- it can warn, fixup, or bail. – GRosenberg Jul 17 '15 at 22:21