Skip to main content

Grammars, parsers, and compilers in Python 3.6+

Project description

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},
            ],
        },
    ],
}

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

shreducer-1.0.1.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

shreducer-1.0.1-py3-none-any.whl (39.7 kB view details)

Uploaded Python 3

File details

Details for the file shreducer-1.0.1.tar.gz.

File metadata

  • Download URL: shreducer-1.0.1.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for shreducer-1.0.1.tar.gz
Algorithm Hash digest
SHA256 35d35ab034c14517ec2ecc6d5b483b519b59d40ceb01e7f660fe402bf6e1a81b
MD5 5ab30f29481e5e0a8ca38623c727e45b
BLAKE2b-256 ac25c65af496d49dea8c2530822f966855a55ce08d62d73c400286aa6b332c9f

See more details on using hashes here.

File details

Details for the file shreducer-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: shreducer-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 39.7 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for shreducer-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 346fb326ff3efe8de246b909e5dbf2694837508a9437c0bf08600076b908bc24
MD5 9e605d3d1a3de5c0056c26f7990a2adc
BLAKE2b-256 4527942ca9a59e030c44fc0c1e58f7db67ff96c6ff07841cf3fccff63eca62fc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page