antlr4如何创建一个允许除两个选定字符以外的所有字符的正则表达式?

发布于 2025-01-25 18:09:40 字数 321 浏览 6 评论 0原文

嗨,例如,我为G4文件有此代码:

a: [A-Z][A-Z];
b: [a-z]'3';

现在我想添加更多行,它识别所有不属于

我尝试过的字符的字符:

a: [A-Z][A-Z];
b: [a-z]'3';
ALLOTHERCHARACTERS: ~[a]|~[b]

但是我没有工作。

例如,输入84209DDJIO29现在应该在Allothercaracter中,但我没有工作。

(Lexer最终给出了一个Java文件,但我认为这不重要,因为此“任务”)

Hi for example I have this code for the g4 file:

a: [A-Z][A-Z];
b: [a-z]'3';

Now I want to add one line more, which recognizes all characters that do not belong to a or b

I tried:

a: [A-Z][A-Z];
b: [a-z]'3';
ALLOTHERCHARACTERS: ~[a]|~[b]

But i didn´t work.

For example the input 84209ddjio29 should now be in ALLOTHERCARACTERS, but i didn ´t work.

(The Lexer gives at the end a java file, but I think this is not important to know, for this "task")

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

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

发布评论

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

评论(2

哆兒滾 2025-02-01 18:09:40

这里有很多问题:在解析器规则内,您无法使用字符集。因此,a:[az] [az];是不可能的。只有lexer规则可以使用字符集,因此a:[az] [az];是有效的。

因此,要定义有效的(Lexer)语法,您需要执行此操作:

A : [A-Z] [A-Z];
B : [a-z] '3';

现在解决第二个问题:如何否定规则a b ?答:你不能。您只能否定单个字符。因此,否定a:[az];将为na:〜[az];(或na:〜a;也有效)。但是您不能否定匹配2个字符的规则,例如a:[az] [az];

如果您想要一条匹配上大写字母以外的任何其他规则,较低的案例字母和数字3,那么您可以这样:

ALLOTHERCHARACTERS : ~[A-Za-z3];

There are many things going wrong here: inside parser rules, you cannot use character sets. So a: [A-Z][A-Z]; is not possible. Only a lexer rule can use character sets, so A: [A-Z][A-Z]; is valid.

So, to define a valid (lexer) grammar, you'd need to do this:

A : [A-Z] [A-Z];
B : [a-z] '3';

Now for your second problem: how to negate rules A and B? Answer: you cannot. You can only negate single characters. So negating A : [A-Z]; would be NA: ~[A-Z]; (or NA : ~A; is also valid). But you cannot negate a rule that matches 2 characters like A : [A-Z] [A-Z];.

If you want a rule that matches anything other than upper case letters, lower case letters and the digit 3, then you can so this:

ALLOTHERCHARACTERS : ~[A-Za-z3];
亢潮 2025-02-01 18:09:40

这是“任何事物”以外的适当语法:

[^ab]

因此,这将匹配任何不a或b的字符。

This is the proper syntax for "anything except":

[^ab]

so that will match any character that is not a or b.

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