:orphan: Examples for Lark ================= **How to run the examples**: After cloning the repo, open the terminal into the root directory of the project, and run the following: .. code:: bash [lark]$ python -m examples. For example, the following will parse all the Python files in the standard library of your local installation: .. code:: bash [lark]$ python -m examples.advanced.python_parser Beginner Examples ~~~~~~~~~~~~~~~~~ .. raw:: html
.. raw:: html
.. only:: html .. image:: /examples/images/thumb/sphx_glr_indented_tree_thumb.png :alt: :ref:`sphx_glr_examples_indented_tree.py` .. raw:: html
Parsing Indentation
.. raw:: html
.. only:: html .. image:: /examples/images/thumb/sphx_glr_lark_grammar_thumb.png :alt: :ref:`sphx_glr_examples_lark_grammar.py` .. raw:: html
Lark Grammar
.. raw:: html
.. only:: html .. image:: /examples/images/thumb/sphx_glr_fruitflies_thumb.png :alt: :ref:`sphx_glr_examples_fruitflies.py` .. raw:: html
Handling Ambiguity
.. raw:: html
.. only:: html .. image:: /examples/images/thumb/sphx_glr_calc_thumb.png :alt: :ref:`sphx_glr_examples_calc.py` .. raw:: html
Basic calculator
.. raw:: html
.. only:: html .. image:: /examples/images/thumb/sphx_glr_turtle_dsl_thumb.png :alt: :ref:`sphx_glr_examples_turtle_dsl.py` .. raw:: html
Turtle DSL
.. raw:: html
.. only:: html .. image:: /examples/images/thumb/sphx_glr_json_parser_thumb.png :alt: :ref:`sphx_glr_examples_json_parser.py` .. raw:: html
Simple JSON Parser
.. raw:: html
.. toctree:: :hidden: /examples/indented_tree /examples/lark_grammar /examples/fruitflies /examples/calc /examples/turtle_dsl /examples/json_parser Advanced Examples ~~~~~~~~~~~~~~~~~ .. raw:: html
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_conf_lalr_thumb.png :alt: :ref:`sphx_glr_examples_advanced_conf_lalr.py` .. raw:: html
LALR’s contextual lexer
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_templates_thumb.png :alt: :ref:`sphx_glr_examples_advanced_templates.py` .. raw:: html
Templates
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_conf_earley_thumb.png :alt: :ref:`sphx_glr_examples_advanced_conf_earley.py` .. raw:: html
Earley’s dynamic lexer
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_error_handling_thumb.png :alt: :ref:`sphx_glr_examples_advanced_error_handling.py` .. raw:: html
Error handling using an interactive parser
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_reconstruct_json_thumb.png :alt: :ref:`sphx_glr_examples_advanced_reconstruct_json.py` .. raw:: html
Reconstruct a JSON
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_custom_lexer_thumb.png :alt: :ref:`sphx_glr_examples_advanced_custom_lexer.py` .. raw:: html
Custom lexer
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_tree_forest_transformer_thumb.png :alt: :ref:`sphx_glr_examples_advanced_tree_forest_transformer.py` .. raw:: html
Transform a Forest
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr__json_parser_thumb.png :alt: :ref:`sphx_glr_examples_advanced__json_parser.py` .. raw:: html
Simple JSON Parser
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_prioritizer_thumb.png :alt: :ref:`sphx_glr_examples_advanced_prioritizer.py` .. raw:: html
Custom SPPF Prioritizer
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_py3to2_thumb.png :alt: :ref:`sphx_glr_examples_advanced_py3to2.py` .. raw:: html
Python 3 to Python 2 converter (tree templates)
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_python_parser_thumb.png :alt: :ref:`sphx_glr_examples_advanced_python_parser.py` .. raw:: html
Grammar-complete Python Parser
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_create_ast_thumb.png :alt: :ref:`sphx_glr_examples_advanced_create_ast.py` .. raw:: html
Creating an AST from the parse tree
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_error_reporting_earley_thumb.png :alt: :ref:`sphx_glr_examples_advanced_error_reporting_earley.py` .. raw:: html
Example-Driven Error Reporting
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_error_reporting_lalr_thumb.png :alt: :ref:`sphx_glr_examples_advanced_error_reporting_lalr.py` .. raw:: html
Example-Driven Error Reporting
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_reconstruct_python_thumb.png :alt: :ref:`sphx_glr_examples_advanced_reconstruct_python.py` .. raw:: html
Reconstruct Python
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_dynamic_complete_thumb.png :alt: :ref:`sphx_glr_examples_advanced_dynamic_complete.py` .. raw:: html
Using lexer dynamic_complete
.. raw:: html
.. only:: html .. image:: /examples/advanced/images/thumb/sphx_glr_qscintilla_json_thumb.png :alt: :ref:`sphx_glr_examples_advanced_qscintilla_json.py` .. raw:: html
Syntax Highlighting
.. raw:: html
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
.. raw:: html
.. only:: html .. image:: /examples/composition/images/thumb/sphx_glr_eval_json_thumb.png :alt: :ref:`sphx_glr_examples_composition_eval_json.py` .. raw:: html
Transformer for evaluating json.lark
.. raw:: html
.. only:: html .. image:: /examples/composition/images/thumb/sphx_glr_eval_csv_thumb.png :alt: :ref:`sphx_glr_examples_composition_eval_csv.py` .. raw:: html
Transformer for evaluating csv.lark
.. raw:: html
.. only:: html .. image:: /examples/composition/images/thumb/sphx_glr_main_thumb.png :alt: :ref:`sphx_glr_examples_composition_main.py` .. raw:: html
Grammar Composition
.. raw:: html
Example Grammars ================ This directory is a collection of lark grammars, taken from real world projects. - `Verilog`_ - Taken from https://github.com/circuitgraph/circuitgraph/blob/main/circuitgraph/parsing/verilog.lark .. _Verilog: https://github.com/lark-parser/lark/blob/master/examples/grammars/verilog.lark .. raw:: html
.. raw:: html
Standalone example ================== To initialize, cd to this folder, and run: .. code-block:: bash ./create_standalone.sh Or: .. code-block:: bash python -m lark.tools.standalone json.lark > json_parser.py Then run using: .. code-block:: bash python json_parser_main.py .. raw:: html
.. raw:: html
.. only:: html .. image:: /examples/standalone/images/thumb/sphx_glr_json_parser_main_thumb.png :alt: :ref:`sphx_glr_examples_standalone_json_parser_main.py` .. raw:: html
Standalone Parser
.. raw:: html
.. toctree:: :hidden: :includehidden: /examples/advanced/index.rst /examples/composition/index.rst /examples/grammars/index.rst /examples/standalone/index.rst .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-gallery .. container:: sphx-glr-download sphx-glr-download-python :download:`Download all examples in Python source code: examples_python.zip ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download all examples in Jupyter notebooks: examples_jupyter.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_