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
Start.png
Advanced
advanced.png
Recursive Grammar
spiral.png
Parse Actions
j0431496.png
May Require Extra Study
j0431520.png

Start.png
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.

Start.png
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.

~ suggested by JH Stovall advanced.png
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.

Start.png
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.

~ 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!

Start.png
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.

urlExtractor.py
Another example using scanString, this time to extract all HREF references found on Yahoo!'s home page, and return them as a dictionary.

Start.png
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.

Start.png
Another updated version of urlExtractor.py, using the new makeHTMLTags() method.

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

~ submissions by Allan Caine Start.pngspiral.png

Interprets LISP like commands for +, -, * and /. For example, (+ (* 2 11) (* 3 10)) --> 52
~ submission by Steven Siew spiral.png

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



~ submission by Mike Ellis spiral.png
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.


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

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

Start.png
An updated version of getNTPservers.py, using the new makeHTMLTags() method.

Start.png
Parser for Apache server log files.

spiral.png
Parser for CORBA IDL files.

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

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

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

~ submission by Dan Griffith
Parser for Delphi forms.

/ ~ submission by Seo Sanghyeon advanced.pngspiral.png j0431520.png
An EBNF-compiler that reads EBNF and generates a pyparsing grammar! Including a test that compiles... EBNF itself!

~ submission by Steven Mooij and Rudolph Froger advanced.png
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

~ submission by Tim Cera
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.


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.

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

j0431520.png(UPDATED 4/19/2010 - handles more address formats)
Parsing street addresses is another tricky parsing application, handling many lookahead and ambiguous cases.

New in Version 1.4.4!

j0431520.png
An arithmetic expression parser, using the new operatorPrecedence helper.

j0431520.png
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) spiral.png
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 eval 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!

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

spiral.png
A demonstration recursive parser to process S-expressions.

j0431520.png spiral.png
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.

New in Version 1.5.0!
j0431496.png
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.

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

New in Version 1.5.1!

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.2!

Expansion on simple_arith.py, including code to evaluate the parsed tokens.

New in Version 1.5.3

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!


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.

New in Version 1.5.4

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!


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

New in Version 1.5.6

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.7?

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