gpt4 book ai didi

How to fix an error in Antlr4-generated Lexer class?(如何修复Antlr4生成的Lexer类中的错误?)

转载 作者:bug小助手 更新时间:2023-10-24 21:08:17 25 4
gpt4 key购买 nike



I am using antlr-4.13.0-complete.jar to generate C# parser and lexer classes for the grammar. Then I am using these classes in C# console app, with Nuget Package Antlr4.Runtime.Standard, version 4.13.0.
I am getting compile-time error in the lexer class:

我使用antlr-4.13.0-Complete e.jar为语法生成C#解析器和词法分析器类。然后,我在C#控制台应用程序中使用这些类,带有Nuget包Antlr4.Runime.Standard,版本4.13.0。我在Lexer类中遇到编译时错误:


    private void DATE_action(RuleContext _localctx, int actionIndex) {
switch (actionIndex) {
case 0: 4 break;
}
}

Error CS1002 ; expected

It points to the column after "4"

错误CS1002;应指向“4”之后的列


To generate the c# classes I am using following bat file, specifying my grammar as argument:

要生成我正在使用的C#类,请使用以下BAT文件,并将我的语法指定为参数:


java -jar d:\Libraries\Antlr4\4130\antlr-4.13.0-complete.jar -Dlanguage=CSharp %*

My grammar:

我的语法是:


schedule: (dateSpec | exceptionSpec)*;

...
DATE: DAY '/' MONTH '/' [0-9]{4};
HOUR: ('0' [0-9] | '1' [0-9] | '2' [0-3]);
MINUTE: [0-5] [0-9];
DAY: [1-9] | '1' [0-9] | '2' [0-9] | '3' [0-1];
MONTH: ('0' [1-9] | '1' [0-2]);

The question is:
What to do in this situation, correct the error manually, or report it here? May be something else?
Thanks!

问题是:在这种情况下该怎么做,手动更正错误,还是在这里报告?可能是别的什么?谢谢!


更多回答

Please edit your question to include de antlr rule(s) that generate this class

请编辑您的问题以包括生成此类的德安特尔规则(S

I edited the question, added the grammar and the command that I use to generate the classes

我编辑了问题,添加了用于生成类的语法和命令

It's been a while since I've done any antlr, but this seems not right to me: [0-9]{4} this looks like a regex not an antlr rule

我已经有一段时间没有做任何反操作了,但这对我来说似乎不太对:[0-9]{4}这看起来像是一个正则表达式,而不是反规则

Note, your grammar will not work for other reasons. DAY and MONTH overlap in recognition, so '1/1/2023' will not tokenize correctly: '1' tokenizes to DAY. Antlr lexers follow two basic rules: (1) The rule that matches the longest input "wins"--regardless of how the lexer rules are ordered, regardless of how you parser is trying to parse. Lexers work independently from the parser. (2) If two or more rules match the same input, then the first one listed in the .g4 "wins".

请注意,由于其他原因,您的语法将不起作用。日和月在识别中重叠,因此‘1/1/2023’将不会正确地标记化:‘1’标记化到日。Antlr词法分析器遵循两个基本规则:(1)匹配最长输入“WINS”的规则--不管词法分析器规则是如何排序的,也不管解析器试图如何解析。词法分析器独立于解析器工作。(2)如果两个或多个规则匹配相同的输入,则.g4中列出的第一个规则“获胜”。

优秀答案推荐


DATE: DAY '/' MONTH '/' [0-9]{4};


Curly braces don't signify repetition in ANTLR, they signify actions, i.e. code that should be executed when the rule is matched. So in this case you're saying the code 4 should be executed when the DATE rule is matched, which is why the 4 ends up in the generated C# code. But 4 by itself is not a legal C# statement and that's why you get a syntax error.

大括号并不表示ANTLR中的重复,它们表示操作,即当规则匹配时应该执行的代码。因此,在本例中,您的意思是代码4应该在日期规则匹配时执行,这就是为什么4在生成的C#代码中结束。但是4本身不是合法的C#语句,这就是为什么会出现语法错误的原因。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com