Created
May 27, 2022 20:46
-
-
Save mbednarski/cc49a457fa3b323a55b1a11e61aa865d to your computer and use it in GitHub Desktop.
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 pathlib import Path | |
import lark | |
import rich | |
class Parser: | |
def __init__( | |
self, grammar_path: Path = Path("grammar.lark"), start: str = "program" | |
) -> None: | |
with grammar_path.open("rt") as f: | |
grammar_text = f.read() | |
self.lark = lark.Lark(grammar_text, start=start, ambiguity="explicit") | |
def parse_text(self, program_text: str) -> lark.ParseTree: | |
parsed = self.lark.parse(program_text) | |
return parsed | |
if __name__ == "__main__": | |
parser = Parser() | |
with Path("xd_programs/hello0.xd").open("rt") as f: | |
tree = parser.parse_text(f.read()) | |
rich.print(tree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment