parser question

D

Daniel Schüle

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
 
D

Daniel Schüle

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 :)
 
M

Micah Elliott

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".
 
K

Kent Johnson

Micah said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top