syntactes


Namesyntactes JSON
Version 0.3.1 PyPI version JSON
download
home_pageNone
SummaryParser generator
upload_time2024-11-05 14:26:39
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseMIT
keywords parser generator
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
[![image](https://img.shields.io/pypi/v/syntactes.svg)](https://pypi.python.org/pypi/syntactes)
[![image](https://img.shields.io/pypi/l/syntactes.svg)](https://opensource.org/license/mit/)
[![image](https://img.shields.io/pypi/pyversions/syntactes.svg)](https://pypi.python.org/pypi/syntactes)
[![Actions status](https://github.com/Maxcode123/syntactes/actions/workflows/test-package.yml/badge.svg?branch=main)](https://github.com/Maxcode123/syntactes/actions/workflows/test-package.yml?query=branch%3Amain)
---
# syntactes
Python parser generator

## Quick start

### Creating a parsing table
```py
from syntactes import Grammar, Rule, SLRGenerator, Token

EOF = Token.eof()
S = Token("S", is_terminal=False)
E = Token("E", False)
T = Token("T", False)
x = Token("x", True)
PLUS = Token("+", True)

tokens = {EOF, S, E, T, x, PLUS}

# 0. S -> E $
# 1. E -> T + E
# 2. E -> T
# 3. T -> x
rule_1 = Rule(0, S, E, EOF)
rule_2 = Rule(1, E, T, PLUS, E)
rule_3 = Rule(2, E, T)
rule_4 = Rule(4, T, x)

rules = (rule_1, rule_2, rule_3, rule_4)

grammar = Grammar(rule_1, rules, tokens)

generator = SLRGenerator(grammar)

parsing_table = generator.generate()

print(parsing_table.pretty_str())
```

Running the above example produces this output:
```
GRAMMAR RULES
-------------
0. S -> E $
1. E -> T + E
2. E -> T
3. T -> x
-------------

SLR PARSING TABLE
-------------------------------------------------
|     |  $   |  +   |  E   |  S   |  T   |  x  |
-------------------------------------------------
|  1  |  --  |  --  |  s4  |  --  |  s2  |  s3 |
-------------------------------------------------
|  2  |  r2  |  s5  |  --  |  --  |  --  |  -- |
-------------------------------------------------
|  3  |  r4  |  r4  |  --  |  --  |  --  |  -- |
-------------------------------------------------
|  4  |  a  |  --  |  --  |  --  |  --  |  -- |
------------------------------------------------
|  5  |  --  |  --  |  s6  |  --  |  s2  |  s3 |
-------------------------------------------------
|  6  |  r1  |  --  |  --  |  --  |  --  |  -- |
-------------------------------------------------
```

### Parsing

```py
from syntactes import Grammar, Rule, Token
from syntactes.parser import ParserError, SLRParser, execute_on

EOF = Token.eof()
S = Token("S", is_terminal=False)
E = Token("E", False)
T = Token("T", False)
x = Token("x", True, 1)  # value of token is 1
PLUS = Token("+", True)

tokens = {EOF, S, E, T, x, PLUS}

# 0. S -> E $
# 1. E -> T + E
# 2. E -> T
# 3. T -> x
rule_1 = Rule(0, S, E, EOF)
rule_2 = Rule(1, E, T, PLUS, E)
rule_3 = Rule(2, E, T)
rule_4 = Rule(4, T, x)

rules = (rule_1, rule_2, rule_3, rule_4)

grammar = Grammar(rule_1, rules, tokens)

parser = SLRParser.from_grammar(grammar)


@execute_on(rule_4)
def push_value(x_token):
    # Add and argument for every token on the right-hand side of the rule.
    print(
        f"received token {x_token} with value: {x_token.value}, reducing by rule: {rule_4}"
    )


@execute_on(rule_2)
def add(left, plus, right):
    print(f"received tokens {left}, {plus}, {right}, reducing by rule: {rule_2}")


print("Parsing stream: x + x + x $\n")
parser.parse([x, PLUS, x, PLUS, x, EOF])

print("\nParsing stream: x + $\n")
try:
    parser.parse([x, PLUS, EOF])
except ParserError as e:
    print("ParserError:", e)
```

Running the above example produces this output:
```
Parsing stream: x + x + x $

received token x with value: 1, reducing by rule: T -> x
received token x with value: 1, reducing by rule: T -> x
received token x with value: 1, reducing by rule: T -> x
received tokens E, +, T, reducing by rule: E -> T + E
received tokens E, +, T, reducing by rule: E -> T + E

Parsing stream: x + $

received token x with value: 1, reducing by rule: T -> x
ParserError: Received token: $; expected one of: ['x', 'T', 'E']
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "syntactes",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "parser, generator",
    "author": null,
    "author_email": "Maximos Nikiforakis <nikiforos@live.co.uk>",
    "download_url": "https://files.pythonhosted.org/packages/02/38/73612f0185f323d61e324c5d03c6e9b7d540e52ab511ebf8a036af9b48aa/syntactes-0.3.1.tar.gz",
    "platform": null,
    "description": "\n[![image](https://img.shields.io/pypi/v/syntactes.svg)](https://pypi.python.org/pypi/syntactes)\n[![image](https://img.shields.io/pypi/l/syntactes.svg)](https://opensource.org/license/mit/)\n[![image](https://img.shields.io/pypi/pyversions/syntactes.svg)](https://pypi.python.org/pypi/syntactes)\n[![Actions status](https://github.com/Maxcode123/syntactes/actions/workflows/test-package.yml/badge.svg?branch=main)](https://github.com/Maxcode123/syntactes/actions/workflows/test-package.yml?query=branch%3Amain)\n---\n# syntactes\nPython parser generator\n\n## Quick start\n\n### Creating a parsing table\n```py\nfrom syntactes import Grammar, Rule, SLRGenerator, Token\n\nEOF = Token.eof()\nS = Token(\"S\", is_terminal=False)\nE = Token(\"E\", False)\nT = Token(\"T\", False)\nx = Token(\"x\", True)\nPLUS = Token(\"+\", True)\n\ntokens = {EOF, S, E, T, x, PLUS}\n\n# 0. S -> E $\n# 1. E -> T + E\n# 2. E -> T\n# 3. T -> x\nrule_1 = Rule(0, S, E, EOF)\nrule_2 = Rule(1, E, T, PLUS, E)\nrule_3 = Rule(2, E, T)\nrule_4 = Rule(4, T, x)\n\nrules = (rule_1, rule_2, rule_3, rule_4)\n\ngrammar = Grammar(rule_1, rules, tokens)\n\ngenerator = SLRGenerator(grammar)\n\nparsing_table = generator.generate()\n\nprint(parsing_table.pretty_str())\n```\n\nRunning the above example produces this output:\n```\nGRAMMAR RULES\n-------------\n0. S -> E $\n1. E -> T + E\n2. E -> T\n3. T -> x\n-------------\n\nSLR PARSING TABLE\n-------------------------------------------------\n|     |  $   |  +   |  E   |  S   |  T   |  x  |\n-------------------------------------------------\n|  1  |  --  |  --  |  s4  |  --  |  s2  |  s3 |\n-------------------------------------------------\n|  2  |  r2  |  s5  |  --  |  --  |  --  |  -- |\n-------------------------------------------------\n|  3  |  r4  |  r4  |  --  |  --  |  --  |  -- |\n-------------------------------------------------\n|  4  |  a  |  --  |  --  |  --  |  --  |  -- |\n------------------------------------------------\n|  5  |  --  |  --  |  s6  |  --  |  s2  |  s3 |\n-------------------------------------------------\n|  6  |  r1  |  --  |  --  |  --  |  --  |  -- |\n-------------------------------------------------\n```\n\n### Parsing\n\n```py\nfrom syntactes import Grammar, Rule, Token\nfrom syntactes.parser import ParserError, SLRParser, execute_on\n\nEOF = Token.eof()\nS = Token(\"S\", is_terminal=False)\nE = Token(\"E\", False)\nT = Token(\"T\", False)\nx = Token(\"x\", True, 1)  # value of token is 1\nPLUS = Token(\"+\", True)\n\ntokens = {EOF, S, E, T, x, PLUS}\n\n# 0. S -> E $\n# 1. E -> T + E\n# 2. E -> T\n# 3. T -> x\nrule_1 = Rule(0, S, E, EOF)\nrule_2 = Rule(1, E, T, PLUS, E)\nrule_3 = Rule(2, E, T)\nrule_4 = Rule(4, T, x)\n\nrules = (rule_1, rule_2, rule_3, rule_4)\n\ngrammar = Grammar(rule_1, rules, tokens)\n\nparser = SLRParser.from_grammar(grammar)\n\n\n@execute_on(rule_4)\ndef push_value(x_token):\n    # Add and argument for every token on the right-hand side of the rule.\n    print(\n        f\"received token {x_token} with value: {x_token.value}, reducing by rule: {rule_4}\"\n    )\n\n\n@execute_on(rule_2)\ndef add(left, plus, right):\n    print(f\"received tokens {left}, {plus}, {right}, reducing by rule: {rule_2}\")\n\n\nprint(\"Parsing stream: x + x + x $\\n\")\nparser.parse([x, PLUS, x, PLUS, x, EOF])\n\nprint(\"\\nParsing stream: x + $\\n\")\ntry:\n    parser.parse([x, PLUS, EOF])\nexcept ParserError as e:\n    print(\"ParserError:\", e)\n```\n\nRunning the above example produces this output:\n```\nParsing stream: x + x + x $\n\nreceived token x with value: 1, reducing by rule: T -> x\nreceived token x with value: 1, reducing by rule: T -> x\nreceived token x with value: 1, reducing by rule: T -> x\nreceived tokens E, +, T, reducing by rule: E -> T + E\nreceived tokens E, +, T, reducing by rule: E -> T + E\n\nParsing stream: x + $\n\nreceived token x with value: 1, reducing by rule: T -> x\nParserError: Received token: $; expected one of: ['x', 'T', 'E']\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Parser generator",
    "version": "0.3.1",
    "project_urls": {
        "Homepage": "https://github.com/Maxcode123/syntactes",
        "Issues": "https://github.com/Maxcode123/syntactes/issues"
    },
    "split_keywords": [
        "parser",
        " generator"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0c415e76fd93f0cbd5dd36cc69c8437739570ca323b1aca613582907b020fff2",
                "md5": "2808ab5fdceefc1e1e23c14627052515",
                "sha256": "c4e17a57413cbb13dca20c075710d9d7b31fae58f3326659e51d025cd8e757c3"
            },
            "downloads": -1,
            "filename": "syntactes-0.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2808ab5fdceefc1e1e23c14627052515",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 14165,
            "upload_time": "2024-11-05T14:26:38",
            "upload_time_iso_8601": "2024-11-05T14:26:38.685495Z",
            "url": "https://files.pythonhosted.org/packages/0c/41/5e76fd93f0cbd5dd36cc69c8437739570ca323b1aca613582907b020fff2/syntactes-0.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "023873612f0185f323d61e324c5d03c6e9b7d540e52ab511ebf8a036af9b48aa",
                "md5": "b89682e98245f47b0853b911ecbb1288",
                "sha256": "a6329c34f4c3388bd97bd759774c3c3b89b8a1a4a83b3ee4ff66463c0891e20e"
            },
            "downloads": -1,
            "filename": "syntactes-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "b89682e98245f47b0853b911ecbb1288",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 11327,
            "upload_time": "2024-11-05T14:26:39",
            "upload_time_iso_8601": "2024-11-05T14:26:39.897453Z",
            "url": "https://files.pythonhosted.org/packages/02/38/73612f0185f323d61e324c5d03c6e9b7d540e52ab511ebf8a036af9b48aa/syntactes-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-05 14:26:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Maxcode123",
    "github_project": "syntactes",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "syntactes"
}
        
Elapsed time: 0.34365s