:orphan:

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

.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

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

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_indented_tree_thumb.png
    :alt:

  :doc:`/examples/indented_tree`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Parsing Indentation</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_lark_grammar_thumb.png
    :alt:

  :doc:`/examples/lark_grammar`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Lark Grammar</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_fruitflies_thumb.png
    :alt:

  :doc:`/examples/fruitflies`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Handling Ambiguity</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_calc_thumb.png
    :alt:

  :doc:`/examples/calc`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Basic calculator</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_turtle_dsl_thumb.png
    :alt:

  :doc:`/examples/turtle_dsl`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Turtle DSL</div>
    </div>


.. 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 an explanation, check out the JSON parser tutorial at /docs/json_tutorial.md">

.. only:: html

  .. image:: /examples/images/thumb/sphx_glr_json_parser_thumb.png
    :alt:

  :doc:`/examples/json_parser`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Simple JSON Parser</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/indented_tree
   /examples/lark_grammar
   /examples/fruitflies
   /examples/calc
   /examples/turtle_dsl
   /examples/json_parser

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

.. raw:: html

.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_conf_lalr_thumb.png
    :alt:

  :doc:`/examples/advanced/conf_lalr`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">LALR’s contextual lexer</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_templates_thumb.png
    :alt:

  :doc:`/examples/advanced/templates`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Templates</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_conf_earley_thumb.png
    :alt:

  :doc:`/examples/advanced/conf_earley`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Earley’s dynamic lexer</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_error_handling_thumb.png
    :alt:

  :doc:`/examples/advanced/error_handling`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Error handling using an interactive parser</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_reconstruct_json_thumb.png
    :alt:

  :doc:`/examples/advanced/reconstruct_json`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Reconstruct a JSON</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_custom_lexer_thumb.png
    :alt:

  :doc:`/examples/advanced/custom_lexer`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Custom lexer</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_tree_forest_transformer_thumb.png
    :alt:

  :doc:`/examples/advanced/tree_forest_transformer`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Transform a Forest</div>
    </div>


.. 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 an explanation, check out the JSON parser tutorial at /docs/json_tutorial.md">

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr__json_parser_thumb.png
    :alt:

  :doc:`/examples/advanced/_json_parser`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Simple JSON Parser</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_prioritizer_thumb.png
    :alt:

  :doc:`/examples/advanced/prioritizer`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Custom SPPF Prioritizer</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="This example demonstrates how to translate between two trees using tree templates. It parses Python 3, translates it to a Python 2 AST, and then outputs the result as Python 2 code.">

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_py3to2_thumb.png
    :alt:

  :doc:`/examples/advanced/py3to2`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Python 3 to Python 2 converter (tree templates)</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_python_parser_thumb.png
    :alt:

  :doc:`/examples/advanced/python_parser`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Grammar-complete Python Parser</div>
    </div>


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

  .. image:: /examples/advanced/images/thumb/sphx_glr_create_ast_thumb.png
    :alt:

  :doc:`/examples/advanced/create_ast`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Creating an AST from the parse tree</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_error_reporting_earley_thumb.png
    :alt:

  :doc:`/examples/advanced/error_reporting_earley`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example-Driven Error Reporting</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_error_reporting_lalr_thumb.png
    :alt:

  :doc:`/examples/advanced/error_reporting_lalr`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Example-Driven Error Reporting</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Demonstrates how Lark&#x27;s experimental text-reconstruction feature can recreate functional Python code from its parse-tree, using just the correct grammar and a small formatter.">

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_reconstruct_python_thumb.png
    :alt:

  :doc:`/examples/advanced/reconstruct_python`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Reconstruct Python</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_dynamic_complete_thumb.png
    :alt:

  :doc:`/examples/advanced/dynamic_complete`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Using lexer dynamic_complete</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/advanced/images/thumb/sphx_glr_qscintilla_json_thumb.png
    :alt:

  :doc:`/examples/advanced/qscintilla_json`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Syntax Highlighting</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>

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.

.. _main.py: https://github.com/lark-parser/lark/blob/master/examples/composition/main.py

.. raw:: html

.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

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

.. only:: html

  .. image:: /examples/composition/images/thumb/sphx_glr_eval_json_thumb.png
    :alt:

  :doc:`/examples/composition/eval_json`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Transformer for evaluating json.lark</div>
    </div>


.. raw:: html

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

.. only:: html

  .. image:: /examples/composition/images/thumb/sphx_glr_eval_csv_thumb.png
    :alt:

  :doc:`/examples/composition/eval_csv`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Transformer for evaluating csv.lark</div>
    </div>


.. 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 allows both CSV and JSON to co-exist.">

.. only:: html

  .. image:: /examples/composition/images/thumb/sphx_glr_main_thumb.png
    :alt:

  :doc:`/examples/composition/main`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Grammar Composition</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>

Example Grammars
================

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

- `Verilog`_ - Taken from https://github.com/circuitgraph/circuitgraph/blob/main/circuitgraph/parsing/verilog.lark

.. _Verilog: https://github.com/lark-parser/lark/blob/master/examples/grammars/verilog.lark

.. raw:: html

.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. thumbnail-parent-div-close

.. raw:: html

    </div>

Standalone example
==================

To initialize, cd to this folder, and run:

.. code-block:: bash

   ./create_standalone.sh

Or:

.. code-block:: bash

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

Then run using:

.. code-block:: bash

   python json_parser_main.py <path-to.json>

.. raw:: html

.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

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

.. only:: html

  .. image:: /examples/standalone/images/thumb/sphx_glr_json_parser_main_thumb.png
    :alt:

  :doc:`/examples/standalone/json_parser_main`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Standalone Parser</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>


.. toctree::
   :hidden:
   :includehidden:


   /examples/advanced/index.rst
   /examples/composition/index.rst
   /examples/grammars/index.rst
   /examples/standalone/index.rst


.. only:: html

  .. container:: sphx-glr-footer 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>`_
