antlr4如何创建一个允许除两个选定字符以外的所有字符的正则表达式?
嗨,例如,我为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有很多问题:在解析器规则内,您无法使用字符集。因此,
a:[az] [az];
是不可能的。只有lexer规则可以使用字符集,因此a:[az] [az];
是有效的。因此,要定义有效的(Lexer)语法,您需要执行此操作:
现在解决第二个问题:如何否定规则
a
b ?答:你不能。您只能否定单个字符。因此,否定a:[az];
将为na:〜[az];
(或na:〜a;
也有效)。但是您不能否定匹配2个字符的规则,例如a:[az] [az];
。如果您想要一条匹配上大写字母以外的任何其他规则,较低的案例字母和数字3,那么您可以这样:
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, soA: [A-Z][A-Z];
is valid.So, to define a valid (lexer) grammar, you'd need to do this:
Now for your second problem: how to negate rules
A
andB
? Answer: you cannot. You can only negate single characters. So negatingA : [A-Z];
would beNA: ~[A-Z];
(orNA : ~A;
is also valid). But you cannot negate a rule that matches 2 characters likeA : [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:
这是“任何事物”以外的适当语法:
因此,这将匹配任何不a或b的字符。
This is the proper syntax for "anything except":
so that will match any character that is not a or b.