Skip to main content

shreducer

Simple compilers in Python for fun and profit.

Example Grammars Included in This Project

If you want to reuse the examples code, feel free to copy the code into your projects, but do not introduce dependencies on examples as the examples may be changed or removed without notice. Build your own grammar and related classes by extending classes provided in the shreducer package only.

  • Python 3.6+ type hints string representation

  • Filter expressions for web APIs. Allows parsing of expressions like this: https://your.api?filter=(status eq open and type eq store) or (status eq closed and type not in office, garage)

  • With a compiler of Elasticsearch queries included (not tested since 2016)

  • A couple of primitive grammars for basic arithmetic expressions as examples

Components

  • Tokenizers (no need to implement your own, we rely on shlex from Python Standard Library)

  • Grammars

  • Parsers (no need to implement your own, we use a shift-reduce parser, hence the name of the project)

  • Generators

Tokenizer splits input string into lexical units of the grammar (we call them tokens here).

Grammar describes the syntax rules of a language that we want to parse.

Parser parses sequence of tokens and generates a parse tree. For example ('+', 2, ('-', 10, 3)) could be a parse tree.

Generator takes a parse tree and evaluates it. For example, an arithmetic generator could take a parse tree ('+', 2, ('-', 10, 3)) as an input and produce 9 as an output.

All these components together make a compiler.

Expressing a New Grammar

See examples under shreducer_examples/:

  • DictG - simplest of all grammars, the most suitable to understand the basic idea, parser produces parsed dictionary

  • ListG - another simple grammar, but unlike dictionary grammar, parser for this one produces parse tree

  • PlusMinusArithmeticsG - simple arithmetic expression parser, parser produces parse tree

  • BetterArithmeticsG - arithmetic expression parser that respects operator precedence, parser produces parse tree

  • FilterExpressionsG - comparison operators and logical operators, parser produces parse tree

  • BetterFiltersG - comparatively rich filter expression language, unlike other grammars this one uses look-ahead, parser produces parse tree

  • TypeHintsG - parsing Python 3.6+ type hints string representation

There is some magic (a meta class) going on in t class to allow declaring a string constant without writing its value twice:

class MyGrammar(Grammar):
    class t:
        IDENT = None
        PLUS_MINUS = '+-'
        EXPR = ()

After the class MyGrammar is created, the value of MyGrammar.t.IDENT will be "IDENT". Similarly, MyGrammar.t.PLUS_MINUS will be "PLUS_MINUS", and MyGrammar.t.EXPR will be "EXPR".

Member of class t with value None is treated as the default token type.

Members of class t with value () are treated as names of tokens of higher order – expressions.

Testing Your New Grammar

If you implement just a grammar, you can try parsing input strings with Grammar.simple_parse (which is a class method).

For example, to try TypeHintsG (grammar for parsing type hints string representation in Python 3.6+), you can do:

print(TypeHintsG.simple_parse('typing.Union[typing.List[str], typing.Dict[str, int]]'))

This will produce the following parse tree:

{
    "name": "typing.Union",
    "args": [
        {
            "name": "typing.List",
            "args": [
                {"name": "str", "args": None},
            ],
        },
        {
            "name": "typing.Dict",
            "args": [
                {"name": "str", "args": None},
                {"name": "int", "args": None},
            ],
        },
    ],
}

Metadata

Release files for shreducer 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shreducer 1.0.1
File Size Uploaded
shreducer-1.0.1.tar.gz 18.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for shreducer 1.0.1
File Interpreter ABI Platform
shreducer-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 58.4 kB

Release files / shreducer-1.0.1.tar.gz

Download URL shreducer-1.0.1.tar.gz
Size 18.7 kB
Tags Source
SHA-256 checksum
How to use checksums
35d35ab034c14517ec2ecc6d5b483b519b59d40ceb01e7f660fe402bf6e1a81b
BLAKE2b-256 checksum
How to use checksums
ac25c65af496d49dea8c2530822f966855a55ce08d62d73c400286aa6b332c9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release files / shreducer-1.0.1-py3-none-any.whl

Download URL shreducer-1.0.1-py3-none-any.whl
Size 39.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
346fb326ff3efe8de246b909e5dbf2694837508a9437c0bf08600076b908bc24
BLAKE2b-256 checksum
How to use checksums
4527942ca9a59e030c44fc0c1e58f7db67ff96c6ff07841cf3fccff63eca62fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page