Skip to main content

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

Project description

license pyversions fury downloads actions 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

TatSu is a tool that takes grammars in a variation of EBNF as input, and outputs memoizing (Packrat) PEG parsers in Python.

Why use a PEG parser? Because regular languages (those parsable with Python’s re package) “cannot count”. Any language with nested structures or with balancing of demarcations requires more than regular expressions to be parsed.

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.

Starting with version 5.8.0 竜 TatSu requires Python 3.10 or later. While no code in 竜 TatSu yet depends on new language or standard library features, the authors don’t want to be constrained by Python version comaptibility consideration when developing features that will be part future releases. Therefore, to simplify version pinning for users of the library, they decided to proactively bump the Python minimum required version to 3.10.

TatSu releases in the 5.7 series closely track releases in the 5.8 series while maintaining compatibility with Python 3.8 and later. Bug fixes are back-ported from 5.8 releases. Features are back-ported from the 5.8 releases unless they depend on Python features not available on the supported Python versions. Refer to the CHANGELOG for details.

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 json
    from tatsu import parse
    from tatsu.util import asjson

    ast = parse(GRAMMAR, '3 + 5 * ( 10 - 20 )')
    print(json.dumps(asjson(ast), indent=2))

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

This is the output:

[
  "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.8.3.zip (205.2 kB view details)

Uploaded Source

Built Distribution

TatSu-5.8.3-py2.py3-none-any.whl (101.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file TatSu-5.8.3.zip.

File metadata

  • Download URL: TatSu-5.8.3.zip
  • Upload date:
  • Size: 205.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for TatSu-5.8.3.zip
Algorithm Hash digest
SHA256 571ecbcdf33b7828c05e5cd95a8e8ad06af111c2c83a6a245be4d8f7c43de7bb
MD5 b86c5b4ba77b1eaa4a46d38ecdccbeae
BLAKE2b-256 dde6f46e17ed341ff7584b23a63508c781e6e5e984a9e6eb0b66b273991a02eb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: TatSu-5.8.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 101.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for TatSu-5.8.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0a836692e67247cad9f251e083b045b13345cc715e69a7fbc16522beaa0f2163
MD5 a7735c93ec02976826ceda567441b8ff
BLAKE2b-256 7867413a03b1048f9f237e3f242e6e829688d8c9cf1fbe6bd8bb5f574ae67ac9

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