language
MGReconstructor ¶
Bases: Reconstructor
Reconstructor for the mG language.
The reconstructor transforms a parse tree into the corresponding string, reversing the operation of parsing. This implementation is a slight
modification of Lark's Reconstructor
class to allow the addition of white spaces between the appropriate tokens.
reconstruct ¶
reconstruct(tree: ParseTree, postproc: Callable[[Iterable[str]], Iterable[str]] | None = None, insert_spaces: bool = True) -> str
Reconstructs a string from a parse tree.
Parameters:
-
tree
(ParseTree
) –The tree to reconstruct.
-
postproc
(Callable[[Iterable[str]], Iterable[str]] | None
, default:None
) –The post-processing function to apply to each word of the reconstructed string.
-
insert_spaces
(bool
, default:True
) –If true, add spaces between any two words of the reconstructed string.
Examples:
>>> from lark import Tree, Token
>>> mg_reconstructor.reconstruct(Tree('rhd', [Tree(Token('RULE', 'label'), [Token('__ANON_1', 'a')]), Tree(Token('RULE', 'label'),
... [Token('__ANON_1', 'b')])]))
'|a>b'
Returns:
-
str
–The reconstructed string.