Recursive regexps?

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri Nov 19 17:37:06 EST 2004


"Magnus Lie Hetland" <mlh at furu.idi.ntnu.no> wrote in message
news:slrncpsqp4.o00.mlh at furu.idi.ntnu.no...
> It seems there are a *couple* of (experimental) PCRE features that
> allow recursive matching:
>
>   [T]here is some experimental support for recursive patterns using
>   the non-Perl items (?R), (?number) and (?P>name). Also, the PCRE
>   "callout" feature allows an external function to be called during
>   pattern matching.
>
>   [http://www.slabihoud.de/spampal/pcrecompat.html]
>
> All of these seem pretty useful/neat :-}
>
> -- 
> Magnus Lie Hetland       Fallen flower I see / Returning to its branch
> http://hetland.org       Ah! a butterfly.           [Arakida Moritake]

For those who find regexp strings to be fairly opaque, pyparsing supports
both of these features.
- Recursive grammars are defined using the Forward() parsing class.  See
this snippet from the fourFn.py infix notation parser:

        addop  = plus | minus
        multop = mult | div
        pi    = CaselessLiteral( "PI" )

        expr = Forward()
        factor = ( pi | e | fnumber ).setParseAction( pushFirst ) | ( "(" +
expr.suppress() + ")" )
        term = factor + ZeroOrMore( ( multop + factor ).setParseAction(
pushFirst ) )
        expr << term + ZeroOrMore( ( addop + term ).setParseAction(
pushFirst ) )

expr is defined to be a Forward() instance, and then the recursive contents
are inserted using the "<<" operator.

- Note also the calls to setParseAction().  These represent callouts to
external functions to be invoked during the parsing process.  These external
functions can modify the matched text, update global data structures, or
even supersede the matching rules and raise a ParseException, thereby
cancelling the match.  In this case, the expression elements are being
pushed onto an expression stack, to be recursively evaluated after parsing
is complete.

-- Paul
Download pyparsing at http://pyparsing.sourceforge.net.





More information about the Python-list mailing list