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
Release history Release notifications | RSS feed
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.1.tar.gz
(15.7 kB
view details)
Built Distribution
parserlib-0.1.1-py3-none-any.whl
(17.4 kB
view details)
File details
Details for the file parserlib-0.1.1.tar.gz
.
File metadata
- Download URL: parserlib-0.1.1.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e17c14faa7883a7da7fe264134a436ff5937d3b89fef3f274563e35b6f500b17 |
|
MD5 | c66145dac47961a926baa892aee4466d |
|
BLAKE2b-256 | cf18e146a3abed95b39440ccd41b0caae73b3c3b9e4e8442a3827fbaf83834fe |
File details
Details for the file parserlib-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: parserlib-0.1.1-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3715da04179c97fe3d7c9474cedb71a11e1ff3db5e5635082a35454f1c507df |
|
MD5 | 467e15c945c6687037b1ecf3f98a15a7 |
|
BLAKE2b-256 | 9dc15dff3a517a62d802b3d60e7ba397e616293b267bb530f31fd28b31dff26c |