Discussion:
[Pyparsing] setParseAction()
Andreas Matthias
2014-08-28 21:56:38 UTC
Permalink
The following example works as expected. But if I uncomment the
line ``p.setParseAction(foo)'' I get the error:

pyparsing.ParseException: Expected start of line (at char 1), (line:1, col:1)

I do not understand what's going on here. Any help?

Ciao
Andreas



import pyparsing as pypa

p = \
pypa.LineStart() + \
pypa.OneOrMore( pypa.Literal('a').setWhitespaceChars(' ') ) + \
pypa.LineEnd()

def foo (t):
print(t)

#p.setParseAction(foo)

g = pypa.Literal('b') + p

res = g.parseString('''b
a a
a
''')

print(res)
Athanasios Anastasiou
2014-08-29 12:29:01 UTC
Permalink
Hello Andreas

I briefly had a look at your example and I noticed two things:

1) Do you really need your parsed string to be enclosed in three single
quotes?

2) The error you are getting is reasonable. The rule p expects to start at
the beginning of the parsed string. Be defining g as 'b'+p, you are placing
rule p AFTER the beginning of the line (which is where the character 'b'
will be).

Removing LineStart and enclosing the last string in single quotes (and of
course, escaping the line breaks) seems to be working as expected (?).

If you want to parse the string EXACTLY as it appears (including the line
breaks) you will have to include them in your rules.

Hope this helps
AA


On Thu, Aug 28, 2014 at 10:56 PM, Andreas Matthias <
Post by Andreas Matthias
The following example works as expected. But if I uncomment the
pyparsing.ParseException: Expected start of line (at char 1), (line:1, col:1)
I do not understand what's going on here. Any help?
Ciao
Andreas
import pyparsing as pypa
p = \
pypa.LineStart() + \
pypa.OneOrMore( pypa.Literal('a').setWhitespaceChars(' ') ) + \
pypa.LineEnd()
print(t)
#p.setParseAction(foo)
g = pypa.Literal('b') + p
res = g.parseString('''b
a a
a
''')
print(res)
------------------------------------------------------------------------------
Slashdot TV.
Video for Nerds. Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
Pyparsing-users mailing list
https://lists.sourceforge.net/lists/listinfo/pyparsing-users
Andreas Matthias
2014-08-29 15:19:55 UTC
Permalink
Post by Athanasios Anastasiou
1) Do you really need your parsed string to be enclosed in three single
quotes?
Actually I'm parsing a file with parseFile().
Post by Athanasios Anastasiou
2) The error you are getting is reasonable. The rule p expects to start at
the beginning of the parsed string. Be defining g as 'b'+p, you are placing
rule p AFTER the beginning of the line (which is where the character 'b'
will be).
But the string consists of 3 line and for the second line I created
a parser with LineStart() and LineEnd(). That's why I'm using
LineStart() to get the beginning of the second line. Is this wrong?

What puzzles me is that the parser yields the correct results, as long
as no actions are attached to it. At least it's the result I expected.
But after adding setParseAction() it is no longer working.


Maybe I should explain my original case: I try to parse a file which has
/one/ particular line containing a few numbers separated by spaces. I do
not know how many numbers there are in this line. I just know that they
are in this particular line. Thus I need to take care about linebreak
in this line. For the rest of the file (before and after this line) I
do not need to (and don't want to) take care about linebreaks.


Ciao
Andreas
Andreas Matthias
2014-08-31 15:42:28 UTC
Permalink
Alright, (I think) I understand what you are after better.
Not sure if you are getting the correct result without the parse action but as a rule of
thumb you would need to write a parser rule for exactly the thing that you mean. So, if
you want to parse ONE line out of a whole file you need to put this in the parser as
well. Something like ".*? [interesting bit] .*". In this way the parser will produce a
valid match for the whole thing you are passing to it.
So, if you have a way to isolate that one line from your file and then parse it with the
(p) or (g) you have defined above then that will probably work as it is. If you want to
pass the complete file and have the parser isolate and interpert that one line only you
will have to specify it explicitly. The parsers that are represented by (p) or (g) don't
produce an output wherever they find a match. They try to match exactly whatever is
passed to them.
I do want to parse the whole file. And skipping newlines is ok except
for one specific line.

Here is a more complete example.

Loading...