Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  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/

Project details

Download files

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

Files for Varas, version 1.1
Filename, size File type Python version Upload date Hashes
Filename, size Varas-1.1.tar.gz (9.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page