1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
Working with the SPPF
=====================
When parsing with Earley, Lark provides the ``ambiguity='forest'`` option
to obtain the shared packed parse forest (SPPF) produced by the parser as
an alternative to it being automatically converted to a tree.
Lark provides a few tools to facilitate working with the SPPF. Here are some
things to consider when deciding whether or not to use the SPPF.
**Pros**
- Efficient storage of highly ambiguous parses
- Precise handling of ambiguities
- Custom rule prioritizers
- Ability to handle infinite ambiguities
- Directly transform forest -> object instead of forest -> tree -> object
**Cons**
- More complex than working with a tree
- SPPF may contain nodes corresponding to rules generated internally
- Loss of Lark grammar features:
- Rules starting with '_' are not inlined in the SPPF
- Rules starting with '?' are never inlined in the SPPF
- All tokens will appear in the SPPF
SymbolNode
----------
.. autoclass:: lark.parsers.earley_forest.SymbolNode
:members: is_ambiguous, children
PackedNode
----------
.. autoclass:: lark.parsers.earley_forest.PackedNode
:members: children
ForestVisitor
-------------
.. autoclass:: lark.parsers.earley_forest.ForestVisitor
:members: visit, visit_symbol_node_in, visit_symbol_node_out,
visit_packed_node_in, visit_packed_node_out,
visit_token_node, on_cycle, get_cycle_in_path
ForestTransformer
-----------------
.. autoclass:: lark.parsers.earley_forest.ForestTransformer
:members: transform, transform_symbol_node, transform_intermediate_node,
transform_packed_node, transform_token_node
TreeForestTransformer
---------------------
.. autoclass:: lark.parsers.earley_forest.TreeForestTransformer
:members: __default__, __default_token__, __default_ambig__
handles_ambiguity
-----------------
.. autofunction:: lark.parsers.earley_forest.handles_ambiguity
|