473,395 Members | 1,473 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,395 software developers and data experts.

pyparsing

Hello !

I am trying to understand pyparsing. Here is a little test program
to check Optional subclass:

from pyparsing import Word,nums,Literal,Optional

lbrack=Literal("[").suppress()
rbrack=Literal("]").suppress()
ddot=Literal(":").suppress()
start = Word(nums+".")
step = Word(nums+".")
end = Word(nums+".")

sequence=lbrack+start+Optional(ddot+step)+ddot+end +rbrack

tokens = sequence.parseString("[0:0.1:1]")
print tokens

tokens1 = sequence.parseString("[1:2]")
print tokens1

It works on tokens, but the error message is showed on
the second string ("[1:2]"). I don't get it. I did use
Optional for ddot and step so I guess they are optional.

Any hints what I am doing wrong?

The versions are pyparsing 1.1.2 and Python 2.3.3.

Thanks,

B.
Jul 18 '05 #1
4 2208
On Thu, 13 May 2004 08:05:32 +0200, bo***********@mf.uni-lj.si
(Boštjan Jerko) wrote:
Hello !

I am trying to understand pyparsing. Here is a little test program
to check Optional subclass:

from pyparsing import Word,nums,Literal,Optional

lbrack=Literal("[").suppress()
rbrack=Literal("]").suppress()
ddot=Literal(":").suppress()
start = Word(nums+".")
step = Word(nums+".")
end = Word(nums+".")

sequence=lbrack+start+Optional(ddot+step)+ddot+en d+rbrack

tokens = sequence.parseString("[0:0.1:1]")
print tokens

tokens1 = sequence.parseString("[1:2]")
print tokens1

It works on tokens, but the error message is showed on
the second string ("[1:2]"). I don't get it. I did use
Optional for ddot and step so I guess they are optional.

Any hints what I am doing wrong?

The versions are pyparsing 1.1.2 and Python 2.3.3.

Thanks,

B.

I don't see anything "obviously" wrong to me, but changing it thusly
seems to resolve the problem (I added a few intermediate rules to
make it more obvious):

pref = lbrack + start
midf = ddot + step
suff = ddot + end + rbrack
sequence = pref + midf + suff | pref + suff

I've run into "this kind of thing" now and again, and have always
been able to resolve it by reorganizing my rules.

--dang
Jul 18 '05 #2
"Boštjan Jerko" <bo***********@mf.uni-lj.si> wrote in message
news:87************@bostjan-pc.mf.uni-lj.si...
Hello !

I am trying to understand pyparsing. Here is a little test program
to check Optional subclass:

from pyparsing import Word,nums,Literal,Optional

lbrack=Literal("[").suppress()
rbrack=Literal("]").suppress()
ddot=Literal(":").suppress()
start = Word(nums+".")
step = Word(nums+".")
end = Word(nums+".")

sequence=lbrack+start+Optional(ddot+step)+ddot+end +rbrack

tokens = sequence.parseString("[0:0.1:1]")
print tokens

tokens1 = sequence.parseString("[1:2]")
print tokens1

It works on tokens, but the error message is showed on
the second string ("[1:2]"). I don't get it. I did use
Optional for ddot and step so I guess they are optional.

Any hints what I am doing wrong?

The versions are pyparsing 1.1.2 and Python 2.3.3.

Thanks,

B.

Bostjan -

Here's how pyparsing is processing your input strings:

[0:0.1:1]
[ = lbrack
0 = start
:0.1 = ddot + step (Optional match)
: = ddot
1 = end
] = rbrack

[1:2]
[ = lbrack
1 = start
:2 = ddot + step (Optional match)
] = oops! expected ddot -> failure
Dang Griffith proposed one alternative construct, here's another, perhaps
more explicit:
lbrack + ( ( ddot + step + ddot + end ) | (ddot + end) ) + rbrack

Note that the order of the inner construct is important, so as to not match
ddot+end before trying ddot+step+ddot+end; '|' is a greedy matching
operator, creating a MatchFirst object from pyparsing's class library. You
could avoid this confusion by using '^', which generates an Or object:
lbrack + ( (ddot + end) ^ ( ddot + step + ddot + end ) ) + rbrack
This will evaluate both subconstructs, and choose the longer of the two.

Or you can use another pyparsing helper, the delimited list
lbrack + delimitedlist( Word(nums+"."), delim=":") + rbrack
This implicitly suppresses delimiters, so that all you will get back are
["1","0.1","1"] in the first case and ["1","2"] in the second.

Happy pyparsing!
-- Paul
Jul 18 '05 #3
> Dang Griffith proposed one alternative construct, here's another, perhaps
more explicit:
lbrack + ( ( ddot + step + ddot + end ) | (ddot + end) ) + rbrack

should be:
lbrack + start + ( ( ddot + step + ddot + end ) | (ddot + end) ) +
rbrack
Note that the order of the inner construct is important, so as to not match ddot+end before trying ddot+step+ddot+end; '|' is a greedy matching
operator, creating a MatchFirst object from pyparsing's class library. You could avoid this confusion by using '^', which generates an Or object:
lbrack + ( (ddot + end) ^ ( ddot + step + ddot + end ) ) + rbrack
should be:
lbrack + start + ( (ddot + end) ^ ( ddot + step + ddot + end ) ) +
rbrack
This will evaluate both subconstructs, and choose the longer of the two.

Or you can use another pyparsing helper, the delimited list
lbrack + delimitedlist( Word(nums+"."), delim=":") + rbrack
at least this one is correct! No, wait, I mis-cased delimitedList!
should be:
lbrack + delimitedList( Word(nums+"."), delim=":") + rbrack
This implicitly suppresses delimiters, so that all you will get back are
["1","0.1","1"] in the first case and ["1","2"] in the second.

Happy pyparsing!
-- Paul

Sorry for the sloppiness,
-- Paul
Jul 18 '05 #4
Paul,

thanks for the explanation.

Boštjan

On Fri, 14 May 2004, pt***@austin.rr._bogus_.com spake:
Dang Griffith proposed one alternative construct, here's another, perhaps
more explicit:
lbrack + ( ( ddot + step + ddot + end ) | (ddot + end) ) +
rbrack


should be:
lbrack + start + ( ( ddot + step + ddot + end ) | (ddot + end)
) +
rbrack
Note that the order of the inner construct is important, so as to
not

match
ddot+end before trying ddot+step+ddot+end; '|' is a greedy matching
operator, creating a MatchFirst object from pyparsing's class
library.

You
could avoid this confusion by using '^', which generates an Or
object: lbrack + ( (ddot + end) ^ ( ddot + step + ddot + end )
) + rbrack


should be:
lbrack + start + ( (ddot + end) ^ ( ddot + step + ddot + end )
) +
rbrack
This will evaluate both subconstructs, and choose the longer of the
two.

Or you can use another pyparsing helper, the delimited list
lbrack + delimitedlist( Word(nums+"."), delim=":") + rbrack


at least this one is correct! No, wait, I mis-cased delimitedList!
should be:
lbrack + delimitedList( Word(nums+"."), delim=":") + rbrack
This implicitly suppresses delimiters, so that all you will get
back are ["1","0.1","1"] in the first case and ["1","2"] in the
second.

Happy pyparsing!
-- Paul

Sorry for the sloppiness,
-- Paul

Jul 18 '05 #5

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

Similar topics

5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
4
by: the.theorist | last post by:
Hey, I'm trying my hand and pyparsing a log file (named l.log): FIRSTLINE PROPERTY1 DATA1 PROPERTY2 DATA2 PROPERTYS LIST ID1 data1 ID2 data2
3
by: rh0dium | last post by:
Hi all, I have a file which I need to parse and I need to be able to break it down by sections. I know it's possible but I can't seem to figure this out. The sections are broken by <> with...
4
by: Bytter | last post by:
Hi, I'm trying to construct a parser, but I'm stuck with some basic stuff... For example, I want to match the following: letter = "A"..."Z" | "a"..."z" literal = letter+ include_bool := "+"...
13
by: 7stud | last post by:
To the developer: 1) I went to the pyparsing wiki to download the pyparsing module and try it 2) At the wiki, there was no index entry in the table of contents for Downloads. After searching...
1
by: Steve | last post by:
Hi All (especially Paul McGuire!) Could you lend a hand in the grammar and paring of the output from the function win32pdhutil.ShowAllProcesses()? This is the code that I have so far (it is...
1
by: Neal Becker | last post by:
I'm just trying out pyparsing. I get stack overflow on my first try. Any help? #/usr/bin/python from pyparsing import Word, alphas, QuotedString, OneOrMore, delimitedList first_line = ''...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
3
by: hubritic | last post by:
I am trying to parse data that looks like this: IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION 2BFA76F6 1208230607 T S SYSPROC SYSTEM SHUTDOWN BY USER...
5
by: Paul McGuire | last post by:
I've just uploaded to SourceForge and PyPI the latest update to pyparsing, version 1.5.1. It has been a couple of months since 1.5.0 was released, and a number of bug-fixes and enhancements have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.