TatSu takes a grammar in a variation of EBNF as input, and outputs a memoizing PEG/Packrat parser in Python.
Project description
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.
竜 TatSu has experimental support for 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"
]
]
]
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 of, 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
Built Distribution
File details
Details for the file TatSu-4.3.0.zip
.
File metadata
- Download URL: TatSu-4.3.0.zip
- Upload date:
- Size: 130.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cdfde0139cf0c17894891cb4a5351875c42367cbb9324dd22e19cc4b2bade7c |
|
MD5 | 976949b71652a17d2c023418661f60d4 |
|
BLAKE2b-256 | 8e88ec6d0364c15e568bbb77e1272ce5712ce5d0ed2f6fe0f057fe2d55d13608 |
File details
Details for the file TatSu-4.3.0-py2.py3-none-any.whl
.
File metadata
- Download URL: TatSu-4.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 81.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6abc825a16dc60def2951c3ac6369594a68c014e7f6e64c734bdb5b530e16fb4 |
|
MD5 | cce2b64d50da8fb5a56053a367af6238 |
|
BLAKE2b-256 | 4a2a73ab41b283bdad217bbcd58751662d7edcb650c3dc810c8caa58f2fdcf49 |