cfpq_data.grammars.readwrite.cnf#

Read (and write) a context-free grammar in Chomsky normal form from (and to) different sources.

Functions

cnf_from_text(text, *[, start_symbol])

Create a context-free grammar in Chomsky normal form [1] from text.

cnf_from_txt(path, *[, start_symbol])

Create a context-free grammar in Chomsky normal form [1] from TXT file.

cnf_from_text(text: str, *, start_symbol: Variable = Variable(S)) CFG[source]#

Create a context-free grammar in Chomsky normal form [1] from text.

Parameters:
textstr

The text with which the context-free grammar in Chomsky normal form will be created.

start_symbolVariable

Start symbol of a context-free grammar.

Returns:
cnfCFG

Context-free grammar in Chomsky normal form.

References

Examples

>>> from cfpq_data import *
>>> cnf = cnf_from_text("S -> a b")
>>> cfg_to_text(cnf)
'S -> a#CNF# b#CNF#\na#CNF# -> a\nb#CNF# -> b'
cnf_from_txt(path: Path | str, *, start_symbol: Variable = Variable(S)) CFG[source]#

Create a context-free grammar in Chomsky normal form [1] from TXT file.

Parameters:
pathUnion[Path, str]

The path to the TXT file with which the context-free grammar in Chomsky normal form will be created.

start_symbolVariable

Start symbol of a context-free grammar.

Returns:
cnfCFG

Context-free grammar in Chomsky normal form.

References

Examples

>>> from cfpq_data import *
>>> cnf_1 = cfg_from_text("S -> a b")
>>> path = cfg_to_txt(cnf_1, "test.txt")
>>> cnf = cnf_from_txt(path)
>>> cfg_to_text(cnf)
'S -> a#CNF# b#CNF#\na#CNF# -> a\nb#CNF# -> b'