TatSu takes a grammar in a variation of EBNF as input, and outputs a memoizing PEG/Packrat parser in Python.
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.
def WARNING(): """ TatSu>=5.6 requires Python>=3.8 TatSu>=5.7 will require Python>=3.9 Python 3.8 and 3.9 introduced new language features that allow writing better programs more clearly. All code written for Python 3.7 should run fine on Python 3.9 with minor, or no changes. Python has adopted an annual release schedule (PEP-602). Python 3.10 will be released in Oct 2021 Python 3.9 was released on Oct 20220 Python 3.8 bugfix releases final in May 2021 Python 3.7 bugfix releases final in mid 2020 Python 3.6 had its last bugfix release on Dec 2019 Python 2.7 reached its end of life on Jan 2020 Compelling reasons to upgrade projects to the latest Python """ pass
竜 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.
$ pip install TatSu
Using the Tool
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" ] ] ]
For a detailed explanation of what 竜 TatSu is capable of, please see the documentation.
See the CHANGELOG for details.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size TatSu-5.6.1-py2.py3-none-any.whl (94.6 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
|Filename, size TatSu-5.6.1.zip (192.8 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for TatSu-5.6.1-py2.py3-none-any.whl