By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

parser question

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
> 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

P: n/a
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

P: n/a
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.