Skip to main content

image image image Actions status

syntactes

A simpler Python parser generator.
The name is derived from Greek συντάκτης (/sin'daktis/) meaning editor/composer.

Features

  • Parsing table creation
  • Token parsing and action execution

Installation

> pip install syntactes

Quick start

Creating a parsing table

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

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']

Release files for syntactes 0.4.0

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

Source distribution (sdist)

Source distribution for syntactes 0.4.0
File Size Uploaded
syntactes-0.4.0.tar.gz 13.5 kB Details

Built distribution (wheel)

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

Total release size:29.8 kB

Release files / syntactes-0.4.0.tar.gz

Download URL syntactes-0.4.0.tar.gz
Size 13.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3b48098c101336c7bc5b58eaaff37a70ed828c14e877428dc1f3c9ec25273126
BLAKE2b-256 checksum
How to use checksums
39f0808d45a037a26492596d732c1dd6243f809a43846eebc38b623f41bb1f24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / syntactes-0.4.0-py3-none-any.whl

Download URL syntactes-0.4.0-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3dc0da8dead0f195847c02c3ad4eb4d1c079ec2e0f3503964f501f1d2a57f5f9
BLAKE2b-256 checksum
How to use checksums
e6f6a17557b320c5b8474fd5e19a6dff8c6b42f9e9db9f8be9d0acdf24db9c1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.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