
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/advanced/templates.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_examples_advanced_templates.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_advanced_templates.py:


Templates
=========

This example shows how to use Lark's templates to achieve cleaner grammars

.. GENERATED FROM PYTHON SOURCE LINES 8-29

.. code-block:: default

    from lark import Lark

    grammar = r"""
    start: list | dict

    list: "[" _seperated{atom, ","} "]"
    dict: "{" _seperated{key_value, ","} "}"
    key_value: atom ":" atom

    _seperated{x, sep}: x (sep x)*  // Define a sequence of 'x sep x sep x ...'

    atom: NUMBER | ESCAPED_STRING

    %import common (NUMBER, ESCAPED_STRING, WS)
    %ignore WS
    """


    parser = Lark(grammar)

    print(parser.parse('[1, "a", 2]'))
    print(parser.parse('{"a": 2, "b": 6}'))

.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.000 seconds)


.. _sphx_glr_download_examples_advanced_templates.py:


.. only :: html

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



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

     :download:`Download Python source code: templates.py <templates.py>`



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

     :download:`Download Jupyter notebook: templates.ipynb <templates.ipynb>`


.. only:: html

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

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