Useful RE patterns (was: Variable Interpolation - status of PEP215)

Pekka Niiranen krissepu at vip.fi
Wed Jul 3 12:11:12 EDT 2002


I know they are two different things, but you could provide additional
tools/fixes
into re-ex module to assist parser builders:

1) equal (a, b , string)
    Returns true is there are equal amount of non-overlapping strings
"a" and "b"
    in "string" to be scanned. Use: parsing nested structures. Parsing
is useless
    if text's limiters do not add up. Examples:
        equal ("(", ")", "aa(AA(BB))aa")    returns True
        equal ("(", ")", "aa(AA(BB)aa")     returns False

2) findall with parameter to control whether empty matches are returned.


3) recursive findall in C. Sketch in Python (not quite working, I know):

    result = []
    parameter = re.findall("[(].*[)]", text)
    if count("(", text) > 1 and equal("(", ")", text) :
    # run findall to all parameters that have (- and ) -sign stripped
(para[1:-1])

-pekka-

Fredrik Lundh wrote:

> Pekka Niiranen wrote:
> > Please make also example of recursive decent
> > parser, for example LL(k)
>
> regular expressions and parsers are two different things.
>
> you can use regexps to implement the scanner part of a
> parser (see e.g. http://effbot.org/guides/xml-scanner.htm),
> but they quickly break down if you try to do something
> more sophisticated.
>
> </F>




More information about the Python-list mailing list