Examples

media type="custom" key="330259" =Examples= Pyparsing comes with an examples directory, containing many sample parsing programs. Some of these have been contributed by pyparsing users - I am happy to include these with the pyparsing distribution, if you have developed a useful sample program.

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

Use this legend for some guidance on which examples to view:


 * Getting Started || [[image:Start.png width="86" height="42"]] ||
 * Advanced || [[image:advanced.png width="58" height="51"]] ||
 * Recursive Grammar || [[image:spiral.png width="56" height="52"]] ||
 * Parse Actions || [[image:j0431496.png width="59" height="59"]] ||
 * May Require Extra Study || [[image:j0431520.png width="60" height="61"]] ||

Parse "Hello, World!".

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

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

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.

An exercise in replacing the builtin Python urlparse module, fixes a few bugs that the Python builtin version has.

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.

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.
 * //[[file:pythonGrammarParser.py]] ~ suggested by JH Stovall [[image:advanced.png width="58" height="51"]]//**

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(',').

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

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

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.

Another example using scanString, this time to extract all HREF references found on Yahoo!'s home page, and return them as a dictionary.
 * //urlExtractor.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.

Another updated version of urlExtractor.py, using the new makeHTMLTags method.

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

**~ submissions by Allan Caine**

Interprets LISP like commands for +, -, * and /. For example, (+ (* 2 11) (* 3 10)) --> 52
 * //[[file:SimpleCalc.py]] ~ submission by Steven Siew [[image:spiral.png width="56" height="52"]]//**

An interactive version of fourFn.py, with support for variables.



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.
 * //[[file:LAparser.py]] ~ submission by Mike Ellis [[image:spiral.png width="56" height="52"]]//**

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

Yet another scanString example, to read/extract the list of NTP servers from NIST's web site.
 * //getNTPservers.py//**

An updated version of getNTPservers.py, using the new makeHTMLTags method.

Parser for Apache server log files.

Parser for CORBA IDL files.

Parser for Mozilla calendar (*.ics) files.
 * //[[file:mozillaCalendarParser.py]] ~ submission by Petri Savolainen//**

Parser for PGN (Portable Game Notation) files, the standard form for documenting the moves in chess games.
 * //[[file:pgn.py]] ~ submission by Alberto Santini//**

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

Parser for Delphi forms.
 * //[[file:dfmparse.py]] ~ submission by Dan Griffith//**

An EBNF-compiler that reads EBNF and generates a pyparsing grammar! Including a test that compiles... EBNF itself!
 * //[[file:ebnf.py]] / [[file:ebnftest.py]] ~ submission by Seo Sanghyeon [[image:advanced.png width="58" height="51"]][[image:spiral.png width="56" height="52"]] [[image:j0431520.png width="60" height="61"]]//**

An expression parser that parses search strings, with special keyword and expression operations using, not, and, or, and quoted strings. //Updates//: [|How to handle quoted phrases with punctuation] and [|An alternative method of evaluating quoted phrases]
 * //[[file:searchparser.py]] ~ submission by Steven Mooij and Rudolph Froger [[image:advanced.png width="58" height="51"]]//**

A configurable parser module that can be configured with a list of tuples, giving a high-level definition for parsing common sets of water table data files. Tim had to contend with several different styles of data file formats, each with slight variations of its own. Tim created a configurable parser (or "SPECIFIED parser" - hence the name "sparser"), that simply works from a config variable listing the field names and data types, and implicitly, their order in the source data file. See for an example configuration file.
 * //[[file:sparser.py]] ~ submission by Tim Cera//**

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

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 and  for a sample before and after (text file courtesy of [|Project Gutenberg]).

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

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

Some tricky pyparsing definitions to handle a simple indentation-based grammar.

Parsing street addresses is another tricky parsing application, handling many lookahead and ambiguous cases.
 * //[[file:streetAddressParser.py]] [[image:j0431520.png width="60" height="61"]](UPDATED 4/19/2010 - handles more address formats)//**


 * New in Version 1.4.4!**

