A parser for regular expressions in PHP?

后端 未结 6 1476
忘了有多久
忘了有多久 2021-02-04 01:28

I need to parse regular expressions into their components in PHP. I have no problem creating the regular expressions or executing them, but I want to display information about t

6条回答
  •  星月不相逢
    2021-02-04 01:51

    What you need is a grammar and a way to generate a parser for it. The easiest approach to producing a parser is to code a recursive descent directly in your target language (e.g., in PHP), in which you build a clean parser that is shaped exactly like your grammar (which makes the parser maintainable, too).

    Lots of details on how do to this, once you have a grammar, are provided in my SO description of how to build recursive descent parsers and additional theory details here

    As for regex grammars, a simple grammar (maybe not the one you had in mind) is:

    REGEX =  ALTERNATIVES ;
    ALTERNATIVES = TERM ( '|' TERM )* ;
    TERM = '(' ALTERNATIVES ')' |  CHARACTER | SET | TERM ( '*' | '+' | '?' ) ;
    SET = '~' ? '[' ( CHARACTER | CHARACTER '-' CHARACTER )* ']' ;
    CHARACTER = 'A' | 'B' | ... | '0' ... '9' | ...  ;
    

    A recursive descent parser written in PHP to process this grammar should be on the order of few hundred lines, max.

    Given this as a starting place, you should be able to add the features of PHP Regexes to it.

    Happy parsing!

提交回复
热议问题