0

I'm trying to build a simple parser using pyparsing.

My example file looks as follows:

# comment
# comment

name1 = value1
name2 = value2


example_list a
  foo
  bar


grp1 (

example_list2 aa
  foo
  bar


example_list3 bb
  foo
  bar

)



grp2 (

example_list4 x
  foo
  bar

example_list5 x
  foo
  bar

example_list6 x
  foo
  bar
)

The parser I've come up with so far looks like this:

#!/usr/bin/python
import sys
from pyparsing import *

blank_line = lineStart + restOfLine
comment = Suppress("#") + restOfLine

alias = Word(alphas, alphanums)
name = Word(alphas, alphanums + "_")
value = Word(printables)

parameter = name + Suppress("=") + value
flag = Literal("*") | Literal("#") | Literal("!")
list_item = Optional(flag) + value
list = name + alias + lineEnd + OneOrMore(list_item) + blank_line
group = alias + Suppress("(") + lineEnd + OneOrMore(list) + lineStart + Suppress(")")
script = ZeroOrMore(Suppress(blank_line) | Suppress(comment) | parameter ^ list ^ group)

if __name__ == "__main__":
    print script.parseFile(sys.argv[1])

but of course it doesn't work.

What I think I need is some way for the parser to know that if we have a string followed by an equals sign, that only then can we expect just one more string.

If we have a string followed by a bracket, then we've started a group.

And if we have two strings, then we've started a list.

How do I do this?

Also, comments could conceivably also appear on the end of lines...

Colin 't Hart
  • 7,372
  • 3
  • 28
  • 51

1 Answers1

0

I'm not sure if you are settled on your file format, but your file could easily be expressed as an RSON file (see http://code.google.com/p/rson/). The RSON format (and associated parser) was developed to be a "readable" version of JSON. I'm using the python RSON parser in some of my projects.

If you are doing this to learn how to parse a file like this, you may still be able to glean some info from the RSON parser.

Brad Campbell
  • 2,969
  • 2
  • 23
  • 21
  • I don't want to go down the generic markup format route as I want to be able to turn the format into a DSL. – Colin 't Hart Jul 25 '12 at 09:36
  • The RSON parser seems to be quite well written, but it's "built by hand" and not using pyparsing. The idea was to use pyparsing so that the Python code becomes some sort of documentation for the DSL. – Colin 't Hart Jul 25 '12 at 09:37