Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

listAllMatches problem

Pascal
2006-09-09
2013-09-02
  • Pascal
    Pascal
    2006-09-09

    hello,
    I'm 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

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

     
    • Paul McGuire
      Paul McGuire
      2006-09-09

      Thanks for posting this test case.  This is a bug in pyparsing.  I'll have a fix ready shortly.

      -- Paul

       
    • Pascal
      Pascal
      2006-09-09

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

       
  • Jdu
    Jdu
    2013-09-02

    Has this bug been fixed? I'm having trouble seeing nested results through the dump() method for grammar rules with result names that were set with listAllMatches = True, or with setParseAction. Thanks for your help!