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:

several bugs fixed in 5.6.0

Project description

license pyversions fury circleci docs

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

def WARNING():
    """
    |TatSu|>=5.0.0 requires Python>=3.8

    Python 3.8 introduced new language features that allow writing better programs
    more clearly, and all code compatible with Python 3.7 should run fine on 3.8
    with minor, or no changes.  Python has adopted an anual release schedule
    (PEP-602), and Python 3.9 is  due to be released on June 2020.

    There are compelling reasons to upgrade 3.x projects to Python 3.8 (and no
    reasons not to).

    Python 3.7 will have bugfix releases only until mid 2020.
    Python 3.6 had its last bugfix release on December 2019.
    Python 3.5 has now entered "security fixes only" mode.
    Python 2.7 reached its end of life on January 2020.

    """
    pass

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.

TatSu supports left-recursive rules in PEG grammars using the algorithm by Laurent and Mens. The generated AST has the expected left associativity.

Installation

$ pip install TatSu

Using the Tool

TatSu can be used as a library, much like Python’s re, by embedding grammars as strings and generating grammar models instead of generating Python 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)
    ast = 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
        =
        | expression '+' term
        | expression '-' term
        | term
        ;


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


    factor
        =
        | '(' expression ')'
        | number
        ;


    number = /\d+/ ;
'''


if __name__ == '__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()

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

TatSu will use the first rule defined in the grammar as the start rule.

This is the output:

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

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

Documentation

For a detailed explanation of what 竜 TatSu is capable of, please see the documentation.

Questions?

Please use the [tatsu] tag on StackOverflow for general Q&A, and limit Github issues to bugs, enhancement proposals, and feature requests.

Changes

See the CHANGELOG for details.

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.

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-5.5.0.zip (227.3 kB view details)

Uploaded Source

Built Distribution

TatSu-5.5.0-py2.py3-none-any.whl (91.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file TatSu-5.5.0.zip.

File metadata

  • Download URL: TatSu-5.5.0.zip
  • Upload date:
  • Size: 227.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.1

File hashes

Hashes for TatSu-5.5.0.zip
Algorithm Hash digest
SHA256 0adbf7189a8c4f9a882b442f7b8ed6c6ab3baae37057db0e96b6888daacffad0
MD5 3528f16defd4a249be7ff8b59d594e61
BLAKE2b-256 a54ed1b6e96871d62ebd04a68e3886fceed945916fca69dcfdbbe73dbf7b3ba7

See more details on using hashes here.

File details

Details for the file TatSu-5.5.0-py2.py3-none-any.whl.

File metadata

  • Download URL: TatSu-5.5.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 91.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.1

File hashes

Hashes for TatSu-5.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3a043490e577632a05374b5033646bbc26cbb17386df81735a569ecbd45d934b
MD5 58ac00009d391b0beef866fa92ade6fe
BLAKE2b-256 2562f10fca3598dbc7beec7883e1050617f0b9abc68a6a741e3638540ffb60d4

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