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

竜 TatSu-LTS

竜 TatSu-LTS is a friendly fork of 竜 TatSu that guarantees compatibility with all supported versions of Python. Compatibility with older Python versions is maintained as long as that does not require additional changes. Only patches required to support Python version older than the ones supported by the upstream project are applied. 竜 TatSu-LTS releases use the same version number as the 竜 TatSu release on which they are based and should be published shortly after the corresponding upstream release.

Both the 竜 TatSu and 竜 TatSu-LTS distributions install a Python package and an executable named tatsu. Users can thus switch seamlessly between the two, depending on which Python version support guarantees they prefer. For project that desire to support a wide range of Python versions, there is no drawback in depending on 竜 TatSu-LTS other than the short delay in the release of new versions.

竜 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.

竜 TatSu expects a maintained version of Python (>=3.14 at the moment). 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 compatibility considerations when developing future releases. That said, currently all tests run in versions down to Python 3.10.

If you need support for previous versions of Python, please consider TatSu-LTS, a friendly fork of 竜 TatSu aimed at compatibility with other versions of Python still used by many projects. The developers of both projects work together to promote compatibility with most versions of Python.

Installation

$ pip install TatSu-LTS

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.

This compiles the grammar and generates an in-memory parser that can subsequently be used for parsing input with:

parser = tatsu.compile(grammar)

Compiles the grammar and parses the given input producing an AST as result:

ast = tatsu.parse(grammar, input)

The result is equivalent to calling:

parser = compile(grammar)
ast = parser.parse(input)

Compiled grammars are cached for efficiency.

This compiles the grammar to the Python sourcecode that implements the parser:

parser_source = tatsu.to_python_sourcecode(grammar)

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 RELEASES 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


Download files

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

Source Distribution

tatsu_lts-5.15.1.tar.gz (127.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tatsu_lts-5.15.1-py3-none-any.whl (79.5 kB view details)

Uploaded Python 3

File details

Details for the file tatsu_lts-5.15.1.tar.gz.

File metadata

  • Download URL: tatsu_lts-5.15.1.tar.gz
  • Upload date:
  • Size: 127.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tatsu_lts-5.15.1.tar.gz
Algorithm Hash digest
SHA256 d63b796fd7d5514c5250c8aa99863e21df64038c2579ea40a5b6f7dedeb29404
MD5 df5e05d3d52e253137130695ed670f4b
BLAKE2b-256 8cc24602b4e1ba962af251773232988d751e698ba0cbf461d7d5f1b304b4ef4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for tatsu_lts-5.15.1.tar.gz:

Publisher: release.yml on dnicolodi/TatSu-LTS

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tatsu_lts-5.15.1-py3-none-any.whl.

File metadata

  • Download URL: tatsu_lts-5.15.1-py3-none-any.whl
  • Upload date:
  • Size: 79.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tatsu_lts-5.15.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0aa4889eacca2045aba31a0f89d285c033e9003a0b8fdcc97cb3f9a02a2887fc
MD5 15e33f84911307f62462d267c02d4c47
BLAKE2b-256 008563e7e9a720045ffccf27440006a3f03088b9ba3df8fb9b5c1243c3558046

See more details on using hashes here.

Provenance

The following attestation bundles were made for tatsu_lts-5.15.1-py3-none-any.whl:

Publisher: release.yml on dnicolodi/TatSu-LTS

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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