Skip to content

language

Defines the parser and reconstructor of the mG language

This package defines the LALR parser, and the (experimental) reconstructor.

The package contains the following objects:

  • mg_parser
  • mg_reconstructor

mg_parser module-attribute

mg_parser = Lark(mg_grammar, maybe_placeholders=False, parser='lalr')

Parser instance on which to call parse.

Examples:

>>> mg_parser.parse('(a || b) ; c')
Tree('sequential_composition', [Tree('parallel_composition', ...)])

mg_reconstructor module-attribute

mg_reconstructor = MGReconstructor(mg_parser)

Reconstructor (unparser) instance on which to call reconstruct.

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'

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.

Source code in libmg/language/grammar.py
def reconstruct(self, tree: ParseTree, postproc: Callable[[Iterable[str]], Iterable[str]] | None = None, insert_spaces: bool = True) -> str:
    """Reconstructs a string from a parse tree.

    Args:
        tree: The tree to reconstruct.
        postproc: The post-processing function to apply to each word of the reconstructed string.
        insert_spaces: 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:
        The reconstructed string.
    """
    x = self._reconstruct(tree)
    if postproc:
        x = postproc(x)
    y = []
    prev_item = ''
    for item in x:
        if insert_spaces and prev_item and item:
            if (prev_item not in {'<', '>', '(', '{', '|'} and item not in {'<', '>', ')', '}', ',', '|'}
                    and not (item == '(' and prev_item not in mg_reserved)
                    or item == ';' or prev_item == ';'):
                y.append(' ')
        y.append(item)
        prev_item = item
    return ''.join(y)