简体   繁体   English

Java库将正则表达式解析为语法树

[英]java library to parse regular expressions into a syntax tree

I'd like a library that can take the string representation of a regexp and convert that into a syntax tree for easy programmatic manipulation. 我想要一个可以使用正则表达式的字符串表示形式并将其转换为语法树以方便进行编程操作的库。 Something that would transform: 会改变的东西:

(\\s?)bla[az] (\\ s?)bla [az]

into something like: 变成类似:

PARENTHESIS
  CHAR:SPACE
    WILD
WORD:bla
CHAR:a-z

Looks like what you're looking for is a syntax parser, right? 看起来您正在寻找的是语法解析器,对吗?

I would give a look on antlr (http://www.antlr.org/), you can create grammars and it will generate a syntax tree which you can walk, translate etc. 我会看一下antlr(http://www.antlr.org/),您可以创建语法,它将生成一个语法树,您可以在其中行走,翻译等。

Parboiled looks like a good choice for what you want to make. 煮熟看起来像是您想要做的一个不错的选择。

It allows easy writing of grammars, way more easily than antlr or javacc. 与antlr或javacc相比,它可以轻松编写语法,更容易。

Sample: 样品:

Rule Digit()
{
    return CharRange('0', '9');
}

Rule Integer()
{
    return OneOrMore(Digit());
}

Rule WhiteSpace()
{
    return ZeroOrMore(AnyOf(" \t"));
}

Rule NToMQuantifier()
{
    return Sequence(
        '{',
        WhiteSpace(),
        Integer(),
        Optional(
            WhiteSpace(),
            Integer()
        ),
        '}'
    );
}

Rule OtherQuantifiers()
{
    return Sequence(AnyOf("+?*"), Optional(AnyOf("+?")));
}

Rule Quantifier()
{
    return FirstOf(OtherQuantifiers(), NToMQuantifier());
}

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

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