An arithmetic expression parser, using the new operatorPrecedence helper.

A boolean expression parser, using the new operatorPrecedence helper.

A simple Wiki markup parser, using transform string to convert markup punctuation to HTML tags.


 * New in Version 1.4.5!**

A sample program to strip HTML tags and scripts from HTML source text, leaving just the readable text.

(updated in 1.4.6) A sample program to read JSON-formatted data and returns a hierarchically nested ParseResults object, similar to a Python list.

A safe alternative to using to convert a string to a Python value. Supports int, float, single- and double-quoted strings, and tuples, lists, and dicts (including nested values).


 * New in Version 1.4.6!**

A simple preprocessor to do macro replacement of #def'ed macro names.

A demonstration recursive parser to process S-expressions.

A dice roll parser and evaluator for evaluating strings such as "4d20+5.5+4d6.takeHighest(3)".


 * New in Version 1.4.8!**

An example showing how to use the new withAttribute parse action helper method.

An example showing how to use the new nestedExpr parse action helper method.

An example showing how to parse a dhcp.leases file. Use to extract MAC addresses, IP addresses, etc. Uses a parse action to convert UTC timestamps to local time.
 * New in Version 1.5.0!**

A parser for output of Perl's "Test Anything Protocol".

Parser that will process a regular-expression, and return a generator yielding all possible matching strings. Recognizes basic RE syntax. Rejects unbounded repetition operators such as '*' and '+', but will accept '{n}' and '{m,n}' repetition notation.
 * New in Version 1.5.1!**

Expansion on simple_arith.py, including code to evaluate the parsed tokens.
 * New in Version 1.5.2!**

An updated version of the code published in the Python Magazine article on DSLs, sent in by Matt Anderson, that is compatible with Python versions 2.7 and above - thanks so much, Matt!
 * New in Version 1.5.3**

A MicroC compiler submitted by Zarko Zivanov. (Note: this example is separately licensed under the GPLv3, and requires Python 2.6 or higher.) Thank you, Zarko!

A subset C parser, using the BNF from the 1996 Obfuscated C Contest.

A parser for reading SQLite SELECT statements, as specified at http://www.sqlite.org/lang_select.html; this goes into much more detail than the simple SQL parser included in pyparsing's source code

A *simplistic* first-cut at a parser for Excel expressions, which I originally posted on comp.lang.python in January, 2010; beware, this parser omits many common Excel cases (addition of numbers represented as strings, references to named ranges)

A nice little parser posted my Mark Tolonen on comp.lang.python in August, 2009 (redistributed here with Mark's permission). Thanks a bunch, Mark!

A sample I posted to Stackoverflow.com, implementing a special variation on Literal that does "close" matching, up to a given number of allowed mismatches. The application was to find matching gene sequences, with allowance for one or two mismatches.

A sample showing how to use a Forward placeholder to enforce matching of text parsed in a previous expression.

A simple demo showing how the matchPreviousLiteral helper method is used to match a previously parsed token.

A demonstration of how to use pyparsing to create your own API-specific lint/style checker, including handling of syntax errors while continuing to scan through the source. Written in response to a question from Peter Lom, thanks for writing, Peter!
 * New in Version 1.5.4**

A parser that can convert conversational time phrases like "10 minutes from now", "next Sunday at 2pm" and "noon tomorrow" into Python datetime objects.

A parser for the Verilog language. If you use this parser for a commercial application, please make a donation to your local Red Cross.
 * New in Version 1.5.6**

A revision of eval_arith.py updated as follows: tested with Python 3.2 (should work on any 3.x) Eliminate LE and friends, stick with Python comparison ops Merge separate precedence lists Make a single class Arith to be the interface Keep vars_ in Arith instances, so multiple instances could have different vars/values Add // and % to multOp & EvalMultOp Keep integer values as integers until something converts them Allow longer var names
 * New in Version 1.5.7?**

media type="custom" key="290869"