ANTLR 不会在无效输入时发出错误消息

发布于 2024-12-21 08:24:20 字数 1029 浏览 4 评论 0原文

我开始学习ANTLR是为了将来实现领域特定语言(DSL)。我已经购买了 The Definitive ANTLR Reference 并开始阅读它,以便熟悉该程序以及语言/编译器设计的概念。我已经成功地让一切在我的环境(Visual Studio 2010 和 C#)中正常工作,并且我已经成功地能够创建一些基本语法,如本书中所述。 java 的源代码可以在这里找到: http://pragprog.com/titles/tpantlr/source_code

然而,在学习第 3 章时,我遇到了一个问题,涉及类不向控制台发出错误,正如书中所示。我使用的语法与本书中使用的语法相同,因此我假设它与 C# 运行时有关。我目前正在使用 ANTLRWorks 生成词法分析器/解析器,并且正在使用 CSharp3 的 3.4 ANTLR 分发运行时(Antlr3.Runtime.dll 和 Antlr4.StringTemplate.dll)。

这是一个已知问题吗?如果没有,我应该尝试使用旧版本的运行时或 ANTLRWorks 吗?

我使用的语法:

grammar Expr;

options
{
   language = CSharp3;
}

prog    :   stat+ ;

stat    :   expr NEWLINE
|   ID '=' expr NEWLINE
|       NEWLINE
;

expr    :   multExpr (('+'|'-') multExpr)*
;

multExpr 
:   atom('*' atom)*
;

atom    :   INT
|   ID
|   '(' expr ')'
;

ID  :   ('a'..'z'|'A'..'Z')+ ;
INT :   '0'..'9'+ ;
NEWLINE :   '\r'? '\n';
WS  :   (' '|'\t')+ {Skip();};

如果有人需要看的话,我会发布 C# 类,但它们很长,所以我会推迟,直到有人请求它们。谢谢。

I have begun learning ANTLR in order to implement a domain-specific language (DSL) in the future. I have purchased The Definitive ANTLR Reference and have begun working my way through it in order to familiarize myself with the program and the concepts of languages/compiler design. I have successfully gotten everything working within my environment (Visual Studio 2010 and C#), and I have successfully been able to create some basic grammars, as described throughout the book. Source code for java can be found here: http://pragprog.com/titles/tpantlr/source_code

However, while working through Chapter 3, I have come across a problem involving the classes not emitting errors to the console, as it shows in the book. I am using the same grammar that is used in the book, so I am assuming it has something to do with the C# runtimes. I am currently using ANTLRWorks to generate the lexer/parser, and I am using the 3.4 ANTLR distribution runtimes for CSharp3 (Antlr3.Runtime.dll and Antlr4.StringTemplate.dll).

Is this a known issue? If not, should I try using an older version of the runtimes or ANTLRWorks?

The grammar I am using:

grammar Expr;

options
{
   language = CSharp3;
}

prog    :   stat+ ;

stat    :   expr NEWLINE
|   ID '=' expr NEWLINE
|       NEWLINE
;

expr    :   multExpr (('+'|'-') multExpr)*
;

multExpr 
:   atom('*' atom)*
;

atom    :   INT
|   ID
|   '(' expr ')'
;

ID  :   ('a'..'z'|'A'..'Z')+ ;
INT :   '0'..'9'+ ;
NEWLINE :   '\r'? '\n';
WS  :   (' '|'\t')+ {Skip();};

I will post the C# classes if anyone needs to see them, but they are very long, so I will hold off until someone requests them. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

财迷小姐 2024-12-28 08:24:20

在对3.4.1.9004源代码进行一些调试后,我发现traceDestination属性为null。因此,我在 Main() 中设置了如下所示的属性,消息开始显示在控制台上。

parser.TraceDestination = Console.Out;

After some debugging on 3.4.1.9004 source code, I found that the traceDestination property was null. So, I set the property as shown below in Main() and messages started showing up on the console.

parser.TraceDestination = Console.Out;

十六岁半 2024-12-28 08:24:20

我只使用过 CSharp2 目标(和运行时),因为我无法使用 MonoDevelop 在我的 Ubuntu 机器上运行 CSharp3 目标。

当您使用 ANTLRWorks 时,org.antlr.Tool 用于生成 ExprLexer.csExprParser.cs 类,这些类是 CSharp3 运行时的如果我没记错的话,有(有时?)问题。

尝试使用 Antlr3.exe 工具生成 .cs 源文件:http://www.tunnelvisionlabs.com/downloads/antlr/antlr-dotnet-tool-3.4.1.9004.7z (请注意,tunnelvisionlabs.com 来自 CSharp3 目标的作者

由于我对 CSharp3 目标不熟悉,我还没有对此进行测试,但值得一试。

I've only worked with the CSharp2 target (and runtime) because I've not been able to get the CSharp3 target running on my Ubuntu-machine with MonoDevelop.

When you use ANTLRWorks, the org.antlr.Tool is used to generate the ExprLexer.cs and ExprParser.cs classes, which the CSharp3 runtime has (sometimes?) issues with, if memory serves me well.

Try generating the .cs source files with the Antlr3.exe tool instead: http://www.tunnelvisionlabs.com/downloads/antlr/antlr-dotnet-tool-3.4.1.9004.7z (note that tunnelvisionlabs.com is from the author of the CSharp3 target).

I haven't tested this, because of my unfamiliarity with the CSharp3 target, but it's worth a try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文