# pyparsing Examples

This directory contains a number of Python scripts that can get you started in learning to use pyparsing.

## LICENSE

The files and source code in this examples directory are covered under the same LICENSE as the core pyparsing package. You will find this license in the LICENSE file of the pyparsing package.

- greeting.py
  Parse "Hello, World!".

- greetingInKorean.py ~ submission by June Kim
  Unicode example to parse "Hello, World!" in Korean.

- greetingInGreek.py ~ submission by ???
  Unicode example to parse "Hello, World!" in Greek.

- hola_mundo.py ~ submission by Marco Alfonso
  "Hello, World!" example translated to Spanish, from Marco Alfonso's blog.

- chemical_formulas.py
  Simple example to demonstrate the use of ParseResults returned from parseString(). Parses a chemical formula (such as "H2O" or "C6H5OH"), and walks the returned list of tokens to calculate the molecular weight.

- wordsToNum.py
  A sample program that reads a number in words (such as "fifteen hundred and sixty four"), and returns the actual number (1564). Also demonstrates some processing of ParseExceptions, including marking where the parse failure was found.

- mongodb_query_expression.py
  A useful utility to convert infix query expressions into the nested dicts required by the MongoDB pymongo library's `find()` command.

- delta_time.py
  A utility to parse colloquial time expressions such as "2pm next Tuesday" and convert them into Python datetime instances.

- pythonGrammarparser.py ~ suggested by JH Stovall
  A sample program that parses the EBNF used in the Python source code to define the Python grammar. From this parser, one can generate Python grammar documentation tools, such as railroad track diagrams. Also demonstrates use of Dict class.

- commasep.py
  Demonstration of the use of the commaSeparatedList helper. Shows examples of proper handling of commas within quotes, trimming of whitespace around delimited entries, and handling of consecutive commas (null arguments). Includes comparison with simple string.split(',').

- dictExample.py
  A demonstration of using the Dict class, to parse a table of ASCII tabulated data.

- dictExample2.py ~ submission by Mike Kelly
  An extended version of dictExample.py, in which Mike Kelly also parses the column headers, and generates a transposed version of the original table!

- scanExamples.py
  Some examples of using scanString and transformString, as alternative parsing methods to parseString, to do macro substitution, and selection and/or removal of matching strings within a source file.

- urlExtractorNew.py
  A sample program showing sample definitions and applications of HTML tag expressions created using makeHTMLTags helper function. Very useful for scraping data from HTML pages.

- fourFn.py
  A simple algebraic expression parser, that performs +,-,*,/, and ^ arithmetic operations. (With suggestions and bug-fixes graciously offered by Andrea Griffini.)

- SimpleCalc.py ~ submission by Steven Siew
  An interactive version of fourFn.py, with support for variables.

- LAParser.py ~ submission by Mike Ellis
  An interactive Linear Algebra Parser, an extension of SimpleCalc.py. Supports linear algebra (LA) notation for vectors, matrices, and scalars, including matrix operations such as inversion and determinants. Converts LA expressions to C code - uses a separate C library for runtime evaluation of results.

- configParse.py
  A simple alternative to Python's ConfigParse module, demonstrating the use of the Dict class to return nested dictionary access to configuration values.

- getNTPserversNew.py
  Yet another scanString example, to read/extract the list of NTP servers from NIST's web site. Uses the new makeHTMLTags() method.

- httpServerLogParser.py
  Parser for Apache server log files.

- idlParse.py
  Parser for CORBA IDL files.

- mozillaCalendarParser.py ~ submission by Petri Savolainen
  Parser for Mozilla calendar (*.ics) files.

- pgn.py ~ submission by Alberto Santini
  Parser for PGN (Portable Game Notation) files, the standard form for documenting the moves in chess games.

- simpleSQL.py
  A simple parser that will extract table and column names from SQL SELECT statements..

- dfmparse.py ~ submission by Dan Griffith
  Parser for Delphi forms.

- ebnf.py / ebnftest.py ~ submission by Seo Sanghyeon
  An EBNF-compiler that reads EBNF and generates a pyparsing grammar! Including a test that compiles... EBNF itself!

- searchparser.py ~ submission by Steven Mooij and Rudolph Froger
  An expression parser that parses search strings, with special keyword and expression operations using (), not, and, or, and quoted strings.

- romanNumerals.py
  A Roman numeral generator and parser example, showing the power of parse actions to compile Roman numerals into their integer values.

- removeLineBreaks.py
  A string transformer that converts text files with hard line-breaks into one with line breaks only between paragraphs. Useful when converting downloads from Project Gutenberg to import to word processing apps that can reformat paragraphs once hard line-breaks are removed, or for loading into your Palm Pilot for portable perusal.

  See Successful Methods of Public Speaking.txt and Successful Methods of Public Speaking(2).txt for a sample before and after (text file courtesy of Project Gutenberg).

- listAllMatches.py
  An example program showing the utility of the listAllMatches option when specifying results naming.

- linenoExample.py
  An example program showing how to use the string location to extract line and column numbers, or the source line of text.

- parseListString.py
  An example program showing a progression of steps, how to parse a string representation of a Python list back into a true list.

- parsePythonValue.py
  An extension of parseListString.py to parse tuples and dicts, including nested values, returning a Python value of the original type.

- indentedGrammarExample.py
  An example program showing how to parse a grammar using indentation for grouping, such as is done in Python.

- simpleArith.py
  An example program showing how to use the new operatorPrecedence helper method to define a 6-function (+, -, *, /, ^, and !) arithmetic expression parser, with unary plus and minus signs.

- simpleBool.py
  An example program showing how to use the new operatorPrecedence helper method to define a boolean expression parser, with parse actions associated with each operator to "compile" the expression into a data structure that will evaluate the expression's boolean value.

- simpleWiki.py
  An example program showing how to use transformString to implement a simple Wiki markup parser.

- sql2dot.py ~ submission by EnErGy [CSDX]
  A nice graphing program that generates schema diagrams from SQL table definition statements.

- htmlStripper.py
  An example implementation of a common application, removing HTML markup tags from an HTML page, leaving just the text content.

- macroExpansion.py
  An example implementation of a simple preprocessor, that will read embedded macro definitions and replace macro references with the defined substitution string.

- sexpParser.py
  A parser that uses a recursive grammar to parse S-expressions.

- nested.py
  An example using nestedExpr, a helper method to simplify definitions of expressions of nested lists.

- withAttribute.py
  An example using withAttribute, a helper method to define parse actions to validate matched HTML tags using additional attributes. Especially helpful for matching common tags such as <DIV> and <TD>.

- stackish.py
  A parser for the data representation format, Stackish.

- builtin_parse_action_demo.py
  Demonstration of using builtins (min, max, sum, len, etc.) as parse actions.

- antlr_grammar.py ~ submission by Luca DellOlio
  Pyparsing example parsing ANTLR .a files and generating a working pyparsing parser.

- shapes.py
  Parse actions example simple shape definition syntax, and returning the matched tokens as domain objects instead of just strings.

- datetimeParseActions.py
  Parse actions example showing a parse action returning a datetime object instead of string tokens, and doing validation of the tokens, raising a ParseException if the given YYYY/MM/DD string does not represent a valid date.

- position.py
  Demonstration of a couple of different ways to capture the location a particular expression was found within the overall input string.