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