472,805 Members | 1,101 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 1335
"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 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....
0
by: Peter Fein | last post by:
I'm trying to write a parser for a simple query language (for a web search form). Things are ok, except that if the first match is an OR_clause, I only get one token back when I access by name,...
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
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...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
2
by: Nathan Harmston | last post by:
Hi, I know this isnt the pyparsing list, but it doesnt seem like there is one. I m trying to use pyparsing to parse a file however I cant get the Optional keyword to work. My file generally...
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...
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: ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.