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
actions
docs
installs
sponsor

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.

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

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

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 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.16.0.tar.gz (228.0 kB view details)

Uploaded Source

Built Distribution

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

tatsu-5.16.0-py3-none-any.whl (82.5 kB view details)

Uploaded Python 3

File details

Details for the file tatsu-5.16.0.tar.gz.

File metadata

  • Download URL: tatsu-5.16.0.tar.gz
  • Upload date:
  • Size: 228.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.2 cpython/3.15.0 HTTPX/0.28.1

File hashes

Hashes for tatsu-5.16.0.tar.gz
Algorithm Hash digest
SHA256 c78e47834d2111dcce327711a5ca96fb83a6e61b2ca283d2fba68bc750835966
MD5 15ab4026123d4668a0c1d24ebee4bb86
BLAKE2b-256 cf432f84054f1115cc7438721e1f7bb8546b08bf1c1ffcc6bb90b68447f6728a

See more details on using hashes here.

File details

Details for the file tatsu-5.16.0-py3-none-any.whl.

File metadata

  • Download URL: tatsu-5.16.0-py3-none-any.whl
  • Upload date:
  • Size: 82.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.2 cpython/3.15.0 HTTPX/0.28.1

File hashes

Hashes for tatsu-5.16.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48efb04a21e3da19d336e58cc022620cf7866790182358ec368c3afaebcddca7
MD5 6e381ff3bdf42d8a4ca9b63c25a6976f
BLAKE2b-256 50cfc0c01753b429b50228ae273522fc85cf723c3e792f8af13a2aaeea624a8a

See more details on using hashes here.

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