Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > parser question

Reply
Thread Tools

parser question

 
 
Daniel Schüle
Guest
Posts: n/a
 
      10-19-2005
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


 
Reply With Quote
 
 
 
 
Daniel Schüle
Guest
Posts: n/a
 
      10-19-2005
> 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


 
Reply With Quote
 
 
 
 
Micah Elliott
Guest
Posts: n/a
 
      10-19-2005
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 http://www.velocityreviews.com/forums/(E-Mail Removed)
" " """
 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      10-19-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
import parser does not import parser.py in same dir on win Joel Hedlund Python 2 11-11-2006 03:46 PM
import parser does not import parser.py in same dir on win Joel Hedlund Python 0 11-11-2006 11:34 AM
XML Parser VS HTML Parser ZOCOR Java 11 10-05-2004 01:58 PM
XMLparser: Difference between parser.setErrorHandler() vs. parser.setContentHandler() Bernd Oninger Java 0 06-09-2004 01:26 AM
XMLparser: Difference between parser.setErrorHandler() vs. parser.setContentHandler() Bernd Oninger XML 0 06-09-2004 01:26 AM



Advertisments