This page details the important classes in Lark.
The Lark class is the main interface for the library. It’s mostly a thin wrapper for the many different parsers, and for the tree constructor.
The Lark class accepts a grammar string or file object, and keyword options:
start - The symbol in the grammar that begins the parse (Default: "start")
parser - Decides which parser engine to use, “earley”, “lalr” or “cyk”. (Default: "earley")
lexer - Overrides default lexer.
transformer - Applies the transformer instead of building a parse tree (only allowed with parser="lalr”)
postlex - Lexer post-processing (Default: None. only works when lexer is “standard” or “contextual”)
ambiguity (only relevant for earley and cyk)
“explicit” - Return all derivations inside an “_ambig” data node.
“resolve” - Let the parser choose the best derivation (greedy for tokens, non-greedy for rules. Default)
debug - Display warnings (such as Shift-Reduce warnings for LALR)
keep_all_tokens - Don’t throw away any terminals from the tree (Default=False)
propagate_positions - Propagate line/column count to tree nodes (default=False)
Return a complete parse tree for the text (of type Tree)
If a transformer is supplied to __init__, returns whatever is the result of the transformation.
The main tree class
data - The name of the rule or aliaschildren - List of matched sub-rules and terminalsmeta - Line & Column numbers, if using propagate_positionsCreates a new tree, and stores “data” and “children” in attributes of the same name.
Returns an indented string representation of the tree. Great for debugging.
Returns all nodes of the tree that evaluate pred(node) as true.
Returns all nodes of the tree whose data equals the given data.
Iterates over all the subtrees, never returning to the same node twice (Lark’s parse-tree is actually a DAG)
Trees can be hashed and compared.
Transformers & Visitors provide a convenient interface to process the parse-trees that Lark returns.
They are used by inheriting from the correct class (visitor or transformer), and implementing methods corresponding to the rule you wish to process. Each methods accepts the children as an argument. That can be modified using the v-args decorator, which allows to inline the arguments (akin to *args), or add the tree meta property as an argument.
See: https://github.com/lark-parser/lark/blob/master/lark/visitors.py
Visitors visit each node of the tree, and run the appropriate method on it according to the node’s data.
They work bottom-up, starting with the leaves and ending at the root of the tree.
Example
class IncreaseAllNumbers(Visitor):
def number(self, tree):
assert tree.data == "number"
tree.children[0] += 1
IncreaseAllNumbers().visit(parse_tree)
There are two classes that implement the visitor interface:
Visitor - Visit every node (without recursion)
Visitor_Recursive - Visit every node using recursion. Slightly faster.
Transformers visit each node of the tree, and run the appropriate method on it according to the node’s data.
They work bottom-up, starting with the leaves and ending at the root of the tree.
Transformers can be used to implement map & reduce patterns.
Because nodes are reduced from leaf to root, at any point the callbacks may assume the children have already been transformed (if applicable).
Transformers can be chained into a new transformer by using multiplication.
Example:
from lark import Tree, Transformer
class EvalExpressions(Transformer):
def expr(self, args):
return eval(args[0])
t = Tree('a', [Tree('expr', ['1+2'])])
print(EvalExpressions().transform( t ))
# Prints: Tree(a, [3])
Here are the classes that implement the transformer interface:
When using a lexer, the resulting tokens in the trees will be of the Token class, which inherits from Python’s string. So, normal string comparisons and operations will work as expected. Tokens also have other useful attributes: