greeting.py 510 B

12345678910111213141516171819202122232425
  1. # greeting.py
  2. #
  3. # Demonstration of the pyparsing module, on the prototypical "Hello, World!"
  4. # example
  5. #
  6. # Copyright 2003, 2019 by Paul McGuire
  7. #
  8. import pyparsing as pp
  9. # define grammar
  10. greet = pp.Word(pp.alphas) + "," + pp.Word(pp.alphas) + pp.oneOf("! ? .")
  11. # input string
  12. hello = "Hello, World!"
  13. # parse input string
  14. print(hello, "->", greet.parseString( hello ))
  15. # parse a bunch of input strings
  16. greet.runTests("""\
  17. Hello, World!
  18. Ahoy, Matey!
  19. Howdy, Pardner!
  20. Morning, Neighbor!
  21. """)