Demonstrates how Lark's experimental text-reconstruction feature can recreate functional Python code from its parse-tree, using just the correct grammar and a small formatter. """ from lark import Token, Lark from lark.reconstruct import Reconstructor from lark.indenter import PythonIndenter python_parser3 = Lark.open_from_package( 'lark', 'python.lark', ['grammars'], parser='lalr', postlex=PythonIndenter(), start='file_input', maybe_placeholders=False # Necessary for reconstructor ) SPACE_AFTER = set(',+-*/~@<>="|:') SPACE_BEFORE = (SPACE_AFTER - set(',:')) | set('\'') def special(sym): return Token('SPECIAL', sym.name) def postproc(items): stack = ['\n']
This example demonstrates usage of the included Python grammars """ import sys import os, os.path from io import open import glob, time from lark import Lark from lark.indenter import PythonIndenter kwargs = dict(postlex=PythonIndenter(), start='file_input') # Official Python grammar by Lark python_parser3 = Lark.open_from_package('lark', 'python.lark', ['grammars'], parser='lalr', **kwargs) # Local Python2 grammar python_parser2 = Lark.open('python2.lark', rel_to=__file__, parser='lalr', **kwargs) python_parser2_earley = Lark.open('python2.lark', rel_to=__file__, parser='earley', lexer='basic', **kwargs) try: xrange