468,293 Members | 1,858 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,293 developers. It's quick & easy.

parser question

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
Oct 19 '05 #1
3 1314
> 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 :)
Oct 19 '05 #2
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 md*@micah.elliott.name
" " """
Oct 19 '05 #3
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
Oct 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by annoyingmouse2002 | last post: by
2 posts views Thread by friend.05 | last post: by
4 posts views Thread by siddharthkhare | last post: by
6 posts views Thread by 31337one | last post: by
28 posts views Thread by Marc Gravell | last post: by
18 posts views Thread by Just Another Victim of the Ambient Morality | last post: by
3 posts views Thread by ups_genius | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.