Skip to main content

A natural language semantic parser

Project description Documentation Status

A natural language semantic parser


pip install parsetron

Parsetron is tested under Python 2.7 and Pypy. It doesn’t support Python 3 yet.

Quick Start

The following is a grammar that parses natural language instruction on lights:

from parsetron import Set, Regex, Optional, OneOrMore, Grammar, RobustParser

class LightGrammar(Grammar):

    action = Set(['change', 'flash', 'set', 'blink'])
    light = Set(['top', 'middle', 'bottom'])
    color = Regex(r'(red|yellow|blue|orange|purple|...)')
    times = Set(['once', 'twice', 'three times']) | Regex(r'\d+ times')
    one_parse = action + light + Optional(times) + color
    GOAL = OneOrMore(one_parse)

    def test():
        parser = RobustParser((LightGrammar()))
        sents = [
            "set my top light to red",
            "set my top light to red and change middle light to yellow",
            "set my top light to red and change middle light to yellow and "
            "flash bottom light twice in blue"
        for sent in sents:
            tree, result = parser.parse(sent)
            assert result.one_parse[0].color == 'red'

            print '"%s"' % sent
            print "parse tree:"
            print tree
            print "parse result:"
            print result


None. Parsetron is a single file.

Parsetron is inspired by pyparsing.

Grammar Modules

Parsetron supports modularized grammars: each grammar focuses on an individual small domain and can be imported via, for instance:

from parsetron.grammars.colors import ColorsGrammar

class YourCustomizedGrammar(Grammar):
    color = ColorsGrammar.GOAL

You are welcome to contribute your own grammar here (under parsetron.grammars). Send us a pull request!


  1. fork this repository

  2. install dev-specific packages:

    pip install -r requirements.txt
  3. then make your changes and follow the Makefile.


  • [ ] Python 3 compatible

  • [ ] Unicode support


The full documentation is at


0.1.1 (2015-08-24)

  • ParseResult now supports lex_span() method.

0.1.0 (2015-06-19)

  • First release on PyPI.

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

parsetron-0.1.1.tar.gz (41.0 kB view hashes)

Uploaded source

Built Distribution

parsetron-0.1.1-py2.py3-none-any.whl (35.6 kB view hashes)

Uploaded 2 7

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page