: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 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 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 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="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="It requires the &#x27;bytecode&#x27; library. You can get it using ::">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_python_bytecode_thumb.png
     :alt: Compile Python to Bytecode

     :ref:`sphx_glr_examples_advanced_python_bytecode.py`

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/advanced/python_bytecode

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="create_transformer() collects every subclass of Ast subclass from the module, and creates a Lar...">

.. only:: html

 .. figure:: /examples/advanced/images/thumb/sphx_glr_create_ast_thumb.png
     :alt: This example demonstrates how to transform a parse-tree into an AST using `lark.ast_utils`.

     :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="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 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>



.. 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>`_
