Skip to content

Instantly share code, notes, and snippets.

@mbednarski
Created May 27, 2022 20:46
Show Gist options
  • Save mbednarski/cc49a457fa3b323a55b1a11e61aa865d to your computer and use it in GitHub Desktop.
Save mbednarski/cc49a457fa3b323a55b1a11e61aa865d to your computer and use it in GitHub Desktop.
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