Parsing nested constructs

2007-09-08 Thread tool69
Hi,

I need to parse some source with nested parenthesis, like this :

 cut-

{
 {item1}
 {
  {item2}
  {item3}
 }
}

 cut-


In fact I'd like to get all start indexes of items and their end (or 
lenght).

I know regexps are rather limited for this type of problems.
I don't need an external module.

What would you suggest me ?

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing nested constructs

2007-09-08 Thread David
On 9/8/07, tool69 [EMAIL PROTECTED] wrote:
 Hi,

 I need to parse some source with nested parenthesis, like this :


If this is exactly how your data looks, then how about a loop which
searches for {item and the following }? You can use the find
string method for that.

Otherwise, if the items don't look exactly like {item, but the
formatting is otherwise exactly the same as above, then look for lines
that have both { and } on them, then get the string between them.

Otherwise, if { and } and the item aren't always on the same line,
then go through the string character by character, keep track of when
you encounter { and }. When you encounter a } and there was a
{ before (ie, not a }), then get the string between the { and
the }
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing nested constructs

2007-09-08 Thread tool69
David a écrit :
 On 9/8/07, tool69 [EMAIL PROTECTED] wrote:
 Hi,

 I need to parse some source with nested parenthesis, like this :

 
 If this is exactly how your data looks, then how about a loop which
 searches for {item and the following }? You can use the find
 string method for that.
 
 Otherwise, if the items don't look exactly like {item, but the
 formatting is otherwise exactly the same as above, then look for lines
 that have both { and } on them, then get the string between them.
 
 Otherwise, if { and } and the item aren't always on the same line,
 then go through the string character by character, keep track of when
 you encounter { and }. When you encounter a } and there was a
 { before (ie, not a }), then get the string between the { and
 the }
Hi David,

thanks for answering, I will choose the last one as my strings are not 
on the same line and may contain a lot of stuff inside.

cheers,

6TooL9
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing nested constructs

2007-09-08 Thread Paul McGuire
On Sep 8, 3:42 pm, tool69 [EMAIL PROTECTED] wrote:
 Hi,

 I need to parse some source with nested parenthesis, like this :

  cut-

 {
  {item1}
  {
   {item2}
   {item3}
  }

 }

  cut-

 In fact I'd like to get all start indexes of items and their end (or
 lenght).

 I know regexps are rather limited for this type of problems.
 I don't need an external module.

 What would you suggest me ?

 Thanks.

Well, it is an external module, but pyparsing makes this pretty
straightforward:

from pyparsing import *

data = 
{
 {item1}
 {
  {item2}
  {item3}
 }

}


# define delimiters, but suppress them from the output
LBRACE,RBRACE = map(Suppress,{})

# forward define recursive items list
items = Forward()

# items is zero or more words of alphas and numbers, or an embedded
# group enclosed in braces
items  ZeroOrMore( Word(alphanums) | Group( LBRACE + items +
RBRACE ) )

# parse the input string, and print out the results
print items.parseString(data)


prints:
[[['item1'], [['item2'], ['item3'

or:
[
[
['item1'],
[
['item2'],
['item3']
]
]
]


-- Paul

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing nested constructs

2007-09-08 Thread Wildemar Wildenburger
Paul McGuire wrote:
 Well, it is an external module, but pyparsing makes this pretty
 straightforward:
 
 [snip delightful parsing]
 
Again pyparsing to the rescue :)

I have to do a parsing project in Java right now and I dearly miss 
pyparsing. I explained it to the guy I'm working for, and he was pretty 
impressed.

Thought that might make you smile.

/W
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing nested constructs

2007-09-08 Thread Paul McGuire
On Sep 8, 10:01 pm, Wildemar Wildenburger
[EMAIL PROTECTED] wrote:

 Again pyparsing to the rescue :)

 I have to do a parsing project in Java right now and I dearly miss
 pyparsing. I explained it to the guy I'm working for, and he was pretty
 impressed.

 Thought that might make you smile.

 /W

Wildemar -

Thanks for such a glowing testimonial! It really is a boost of
encouragement when I find projects that are using pyparsing, or see
postings on c.l.py or the tutor list (by people other than me!)
recommending using pyparsing in response to someone's post.

I'm glad you find pyparsing so useful in your Python endeavors.

-- Paul

-- 
http://mail.python.org/mailman/listinfo/python-list