:orphan:



.. _sphx_glr_examples:

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:

.. code:: bash

   [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:

.. code:: bash

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

Beginner Examples
~~~~~~~~~~~~~~~~~



.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A demonstration of parsing indentation (“whitespace significant” language) and the usage of the...">

.. only:: html

 .. figure:: /examples/images/thumb/sphx_glr_indented_tree_thumb.png
     :alt: Parsing Indentation

     :ref:`sphx_glr_examples_indented_tree.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/indented_tree

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A reference implementation of the Lark grammar (using LALR(1))">

.. only:: html

 .. figure:: /examples/images/thumb/sphx_glr_lark_grammar_thumb.png
     :alt: Lark Grammar

     :ref:`sphx_glr_examples_lark_grammar.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/lark_grammar

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A demonstration of ambiguity">

.. only:: html

 .. figure:: /examples/images/thumb/sphx_glr_fruitflies_thumb.png
     :alt: Handling Ambiguity

     :ref:`sphx_glr_examples_fruitflies.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/fruitflies

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A simple example of a REPL calculator">

.. only:: html

 .. figure:: /examples/images/thumb/sphx_glr_calc_thumb.png
     :alt: Basic calculator

     :ref:`sphx_glr_examples_calc.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/calc

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Implements a LOGO-like toy language for Python’s turtle, with interpreter.">

.. only:: html

 .. figure:: /examples/images/thumb/sphx_glr_turtle_dsl_thumb.png
     :alt: Turtle DSL

     :ref:`sphx_glr_examples_turtle_dsl.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/turtle_dsl

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="The code is short and clear, and outperforms every other parser (that&#x27;s written in Python). For...">

.. only:: html

 .. figure:: /examples/images/thumb/sphx_glr_json_parser_thumb.png
     :alt: Simple JSON Parser

     :ref:`sphx_glr_examples_json_parser.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/json_parser
.. raw:: html

    <div class="sphx-glr-clear"></div>



.. _sphx_glr_examples_advanced:

Advanced Examples
~~~~~~~~~~~~~~~~~



.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates the power of LALR&#x27;s contextual lexer, by parsing a toy configuration ...">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_conf_lalr_thumb.png
     :alt: LALR’s contextual lexer

     :ref:`sphx_glr_examples_advanced_conf_lalr.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/conf_lalr

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example shows how to use Lark&#x27;s templates to achieve cleaner grammars">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_templates_thumb.png
     :alt: Templates

     :ref:`sphx_glr_examples_advanced_templates.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/templates

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Demonstrates the power of Earley’s dynamic lexer on a toy configuration language">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_conf_earley_thumb.png
     :alt: Earley’s dynamic lexer

     :ref:`sphx_glr_examples_advanced_conf_earley.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/conf_earley

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates error handling using an interactive parser in LALR">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_error_handling_thumb.png
     :alt: Error handling using an interactive parser

     :ref:`sphx_glr_examples_advanced_error_handling.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/error_handling

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates how to use the %extend statement, to add new syntax to the example Py...">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_extend_python_thumb.png
     :alt: Extend the Python Grammar

     :ref:`sphx_glr_examples_advanced_extend_python.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/extend_python

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Demonstrates the experimental text-reconstruction feature">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_reconstruct_json_thumb.png
     :alt: Reconstruct a JSON

     :ref:`sphx_glr_examples_advanced_reconstruct_json.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/reconstruct_json

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Demonstrates using a custom lexer to parse a non-textual stream of data">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_custom_lexer_thumb.png
     :alt: Custom lexer

     :ref:`sphx_glr_examples_advanced_custom_lexer.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/custom_lexer

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates how to subclass TreeForestTransformer to directly transform a SPPF.">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_tree_forest_transformer_thumb.png
     :alt: Transform a Forest

     :ref:`sphx_glr_examples_advanced_tree_forest_transformer.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/tree_forest_transformer

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="The code is short and clear, and outperforms every other parser (that&#x27;s written in Python). For...">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr__json_parser_thumb.png
     :alt: Simple JSON Parser

     :ref:`sphx_glr_examples_advanced__json_parser.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/_json_parser

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates how to subclass ForestVisitor to make a custom SPPF node prioritizer ...">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_prioritizer_thumb.png
     :alt: Custom SPPF Prioritizer

     :ref:`sphx_glr_examples_advanced_prioritizer.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/prioritizer

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates how to translate between two trees using tree templates. It parses Py...">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_py3to2_thumb.png
     :alt: Python 3 to Python 2 converter (tree templates)

     :ref:`sphx_glr_examples_advanced_py3to2.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/py3to2

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A fully-working Python 2 &amp; 3 parser (but not production ready yet!)">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_python_parser_thumb.png
     :alt: Grammar-complete Python Parser

     :ref:`sphx_glr_examples_advanced_python_parser.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/python_parser

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="    This example demonstrates how to transform a parse-tree into an AST using lark.ast_utils.">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_create_ast_thumb.png
     :alt: Creating an AST from the parse tree

     :ref:`sphx_glr_examples_advanced_create_ast.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/create_ast

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A demonstration of example-driven error reporting with the Earley parser (See also: error_repor...">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_error_reporting_earley_thumb.png
     :alt: Example-Driven Error Reporting

     :ref:`sphx_glr_examples_advanced_error_reporting_earley.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/error_reporting_earley

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A demonstration of example-driven error reporting with the LALR parser (See also: error_reporti...">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_error_reporting_lalr_thumb.png
     :alt: Example-Driven Error Reporting

     :ref:`sphx_glr_examples_advanced_error_reporting_lalr.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/error_reporting_lalr

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Demonstrates how Lark&#x27;s experimental text-reconstruction feature can recreate functional Python...">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_reconstruct_python_thumb.png
     :alt: Reconstruct Python

     :ref:`sphx_glr_examples_advanced_reconstruct_python.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/reconstruct_python

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Demonstrates how to use lexer=&#x27;dynamic_complete&#x27; and ambiguity=&#x27;explicit&#x27;">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_dynamic_complete_thumb.png
     :alt: Using lexer dynamic_complete

     :ref:`sphx_glr_examples_advanced_dynamic_complete.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/dynamic_complete

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example shows how to write a syntax-highlighted editor with Qt and Lark">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_qscintilla_json_thumb.png
     :alt: Syntax Highlighting

     :ref:`sphx_glr_examples_advanced_qscintilla_json.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/qscintilla_json
.. raw:: html

    <div class="sphx-glr-clear"></div>



.. _sphx_glr_examples_composition:

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``](main.py) for more details.


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Transformer for evaluating json.lark">

.. only:: html

 .. figure:: /examples/composition/images/thumb/sphx_glr_eval_json_thumb.png
     :alt: Transformer for evaluating json.lark

     :ref:`sphx_glr_examples_composition_eval_json.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/composition/eval_json

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Transformer for evaluating csv.lark">

.. only:: html

 .. figure:: /examples/composition/images/thumb/sphx_glr_eval_csv_thumb.png
     :alt: Transformer for evaluating csv.lark

     :ref:`sphx_glr_examples_composition_eval_csv.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/composition/eval_csv

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example shows how to do grammar composition in Lark, by creating a new file format that al...">

.. only:: html

 .. figure:: /examples/composition/images/thumb/sphx_glr_main_thumb.png
     :alt: Grammar Composition

     :ref:`sphx_glr_examples_composition_main.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/composition/main
.. raw:: html

    <div class="sphx-glr-clear"></div>



.. _sphx_glr_examples_grammars:

# Example Grammars

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

- [Verilog](verilog.lark) - Taken from https://github.com/circuitgraph/circuitgraph/blob/master/circuitgraph/parsing/verilog.lark

.. raw:: html

    <div class="sphx-glr-clear"></div>



.. _sphx_glr_examples_standalone:

# Standalone example

To initialize, cd to this folder, and run:


```bash
	 ./create_standalone.sh
```

Or:
```bash
python -m lark.tools.standalone json.lark > json_parser.py
````

Then run using:

```bash
python json_parser_main.py <path-to.json>
```




.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="    This example demonstrates how to generate and use the standalone parser,     using the JSON...">

.. only:: html

 .. figure:: /examples/standalone/images/thumb/sphx_glr_json_parser_main_thumb.png
     :alt: Standalone Parser

     :ref:`sphx_glr_examples_standalone_json_parser_main.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/standalone/json_parser_main
.. raw:: html

    <div class="sphx-glr-clear"></div>



.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-gallery


  .. container:: sphx-glr-download sphx-glr-download-python

    :download:`Download all examples in Python source code: examples_python.zip </examples/examples_python.zip>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

    :download:`Download all examples in Jupyter notebooks: examples_jupyter.zip </examples/examples_jupyter.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
