1

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.

4

1 回答 1

2

ANTLR 唯一的内置错误报告机制非常简单,并且没有提供一种方法来为特定错误提供错误类别或编号。通常,在解析时发生的所有语法错误都被赋予相同的错误编号。例如,ANTLR 4 工具将解析器错误报告为错误 50。

在初始解析完成并且您有可用的解析树 (ANTLR 4) 或 AST (ANTLR 3) 后,您可以继续执行语义评估。从那里识别的错误可以被视为错误或警告,具体取决于它们的整体影响。您为此使用的数据结构有时是特定于应用程序的,例如需要向特定 UI 组件报告错误/警告的 Visual Studio 或 NetBeans 扩展,但您可以以任何对您有意义的方式自由定义。

于 2013-07-24T11:22:26.027 回答