File: nested.py

package info (click to toggle)
pyparsing 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 12,200 kB
  • sloc: python: 30,867; ansic: 422; sh: 112; makefile: 24
file content (28 lines) | stat: -rw-r--r-- 586 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
#  nested.py
#  Copyright, 2007 - Paul McGuire
#
#  Simple example of using nested_expr to define expressions using
#  paired delimiters for grouping lists and sublists
#

from pyparsing import *

data = """
{
     { item1 "item with } in it" }
     {
      {item2a item2b }
      {item3}
     }

}
"""

# use {}'s for nested lists
nestedItems = nested_expr("{", "}")
print((nestedItems + string_end).parse_string(data).as_list())

# use default delimiters of ()'s
mathExpr = nested_expr()
print(mathExpr.parse_string("((( ax + by)*C) *(Z | (E^F) & D))"))