Skip to main content

Tatsu takes a grammar in a variation of EBNF as input, and outputs a memoizing PEG/Packrat parser in Python.

Reason this release was yanked:

outdated

Project description

fury license pyversions travis landscape

At least for the people who send me mail about a new language that they’re designing, the general advice is: do it to learn about how to write a compiler. Don’t have any expectations that anyone will use it, unless you hook up with some sort of organization in a position to push it hard. It’s a lottery, and some can buy a lot of the tickets. There are plenty of beautiful languages (more beautiful than C) that didn’t catch on. But someone does win the lottery, and doing a language at least teaches you something.

Dennis Ritchie (1941-2011) Creator of the C programming language and of Unix

Tatsu

Tatsu (the successor to Grako) is a tool that takes grammars in a variation of EBNF as input, and outputs memoizing (Packrat) PEG parsers in Python.

Tatsu can compile a grammar stored in a string into a tatsu.grammars.Grammar object that can be used to parse any given input, much like the re module does with regular expressions, or it can generate a Python module that implements the parser.

Installation

$ pip install tatsu

Using the Tool

Tatsu can generate Python code for a parser, be used as a library, much like Python’s re, by embedding grammars as strings and generating grammar models instead of generating code.

  • tatsu.compile(grammar, name=None, **kwargs) > Compiles the grammar and generates a model that can subsequently be used for parsing input with.

  • tatsu.parse(grammar, input, **kwargs) > Compiles the grammar and parses the given input producing an AST as result. The result is equivalent to calling model = compile(grammar); model.parse(input). Compiled grammars are cached for efficiency.

  • tatsu.to_python_sourcecode(grammar, name=None, filename=None, **kwargs) > Compiles the grammar to the Python sourcecode that implements the parser.

This is an example of how to use Tatsu as a library:

GRAMMAR = '''
    @@grammar::Calc

    start = expression $ ;

    expression
        =
        | term '+' ~ expression
        | term '-' ~ expression
        | term
        ;

    term
        =
        | factor '*' ~ term
        | factor '/' ~ term
        | factor
        ;

    factor
        =
        | '(' ~ @:expression ')'
        | number
        ;

    number = /\d+/ ;
'''


def main():
    import pprint
    import json
    from tatsu import parse
    from tatsu.util import asjson

    ast = parse(GRAMMAR, '3 + 5 * ( 10 - 20 )')
    print('PPRINT')
    pprint.pprint(ast, indent=2, width=20)
    print()

    json_ast = asjson(ast)
    print('JSON')
    print(json.dumps(json_ast, indent=2))
    print()


if __name__ == '__main__':
    main()

And this is the output:

PPRINT
[ '3',
  '+',
  [ '5',
    '*',
    [ '10',
      '-',
      '20']]]

JSON
[
  "3",
  "+",
  [
    "5",
    "*",
    [
      "10",
      "-",
      "20"
    ]
  ]
]

License

You may use Tatsu under the terms of the BSD-style license described in the enclosed LICENSE.txt file. If your project requires different licensing please email.

Documentation

For a detailed explanation of what Tatsu is capable off, please see the documentation.

Questions?

For general Q&A, please use the [tatsu] tag on StackOverflow.

Changes

See the CHANGELOG for details.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

tatsu-4.0.0.zip (131.8 kB view details)

Uploaded Source

Built Distribution

tatsu-4.0.0-py2.py3-none-any.whl (78.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tatsu-4.0.0.zip.

File metadata

  • Download URL: tatsu-4.0.0.zip
  • Upload date:
  • Size: 131.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tatsu-4.0.0.zip
Algorithm Hash digest
SHA256 09b948762321d9b6c74de4e7f71f69d872dcd1709396860b221b3be477ca25f2
MD5 cb01db94c9289bedc55d97813d44fbf3
BLAKE2b-256 d8404c99979aba69cac76688a56ae6a61b2cf901afe42c06c136c1b6ad0564e5

See more details on using hashes here.

File details

Details for the file tatsu-4.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for tatsu-4.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fc4f1f7c2f3c4564bfc81b71df46563cae9cfd8654232d724a12285374ad5079
MD5 82103b6e430c16a7a579b65981f946fa
BLAKE2b-256 4fadc1f18f126c94ab83679c6cccf5c6622c9ed3cd7acc791a8c1953212c1cda

See more details on using hashes here.

Supported by

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