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

        :ref:`Go to the end <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-30

.. code-block:: Python

    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}'))


.. _sphx_glr_download_examples_advanced_templates.py:

.. only:: html

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

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

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

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

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: templates.zip <templates.zip>`


.. only:: html

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

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