flpq_data.queries.mcfpq.readwrite.mcfg#

Read (and write) a multiple context-free grammar from (and to) different sources.

Functions

mcfg_from_text(text, *[, start_symbol])

Create a multiple context-free grammar [1] from text.

mcfg_from_txt(path, *[, start_symbol])

Create a multiple context-free grammar [1] from an .mcfg file.

mcfg_to_text(mcfg)

Turns a multiple context-free grammar [1] into its text representation.

mcfg_to_txt(mcfg, path)

Saves a multiple context-free grammar [1] text representation into an .mcfg file.

Classes

MCFG(rules[, start_symbol])

A multiple context-free grammar in the Datalog-like .mcfg syntax.

MCFGRule(head, head_args[, body])

A rule of a multiple context-free grammar.

class MCFG(rules: tuple[MCFGRule, ...], start_symbol: str = 'S')[source]#

A multiple context-free grammar in the Datalog-like .mcfg syntax.

property dimension: int#

The maximum arity of a nonterminal (the \(d\) of \(d\)-MCFG(\(r\))).

property rank: int#

The maximum number of body atoms of a rule (the \(r\) of \(d\)-MCFG(\(r\))).

class MCFGRule(head: str, head_args: tuple[tuple[str, ...], ...], body: tuple[tuple[str, tuple[str, ...]], ...] = ())[source]#

A rule of a multiple context-free grammar.

A basic rule is a production with an empty body: A(eps, eps) is stored as MCFGRule("A", (("eps",), ("eps",)), ()).

mcfg_from_text(text: str, *, start_symbol: str = 'S') → MCFG[source]#

Create a multiple context-free grammar [1] from text.

Parameters:
textstr

The text with which the multiple context-free grammar will be created.

start_symbolstr

Start symbol of a multiple context-free grammar.

Returns:
mcfgMCFG

Multiple context-free grammar.

References

Examples

>>> from flpq_data import *
>>> text = (
...     "A(eps, eps)\n"
...     "A(x1 0, x2 0) <- A(x1, x2)\n"
...     "A(x1 1, x2 1) <- A(x1, x2)\n"
...     "S(x1 y1 # y2 x2) <- A(x1, x2), A(y1, y2)"
... )
>>> mcfg = mcfg_from_text(text)
>>> len(mcfg.rules)
4
>>> mcfg.rules[0]
MCFGRule(head='A', head_args=(('eps',), ('eps',)), body=())
>>> mcfg.dimension, mcfg.rank
(2, 2)
mcfg_from_txt(path: Path | str, *, start_symbol: str = 'S') → MCFG[source]#

Create a multiple context-free grammar [1] from an .mcfg file.

Parameters:
pathUnion[Path, str]

The path to the .mcfg file with which the multiple context-free grammar will be created.

start_symbolstr

Start symbol of a multiple context-free grammar.

Returns:
mcfgMCFG

Multiple context-free grammar.

References

Examples

>>> from flpq_data import *
>>> mcfg = mcfg_from_text("A(eps)\nS(x1) <- A(x1)")
>>> path = mcfg_to_txt(mcfg, "test.mcfg")
>>> mcfg_from_txt(path) == mcfg
True
mcfg_to_text(mcfg: MCFG) → str[source]#

Turns a multiple context-free grammar [1] into its text representation.

The rendering is canonical: one rule per line, argument tokens space-joined, so the same model always produces the same text and mcfg_to_text(mcfg_from_text(text)) reproduces text up to whitespace and comments.

Parameters:
mcfgMCFG

Multiple context-free grammar.

Returns:
textstr

Multiple context-free grammar text representation.

References

Examples

>>> from flpq_data import *
>>> text = (
...     "A(eps, eps)\n"
...     "A(x1 0, x2 0) <- A(x1, x2)\n"
...     "A(x1 1, x2 1) <- A(x1, x2)\n"
...     "S(x1 y1 # y2 x2) <- A(x1, x2), A(y1, y2)"
... )
>>> mcfg_to_text(mcfg_from_text(text)) == text
True
mcfg_to_txt(mcfg: MCFG, path: Path | str) → Path[source]#

Saves a multiple context-free grammar [1] text representation into an .mcfg file.

Parameters:
mcfgMCFG

Multiple context-free grammar.

pathUnion[Path, str]

The path to the .mcfg file where the multiple context-free grammar text representation will be saved.

Returns:
pathPath

The path to the .mcfg file where the text representation will be saved.

References

Examples

>>> from flpq_data import *
>>> mcfg = mcfg_from_text("A(eps)\nS(x1) <- A(x1)")
>>> path = mcfg_to_txt(mcfg, "test.mcfg")