How To Use Lark - Guide
This is the recommended process for working with Lark:
Collect or create input samples, that demonstrate key features or behaviors in the language you're trying to parse.
Write a grammar. Try to aim for a structure that is intuitive, and in a way that imitates how you would explain your language to a fellow human.
Try your grammar in Lark against each input sample. Make sure the resulting parse-trees make sense.
Use Lark's grammar features to [[shape the tree|Tree Construction]]: Get rid of superfluous rules by inlining them, and use aliases when specific cases need clarification.
You can perform steps 1-4 repeatedly, gradually growing your grammar to include more sentences.
Create a transformer to evaluate the parse-tree into a structure you'll be comfortable to work with. This may include evaluating literals, merging branches, or even converting the entire tree into your own set of AST classes.
Of course, some specific use-cases may deviate from this process. Feel free to suggest these cases, and I'll add them to this page.
Browse the Examples to find a template that suits your purposes.
Read the tutorials to get a better understanding of how everything works. (links in the main page)
Use the Cheatsheet (PDF) for quick reference.
Use the reference pages for more in-depth explanations. (links in the main page]
By default Lark silently resolves Shift/Reduce conflicts as Shift. To enable warnings pass
debug=True. To get the messages printed you have to configure
logging framework beforehand. For example:
from lark import Lark import logging logging.basicConfig(level=logging.DEBUG) collision_grammar = ''' start: as as as: a* a: "a" ''' p = Lark(collision_grammar, parser='lalr', debug=True)