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

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_fruitflies.py>`
        to download the full example code.

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

.. _sphx_glr_examples_fruitflies.py:


Handling Ambiguity
==================

A demonstration of ambiguity

This example shows how to use get explicit ambiguity from Lark's Earley parser.

.. GENERATED FROM PYTHON SOURCE LINES 10-59

.. code-block:: Python

    import sys
    from lark import Lark, tree

    grammar = """
        sentence: noun verb noun        -> simple
                | noun verb "like" noun -> comparative

        noun: adj? NOUN
        verb: VERB
        adj: ADJ

        NOUN: "flies" | "bananas" | "fruit"
        VERB: "like" | "flies"
        ADJ: "fruit"

        %import common.WS
        %ignore WS
    """

    parser = Lark(grammar, start='sentence', ambiguity='explicit')

    sentence = 'fruit flies like bananas'

    def make_png(filename):
        tree.pydot__tree_to_png( parser.parse(sentence), filename)

    def make_dot(filename):
        tree.pydot__tree_to_dot( parser.parse(sentence), filename)

    if __name__ == '__main__':
        print(parser.parse(sentence).pretty())
        # make_png(sys.argv[1])
        # make_dot(sys.argv[1])

    # Output:
    #
    # _ambig
    #   comparative
    #     noun	fruit
    #     verb	flies
    #     noun	bananas
    #   simple
    #     noun
    #       fruit
    #       flies
    #     verb	like
    #     noun	bananas
    #
    # (or view a nicer version at "./fruitflies.png")


.. _sphx_glr_download_examples_fruitflies.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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