Skip to main content

A simple parsing tool in 200 lines of python

Give it a grammar and tokens, and it’ll give you back an ast.

Inputs

Use the parse() function to parse text. The tokens parameter is a dictionary of token names to compiled regular expressions. Prefixing a token with % will ignore it. Token names must be uppercase.

The rules parameter is a dictionary of names to lists of rules, and the start parameter is what node to start parsing first. postlex and postparse and post-processing functions. The default will:

  • after lexing remove all tokens that start with a %

  • after parsing flatten all nodes with 1 child

Rules

A rule is a list of rulesegments. A rulesegment can be a string, which means parse that TOKEN/rule_name. A list rule segement is a group, the last item in it can be one of the following:

  • ?: this group is optional

  • +: match at least one of this group, and possibly more

  • *: match any number of this group, including zero

An example rule looks like this:

[["NUM", "?"], ["var", "*"]]

The rule dictionary is a dictionary of rule_names to lists of these rules, each one being a possibility, like this:

{
    "multi_poly": [
        ["LPAREN", "polynomial", "RPAREN", "LPAREN", "polynomial", "RPAREN"],
        ["NUM", "LPAREN", "polynomial", "RPAREN"],
        ["polynomial"]
    ]
}

Output

The tree format of ParseIt is very simple: for rule instances (nodes) you have a tuple with the first value being the node name, and the second being its children.

Tokens are represented as a tuple of: the token name, the token content, and its position in the stream

treeify

There is also a module called treeify in the package that (if pydot is installed) allows you to create a graphical representation of the generated trees.

Example

example.py has an example grammar and code to convert the generated tree into a pydot

Metadata

Release files for parseit 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 parseit 1.0.1
File Size Uploaded Provenance
parseit-1.0.1.tar.gz 4.9 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for parseit 1.0.1
File
parseit-1.0.1-py3.6.egg Legacy Egg format - - Details
parseit-1.0.1-py3-none-any.whl Python 3 none any Details
parseit-1.0.1-py2.7.egg Legacy Egg format - - Details
parseit-1.0.1-py2-none-any.whl Python 2 none any Details

Total release size: 33.7 kB

Release files / parseit-1.0.1.tar.gz

Download URL parseit-1.0.1.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
fcc54bd5b83ae734f2e86249834be74bf0f552157656d7225e98b9bfb429f0ef
BLAKE2b-256 checksum
How to use checksums
44a6b34b5f636e689429f96621295ed6e1f234a8e2b9ba2622029ac9f530661b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / parseit-1.0.1-py3.6.egg

Download URL parseit-1.0.1-py3.6.egg
Size 5.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
580d1d2dc2127623a2b41e099c263a129f35529e237fcdbebaa0a1b700a73cb3
BLAKE2b-256 checksum
How to use checksums
62d0f2f2dcd7f461ad6c6c90d67811824bab842b939e675d92db6ef19a4b5c4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

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

Download URL parseit-1.0.1-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ed8d639124bbb9c7017d6bed2cee55e63ed1f124c47c8b2b07f3efb16d2ea10
BLAKE2b-256 checksum
How to use checksums
a867a2f8ea8ff2d1b02b4e199a75dbd2d66fc9ce98718e4a4848a83005a6920a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / parseit-1.0.1-py2.7.egg

Download URL parseit-1.0.1-py2.7.egg
Size 9.8 kB
Tags Egg
SHA-256 checksum
How to use checksums
5c0a9f1b4ce16c65540cd1d1126e9a153e211d56ea0a143c9334fbf9928cf4d6
BLAKE2b-256 checksum
How to use checksums
2d6fb68cca6dcbc84d40797751edbad19e79f00dd1cd97641c43cde8b7dfd01a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / parseit-1.0.1-py2-none-any.whl

Download URL parseit-1.0.1-py2-none-any.whl
Size 6.9 kB
Tags Python 2
SHA-256 checksum
How to use checksums
aa09a70c8207b216657b0b720e6f42186ef72134dc52a27b2bb4f5f0b303770c
BLAKE2b-256 checksum
How to use checksums
f53f477dee48e0cd0a8458ea175766aa48b2f4e39d818fbd39b965080f8e71f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for parseit 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

5 release files No provenance

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