Lark LALR calculator with a Transformer and error handling: working versions (tested 2026-10-16)
A small LALR arithmetic grammar parsed with lark and evaluated by a Transformer, plus how syntax errors and exceptions raised inside transformer methods surface.
passed5 passed · 0 failed2026-10-16 21:57 UTC- Status
- passed
- Last passed
- 2026-10-16 21:57 UTC
- Tests
- 5 / 5
- Language
- Python
- Runtime
- Python 3.13.16
pinned versions2
lark@1.3.1pytest@9.1.1
run it
uv venv -q .venv && uv pip install -q --python .venv/bin/python -r requirements.txt .venv/bin/python -m pytest -q
test environment
Python 3.13.16 linux 6.17.0-1022-azure x64 runner: github-actions 38972656245
The code
Exactly the files that passed. Your agent can pull them with get_recipe("lark-lalr-calculator-transformer") (install), or see them on GitHub.
requirements.txt
lark==1.3.1 pytest==9.1.1
calculator.py
from lark import Lark, Transformer
GRAMMAR = r"""
?start: sum
?sum: product
| sum "+" product -> add
| sum "-" product -> sub
?product: atom
| product "*" atom -> mul
| product "/" atom -> div
?atom: NUMBER -> number
| "-" atom -> neg
| "(" sum ")"
%import common.NUMBER
%import common.WS_INLINE
%ignore WS_INLINE
"""
def parse(text):
return Lark(GRAMMAR, parser="lalr").parse(text)
class Calc(Transformer):
def number(self, items):
return float(items[0])
def add(self, items):
return items[0] + items[1]
def sub(self, items):
return items[0] - items[1]
def mul(self, items):
return items[0] * items[1]
def div(self, items):
return items[0] / items[1]
def neg(self, items):
return -items[0]
def evaluate(text):
return Calc().transform(parse(text))
tests/test_calculator.py
import pytest
from lark.exceptions import (
UnexpectedCharacters,
UnexpectedInput,
UnexpectedToken,
VisitError,
)
from calculator import evaluate, parse
def test_precedence_and_parens():
assert evaluate("2 + 3 * 4") == 14.0
assert evaluate("-(2 + 3) * 4") == -20.0
def test_division_is_float():
assert evaluate("7 / 2") == 3.5
def test_unexpected_token_reports_position_and_expected():
with pytest.raises(UnexpectedToken) as exc:
parse("1 + * 2")
assert exc.value.line == 1 and exc.value.column == 5
assert "NUMBER" in exc.value.expected
def test_unknown_character_is_not_a_token_error():
with pytest.raises(UnexpectedCharacters) as exc:
parse("1 # 2")
assert (exc.value.line, exc.value.column) == (1, 3)
assert isinstance(exc.value, UnexpectedInput)
def test_transformer_errors_are_wrapped_in_visit_error():
with pytest.raises(VisitError) as exc:
evaluate("1 / 0")
assert isinstance(exc.value.orig_exc, ZeroDivisionError)
pytest.ini
[pytest] pythonpath = .
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.