Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

Pratt parser framework

Project Description

Varas is a python Pratt parser framework, which aims to be general purpose and easy to use.

Pratt parsers (also known as top down operator precendence parsers) are simple to use and resonably efficent. There are a few articles describing them on the web:


The source is available for download from github here:

You can run the test code like this:


And you can install it using python distutils in the normal way:

$ python install

A simple example

As a simple example, we can build a calculator that computes numerical expressions.

First we create a tokenizer. There will be two types of tokens - literal numbers and mathematical operators. Tokens are matched by regular expressions.

>>> from varas import *
>>> tok = Tokenizer( ("\d+", "NUMBER"),
...                  ("[-+*/^]", None) )
>>> list(tok.tokenize("2 +"))
[Token('NUMBER', '2'), Token('+', '+'), Token(Token.END_TOKEN, '')]

Next we set up an expression spec to tell the parser what to do when it encounters tokens.

>>> expr = ExprSpec()

To deal with number literals we add an action that just calls the int() function on the content of the token:

>>> expr.add_word("NUMBER", lambda token: int(token.content))

To parse an expression and calculate the answer, we need to plug the parser and tokenizer together. This is done by creating a Parser object which takes both the expression spec and a token generator. We can then call the parse() method to actually parse the input:

>>> def calc(text):
...   return Parser(expr, tok.tokenize(text)).parse()

We can check that this parses numbers:

>>> calc("42")

Operators for addition and subtraction can be added to the expression spec like this:

>>> expr.add_binary_op("+", 10, Assoc.LEFT,
...                    lambda token, left, right: left + right)
>>> expr.add_binary_op("-", 10, Assoc.LEFT,
...                    lambda token, left, right: left - right)

This allows us to process simple expressions:

>>> calc("2 + 2")

The number ‘10’ in the lines above indicates the precedence of the operator. To add multiplication and division operators, we need to set a higher precendence value to ensure the correct result:

>>> expr.add_binary_op("*", 20, Assoc.LEFT,
...                    lambda token, left, right: left * right)
>>> expr.add_binary_op("/", 20, Assoc.LEFT,
...                    lambda token, left, right: left / right)
>>> calc("1 + 2 * 3")

A more detailed example can be found in the file test/

Release History

Release History

This version
History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
Varas-1.1.tar.gz (9.1 kB) Copy SHA256 Checksum SHA256 Source Apr 24, 2012

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting