简体   繁体   English

用于Java正则表达式语法的ANTLR语法

[英]ANTLR Grammar for Java Regular Expression syntax

I'm currently working on a testing framework for regular expressions, and I need to be able to parse Java regular expressions into ASTs to be able to generate sample strings which match the given regex. 我目前正在研究正则表达式的测试框架,我需要能够将Java正则表达式解析为AST,以便能够生成与给定正则表达式匹配的样本字符串。

I looked at the implementation of java.util.regex.Pattern but the code looks quite unwieldy (the emphasis was on speed over readability I would imagine), so I decided to use ANTLR to write a regex parser from scratch. 我查看了java.util.regex.Pattern的实现,但代码看起来相当笨重(重点在于速度超过我想象的可读性),所以我决定使用ANTLR从头开始编写正则表达式解析器。

I'm wondering if anyone knows of a ready ANTLR grammar for regex, before I start bashing one out myself. 我想知道是否有人知道正则表达式的准备好的ANTLR语法,然后我开始抨击我自己。

There's a similar question here: 这里有一个类似的问题:

Regex Grammar 正则表达式语法

The answers contain links to assorted grammars, none of them ANTLR ones. 答案包含各种语法的链接,其中没有一个是ANTLR语法。 I suspect that if you translated the BNF one to ANTLR, you'd be plagued by left-recursion headaches but it's worth a try, you might get lucky. 我怀疑,如果你将BNF翻译成ANTLR,你会被左旋递头痛困扰,但值得一试,你可能会很幸运。

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

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