-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
663 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .generator import LR0Generator, SLRGenerator | ||
from .grammar import Grammar | ||
from .rule import Rule | ||
from .token import Token | ||
from .rule import Rule | ||
from .grammar import Grammar | ||
from .generator import LR0Generator, SLRGenerator | ||
from .table import LR0ParsingTable, SLRParsingTable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .exception import NotAcceptedError, ParserError, UnexpectedTokenError | ||
from .execute import ExecutablesRegistry, execute_on | ||
from .parser import LR0Parser, SLRParser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class ParserError(Exception): ... | ||
|
||
|
||
class UnexpectedTokenError(ParserError): | ||
""" | ||
A token was received that does not map to an action. The stream of tokens | ||
is syntactically invalid. | ||
""" | ||
|
||
def __init__(self, received_token, expected_tokens): | ||
self.received_token = received_token | ||
self.expected_tokens = expected_tokens | ||
msg = f"Received token: {received_token}; expected one of: {[str(e) for e in expected_tokens]}" | ||
super().__init__(msg) | ||
|
||
|
||
class NotAcceptedError(ParserError): | ||
""" | ||
The parser did not receive an accept action. The stream of tokens is | ||
syntactically invalid. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import functools | ||
from collections.abc import Callable | ||
from typing import TypeAlias | ||
|
||
from syntactes import Rule | ||
|
||
Executable: TypeAlias = Callable[[...], None] | ||
|
||
|
||
def execute_on(rule: Rule): | ||
""" | ||
Decorate a function to be executed upon recognition of `rule` by the parser. | ||
""" | ||
|
||
def executable_decorator(executable_fn: Executable) -> Executable: | ||
ExecutablesRegistry.register(rule, executable_fn) | ||
|
||
@functools.wraps(executable_fn) | ||
def wrapped_executable_fn(*args, **kwargs) -> None: | ||
return executable_fn(*args, **kwargs) | ||
|
||
return wrapped_executable_fn | ||
|
||
return executable_decorator | ||
|
||
|
||
class ExecutablesRegistry: | ||
""" | ||
Registry of executable functions, i.e. functions that get called when a grammar | ||
rule is recognized by the parser. | ||
""" | ||
|
||
_registry: dict[Rule, Executable] = {} | ||
|
||
@classmethod | ||
def register(cls, rule: Rule, executable_fn: Executable) -> None: | ||
""" | ||
Register a function to be executed upon recognition of the given rule. | ||
""" | ||
cls._registry[rule] = executable_fn | ||
|
||
@classmethod | ||
def get(cls, rule: Rule) -> Executable: | ||
""" | ||
Get the executable registered for the given rule. | ||
If no executable is registered returns a function that does nothing. | ||
""" | ||
return cls._registry.get(rule, lambda *_, **__: None) | ||
|
||
@classmethod | ||
def clear(cls) -> None: | ||
""" | ||
Clear all registered rules. | ||
""" | ||
cls._registry.clear() |
Oops, something went wrong.