473,246 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Parsing nested constructs

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.
Sep 8 '07 #1
5 1295
On 9/8/07, tool69 <ki**@free.frwrote:
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 "}"
Sep 8 '07 #2
David a écrit :
On 9/8/07, tool69 <ki**@free.frwrote:
>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
Sep 8 '07 #3
On Sep 8, 3:42 pm, tool69 <k...@free.frwrote:
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

Sep 9 '07 #4
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
Sep 9 '07 #5
On Sep 8, 10:01 pm, Wildemar Wildenburger
<lasses_w...@klapptsowieso.netwrote:
>
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

Sep 9 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Viktor Rosenfeld | last post by:
Hi, I need to create a parser for a Python project, and I'd like to use process kinda like lex/yacc. I've looked at various parsing packages online, but didn't find anything useful for me: -...
0
by: Dean H. Saxe | last post by:
I'm currently developing a tool in perl to search out potential XSS (Cross Site Scripting) vulnerabilities and correct them in a ColdFusion based web app. I've been having great success so far,...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
12
by: Klaus Alexander Seistrup | last post by:
Hi group, I am new to xgawk (and seemingly to xml also), and I've been struggling all afternoon to have xgawk¹ parsing an XHTML file containing a hCard², without luck. I wonder if you guys...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
6
by: James Arnold | last post by:
Hello, I am new to C and I am trying to write a few small applications to get some hands-on practise! I am trying to write a random string generator, based on a masked input. For example, given...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.