pyparser and recursion problem

Neil Cerutti horpner at yahoo.com
Thu Jul 26 22:27:28 CEST 2007


On 2007-07-26, pyscottishguy at hotmail.com <pyscottishguy at hotmail.com> wrote:
> Using pyparser, I'm trying to parse a string like this:
>
>:Start: first SECOND THIRD :SECOND: second1 | second2 :THIRD: third1 |
> FOURTH  :FOURTH: fourth1 | fourth2
>
> I want the parser to do the following:
> 1) Get the text for the :Start: label   e.g ('first SECOND THIRD')
> 2) Do nothing with the lower-case words  e.g ('first')
> 3) For each upper-case word find the corresponding entries, and
> replace the word
>     with these entries (the '|' indicates separate records)
>     e.g. for 'SECOND', replace the word with ("second1", "second2")
> 4 Do this recursively, because each item in '3' can have upper-case
> words
>
> I can do this - but not within pyparser.  I had to write a
> recursive function to do it.  I would like to do it within
> pyparser however.

pyparser is a great parser, but out of the box it only parses. To
get it to convert a text file into a new form you'll have to
write most of the conversion code yourself, and merely hook it
into pyparser.

The critical step you're missing (and that I missed until my
third try) is the setParseAction method, with which you can
monkey around with the resulting parse tree.

On the other hand, since you got it working without pyparsing,
probably you're problem doesn't need pyparsing.

Hopefully I'll have time to help you a bit more later, or Paul
MaGuire will swoop down in his pyparsing powered super-suit. ;)

-- 
Neil Cerutti



More information about the Python-list mailing list