

.. _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`_ for more details.

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



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

  :ref:`sphx_glr_examples_composition_eval_json.py`

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

  :ref:`sphx_glr_examples_composition_eval_csv.py`

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

.. only:: html

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

  :ref:`sphx_glr_examples_composition_main.py`

.. raw:: html

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


.. thumbnail-parent-div-close

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /examples/composition/eval_json
   /examples/composition/eval_csv
   /examples/composition/main

