Examples for Lark

How to run the examples:

After cloning the repo, open the terminal into the root directory of the project, and run the following:

[lark]$ python -m examples.<name_of_example>

For example, the following will parse all the Python files in the standard library of your local installation:

[lark]$ python -m examples.advanced.python_parser

Beginner Examples

Parsing Indentation

Parsing Indentation

Lark Grammar

Lark Grammar

Handling Ambiguity

Handling Ambiguity

Basic calculator

Basic calculator

Turtle DSL

Turtle DSL

Simple JSON Parser

Simple JSON Parser

Advanced Examples

LALR’s contextual lexer

LALR’s contextual lexer

Templates

Templates

Earley’s dynamic lexer

Earley’s dynamic lexer

Error handling using an interactive parser

Error handling using an interactive parser

Reconstruct a JSON

Reconstruct a JSON

Custom lexer

Custom lexer

Transform a Forest

Transform a Forest

Simple JSON Parser

Simple JSON Parser

Custom SPPF Prioritizer

Custom SPPF Prioritizer

Python 3 to Python 2 converter (tree templates)

Python 3 to Python 2 converter (tree templates)

Grammar-complete Python Parser

Grammar-complete Python Parser

Creating an AST from the parse tree

Creating an AST from the parse tree

Example-Driven Error Reporting

Example-Driven Error Reporting

Example-Driven Error Reporting

Example-Driven Error Reporting

Reconstruct Python

Reconstruct Python

Using lexer dynamic_complete

Using lexer dynamic_complete

Syntax Highlighting

Syntax Highlighting

Grammar Composition

This example shows how to do grammar composition in Lark, by creating a new file format that allows both CSV and JSON to co-exist.

We show how, by using namespaces, Lark grammars and their transformers can be fully reused - they don’t need to care if their grammar is used directly, or being imported, or who is doing the importing.

See main.py for more details.

sphx_glr_examples_composition_eval_json.py

Transformer for evaluating json.lark

sphx_glr_examples_composition_eval_csv.py

Transformer for evaluating csv.lark

Grammar Composition

Grammar Composition

Example Grammars

This directory is a collection of lark grammars, taken from real world projects.

Standalone example

To initialize, cd to this folder, and run:

./create_standalone.sh

Or:

python -m lark.tools.standalone json.lark > json_parser.py

Then run using:

python json_parser_main.py <path-to.json>

Standalone Parser

Standalone Parser

Gallery generated by Sphinx-Gallery