Need help to tree-ify nested parenthesis...

Discussion in 'Python' started by Gregory Nans, Aug 30, 2003.

  1. Gregory Nans

    Gregory Nans Guest

    hello,

    i need some help to 'tree-ify' a string...

    for example i have strings such as :

    s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i
    need))"""

    and i need to parse them to have a tree representation such as :

    ["A", [
    "here 's" ,
    [ "B", [
    'A ) "silly test'
    ]],
    ["C", [
    "to show",
    ["D", [
    "what kind"
    ]],
    "of stuff i need",
    ]]
    ]]

    notes : "ALLCAPS(something else)" are some kind of tag, "something
    else" may contain ALLCAPS() and thus be nested or contain others
    characters. the parenthesis between 'A' and 'silly test' isn't a
    mistake but a case to handle string is quite well formed = orphan
    parenthesis will always be surrounded by whitespace - like in 'A )
    silly test' or 'A ( silly test'

    if have started to write a function which give me pos and nested
    string, but still need help to finish to tree-ify it from there... or
    maybe there is a more obvious way ? (i think i didn't need to create
    real finite state automata for this stuff)

    thanks

    >>> parse(d)

    -1 D what kind 42 @ 51
    -1 C to show D(what kind) of stuff i need 32 @ 68
    -1 B A ) silly test 14 @ 28
    -1 A here 's , B(A ) silly test) C(to show D(what kind) of stuff i
    need) 2 @ 69


    -------------------%<------------------

    def parse(s):

    n = len(s)

    p = re.compile(r'([A-Z_]+\()')
    pos = []
    iterator = p.finditer(s)
    for match in iterator:
    pos.append(match.span())

    j = 0
    pos.reverse()
    for (start, end) in pos:
    opened = 0
    i = end
    while i < n - j:
    if s == '(':
    opened += 1
    if s == ')' and s[i - 1] != ' ':
    opened -= 1
    if opened < 0:
    print opened, s[start:end-1], s[end:i], "%s @ %s" % (end,
    i)
    break
    i += 1
     
    Gregory Nans, Aug 30, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. .Net Sports
    Replies:
    4
    Views:
    2,157
    Eliyahu Goldin
    Jun 29, 2005
  2. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,244
  3. Replies:
    3
    Views:
    516
  4. Phil Powell
    Replies:
    1
    Views:
    135
    Randy Webb
    Jun 15, 2006
  5. C++ify tag

    , Jun 6, 2014, in forum: C++
    Replies:
    6
    Views:
    129
    Öö Tiib
    Jun 9, 2014
Loading...

Share This Page