cfpq_data.grammars.readwrite.cfg#
Read (and write) a context-free grammar from (and to) different sources.
Functions
|
Create a context-free grammar [1] from text. |
|
Create a context-free grammar [1] from TXT file. |
|
Turns a context-free grammar [1] into its text representation. |
|
Saves a context-free grammar [1] text representation into TXT file. |
- cfg_from_text(text: str, *, start_symbol: Variable = Variable(S)) CFG [source]#
Create a context-free grammar [1] from text.
- Parameters:
text (str) -- The text with which the context-free grammar will be created.
start_symbol (Variable) -- Start symbol of a context-free grammar.
Examples
>>> from cfpq_data import * >>> cfg = cfg_from_text("S -> a S b S") >>> cfg_to_text(cfg) 'S -> a S b S'
- Returns:
cfg -- Context-free grammar.
- Return type:
CFG
References
- cfg_from_txt(path: Path | str, *, start_symbol: Variable = Variable(S)) CFG [source]#
Create a context-free grammar [1] from TXT file.
- Parameters:
path (Union[Path, str]) -- The path to the TXT file with which the context-free grammar will be created.
start_symbol (Variable) -- Start symbol of a context-free grammar.
Examples
>>> from cfpq_data import * >>> cfg_1 = cfg_from_text("S -> a S b S | epsilon") >>> path = cfg_to_txt(cfg_1, "test.txt") >>> cfg = cfg_from_txt(path) >>> cfg_to_text(cfg) 'S -> \nS -> a S b S'
- Returns:
cfg -- Context-free grammar.
- Return type:
CFG
References
- cfg_to_text(cfg: CFG) str [source]#
Turns a context-free grammar [1] into its text representation.
- Parameters:
cfg (CFG) -- Context-free grammar.
Examples
>>> from cfpq_data import * >>> cfg = cfg_from_text("S -> a S b S | epsilon") >>> cfg_to_text(cfg) 'S -> \nS -> a S b S'
- Returns:
text -- Context-free grammar text representation.
- Return type:
References
- cfg_to_txt(cfg: CFG, path: Path | str) Path [source]#
Saves a context-free grammar [1] text representation into TXT file.
- Parameters:
cfg (CFG) -- Context-free grammar.
path (Union[Path, str]) -- The path to the TXT file where context-free grammar text representation will be saved.
Examples
>>> from cfpq_data import * >>> cfg = cfg_from_text("S -> a S b S") >>> path = cfg_to_txt(cfg, "test.txt")
- Returns:
path -- The path to the TXT file where context-free grammar text representation will be saved.
- Return type:
Path
References