nested.py 586 B

12345678910111213141516171819202122232425262728
  1. #
  2. # nested.py
  3. # Copyright, 2007 - Paul McGuire
  4. #
  5. # Simple example of using nestedExpr to define expressions using
  6. # paired delimiters for grouping lists and sublists
  7. #
  8. from pyparsing import *
  9. data = """
  10. {
  11. { item1 "item with } in it" }
  12. {
  13. {item2a item2b }
  14. {item3}
  15. }
  16. }
  17. """
  18. # use {}'s for nested lists
  19. nestedItems = nestedExpr("{", "}")
  20. print(( (nestedItems+stringEnd).parseString(data).asList() ))
  21. # use default delimiters of ()'s
  22. mathExpr = nestedExpr()
  23. print(( mathExpr.parseString( "((( ax + by)*C) *(Z | (E^F) & D))") ))