Skip to main content

A Pythonic parser implementation!

Project description

parserlib

A Pythonic parser implementation!

Installation

    pip install parserlib

Example

from parserlib.lexer import Lexer
from parserlib.parser import Rule, ShiftReduceParser
from parserlib.token import Token


class NumberToken(Token):
    name = "NUMBER"
    regex = r"\d+"


class PlusToken(Token):
    name = "PLUS"
    regex = r"\+"


class MinusToken(Token):
    name = "MINUS"
    regex = r"-"


class LeftParenthesisToken(Token):
    name = "LEFT_PARENTHESIS"
    regex = r"\("


class RightParenthesisToken(Token):
    name = "RIGHT_PARENTHESIS"
    regex = r"\)"


class AsteriskToken(Token):
    name = "ASTERISK"
    regex = r"\*"


class SlashToken(Token):
    name = "SLASH"
    regex = r"/"


class Expression(Rule):
    pass


class NumberExpression(Expression):
    def __init__(self, number: NumberToken):
        self.number = number

    @classmethod
    def reduce(cls, number: NumberToken) -> Expression:
        return cls(number)


class ParenthesisExpression(Expression):
    @classmethod
    def reduce(
        cls, _: LeftParenthesisToken, expression: Expression, __: RightParenthesisToken
    ) -> Expression:
        return expression


class PlusExpression(Expression):
    def __init__(self, left: Expression, right: Expression):
        self.left = left
        self.right = right

    @classmethod
    def reduce(cls, left: Expression, _: PlusToken, right: Expression) -> Expression:
        return cls(left, right)


class MinusExpression(Expression):
    def __init__(self, left: Expression, right: Expression):
        self.left = left
        self.right = right

    @classmethod
    def reduce(cls, left: Expression, _: MinusToken, right: Expression) -> Expression:
        return cls(left, right)


class TimesExpression(Expression):
    def __init__(self, left: Expression, right: Expression):
        self.left = left
        self.right = right

    @classmethod
    def reduce(
        cls, left: Expression, _: AsteriskToken, right: Expression
    ) -> Expression:
        return cls(left, right)


class DivideExpression(Expression):
    def __init__(self, left: Expression, right: Expression):
        self.left = left
        self.right = right

    @classmethod
    def reduce(cls, left: Expression, _: SlashToken, right: Expression) -> Expression:
        return cls(left, right)


lexer = Lexer(
    tokens=(
        NumberToken,
        PlusToken,
        MinusToken,
        LeftParenthesisToken,
        RightParenthesisToken,
        AsteriskToken,
        SlashToken,
    ),
    ignore_characters=" \t",
)
parser = ShiftReduceParser(
    rules=(
        ParenthesisExpression,
        PlusExpression,
        MinusExpression,
        TimesExpression,
        DivideExpression,
        NumberExpression,
    ),
    precedence=(
        (ParenthesisExpression,),
        (TimesExpression, DivideExpression),
        (PlusExpression, MinusExpression),
    ),
)
tree = parser.parse(lexer.feed("1 + \n(2) * 3"))
print(tree)

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

parserlib-0.1.0.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

parserlib-0.1.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file parserlib-0.1.0.tar.gz.

File metadata

  • Download URL: parserlib-0.1.0.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.19.0-35-generic

File hashes

Hashes for parserlib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 95f966f40697d62e6618a7675f73fb0cc637ae8d5565ae3b6b70d98e230ef31a
MD5 f0d367710d2eb2cb475df67ea523a5f5
BLAKE2b-256 9f71d69f42893656e693a4d6c06358c183c2484f3033ecbff34f148ae3bb57b9

See more details on using hashes here.

File details

Details for the file parserlib-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: parserlib-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.19.0-35-generic

File hashes

Hashes for parserlib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b03e9737f9ee8c466a4be3b80bbfc519cd2a36227533729b39f96ff981e82771
MD5 a3e6382c753142bd51436268d40cfa2a
BLAKE2b-256 5097d6eb819c6444aa1fb2d8391c82ce81bdac4ad3988d161887e4c7902913d7

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