Transformers & Visitors¶
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
method accepts the children as an argument. That can be modified using the
v_args
decorator, which allows one to inline the arguments (akin to *args
),
or add the tree meta
property as an argument.
See: visitors.py
Visitor¶
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.
There are two classes that implement the visitor interface:
Visitor
: Visit every node (without recursion)Visitor_Recursive
: Visit every node using recursion. Slightly faster.
- Example:
class IncreaseAllNumbers(Visitor): def number(self, tree): assert tree.data == "number" tree.children[0] += 1 IncreaseAllNumbers().visit(parse_tree)
-
class
lark.visitors.
Visitor
¶ Bottom-up visitor, non-recursive.
Visits the tree, starting with the leaves and finally the root (bottom-up) Calls its methods (provided by user via inheritance) according to
tree.data
-
class
lark.visitors.
Visitor_Recursive
¶ Bottom-up visitor, recursive.
Visits the tree, starting with the leaves and finally the root (bottom-up) Calls its methods (provided by user via inheritance) according to
tree.data
Interpreter¶
-
class
lark.visitors.
Interpreter
¶ Interpreter walks the tree starting at the root.
Visits the tree, starting with the root and finally the leaves (top-down)
For each tree node, it calls its methods (provided by user via inheritance) according to
tree.data
.Unlike
Transformer
andVisitor
, the Interpreter doesn’t automatically visit its sub-branches. The user has to explicitly callvisit
,visit_children
, or use the@visit_children_decor
. This allows the user to implement branching and loops.
- Example:
class IncreaseSomeOfTheNumbers(Interpreter): def number(self, tree): tree.children[0] += 1 def skip(self, tree): # skip this subtree. don't change any number node inside it. pass IncreaseSomeOfTheNumbers().visit(parse_tree)
Transformer¶
-
class
lark.visitors.
Transformer
(visit_tokens=True)¶ Transformers visit each node of the tree, and run the appropriate method on it according to the node’s data.
Calls its methods (provided by user via inheritance) according to
tree.data
. The returned value replaces the old one in the structure.They work bottom-up (or depth-first), 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).
Transformer
can do anythingVisitor
can do, but because it reconstructs the tree, it is slightly less efficient. It can be used to implement map or reduce patterns.All these classes implement the transformer interface:
Transformer
- Recursively transforms the tree. This is the one you probably want.Transformer_InPlace
- Non-recursive. Changes the tree in-place instead of returning new instancesTransformer_InPlaceRecursive
- Recursive. Changes the tree in-place instead of returning new instances
- Parameters
visit_tokens – By default, transformers only visit rules. visit_tokens=True will tell
Transformer
to visit tokens as well. This is a slightly slower alternative to lexer_callbacks but it’s easier to maintain and works for all algorithms (even when there isn’t a lexer).
-
__default__
(data, children, meta)¶ Default operation on tree (for override)
Function that is called on if a function with a corresponding name has not been found. Defaults to reconstruct the Tree.
-
__default_token__
(token)¶ Default operation on token (for override)
Function that is called on if a function with a corresponding name has not been found. Defaults to just return the argument.
- 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])
- Example:
class T(Transformer): INT = int NUMBER = float def NAME(self, name): return lookup_dict.get(name, name) T(visit_tokens=True).transform(tree)
v_args¶
-
lark.visitors.
v_args
(inline=False, meta=False, tree=False, wrapper=None)¶ A convenience decorator factory for modifying the behavior of user-supplied visitor methods.
By default, callback methods of transformers/visitors accept one argument - a list of the node’s children.
v_args
can modify this behavior. When used on a transformer/visitor class definition, it applies to all the callback methods inside it.- Parameters
inline – Children are provided as
*args
instead of a list argument (not recommended for very long lists).meta – Provides two arguments:
children
andmeta
(instead of just the first)tree – Provides the entire tree as the argument, instead of the children.
Example
@v_args(inline=True) class SolveArith(Transformer): def add(self, left, right): return left + right class ReverseNotation(Transformer_InPlace): @v_args(tree=True) def tree_node(self, tree): tree.children = tree.children[::-1]