简体   繁体   English

检测错误的语法(错误替代)以进行快速修复

[英]Detect wrong grammar (error alternatives) for quick fixes

In my ANTLR grammar I would like to detect wrong keywords or wrong typed constants, eg 'null' instead of 'NULL'. 在我的ANTLR语法中,我想检测错误的关键字或错误的类型常量,例如“ null”而不是“ NULL”。 I added error alternatives in the grammar, eg: 我在语法中添加了错误替代,例如:

|   extra='null' #error1

If the wrong constant is detected in my custom editor I can fix it by replacing it with the correct constant. 如果在我的自定义编辑器中检测到错误的常数,则可以通过用正确的常数替换它来修复它。

But I don't know if this is the correct way to address and detect wrong keywords or constants in a grammar. 但是我不知道这是否是解决和检测语法错误的关键字或常量的正确方法。

In addition I tried to detect missing closing in the grammar (see ANTLR book chapter 9.4): 另外,我尝试检测语法中缺少的结尾(请参阅ANTLR书的第9.4章):

.......
|   'if' '(' expr  expr #error2
|   '{'  exprlist #error3
|   'while' '(' expr  expr #error4
........

But this massively slows down the parsing process and so I think that it is wrong to do so. 但这极大地减慢了解析过程,因此我认为这样做是错误的。

My questions are: 我的问题是:

Is it correct to detect wrong keywords, constants, etc. in that way as described above? 如上所述以这种方式检测错误的关键字,常量等是否正确?

Is it somehow possible to catch missing closing in the grammar without the massive speed decrease? 在不大幅降低速度的情况下,是否有可能以某种方式捕获语法中缺少的结尾?

Any help is appreciated. 任何帮助表示赞赏。

I found out that I can extract the second Token from the rule (to add a brace at that position): 我发现我可以从规则中提取第二个令牌(在该位置添加大括号):

|   'if' '(' expr  expr #error2

with: 与:

Token myToken = tokens.get(ctx.getChild(2).getSourceInterval().b);
parser.notifyErrorListeners(........

However it massively decreases the speed of the parser. 但是,它大大降低了解析器的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM