如何匹配字符串,但不区分大小写?
假设我想匹配“beer”,但不关心大小写。
目前我将一个标记定义为 ('b'|'B' 'e'|'E' 'e'|'E' 'r'|'R') 但我有很多这样的标记,但实际上并没有想要处理“确实这是非常长的令牌,确实是肌肉炎”。
antlr 维基 似乎表明它无法完成(在antlr中)...但我只是想知道是否有人有一些聪明的技巧...
Let's say that I want to match "beer", but don't care about case sensitivity.
Currently I am defining a token to be ('b'|'B' 'e'|'E' 'e'|'E' 'r'|'R') but I have a lot of such and don't really want to handle 'verilythisisaverylongtokenindeedomyyesitis'.
The antlr wiki seems to suggest that it can't be done (in antlr) ... but I just wondered if anyone had some clever tricks ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想添加已接受的答案:可以在 不区分大小写的antlr构建块,为了方便起见,下面包含相关部分
所以一个例子是
I would like to add to the accepted answer: a ready -made set can be found at case insensitive antlr building blocks, and the relevant portion included below for convenience
So an example is
如何为每个允许的标识符字符定义一个词法分析器标记,然后将解析器标记构造为一系列这些标记?
ETC。
How about define a lexer token for each permissible identifier character, then construct the parser token as a series of those?
etc.
ANTLR 刚刚添加了不区分大小写的选项
https:// github.com/antlr/antlr4/blob/master/doc/options.md#caseinsensitive
旧链接现已损坏,这些应该可以继续工作。
A case-insensitive option was just added to ANTLR
https://github.com/antlr/antlr4/blob/master/doc/options.md#caseinsensitive
The old links are now broken, these should continue to work.
定义不区分大小写的标记
Define case-insensitive tokens with
新的文档页面已出现在 ANTLR GitHub 存储库中: Case-不敏感的词法分析。您可以使用两种方法:
我认为,最好使用第一种方法并拥有描述所有规则的语法。但如果您使用众所周知的语法,例如来自 为 ANTLR v4 编写的语法,那么第二个方法可能更合适。
New documentation page has appeared in ANTLR GitHub repo: Case-Insensitive Lexing. You can use two approaches:
My opinion, it's better to use the first approach and have the grammar which describes all the rules. But if you use well-known grammar, for example from Grammars written for ANTLR v4, then second approach may be more appropriate.
我使用antlr-4.7.1-complete.jar生成TrinoSqlParser java代码,但发现“警告(83):TrinoLexer.g4:22:4:不支持的选项caseInsensitive”。
因此,我尝试使用 com.facebook.presto.sql.parser.CaseInsensitiveStream 来包装 charstream,然后它就可以工作了。看起来
CaseInsensitiveStream
只是将小写转换为大写。我的代码如下:
I use antlr-4.7.1-complete.jar to generate TrinoSqlParser java code, but found "warning(83): TrinoLexer.g4:22:4: unsupported option caseInsensitive".
So, I try to use
com.facebook.presto.sql.parser.CaseInsensitiveStream
to wrap thecharstream
, then it works. Seems like thatCaseInsensitiveStream
just transform lowercase to upppercase.My code is below:
我在 C# 中使用的解决方案:使用 ASCII 代码将字符转换为较小的大小写。
A solution I used in C#: use ASCII code to shift character to smaller case.