def test_cfg_from_and_to_txt(grammar): (fd, fname) = tempfile.mkstemp() cfg_1 = cfpq_data.cfg_from_text(grammar) path = cfpq_data.cfg_to_txt(cfg_1, fname) cfg_2 = cfpq_data.cfg_from_txt(path) os.close(fd) os.unlink(fname) assert set(cfpq_data.cfg_to_text(cfg_1).splitlines()) == set( cfpq_data.cfg_to_text(cfg_2).splitlines() )
def test_cfg_to_text(grammar, expected): cfg = cfpq_data.cfg_from_text(grammar) actual = set(cfpq_data.cfg_to_text(cfg).splitlines()) assert actual == expected
import pytest import cfpq_data cfg_1 = cfpq_data.cfg_from_text("S -> a S b | a b") cfg_2 = cfpq_data.cfg_from_text( "S -> sco_r S sco | t_r S t | sco_r sco | t_r t") rsm_1 = cfpq_data.rsm_from_text("S -> a S b | a b") rsm_2 = cfpq_data.rsm_from_text( "S -> sco_r S sco | t_r S t | sco_r sco | t_r t") @pytest.mark.parametrize("cfg", [cfg_1, cfg_2]) def test_cnf_from_cfg(cfg): cnf = cfpq_data.cnf_from_cfg(cfg) for word in cnf.get_words(4): assert cnf.contains(word) and cfg.contains(word) @pytest.mark.parametrize("rsm", [rsm_1, rsm_2]) def test_cnf_from_rsm(rsm): cnf = cfpq_data.cnf_from_rsm(rsm) for word in cnf.get_words(4): assert cnf.contains(word) and rsm.contains(word)
def test_cfg_from_text(grammar, expected): cfg = cfpq_data.cfg_from_text(grammar) for word in expected: assert cfg.contains(word)
import pytest import cfpq_data cfg_1 = cfpq_data.cfg_from_text("S -> a S b S\nS -> a b\n") exp_cfg_1 = cfpq_data.cfg_from_text("S -> b S c S\nS -> b c\n") cfg_2 = cfpq_data.cfg_from_text("S -> a b c d\n") exp_cfg_2 = cfpq_data.cfg_from_text("S -> b c c d\n") cnf_1 = cfpq_data.cnf_from_text("S -> a") exp_cnf_1 = cfpq_data.cnf_from_text("S -> b") cnf_2 = cfpq_data.cnf_from_text("S -> b") exp_cnf_2 = cfpq_data.cnf_from_text("S -> c") rsm_1 = cfpq_data.rsm_from_text("S -> a b\n") exp_rsm_1 = cfpq_data.rsm_from_text("S -> b c\n") rsm_2 = cfpq_data.rsm_from_text("S -> b a\n") exp_rsm_2 = cfpq_data.rsm_from_text("S -> c b\n") @pytest.mark.parametrize( "cfg, exp_cfg", [ (cfg_1, exp_cfg_1), (cfg_2, exp_cfg_2), ], ) def test_change_terminals_in_cfg(cfg, exp_cfg):