Skip to content

Instantly share code, notes, and snippets.

View kartynnik's full-sized avatar

Yury Kartynnik kartynnik

View GitHub Profile
@kartynnik
kartynnik / parse_brackets.py
Last active August 29, 2021 06:21
Parsing bracket expressions via recursive descent
#!/usr/bin/env python3
"""Demonstrates parsing of bracket expressions via recursive descent.
The formal grammar is as follows (notice the lack of left recursion):
S := '(' S ')' S | '[' S ']' S | '{' S '}' S | '<' S '>' S | ''
Observe how `parse_expression` follows this definition.
"""
import enum
from typing import Iterable, Tuple