471,350 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

pyparsing listAllMatches problem

hello,
I'm using pyparsing and trying to parse something like:
test="""Q(x,y,z):-Bloo(x,"Mitsis",y),Foo(y,z,1243),y>28,x<12,x>3"""

and also have all comparison predicates in a separate list apart from the
parse tree. So my grammar has this line in it:

Comparison_Predicate = Group(variable + oneOf("< >")
+ integer).setResultsName("pred",listAllMatches=True )

but it doesn't work at all... only the last comp.pred. gets in the pred
attribute...
here's the full listing:

from pyparsing import Literal, Word, nums, Group, Dict, alphas,
quotedString, oneOf, delimitedList, removeQuotes, alphanums

lbrack = Literal("(").suppress()
rbrack = Literal(")").suppress()
integer = Word( nums )
variable = Word( alphas, max=1 )
relation_body_item = variable | integer |
quotedString.setParseAction(removeQuotes)
relation_name = Word( alphas+"_", alphanums+"_" )
relation_body = lbrack + Group(delimitedList(relation_body_item)) + rbrack
Goal = Dict(Group( relation_name + relation_body ))
Comparison_Predicate = Group(variable + oneOf("< >")
+ integer).setResultsName("pred",listAllMatches=True )
Query = Goal.setResultsName("head") + ":-" + delimitedList(Goal |
Comparison_Predicate)

test="""Q(x,y,z):-Bloo(x,"Mitsis",y),Foo(y,z,1243),y>28,x<12,x>3"""

print Query.parseString(test).pred

P.S. another weird thing is that, depending on if I setResultsName("head")
in the first Goal match, the first Goal, gets inside an extra list!What I
mean is that the parsed token without setResultsName("head") is this:
['Q', ['x', 'y', 'z']]
and with setResultsName("head") is this:
[['Q', ['x', 'y', 'z']]]
--
Χρησιμοποιώ κάποια δόση υπερβολής...
Sep 9 '06 #1
2 1287
"don pasquale" <don.pasquale@blabla_tzimeil.comwrote in message
news:op***************@fafoi.lan...
hello,
I'm using pyparsing and trying to parse something like:
test="""Q(x,y,z):-Bloo(x,"Mitsis",y),Foo(y,z,1243),y>28,x<12,x>3"""

and also have all comparison predicates in a separate list apart from the
parse tree. So my grammar has this line in it:

Comparison_Predicate = Group(variable + oneOf("< >") +
integer).setResultsName("pred",listAllMatches=True )

but it doesn't work at all... only the last comp.pred. gets in the pred
attribute...
Thanks for posting this test case. This is a bug in pyparsing. I'll have a
fix ready shortly.

-- Paul
Sep 9 '06 #2
On Sat, 09 Sep 2006 20:46:29 +0300, Paul McGuire
<pt***@austin.rr._bogus_.comwrote:
Thanks for posting this test case. This is a bug in pyparsing. I'll
have a
fix ready shortly.

-- Paul
Ur welcome, I hope you find the bug and squash it :). I temporalily solved
my problem (in case anyone else has it) by changing another definition and
including a second call to setResultsName.

I modified the Goal definition from
Goal = Dict(Group( relation_name + relation_body ))
to
Goal = Dict(Group( relation_name + relation_body
)).setResultsName("glist",listAllMatches=True)

if an extra .setResultsName("glist",listAllMatches=True) is added, then
both calls to setResultsName (in Goal and Comparison_Predicate) suddenly
work as planned! weird huh? I don't think this generally works, but it's
worth a try until the bug is fixed...

--
Χρησιμοποιώ κάποια δόση υπερβολής...
Sep 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Lukas Holcik | last post: by
reply views Thread by Peter Fein | last post: by
4 posts views Thread by the.theorist | last post: by
4 posts views Thread by Bytter | last post: by
13 posts views Thread by 7stud | last post: by
reply views Thread by napolpie | last post: by
2 posts views Thread by Nathan Harmston | last post: by
18 posts views Thread by Just Another Victim of the Ambient Morality | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.