
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/advanced/conf_earley.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_conf_earley.py>`
        to download the full example code.

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

.. _sphx_glr_examples_advanced_conf_earley.py:


Earley’s dynamic lexer
======================

Demonstrates the power of Earley’s dynamic lexer on a toy configuration language

Using a lexer for configuration files is tricky, because values don't
have to be surrounded by delimiters. Using a basic lexer for this just won't work.

In this example we use a dynamic lexer and let the Earley parser resolve the ambiguity.

Another approach is to use the contextual lexer with LALR. It is less powerful than Earley,
but it can handle some ambiguity when lexing and it's much faster.
See examples/conf_lalr.py for an example of that approach.

.. GENERATED FROM PYTHON SOURCE LINES 17-46

.. code-block:: Python

    from lark import Lark

    parser = Lark(r"""
            start: _NL? section+
            section: "[" NAME "]" _NL item+
            item: NAME "=" VALUE? _NL

            NAME: /\w/+
            VALUE: /./+

            %import common.NEWLINE -> _NL
            %import common.WS_INLINE
            %ignore WS_INLINE
        """, parser="earley")

    def test():
        sample_conf = """
    [bla]

    a=Hello
    this="that",4
    empty=
    """

        r = parser.parse(sample_conf)
        print (r.pretty())

    if __name__ == '__main__':
        test()


.. _sphx_glr_download_examples_advanced_conf_earley.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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