1
votes

I am new to Antlr. I am trying to implement a CSS parser. As a start I am using this grammar to generate the parser. I am following this tutorial as a guide. I am generating the code in C# using Antlr 3.4 (Next, I am going to try it using Antlr 4.0 as well).

I am facing several issues and I was unable to find resources on the internet so that I can understand them clearly.

Issues I am having:

  1. Generate customized error messages in different types (Errors, warnings). Is this provided in Antlr. Please provide me some resources to understand how to achieve this.

  2. In the tutorial I am following, I was able to catch the exceptions in parsing and lexing. But in the grammar I tried does not give any errors even though I have added this following code and tested on wrong css content.

    partial class CSS3Lexer
    {
        public override void ReportError(RecognitionException e)
        {
            base.ReportError(e);
            Console.WriteLine("Error in lexer at line " + e.Line + ":" + e.CharPositionInLine + e.Message);
        }
    }
    
  3. I want to collect the list of errors (parser and lexer errors) in to a some kind of data structure (list of error object which has error type, message, location) so that I can use them for another task. Is there a more meaningful way of doing this.

I would like to get suggestions on my approach as well because I am still unable to reach a more elegant design.

1
FYI, the code-block didn't format properly because it was placed inside a list-item. In such cases, it needs an extra 4 indentation spaces (8 in total).Bart Kiers
@BartKiers good to know that. thanks!!diyoda_

1 Answers

2
votes

ANTLR's only built-in error reporting mechanism is pretty simple, and doesn't provide a way to give error categories or numbers to particular errors. Often, all syntax errors that occur at parse time are given the same error number. For example, the ANTLR 4 tool reports parser errors as error 50.

After the initial parse is complete and you have a parse tree (ANTLR 4) or AST (ANTLR 3) available, you can continue to perform semantic evaluation. Errors that are identified from there on can be considered either errors or warnings depending on their overall impact. The data structures you use for this are sometime application-specific, such as a Visual Studio or NetBeans extension which needs to report the errors/warnings to specific UI components, but are otherwise free for you to define in whatever manner makes sense to you.