flpq_data.queries.mcfpq.readwrite.mcfg#
Read (and write) a multiple context-free grammar from (and to) different sources.
Functions
|
Create a multiple context-free grammar [1] from text. |
|
Create a multiple context-free grammar [1] from an |
|
Turns a multiple context-free grammar [1] into its text representation. |
|
Saves a multiple context-free grammar [1] text representation into an |
Classes
|
A multiple context-free grammar in the Datalog-like |
|
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
.mcfgsyntax.
- 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 asMCFGRule("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
.mcfgfile.- Parameters:
- pathUnion[Path, str]
The path to the
.mcfgfile 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))reproducestextup 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
.mcfgfile.- Parameters:
- mcfgMCFG
Multiple context-free grammar.
- pathUnion[Path, str]
The path to the
.mcfgfile where the multiple context-free grammar text representation will be saved.
- Returns:
- pathPath
The path to the
.mcfgfile 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")