### Run Lark Example Source: https://github.com/lark-parser/lark/blob/master/examples/README.rst Execute a Lark example script from the command line. Replace with the specific example script name. ```bash python -m examples. ``` -------------------------------- ### Basic Tree Construction Example Source: https://github.com/lark-parser/lark/blob/master/docs/tree_construction.md Demonstrates how Lark builds a tree from a simple grammar with named and unnamed terminals. Unnamed literals and terminals starting with an underscore are filtered by default. ```lark start: PNAME pname PNAME: "(" NAME ")" pname: "(" NAME ")" NAME: /\w+/ %ignore /\s+/ ``` -------------------------------- ### Run Python Parser Example Source: https://github.com/lark-parser/lark/blob/master/examples/README.rst Execute the Python parser example to parse all Python files in the local standard library. This demonstrates advanced parsing capabilities. ```bash python -m examples.advanced.python_parser ``` -------------------------------- ### Basic 'Hello World' Parsing Example Source: https://github.com/lark-parser/lark/blob/master/README.md A simple example demonstrating how to parse the phrase 'Hello, World!' using Lark. It defines a basic grammar and imports common terminals. ```python from lark import Lark l = Lark('''start: WORD "," WORD "!" %import common.WORD // imports from terminal library %ignore " " // Disregard spaces in text ''') print( l.parse("Hello, World!") ) ``` ```python Tree(start, [Token(WORD, 'Hello'), Token(WORD, 'World')]) ``` -------------------------------- ### Install Lark using pip Source: https://github.com/lark-parser/lark/blob/master/README.md Install Lark with the latest version using pip. Lark has no external dependencies. ```bash $ pip install lark --upgrade ``` -------------------------------- ### Visualize Parse Trees with dot/png Source: https://github.com/lark-parser/lark/blob/master/docs/features.md Example demonstrating how to visualize parse trees. Requires the 'fruitflies.py' script. ```python from lark import Lark parser = Lark.open('grammar.lark', parser='lalr', lexer='standard') with open('fruitflies.dot', 'w') as f: f.write(parser.pretty()) ``` -------------------------------- ### Install Lark with Nearley Support Source: https://github.com/lark-parser/lark/blob/master/docs/tools.md Install Lark along with the necessary components for Nearley.js grammar conversion. This command ensures all dependencies for the Nearley importer are met. ```bash pip install lark[nearley] ``` -------------------------------- ### Basic Lark Parser and Transformer Example Source: https://github.com/lark-parser/lark/wiki/How-to-use-Lark This example shows how to define a grammar, create a Lark parser instance, parse an input string, and apply a custom transformer to the resulting parse tree. You can omit transformer methods for rules you don't need to process. ```python from lark import Lark, Transformer gramamr = """start: rules and more rules rule1: other rules AND TOKENS | rule1 "+" rule2 -> add | some value [maybe] rule2: rule1 "-" (rule2 | "whatever")* TOKEN1: "a literal" TOKEN2: TOKEN1 "and literals" """ parser = Lark(grammar) tree = parser.parse("some input string") class MyTransformer(Transformer): def rule1(self, matches): return matches[0] + matches[1] # I don't have to implement rule2 if I don't feel like it! new_tree = MyTransformer().transform(tree) ``` -------------------------------- ### Import Nearley Calculator Example into Lark Source: https://github.com/lark-parser/lark/blob/master/docs/tools.md An example demonstrating the conversion of Nearley's arithmetic calculator grammar into a Lark-compatible Python module. This allows you to use the converted grammar for parsing. ```bash git clone https://github.com/Hardmath123/nearley python -m lark.tools.nearley nearley/examples/calculator/arithmetic.ne main ./nearley > ncalc.py ``` -------------------------------- ### Visitor Example: Increase All Numbers Source: https://github.com/lark-parser/lark/wiki/Classes-Reference Demonstrates how to use a Visitor to traverse a parse tree and modify nodes. This example specifically increments numbers found in the tree. ```python class IncreaseAllNumbers(Visitor): def number(self, tree): assert tree.data == "number" tree.children[0] += 1 IncreaseAllNumbers().visit(parse_tree) ``` -------------------------------- ### Install Lark Parser Source: https://github.com/lark-parser/lark/blob/master/docs/index.md Use pip to install the Lark parsing library. This is the primary method for adding Lark to your Python environment. ```bash pip install lark ``` -------------------------------- ### visit_children_decor Example Source: https://github.com/lark-parser/lark/blob/master/docs/visitors.md Demonstrates the use of the visit_children_decor wrapper to automatically visit a node's children before executing the method's logic. ```python class ProcessQuery(Interpreter): @visit_children_decor def query(self, tree): pass ``` -------------------------------- ### Reconstruct Input from Parse Tree (Python) Source: https://github.com/lark-parser/lark/blob/master/docs/features.md Example demonstrating reconstruction of Python-like input from a parse tree. ```python from lark import Lark python_parser = Lark(r""" start: assignment assignment: NAME "=" expr expr: NUMBER | NAME %import common.NUMBER %import common.NAME %import common.WS %ignore WS """, start='assignment') python_string = 'x = 10' tree = python_parser.parse(python_string) print(tree.pretty()) # Reconstruct the Python string from the parse tree print(python_parser.transform_tree(tree)) ``` -------------------------------- ### Transformer Example Source: https://github.com/lark-parser/lark/blob/master/docs/visitors.md Demonstrates a basic Transformer implementation for converting tokens to integers and floats, and looking up names in a dictionary. ```python class T(Transformer): INT = int NUMBER = float def NAME(self, name): return lookup_dict.get(name, name) T(visit_tokens=True).transform(tree) ``` -------------------------------- ### Build Documentation Source: https://github.com/lark-parser/lark/blob/master/docs/how_to_develop.md Build the project's HTML documentation. This involves navigating to the docs directory, installing requirements, and running the make command. ```bash cd docs/ pip install -r requirements.txt make html ``` -------------------------------- ### Visitor Example: Increase All Numbers Source: https://github.com/lark-parser/lark/blob/master/docs/visitors.md Demonstrates how to create a custom Visitor to modify nodes. This example increments all 'number' nodes in a parse tree. Inherit from `Visitor` and implement methods matching rule names. ```python class IncreaseAllNumbers(Visitor): def number(self, tree): assert tree.data == "number" tree.children[0] += 1 IncreaseAllNumbers().visit(parse_tree) ``` -------------------------------- ### Run Tests with Pytest Source: https://github.com/lark-parser/lark/blob/master/docs/how_to_develop.md Execute the test suite using the pytest framework. Ensure pytest is installed. ```bash pytest tests ``` -------------------------------- ### Interpreter Example: Increase Some Numbers Source: https://github.com/lark-parser/lark/blob/master/docs/visitors.md Shows how to use the `Interpreter` class for more controlled tree traversal. This example increments numbers but skips subtrees defined by 'skip' rules. Import `Interpreter` from `lark.visitors`. ```python from lark.visitors import Interpreter class IncreaseSomeOfTheNumbers(Interpreter): def number(self, tree): tree.children[0] += 1 def skip(self, tree): # skip this subtree. don't change any number node inside it. pass IncreaseSomeOfTheNumbers().visit(parse_tree) ``` -------------------------------- ### EBNF Expansion Example Source: https://github.com/lark-parser/lark/blob/master/docs/grammar.md Illustrates how Lark expands optional and repetitive EBNF constructs into equivalent recursive rules. ```plaintext a b? c -> (a c | a b c) ``` ```plaintext a: b* -> a: _b_tag _b_tag: (_b_tag b)? ``` -------------------------------- ### merge_transformers Example Source: https://github.com/lark-parser/lark/blob/master/docs/visitors.md Shows how to merge multiple transformers into a base transformer, with methods from merged transformers being prefixed. This is useful for handling imported grammars. ```python class TBase(Transformer): def start(self, children): return children[0] + 'bar' class TImportedGrammar(Transformer): def foo(self, children): return "foo" composed_transformer = merge_transformers(TBase(), imported=TImportedGrammar()) t = Tree('start', [ Tree('imported__foo', []) ]) assert composed_transformer.transform(t) == 'foobar' ``` -------------------------------- ### Create a TextSlice Instance Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Instantiate TextSlice with the text, start, and end indices. Negative indices are supported. ```pycon >>> TextSlice("Hello, World!", 7, -1) TextSlice(text='Hello, World!', start=7, end=12) ``` -------------------------------- ### Define a terminal with a character range Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference Terminals can match ranges of characters. This example defines 'INTEGER2' using a range, which is equivalent to the previous 'INTEGER' example. ```haskell INTEGER2 : ("0".."9")+ ``` -------------------------------- ### Transformer Example: Evaluate Expressions Source: https://github.com/lark-parser/lark/wiki/Classes-Reference Illustrates using a Transformer to process a parse tree and evaluate expressions. This example transforms an expression tree into its evaluated result. ```python from lark import Tree, Transformer class EvalExpressions(Transformer): def expr(self, args): return eval(args[0]) t = Tree('a', [Tree('expr', ['1+2'])]) print(EvalExpressions().transform( t )) ``` -------------------------------- ### Reconstruct Input from Parse Tree (JSON) Source: https://github.com/lark-parser/lark/blob/master/docs/features.md Example showing how to reconstruct input from a parse tree, specifically for JSON-like structures. ```python from lark import Lark json_parser = Lark(r""" value: object | array | ESCAPED_STRING | NUMBER array : "[" [value ("," value)*] "]" object : "{" [ (ESCAPED_STRING ":" value) ("," ESCAPED_STRING ":" value)* ] "}" %import common.ESCAPED_STRING %import common.NUMBER %import common.WS %ignore WS """, start='value') json_string = '{"key": [1, 2, 3], "other": "value"}' tree = json_parser.parse(json_string) print(tree.pretty()) # Reconstruct the JSON string from the parse tree print(json_parser.transform_tree(tree)) ``` -------------------------------- ### Define a terminal for whitespace Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference This example defines a 'WHITESPACE' terminal that matches one or more spaces or tabs. ```haskell WHITESPACE: (" " | /\t/ )+ ``` -------------------------------- ### Run Unit Tests with Tox Source: https://github.com/lark-parser/lark/blob/master/docs/how_to_develop.md Use tox to run all unit tests across different Python environments. Ensure tox and the required Python interpreters are installed. ```bash tox ``` -------------------------------- ### Start Interactive Parsing Session Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Initiates an interactive parsing session. This functionality is only available when the 'lalr' parser is used. It returns an `InteractiveParser` instance. ```python parse_interactive(text: AnyStr | [TextSlice](#lark.utils.TextSlice) | Any | None = None, start: str | None = None) → [InteractiveParser](#lark.parsers.lalr_interactive_parser.InteractiveParser) ``` -------------------------------- ### Regular Expression Flags in Lark Source: https://github.com/lark-parser/lark/blob/master/docs/grammar.md Provides examples of using flags with regular expressions and strings for case-insensitivity and multi-line matching. ```perl SELECT: "select"i //# Will ignore case, and match SELECT or Select, etc. MULTILINE_TEXT: /.+/s SIGNED_INTEGER: / [+-]? # the sign (0|[1-9][0-9]*) # the digits /x ``` -------------------------------- ### Define a basic rule with multiple items Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference Rules define the structure of the language. This example shows a rule named 'hello_world' matching two literal strings. ```haskell hello_world: "hello" "world" ``` -------------------------------- ### v_args with tree=True Example Source: https://github.com/lark-parser/lark/blob/master/docs/visitors.md Illustrates using the v_args decorator with the 'tree=True' option to pass the entire tree node as an argument to the method. This is used here to reverse the order of children in-place. ```python class ReverseNotation(Transformer_InPlace): @v_args(tree=True) def tree_node(self, tree): tree.children = tree.children[::-1] ``` -------------------------------- ### Lexer Precedence Examples in Lark Source: https://github.com/lark-parser/lark/blob/master/docs/grammar.md Illustrates how Lark's lexer resolves ambiguities using terminal priority, match length, and definition order. ```perl IF: "if" INTEGER : "/[0-9]+/" INTEGER2 : ("0".."9")+ //# Same as INTEGER DECIMAL.2: INTEGER? "." INTEGER //# Will be matched before INTEGER WHITESPACE: (" " | /\t/ )+ SQL_SELECT: "select"i ``` -------------------------------- ### Define and Use a Tiny Calculator Grammar Source: https://github.com/lark-parser/lark/wiki/Examples Defines a simple arithmetic grammar and uses an InlineTransformer to calculate the result of parsed expressions. Requires Lark to be installed. ```python from lark import Lark, InlineTransformer parser = Lark('?sum: product | sum "+" product -> add | sum "-" product -> sub ?product: item | product "*" item -> mul | product "/" item -> div ?item: NUMBER -> number | "-" item -> neg | "(" sum ")" %import common.NUMBER %import common.WS %ignore WS ', start='sum') ``` ```python class CalculateTree(InlineTransformer): from operator import add, sub, mul, truediv as div, neg number = float def calc(expr): return CalculateTree().transform( parser.parse(expr) ) ``` ```python >>> calc("(200 + 3*-3) * 7") 1337.0 ``` -------------------------------- ### v_args Decorator Example Source: https://github.com/lark-parser/lark/blob/master/docs/visitors.md Shows how to use the v_args decorator to modify the arguments passed to Transformer methods. The 'inline=True' option passes children as *args, and 'meta=True' provides both meta information and children. ```python @v_args(inline=True) class SolveArith(Transformer): def add(self, left, right): return left + right @v_args(meta=True) def mul(self, meta, children): logger.info(f'mul at line {meta.line}') left, right = children return left * right ``` -------------------------------- ### Import a common terminal Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference The %import directive allows importing predefined terminals from built-in modules. This example imports the 'NUMBER' terminal from the 'common' module. ```ruby %import common.NUMBER ``` -------------------------------- ### Define a terminal with a regular expression Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference Terminals can be defined using regular expressions. This example defines an 'INTEGER' terminal matching one or more digits. ```haskell INTEGER : /[0-9]+/ ``` -------------------------------- ### Inlining Rules with Underscore Prefix Source: https://github.com/lark-parser/lark/blob/master/docs/tree_construction.md Illustrates how rules starting with an underscore (e.g., `_greet`) are inlined into their containing rule, simplifying the resulting tree structure by omitting the intermediate rule node. ```lark start: "(" _greet ")" _greet: /\w+/ /\w+/ ``` -------------------------------- ### Parse with Unicode Character Classes using Regex Module Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Demonstrates parsing text using Unicode character classes for identifiers, leveraging the `regex` module for advanced pattern matching. This requires `pip install lark[regex]` and initializing Lark with `regex=True`. ```python from lark import Lark >>> g = Lark(r""" ?start: NAME NAME: ID_START ID_CONTINUE* ID_START: /[PuLuLtLmLoNl_]+/ ID_CONTINUE: ID_START | /[\p{Mn}\p{Mc}\p{Nd}\p{Pc}·]+/ """, regex=True) >>> g.parse('வணக்கம்') 'வணக்கம்' ``` -------------------------------- ### Run Tests using setup.py Source: https://github.com/lark-parser/lark/blob/master/docs/how_to_develop.md Execute the test suite using the project's setup.py script. This is an alternative method for running tests. ```bash python setup.py test ``` -------------------------------- ### Initialize Lark Parser Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Demonstrates basic initialization of the Lark parser with a simple grammar string. ```python >>> Lark(r'''start: "foo" ''') Lark(...) ``` -------------------------------- ### Lark.open_from_package Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Creates a Lark instance with grammar loaded from within a specified package, enabling grammar loading from zipapps. ```APIDOC ## Lark.open_from_package ### Description Creates a Lark instance with grammar loaded from within a specified package, enabling grammar loading from zipapps. ### Method classmethod ### Parameters * **package** (str) - The name of the package to load the grammar from. * **grammar_path** (str) - The path to the grammar file within the package. * **search_paths** (Sequence[str], optional) - Additional search paths for the loader. * ****options** - Additional options for the Lark parser. ### Example ```python Lark.open_from_package(_\_name_\_, "example.lark", ("grammars",), parser=...) ``` ``` -------------------------------- ### Lark.parse_interactive Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Starts an interactive parsing session, suitable for LALR parsers. ```APIDOC ## Lark.parse_interactive ### Description Starts an interactive parsing session, suitable for LALR parsers. ### Method Interactive parser method ### Parameters * **text** (LarkInput, optional) - The text to be parsed. Required for `resume_parse()`. * **start** (str, optional) - The start symbol for parsing. ### Returns InteractiveParser - A new InteractiveParser instance. ### See Also `Lark.parse()` ``` -------------------------------- ### open(grammar_filename: str, rel_to: str | None = None, transformer: [Transformer](visitors.md#lark.visitors.Transformer)[[Token](#lark.Token), _Return_T], **options: Any) Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Create an instance of Lark with the grammar given by its filename. If `rel_to` is provided, the function will find the grammar filename in relation to it. ```APIDOC ## open(grammar_filename: str, rel_to: str | None = None, transformer: [Transformer](visitors.md#lark.visitors.Transformer)[[Token](#lark.Token), _Return_T], **options: Any) ### Description Create an instance of Lark with the grammar given by its filename. If `rel_to` is provided, the function will find the grammar filename in relation to it. ### Method `open` ### Parameters - **grammar_filename** (str) - Required - The name of the grammar file. - **rel_to** (str | None) - Optional - The file to resolve the grammar filename relative to. - **transformer** ([Transformer](visitors.md#lark.visitors.Transformer)[[Token](#lark.Token), _Return_T]) - Optional - A transformer to apply to the parse tree. - **options** (Any) - Optional - Additional options for the Lark parser. ``` -------------------------------- ### Get Terminal Definition Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Retrieves the definition of a specific terminal by its name. ```python get_terminal(name: str) → TerminalDef ``` -------------------------------- ### Open Lark Grammar from Package Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Creates a Lark instance by loading the grammar from within a specified Python package. This is useful for distributing grammars within applications, including zipapps. Imports within the grammar will be resolved relative to the package. ```python Lark.open_from_package(_\_name_\_, “example.lark”, (“grammars”,), parser=…) ``` -------------------------------- ### open(grammar_filename: str, rel_to: str | None = None, **options: Any) Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Create an instance of Lark with the grammar given by its filename. If `rel_to` is provided, the function will find the grammar filename in relation to it. ```APIDOC ## open(grammar_filename: str, rel_to: str | None = None, **options: Any) ### Description Create an instance of Lark with the grammar given by its filename. If `rel_to` is provided, the function will find the grammar filename in relation to it. ### Method `open` ### Parameters - **grammar_filename** (str) - Required - The name of the grammar file. - **rel_to** (str | None) - Optional - The file to resolve the grammar filename relative to. - **options** (Any) - Optional - Additional options for the Lark parser. ``` -------------------------------- ### Run Standalone Parser Source: https://github.com/lark-parser/lark/blob/master/examples/standalone/README.rst Execute the generated standalone parser with a JSON file as input. Replace '' with the actual path to your JSON file. ```bash python json_parser_main.py ``` -------------------------------- ### Ignore comments Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference This example shows how to ignore comments defined by a '#' followed by any non-newline characters. It first defines the COMMENT terminal and then ignores it. ```ruby %ignore " " COMMENT: "#" /[^\n]/* %ignore COMMENT ``` -------------------------------- ### Ignore a specific terminal Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference The %ignore directive tells Lark to completely disregard occurrences of a specified terminal. This example ignores spaces. ```ruby %ignore " " ``` -------------------------------- ### Define a case-insensitive terminal Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference Terminals can be defined as case-insensitive using the 'i' flag. This example defines 'SQL_SELECT' to match 'select' regardless of case. ```haskell SQL_SELECT: "select"i ``` -------------------------------- ### Create and Print a Lark Tree Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Demonstrates how to create a basic Tree object and print its representation using the 'rich' library. This is useful for visualizing simple tree structures. ```python from rich import print from lark import Tree tree = Tree('root', ['node1', 'node2']) print(tree) ``` -------------------------------- ### Rule Definition with Repetition Operators Source: https://github.com/lark-parser/lark/blob/master/docs/grammar.md Provides examples of using repetition operators like '?', '*', '+', and '~' for quantifiers in Lark rule definitions. ```perl mul: (mul "*")? number //# Left-recursion is allowed and encouraged! four_words: word ~ 4 ``` -------------------------------- ### Open Lark Grammar from File Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Opens a Lark instance with a grammar loaded from a file. Use `rel_to` to specify the base path for relative grammar paths. Supports 'lalr' parser. ```python >>> Lark.open("grammar_file.lark", rel_to=__file__, parser="lalr") Lark(...) ``` -------------------------------- ### Define a terminal with a string literal Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference Terminals define the alphabet of the language. This example defines an 'IF' terminal matching the literal string 'if'. ```haskell IF: "if" ``` -------------------------------- ### Define a multi-line rule Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference Rule alternatives can span multiple lines. This example shows the 'expr' rule with two alternatives, one of which is multi-line. ```haskell expr: expr operator expr | value //# Multi-line, belongs to expr ``` -------------------------------- ### Run a Single Unit Test Source: https://github.com/lark-parser/lark/blob/master/docs/how_to_develop.md Execute a specific unit test by providing its class and method name. This is useful for targeted debugging. ```bash python -m tests TestLalrBasic.test_keep_all_tokens ``` -------------------------------- ### Template Definition and Usage in Lark Source: https://github.com/lark-parser/lark/blob/master/docs/grammar.md Demonstrates how to define reusable grammar templates with parameters and how to use them in rules. ```javascript my_template{param1, param2, ...}: ``` ```javascript some_rule: my_template{arg1, arg2, ...} ``` ```javascript _separated{x, sep}: x (sep x)* // Define a sequence of 'x sep x sep x ...' num_list: "[" _separated{NUMBER, ","} "]" // Will match "[1, 2, 3]" etc. ``` -------------------------------- ### Collect Comments with Lexer Callbacks Source: https://github.com/lark-parser/lark/blob/master/docs/recipes.md Utilize `lexer_callbacks` to process ignored tokens, such as comments, as they are generated by the lexer. This example collects all comment tokens into a list. ```python from lark import Lark comments = [] parser = Lark(""" start: INT* COMMENT: /#.*/ %import common (INT, WS) %ignore COMMENT %ignore WS """, parser="lalr", lexer_callbacks={'COMMENT': comments.append}) parser.parse(""" 1 2 3 # hello # world 4 5 6 """) print(comments) ``` ```python [Token(COMMENT, '# hello'), Token(COMMENT, '# world')] ``` -------------------------------- ### Lark.open Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Opens a Lark parser instance with a grammar from a file. Supports specifying the parser type and relative path. ```APIDOC ## Lark.open ### Description Opens a Lark parser instance with a grammar from a file. Supports specifying the parser type and relative path. ### Method classmethod ### Parameters * **grammar_file** (str) - The path to the grammar file. * **rel_to** (str, optional) - The base path for resolving relative grammar paths. * **parser** (str, optional) - The type of parser to use (e.g., 'lalr', 'earley'). * **transformer** (Transformer, optional) - A transformer to apply to the parse tree. * ****options** - Additional options for the Lark parser. ### Example ```pycon >>> Lark.open("grammar_file.lark", rel_to=__file__, parser="lalr") Lark(...) ``` ``` -------------------------------- ### Define a rule with quantified items Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference Rules can specify the number of occurrences for items using quantifiers. This example defines 'four_words' to match exactly four 'word' items. ```haskell four_words: word ~ 4 ``` -------------------------------- ### Define a rule with optional alias Source: https://github.com/lark-parser/lark/wiki/Grammar-Reference Rules can have optional aliases for specific alternatives, affecting tree construction. This example shows a rule 'mul' with an optional left-recursive part. ```haskell mul: [mul "*"] number //# Left-recursion is allowed! ``` -------------------------------- ### pretty() Method Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Returns an indented string representation of the tree, which is useful for debugging purposes. ```APIDOC #### pretty(indent_str: str = ' ') → str ### Description Returns an indented string representation of the tree. Great for debugging. ``` -------------------------------- ### Run Lark parser with PyPy Source: https://github.com/lark-parser/lark/blob/master/docs/json_tutorial.md Execute a Lark-based Python script using PyPy to measure performance improvements. ```default $ time pypy tutorial_json.py json_data > /dev/null real 0m1.397s user 0m1.296s sys 0m0.083s ``` -------------------------------- ### Parse Expression using Imported Nearley Grammar Source: https://github.com/lark-parser/lark/blob/master/docs/tools.md Demonstrates how to use the Python module generated from a Nearley grammar to parse an expression. This shows the practical application of the conversion tool. ```python import ncalc ncalc.parse('sin(pi/4) ^ e') # Expected output: 0.38981434460254655 ``` -------------------------------- ### Detect Regex Collisions with Interegular Source: https://github.com/lark-parser/lark/blob/master/docs/how_to_use.md Use Lark with the 'interegular' library to detect potential regex collisions in your grammar. Ensure 'lark[interegular]' is installed and the lexer is 'basic' or 'contextual'. ```python import logging from lark import Lark, logger logger.setLevel(logging.WARN) collision_grammar = ''' start: A | B A: /a+/ B: /[ab]+/ ''' p = Lark(collision_grammar, parser='lalr') ``` -------------------------------- ### TextSlice Class Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md The TextSlice class represents a view of a string or bytes object between specified start and end indices. It does not create copies and can be used as input for Lark's lexer. ```APIDOC ## class lark.utils.TextSlice(text: AnyStr, start: int, end: int) A view of a string or bytes object, between the start and end indices. Never creates a copy. Lark accepts instances of TextSlice as input (instead of a string), when the lexer is ‘basic’ or ‘contextual’. ### Parameters: * **text** (*str* *or* *bytes*) – The text to slice. * **start** (*int*) – The start index. Negative indices are supported. * **end** (*int*) – The end index. Negative indices are supported. ### Raises: * **TypeError** – If text is not a str or bytes. * **AssertionError** – If start or end are out of bounds. ### Examples: ```pycon >>> TextSlice("Hello, World!", 7, -1) TextSlice(text='Hello, World!', start=7, end=12) ``` ```pycon >>> TextSlice("Hello, World!", 7, None).count("o") 1 ``` ``` -------------------------------- ### Importing Terminals and Rules in Lark Source: https://github.com/lark-parser/lark/blob/master/docs/grammar.md Shows how to import grammar components from other files using the %import directive, including aliasing. ```perl %import common.NUMBER %import .terminals_file (A, B, C) %import .rules_file.rule_a -> rule_b ``` -------------------------------- ### Lark Grammar with Aliased Rule Options Source: https://github.com/lark-parser/lark/wiki/Tree-Construction Illustrates how to use aliases (e.g., `-> hello`) to specify custom branch names for specific options within a rule, overriding the default rule name. ```lark start: greet greet greet: "hello" -> hello | "world" ``` -------------------------------- ### lark.ast_utils.create_transformer Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Collects Ast subclasses from a given module and creates a Lark transformer that builds the AST. It converts CamelCase class names to snake_case and skips classes starting with an underscore. ```APIDOC ## lark.ast_utils.create_transformer(ast_module: ModuleType, transformer: [Transformer](visitors.md#lark.visitors.Transformer) | None = None, decorator_factory: Callable = ) → [Transformer](visitors.md#lark.visitors.Transformer) ### Description Collects Ast subclasses from the given module, and creates a Lark transformer that builds the AST. For each class, we create a corresponding rule in the transformer, with a matching name. CamelCase names will be converted into snake_case. Example: “CodeBlock” -> “code_block”. Classes starting with an underscore (_) will be skipped. ### Parameters #### Path Parameters * **ast_module** (ModuleType) - Required - A Python module containing all the subclasses of `ast_utils.Ast` * **transformer** ([Transformer](visitors.md#lark.visitors.Transformer) | None) - Optional - An initial transformer. Its attributes may be overwritten. * **decorator_factory** (Callable) - Required - An optional callable accepting two booleans, inline, and meta, and returning a decorator for the methods of `transformer`. (default: `v_args`). ``` -------------------------------- ### Parse Integers with Transformer Source: https://github.com/lark-parser/lark/blob/master/docs/recipes.md Use a transformer to convert parsed integer tokens from strings to integers during the parsing process for improved performance. This example demonstrates updating a token's value. ```python from lark import Lark, Transformer class T(Transformer): def INT(self, tok): """Convert the value of `tok` from string to int, while maintaining line number & column.""" return tok.update(value=int(tok)) parser = Lark(""" start: INT* %import common.INT %ignore " " """, parser="lalr", transformer=T()) print(parser.parse('3 14 159')) ``` ```python Tree(start, [Token(INT, 3), Token(INT, 14), Token(INT, 159)]) ``` -------------------------------- ### Equivalent Terminals in Lark Grammar Source: https://github.com/lark-parser/lark/blob/master/docs/grammar.md Demonstrates how string literals and regular expressions can be equivalent for terminal definitions in Lark. ```perl A1: "a" | "b" A2: /a|b/ ``` -------------------------------- ### Interpreter Class Source: https://github.com/lark-parser/lark/blob/master/docs/visitors.md The Interpreter class walks the tree starting at the root, visiting nodes top-down. It allows for custom branching and looping logic by requiring explicit calls to visit methods. ```APIDOC ## class lark.visitors.Interpreter Interpreter walks the tree starting at the root. Visits the tree, starting with the root and finally the leaves (top-down) For each tree node, it calls its methods (provided by user via inheritance) according to `tree.data`. Unlike `Transformer` and `Visitor`, the Interpreter doesn’t automatically visit its sub-branches. The user has to explicitly call `visit`, `visit_children`, or use the `@visit_children_decor`. This allows the user to implement branching and loops. ### visit(tree: [Tree](classes.md#lark.Tree)[Leaf_T]) -> _Return_T Visit the tree, starting with the root and finally the leaves (top-down). ### visit_children(tree: [Tree](classes.md#lark.Tree)[Leaf_T]) -> List Visit all the children of this tree and return the results as a list. ### __default__(tree) Default function that is called if there is no attribute matching `tree.data`. Can be overridden. Defaults to visiting all the tree’s children. ``` -------------------------------- ### load(f) Source: https://github.com/lark-parser/lark/blob/master/docs/classes.md Loads an instance from the given file object. Useful for caching and multiprocessing. ```APIDOC ## load(f) ### Description Loads an instance from the given file object. Useful for caching and multiprocessing. ### Method `load` ### Parameters - **f** (file object) - Required - The file object to load from. ``` -------------------------------- ### Transforming Expressions with Lark Transformer Source: https://github.com/lark-parser/lark/blob/master/docs/visitors.md Demonstrates how to use a Lark Transformer to evaluate expression nodes in a parse tree. This example defines a custom transformer to process 'expr' nodes by evaluating their string content. ```python from lark import Tree, Transformer class EvalExpressions(Transformer): def expr(self, args): return eval(args[0]) t = Tree('a', [Tree('expr', ['1+2'])]) print(EvalExpressions().transform( t )) ```