cfpq_data.grammars.readwrite.cnf#
Read (and write) a context-free grammar in Chomsky normal form from (and to) different sources.
Functions
|
Create a context-free grammar in Chomsky normal form [1] from text. |
|
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:
text (str) -- The text with which the context-free grammar in Chomsky normal form will be created.
start_symbol (Variable) -- Start symbol of a context-free grammar.
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'
- Returns:
cnf -- Context-free grammar in Chomsky normal form.
- Return type:
CFG
References
- 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:
path (Union[Path, str]) -- The path to the TXT file with which the context-free grammar in Chomsky normal form will be created.
start_symbol (Variable) -- Start symbol of a context-free grammar.
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'
- Returns:
cnf -- Context-free grammar in Chomsky normal form.
- Return type:
CFG
References