Last active
January 7, 2023 11:16
-
-
Save zehnpaard/5dbd8989c38d03d4dfb7ee08864f5ad4 to your computer and use it in GitHub Desktop.
Python Implementation of LL(1) Non-Recursive Parser based loosely on Language Implementation Patterns
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
list : '[' elements ']'; | |
elements : element (',' element)*; | |
element : ('a'..'z' | 'A'..'Z')+; |
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
class Peekable: | |
def __init__(self, input_, k, sentinel=None): | |
self.sentinel = sentinel | |
self._k = k | |
self._stream = iter(input_) | |
self._peek = [next(self._stream, sentinel) for _ in range(k)] | |
def __getitem__(self, n): | |
if isinstance(n, int) and n >= self._k: | |
raise IndexError(f"Invalid lookahead index {n} on Peekable with k={self._k}") | |
return self._peek[n] | |
def __iter__(self): | |
return self | |
def __next__(self): | |
if self._peek[0] == self.sentinel: | |
raise StopIteration | |
res = self._peek[0] | |
self._peek = self._peek[1:] | |
self._peek.append(next(self._stream, self.sentinel)) | |
return res |
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
import string | |
from utils import Peekable | |
import xtokens as t | |
def lex(char_iterable): | |
stream = Peekable(char_iterable, 1) | |
return _lex(stream) | |
def _lex(stream): | |
while True: | |
match stream[0]: | |
case stream.sentinel: | |
yield t.Eof() | |
break | |
case '[': | |
next(stream) | |
yield t.Lbrack() | |
case ']': | |
next(stream) | |
yield t.Rbrack() | |
case '=': | |
next(stream) | |
yield t.Equal() | |
case ',': | |
next(stream) | |
yield t.Comma() | |
case c if _is_letter(c): | |
yield _lex_ident(stream) | |
case c if c in string.whitespace: | |
next(stream) | |
case c: | |
raise ValueError(f"Invalid character {c}") | |
def _lex_ident(stream): | |
cs = [] | |
while _is_letter(stream[0]): | |
cs.append(next(stream)) | |
return t.Ident(''.join(cs)) | |
def _is_letter(c): | |
return c in string.ascii_letters |
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
import xtokens as t | |
import xlexer as xl | |
from utils import Peekable | |
def parse(char_stream): | |
tokens = Peekable(xl.lex(char_stream), 1) | |
_list(tokens) | |
_match(tokens, t.Eof) | |
def _list(tokens): | |
_match(tokens, t.Lbrack) | |
_elements(tokens) | |
_match(tokens, t.Rbrack) | |
def _elements(tokens): | |
_element(tokens) | |
while tokens[0] == t.Comma(): | |
next(tokens) | |
_element(tokens) | |
def _element(tokens): | |
match tokens[0]: | |
case t.Ident(s): | |
next(tokens) | |
case token: | |
raise ValueError(f"Invalid token {token}") | |
def _match(tokens, token_type): | |
if isinstance(tokens[0], token_type): | |
next(tokens) | |
else: | |
raise ValueError(f"Failed to match {tokens[0]} to type {token_type}") |
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
from dataclasses import dataclass | |
@dataclass | |
class Lbrack: | |
pass | |
@dataclass | |
class Rbrack: | |
pass | |
@dataclass | |
class Equal: | |
pass | |
@dataclass | |
class Comma: | |
pass | |
@dataclass | |
class Eof: | |
pass | |
@dataclass | |
class Ident: | |
s : str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment