Skip to main content

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:

http://javascript.crockford.com/tdop/tdop.html

http://eli.thegreenplace.net/2010/01/02/top-down-operator-precedence-parsing/

Installation

The source is available for download from github here:

https://github.com/JonCoppeard/varas/downloads

You can run the test code like this:

python run_tests.py

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

$ python setup.py 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")
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")
4

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")
7

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

Project details


Download files

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

Source Distribution

Varas-1.1.tar.gz (9.1 kB view details)

Uploaded Source

File details

Details for the file Varas-1.1.tar.gz.

File metadata

  • Download URL: Varas-1.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Varas-1.1.tar.gz
Algorithm Hash digest
SHA256 51b101e0729de6cb84c3010278382db993314deb5b20bda502daa08b5207eccd
MD5 7c26e15a666f3fe18283f1443a819f7c
BLAKE2b-256 488dddf6c06c7857d9d33d4b4eac046434a4fadcdfc0dbeae933c09444e66175

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page