Lark Parsing Library

1.3.1 · active · verified Sat Mar 28

Lark is a modern and comprehensive parsing toolkit for Python, designed to handle any context-free grammar. It offers multiple parsing algorithms (Earley, LALR(1), CYK), EBNF-inspired grammar syntax, and automatically constructs parse trees (ASTs). Known for its ergonomics, performance, and modularity, Lark receives frequent updates and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple arithmetic grammar, create a Lark parser with a `Transformer` to evaluate expressions, and parse input. It uses the LALR(1) parser for efficiency.

from lark import Lark, Transformer, v_args

# Define your grammar using EBNF syntax
grammar = '''
    ?start: expr
    ?expr: term (("+" | "-") term)*
    ?term: factor (("*") factor)*
    ?factor: NUMBER | "(" expr ")"

    %import common.NUMBER
    %import common.WS
    %ignore WS
'''

@v_args(inline=True)    # Affects the signatures of the methods
class CalculateTree(Transformer):
    from operator import add, sub, mul
    number = int

    def expr(self, *items):
        res = items[0]
        for op, val in zip(items[1::2], items[2::2]):
            if op == '+': res += val
            elif op == '-': res -= val
        return res

    def term(self, *items):
        res = items[0]
        for op, val in zip(items[1::2], items[2::2]):
            if op == '*': res *= val
        return res


# Create a parser instance
math_parser = Lark(grammar, start='expr', parser='lalr', transformer=CalculateTree())

# Parse and evaluate an expression
expression = "(1 + 2) * 3 - 4"
result = math_parser.parse(expression)

print(f"Expression: {expression}")
print(f"Result: {result}")

# Example of getting the parse tree without a transformer
# tree_parser = Lark(grammar, start='expr', parser='lalr')
# tree = tree_parser.parse("1 + 2 * 3")
# print(tree.pretty())

view raw JSON →