Skip to content

Instantly share code, notes, and snippets.

@mbednarski
Created June 1, 2022 14:32
Show Gist options
  • Save mbednarski/ea004594b22dbe96cafd799eb0f3e27f to your computer and use it in GitHub Desktop.
Save mbednarski/ea004594b22dbe96cafd799eb0f3e27f 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment