Pyparsing Question

Ant antroy at gmail.com
Fri May 16 07:43:17 EDT 2008


Hi all,

I have a question on PyParsing. I am trying to create a parser for a 
hierarchical todo list format, but have hit a stumbling block. I have 
parsers for the header of the list (title and description), and the body 
(recursive descent on todo items).

Individually they are working fine, combined they throw an exception. 
The code follows:

#!/usr/bin/python
# parser.py
import pyparsing as pp

def grammar():
     underline = pp.Word("=").suppress()
     dotnum = pp.Combine(pp.Word(pp.nums) + ".")
     textline = pp.Combine(pp.Group(pp.Word(pp.alphas, pp.printables) + 
pp.restOfLine))
     number = pp.Group(pp.OneOrMore(dotnum))

     headtitle = textline
     headdescription = pp.ZeroOrMore(textline)
     head = pp.Group(headtitle + underline + headdescription)

     taskname = pp.OneOrMore(dotnum) + textline
     task = pp.Forward()
     subtask = pp.Group(dotnum + task)
     task << (taskname + pp.ZeroOrMore(subtask))
     maintask = pp.Group(pp.LineStart() + task)

     parser = pp.OneOrMore(maintask)

     return head, parser

text = """


My Title
========

Text on a longer line of several words.
More test
and more.

"""

text2 = """

1. Task 1
     1.1. Subtask
         1.1.1. More tasks.
     1.2. Another subtask
2. Task 2
     2.1. Subtask again"""

head, parser = grammar()

print head.parseString(text)
print parser.parseString(text2)

comb = head + pp.OneOrMore(pp.LineStart() + pp.restOfLine) + parser
print comb.parseString(text + text2)

#===================================================================

Now the first two print statements output the parse tree as I would 
expect, but the combined parser fails with an exception:

Traceback (most recent call last):
   File "parser.py", line 50, in ?
     print comb.parseString(text + text2)
.
. [Stacktrace snipped]
.
     raise exc
pyparsing.ParseException: Expected start of line (at char 81), (line:9, 
col:1)

Any help appreciated!

Cheers,

-- 
Ant.



More information about the Python-list mailing list