parser question

Discussion in 'Python' started by Daniel Schüle, Oct 19, 2005.

  1. Hello *,

    I have one format with this structure

    A {
    x=1
    y=3
    B {
    z = "something here"
    }
    }

    C {
    }

    A {
    x=0
    y=0
    B {
    z = "other"
    }
    }

    are there parsers for this kind of structure?
    specially I am interested in all A blocks

    just now, I am trying to solve it with re,

    pattern = re.compile(r"^\s*A\s*{.*}\s*", re.MULTILINE | re.DOTALL)

    A_blocks = pattern.findall( file("myFile").read() )

    but since there might be subblocks it will fail finding the closing "}"
    character

    Regards, Daniel
    Daniel Schüle, Oct 19, 2005
    #1
    1. Advertising

  2. > pattern = re.compile(r"^\s*A\s*{.*}\s*", re.MULTILINE | re.DOTALL)

    correction

    pattern = re.compile(r"^\s*A\s*{.*?}\s*", re.MULTILINE | re.DOTALL)

    I forgot to make it non gready :)
    Daniel Schüle, Oct 19, 2005
    #2
    1. Advertising

  3. On Oct 19, Daniel Sch�le wrote:
    > I have one format with this structure
    >
    > A {
    > x=1
    > y=3
    > B {
    > z = "something here"
    > }
    > }
    >
    > C {
    > }
    >
    > A {
    > x=0
    > y=0
    > B {
    > z = "other"
    > }
    > }
    >
    > are there parsers for this kind of structure?
    > specially I am interested in all A blocks
    >
    > just now, I am trying to solve it with re,


    You might be able to tackle this easily enough with REs if your
    structures don't nest arbitrarily. It's hard to tell if this deserves
    a formal grammar based on the example. If it does, you could try PLY
    <http://www.dabeaz.com/ply/> (which I've had a pleasant experience
    with in the past) or any of the other parsers listed on the PLY site's
    "Other Python Parsing Tools".

    --
    _ _ ___
    |\/|icah |- lliott http://micah.elliott.name
    " " """
    Micah Elliott, Oct 19, 2005
    #3
  4. Daniel Schüle

    Kent Johnson Guest

    Micah Elliott wrote:
    > You might be able to tackle this easily enough with REs if your
    > structures don't nest arbitrarily. It's hard to tell if this deserves
    > a formal grammar based on the example. If it does, you could try PLY
    > <http://www.dabeaz.com/ply/> (which I've had a pleasant experience
    > with in the past) or any of the other parsers listed on the PLY site's
    > "Other Python Parsing Tools".


    A more complete list is here:
    http://www.nedbatchelder.com/text/python-parsers.html

    I have found pyparsing easy to work with.

    Kent
    Kent Johnson, Oct 19, 2005
    #4
    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. Bernd Oninger
    Replies:
    0
    Views:
    756
    Bernd Oninger
    Jun 9, 2004
  2. ZOCOR

    XML Parser VS HTML Parser

    ZOCOR, Oct 3, 2004, in forum: Java
    Replies:
    11
    Views:
    810
    Paul King
    Oct 5, 2004
  3. Bernd Oninger
    Replies:
    0
    Views:
    810
    Bernd Oninger
    Jun 9, 2004
  4. Joel Hedlund
    Replies:
    2
    Views:
    506
    Joel Hedlund
    Nov 11, 2006
  5. Joel Hedlund
    Replies:
    0
    Views:
    304
    Joel Hedlund
    Nov 11, 2006
Loading...

Share This Page