Skip to main content

Simple parser that creates ASTs

Project description

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

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

parseit-1.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distributions

parseit-1.0.1-py3.6.egg (5.1 kB view details)

Uploaded Source

parseit-1.0.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

parseit-1.0.1-py2.7.egg (9.8 kB view details)

Uploaded Source

parseit-1.0.1-py2-none-any.whl (6.9 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: parseit-1.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for parseit-1.0.1.tar.gz
Algorithm Hash digest
SHA256 fcc54bd5b83ae734f2e86249834be74bf0f552157656d7225e98b9bfb429f0ef
MD5 c8125a599ef2d71b91737312bb4a6eca
BLAKE2b-256 44a6b34b5f636e689429f96621295ed6e1f234a8e2b9ba2622029ac9f530661b

See more details on using hashes here.

File details

Details for the file parseit-1.0.1-py3.6.egg.

File metadata

  • Download URL: parseit-1.0.1-py3.6.egg
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for parseit-1.0.1-py3.6.egg
Algorithm Hash digest
SHA256 580d1d2dc2127623a2b41e099c263a129f35529e237fcdbebaa0a1b700a73cb3
MD5 6b3fe83cd51e9d26cb7a6469e135337b
BLAKE2b-256 62d0f2f2dcd7f461ad6c6c90d67811824bab842b939e675d92db6ef19a4b5c4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for parseit-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8ed8d639124bbb9c7017d6bed2cee55e63ed1f124c47c8b2b07f3efb16d2ea10
MD5 b49dccab6658c91624a16b363698d813
BLAKE2b-256 a867a2f8ea8ff2d1b02b4e199a75dbd2d66fc9ce98718e4a4848a83005a6920a

See more details on using hashes here.

File details

Details for the file parseit-1.0.1-py2.7.egg.

File metadata

  • Download URL: parseit-1.0.1-py2.7.egg
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for parseit-1.0.1-py2.7.egg
Algorithm Hash digest
SHA256 5c0a9f1b4ce16c65540cd1d1126e9a153e211d56ea0a143c9334fbf9928cf4d6
MD5 4e32e120dd2985098a84223cac396eae
BLAKE2b-256 2d6fb68cca6dcbc84d40797751edbad19e79f00dd1cd97641c43cde8b7dfd01a

See more details on using hashes here.

File details

Details for the file parseit-1.0.1-py2-none-any.whl.

File metadata

File hashes

Hashes for parseit-1.0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 aa09a70c8207b216657b0b720e6f42186ef72134dc52a27b2bb4f5f0b303770c
MD5 04f98ac1305392c691adfabd74542415
BLAKE2b-256 f53f477dee48e0cd0a8458ea175766aa48b2f4e39d818fbd39b965080f8e71f0

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