Skip to main content

Parser generator

Project description

image image image Actions status

syntactes

Python parser generator

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

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

syntactes-0.3.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

syntactes-0.3.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file syntactes-0.3.0.tar.gz.

File metadata

  • Download URL: syntactes-0.3.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for syntactes-0.3.0.tar.gz
Algorithm Hash digest
SHA256 aab54485238ff8b8daf568e32d4b31e7db6b7993e06123f54f9868009c65a092
MD5 cedce066c03841953d753066223b2926
BLAKE2b-256 2406bd8ddac173d0f571c0236479907fefac4051d94b4786289c096a3b3b59ed

See more details on using hashes here.

File details

Details for the file syntactes-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: syntactes-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for syntactes-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a2682584fcfa122a29dbf72f99a2f690c406760c7f12f02657f60e929a8faa8
MD5 1091c478604523578be3bded5618817e
BLAKE2b-256 166ef1bd37a80a0588d7c696036d3936085a41d87cf415479f70055779210cd1

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