pyparsing listAllMatches problem

D

don pasquale

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']]]
 
P

Paul McGuire

don pasquale said:
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
 
D

don pasquale

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top