{"id":528,"library":"lark","title":"Lark Parser","description":"Lark is a modern, general-purpose parsing library for Python. It allows users to parse any context-free grammar efficiently with minimal code, supporting algorithms like Earley, LALR(1), and CYK. Lark automatically builds a parse-tree (AST) based on grammar structure and features a fast Unicode lexer. The library is actively maintained with frequent releases, currently at version 1.3.1.","status":"active","version":"1.3.1","language":"python","source_language":"en","source_url":"https://github.com/lark-parser/lark","tags":["parsing","parser","lexer","grammar","DSL","AST","EBNF"],"install":[{"cmd":"pip install lark","lang":"bash","label":"Install latest stable version"}],"dependencies":[{"reason":"Used for lexing operations, provides enhanced regular expression features.","package":"regex","optional":false}],"imports":[{"note":"Main parser class for defining and using grammars.","symbol":"Lark","correct":"from lark import Lark"},{"note":"Base class for processing parse trees into custom data structures.","symbol":"Transformer","correct":"from lark import Transformer"},{"note":"Decorator for Transformer methods, often used to simplify argument handling.","symbol":"v_args","correct":"from lark import v_args"}],"quickstart":{"code":"from lark import Lark, Transformer, v_args\n\n# Define your grammar as a string\ngrammar = \"\"\"\n    ?start: expression\n\n    ?expression: term ((\"+\" | \"-\") term)*\n    ?term: factor ((\"*\") factor)*\n    ?factor: NUMBER | \"(\" expression \")\"\n\n    %import common.NUMBER\n    %import common.WS\n    %ignore WS\n\"\"\"\n\n# Create the parser\narith_parser = Lark(grammar, start='expression')\n\n# Define a transformer to evaluate the expression\n@v_args(inline=True)\nclass CalculateString(Transformer):\n    from operator import add, sub, mul\n    number = int\n\n    def expression(self, first, *rest):\n        for op, num in zip(rest[::2], rest[1::2]):\n            if op == '+':\n                first = self.add(first, num)\n            elif op == '-':\n                first = self.sub(first, num)\n        return first\n\n    def term(self, first, *rest):\n        for op, num in zip(rest[::2], rest[1::2]):\n            if op == '*':\n                first = self.mul(first, num)\n        return first\n\n# Example usage\ntext_to_parse = \"(1 + 2) * 3\"\ntree = arith_parser.parse(text_to_parse)\nresult = CalculateString().transform(tree)\n\nprint(f\"Parsed expression: {text_to_parse}\")\nprint(f\"Result: {result}\")\n","lang":"python","description":"This quickstart demonstrates how to define a simple arithmetic grammar, parse an expression, and then use a Transformer to evaluate the resulting parse tree. It showcases the core `Lark` parser and `Transformer` classes."},"warnings":[{"fix":"Upgrade Python to 3.8 or higher. If unable to upgrade, pin `lark<1.2.1`.","message":"Lark dropped official support for Python versions lower than 3.8 starting with version 1.2.1. Users on older Python environments will need to upgrade Python or use an older Lark version.","severity":"breaking","affected_versions":">=1.2.1"},{"fix":"Use `pip install lark` for the current version. If you were using `lark-parser`, migrate your imports and ensure you install `lark` instead.","message":"The PyPI package name changed from `lark-parser` to `lark` around version 1.0.0. The `lark-parser` package is significantly outdated (0.12.0) and incompatible with recent GitHub releases.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Thoroughly test grammars after upgrading. If specific ambiguity resolution is critical, explicitly handle it in your grammar or code, or review the `ambiguity` parameter options for `Lark`.","message":"Changes to the Earley parser (e.g., in 1.2.2 and 1.3.0) related to ambiguity resolution might subtly change the parse tree output for certain ambiguous grammars. While often bug fixes, they can alter behavior if you relied on previous implicit resolution.","severity":"gotcha","affected_versions":">=1.2.2"},{"fix":"Clear any stored Lark parser cache files or re-instantiate `Lark` objects to force recompilation.","message":"Lark's cache hashing mechanism changed from MD5 to SHA256 in version 1.1.6. If you rely on cached parsers, you might need to clear your cache or recompile parsers after upgrading to avoid unexpected behavior.","severity":"gotcha","affected_versions":">=1.1.6"},{"fix":"Only use `Lark.save()` for LALR parsers. For other parser types, consider regenerating the parser from the grammar each time or exploring custom serialization if absolutely necessary.","message":"The `Lark.save()` method now explicitly raises an error if the parser type is not LALR (`parser!='lalr'`). Attempting to save Earley or CYK parsers will fail.","severity":"gotcha","affected_versions":">=1.1.8"},{"fix":"Carefully design regex terminals in your grammar. Use non-greedy quantifiers (`*?`, `+?`) where appropriate, or consider `strict=True` for debugging, or using explicit negative lookaheads/lookbehinds. For highly ambiguous or free-form text, consider if Lark is the best tool or adapt your grammar significantly.","message":"Lark's default regex behavior is greedy, which can lead to unexpected parsing results for free-form text or ambiguous patterns where a shorter match is desired.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T14:40:53.302Z","next_check":"2026-07-09T00:00:00.000Z","problems":[{"fix":"Install the lark library using pip: `pip install lark`","cause":"The 'lark' package has not been installed in the Python environment being used.","error":"ModuleNotFoundError: No module named 'lark'"},{"fix":"Review your grammar definition and the input string to ensure they match, paying attention to the reported line and column number. Often, it's a mismatch between expected terminals/rules and the actual input. Debugging with `Lark(grammar, debug=True)` can provide more detailed conflict information.","cause":"The input text provided to the parser does not conform to the defined grammar at the point where the unexpected token was encountered.","error":"lark.exceptions.UnexpectedToken: Unexpected token"},{"fix":"First, parse the input string to get a `Tree` object, then call `.pretty()` on that `Tree` object: `tree = parser.parse(text); print(tree.pretty())`","cause":"The `.pretty()` method is intended to be called on a `Tree` object (the result of parsing), not directly on the `Lark` parser class itself.","error":"AttributeError: type object 'Lark' has no attribute 'pretty'"},{"fix":"Adjust the `Transformer` or `Visitor` method to handle `Token` objects directly, or modify the grammar to ensure the expected structure is always a `Tree` where `children` are anticipated. Using `v_args(inline=True)` or `v_args(meta=True)` decorators on transformer methods can help manage arguments.","cause":"This error typically occurs within a `Transformer` or `Visitor` when a callback method expects a `Tree` object (which has `children`) but instead receives a `Token` object directly, often because a rule was inlined or simplified, making a token the direct child of another node.","error":"AttributeError: 'Token' object has no attribute 'children'"},{"fix":"Modify the regular expression for the specified terminal to ensure it always matches at least one character, for example, by changing `*` (zero or more) to `+` (one or more) if appropriate, or by making sure regex components are not optional in a way that allows an empty match.","cause":"A regular expression defined for a terminal in the grammar is able to match an empty string, which is not permitted by Lark's lexer.","error":"lark.exceptions.LexError: Lexer does not allow zero-width tokens."}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":3.6,"disk_size":"18.6M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.1,"mem_mb":3.6,"disk_size":"19M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.21,"mem_mb":4.2,"disk_size":"20.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":4.2,"disk_size":"21M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.13,"mem_mb":4.1,"disk_size":"12.5M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.18,"mem_mb":4.1,"disk_size":"13M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":4.5,"disk_size":"12.1M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":4.5,"disk_size":"13M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.08,"mem_mb":3.5,"disk_size":"18.1M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":3.5,"disk_size":"19M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}