def test_intersection_dfa2(self): state0 = State(0) symb_a = Symbol("a") symb_b = Symbol("b") dfa = DeterministicFiniteAutomaton({state0}, {symb_a, symb_b}, start_state=state0, final_states={state0}) dfa.add_transition(state0, symb_a, state0) dfa.add_transition(state0, symb_b, state0) self.assertTrue(dfa.accepts([symb_a, symb_a, symb_b, symb_b])) ter_a = Terminal("a") ter_b = Terminal("b") var_s = Variable("S") var_s1 = Variable("S1") var_l = Variable("L") productions = { Production(var_s, [var_l, var_s1]), Production(var_l, [Epsilon()]), Production(var_s1, [ter_a, var_s1, ter_b]), Production(var_s1, [ter_b, var_s1, ter_a]), Production(var_s1, []) } cfg = CFG(productions=productions, start_symbol=var_s) self.assertTrue(cfg.contains([ter_a, ter_a, ter_b, ter_b])) self.assertFalse(cfg.contains([ter_a, ter_a, ter_b])) cfg_i = cfg.intersection(dfa) self.assertFalse(cfg_i.is_empty()) self.assertTrue(cfg_i.contains([ter_a, ter_a, ter_b, ter_b])) self.assertTrue(cfg_i.contains([]))
def test_intersection(self): """ Tests the intersection with a regex """ regex = Regex("a*b*") dfa = regex.to_epsilon_nfa() symb_a = Symbol("a") symb_b = Symbol("b") self.assertTrue(dfa.accepts([symb_a, symb_a, symb_b, symb_b])) self.assertFalse(dfa.accepts([symb_b, symb_b, symb_a])) ter_a = Terminal("a") ter_b = Terminal("b") var_s = Variable("S") productions = { Production(var_s, [ter_a, var_s, ter_b]), Production(var_s, [ter_b, var_s, ter_a]), Production(var_s, []) } cfg = CFG(productions=productions, start_symbol=var_s) self.assertTrue(cfg.contains([ter_a, ter_a, ter_b, ter_b])) self.assertFalse(cfg.contains([ter_a, ter_a, ter_b])) cfg_i = cfg.intersection(regex) self.assertTrue(cfg_i.contains([ter_a, ter_a, ter_b, ter_b])) self.assertFalse(cfg_i.contains([ter_a, ter_a, ter_b])) self.assertTrue(cfg_i.contains([])) cfg_i = cfg.intersection(dfa) self.assertTrue(cfg_i.contains([ter_a, ter_a, ter_b, ter_b])) self.assertFalse(cfg_i.contains([ter_a, ter_a, ter_b])) self.assertTrue(cfg_i.contains([]))
class RuleSet: rules: CFG def __init__(self, rules: Iterable[str], patch: bool = False): start_var: Variable vars: set[Variable] = set() terminals: Set[Terminal] = set() productions: Set[Production] = set() for rule in rules: i, r = rule.split(": ") var = Variable(i) if i == "0": start_var = var if r[0] == '"': ter = Terminal(r[1]) terminals.add(ter) productions.add(Production(var, [ter])) continue if patch: if i == "8": r = "42 | 42 8" if i == "11": r = "42 31 | 42 11 31" rr = r.split(" | ") for r in rr: productions.add( Production(var, [Variable(x) for x in r.split(" ")])) self.CFG = CFG(vars, terminals, start_var, productions) def validate(self, to_validate: str) -> bool: return self.CFG.contains(to_validate)
def test_intersection_with_epsilon(self): state0 = State(0) state1 = State(1) symb_a = Symbol("a") dfa = DeterministicFiniteAutomaton({state0, state1}, {symb_a}, start_state=state0, final_states={state1}) dfa.add_transition(state0, symb_a, state1) self.assertTrue(dfa.accepts([symb_a])) ter_a = Terminal("a") var_s = Variable("S") var_l = Variable("L") var_t = Variable("T") productions = { Production(var_s, [var_l, var_t]), Production(var_l, [Epsilon()]), Production(var_t, [ter_a]) } cfg = CFG(productions=productions, start_symbol=var_s) self.assertFalse(cfg.is_empty()) self.assertTrue(cfg.contains([ter_a])) cfg_temp = cfg.to_pda().to_cfg() self.assertFalse(cfg_temp.is_empty()) self.assertTrue(cfg_temp.contains([ter_a])) cfg_temp = cfg.to_pda().to_final_state().to_empty_stack().to_cfg() self.assertFalse(cfg_temp.is_empty()) self.assertTrue(cfg_temp.contains([ter_a])) cfg_i = cfg.intersection(dfa) self.assertFalse(cfg_i.is_empty())
def part2(file='input_test.txt'): rules, messages = get_input(file) # 8: 42 | 42 8 # 11: 42 31 | 42 11 31 rules['8'] = '42 | 42 8' rules['11'] = '42 31 | 42 11 31' rule_variables = set() rule_products = set() for rule in rules: subs = rules[rule].split(' | ') rule_variables.add(Variable(rule)) for sub in subs: if sub == '"a"' or sub == '"b"': rule_products.add( Production(Variable(rule), [Terminal(sub.replace('"', ''))])) else: rule_products.add( Production(Variable(rule), [Variable(x) for x in sub.split(' ')])) cfg = CFG(rule_variables, {Terminal('a'), Terminal('b')}, Variable('0'), rule_products) count = 0 for message in messages: if cfg.contains(message): count += 1 print('Part 2: Solution {}'.format(count))
def part2(): rules, words = open("in.txt").read().split("\n\n") rules = rules.replace("8: 42", "8: 42 | 42 8") rules = rules.replace("11: 42 31", "11: 42 31 | 42 11 31") variables = set() productions = set() terminals = set() for line in rules.split("\n"): left, right = line.split(":") left = Variable(left) variables.add(left) for expression in right.split("|"): if '"' in expression: # Terminal expression expression = expression.strip('" ') right = [Terminal(expression)] terminals.add(Terminal(expression)) productions.add(Production(left, right)) else: right = [ Variable(token) for token in expression.strip().split() ] productions.add(Production(left, right)) cfg = CFG(variables, terminals, Variable("0"), productions) count = sum(map(lambda x: 1 if cfg.contains(x) else 0, words.split("\n"))) print(count)
def test_conversions(self): """ Tests multiple conversions """ ter_a = Terminal("a") ter_b = Terminal("b") ter_c = Terminal("c") var_s = Variable("S") productions = { Production(var_s, [ter_a, var_s, ter_b]), Production(var_s, [ter_c]) } cfg = CFG(productions=productions, start_symbol=var_s) cfg = cfg.to_pda().to_final_state().to_empty_stack().to_cfg() self.assertTrue(cfg.contains([ter_c])) self.assertTrue(cfg.contains([ter_a, ter_c, ter_b])) self.assertTrue(cfg.contains([ter_a, ter_a, ter_c, ter_b, ter_b])) self.assertFalse(cfg.contains([ter_b, ter_c, ter_a])) self.assertFalse(cfg.contains([ter_b, ter_b, ter_c, ter_a, ter_a]))
def test_membership(self): """ Tests the membership of a CFG """ # pylint: disable=too-many-locals var_useless = Variable("USELESS") var_s = Variable("S") var_b = Variable("B") ter_a = Terminal("a") ter_b = Terminal("b") ter_c = Terminal("c") prod0 = Production(var_s, [ter_a, var_s, var_b]) prod1 = Production(var_useless, [ter_a, var_s, var_b]) prod2 = Production(var_s, [var_useless]) prod4 = Production(var_b, [ter_b]) prod5 = Production(var_useless, []) cfg0 = CFG({var_useless, var_s}, {ter_a, ter_b}, var_s, {prod0, prod1, prod2, prod4, prod5}) self.assertTrue(cfg0.contains([Epsilon()])) self.assertTrue(cfg0.contains([ter_a, ter_b])) self.assertTrue(cfg0.contains([ter_a, ter_a, ter_b, ter_b])) self.assertTrue( cfg0.contains([ter_a, ter_a, ter_a, ter_b, ter_b, ter_b])) self.assertFalse(cfg0.contains([ter_a, ter_b, ter_b])) self.assertFalse(cfg0.contains([ter_a, ter_b, ter_c, ter_b])) self.assertFalse(cfg0.contains([ter_a, ter_a, ter_a, ter_b, ter_b])) prod3 = Production(var_s, [ter_c]) cfg0 = CFG({var_s}, {ter_a, ter_b, ter_c}, var_s, {prod0, prod3}) self.assertFalse(cfg0.contains([Epsilon()])) var_a = Variable("A") prod6 = Production(var_s, [var_a, var_b]) prod7 = Production(var_a, [var_a, var_b]) prod8 = Production(var_a, [ter_a]) prod9 = Production(var_b, [ter_b]) cfg1 = CFG({var_a, var_b, var_s}, {ter_a, ter_b}, var_s, {prod6, prod7, prod8, prod9}) self.assertTrue(cfg1.contains([ter_a, ter_b, ter_b])) cfg1 = CFG({"A", "B", "S"}, {"a", "b"}, "S", {prod6, prod7, prod8, prod9}) self.assertTrue(cfg1.contains(["a", "b", "b"]))
result = set() for order in data: subParts = [getMatches(o) for o in order] result.update("".join(e) for e in itertools.product(*subParts)) gotResult[ruleNumber] = result return result validZero = getMatches("0") print(sum(s in validZero for s in strings)) # Part 2 from pyformlang.cfg import Production, Variable, Terminal, CFG vars = {k: Variable(k) for k in ruleParsed} terminals = set() prods = set() ruleParsed["8"] = [["42"], ["42", "8"]] ruleParsed["11"] = [["42", "31"], ["42", "11", "31"]] for num, data in ruleParsed.items(): if isinstance(data, str): ter = Terminal(data) terminals.add(ter) prods.add(Production(vars[num], [ter])) else: for order in data: prods.add(Production(vars[num], [vars[n] for n in order])) cfg = CFG(set(), terminals, vars["0"], prods) print(sum(cfg.contains(s) for s in strings))
term_a = Terminal("a") term_b = Terminal("b") rule_map = dict() for rule in rules: no, item = rule.split(": ") rule_map[int(no)] = item for index, rule in rule_map.items(): variables.add(Variable(index)) if "a" in rule: productions.add(Production(Variable(index), [term_a])) elif "b" in rule: productions.add(Production(Variable(index), [term_b])) else: to_add = rule.split(" | ") for item in to_add: productions.add( Production(Variable(index), [Variable(int(x)) for x in item.split(" ")])) cfg = CFG(variables, {term_a, term_b}, Variable(0), productions) total = 0 for item in data: print(item) if cfg.contains(item): total += 1 print(total)
if "|" in production: # create multiple productions part1, part2 = production.split(' | ') tmp_p1 = [Variable(p1) for p1 in part1.split()] productions.add(Production(Variable(var), tmp_p1)) tmp_p2 = [Variable(p2) for p2 in part2.split()] productions.add(Production(Variable(var), tmp_p2)) elif '"' in production: # create a terminal t = production[1] terminals.add(Terminal(t)) productions.add(Production(Variable(var), [Terminal(t)])) else: # just a normal production tmp_p = [Variable(p) for p in production.split()] productions.add(Production(Variable(var), tmp_p)) else: strings.append(line) cfg = CFG(variables, terminals, Variable("0"), productions) print(f'[INFO] CFG is built') for string in strings: if cfg.contains(string): cnt += 1 print(f'Total strings in grammar is ** {cnt} **')
if name not in non_terminals: non_terminals[name] = Variable(name) return non_terminals[name] else: if name not in terminals: terminals[name] = Terminal(name) return terminals[name] rules = set() rules2 = set() with Path('input.txt').open() as file: while line := file.readline().strip(): left, rights = line.split(': ') for right in rights.split(' | '): rule = Production(trans(int(left)), list(map(trans, map(eval, right.split())))) rules.add(rule) rules2.add(rule) if left == '8': rules2.add(Production(trans(int(left)), list(map(trans, (42, 8))))) if left == '11': rules2.add(Production(trans(int(left)), list(map(trans, (42, 11, 31))))) words = [line.strip() for line in file.readlines()] cfg = CFG(set(non_terminals.values()), set(terminals.values()), non_terminals[0], rules) print(sum(cfg.contains(word) for word in words)) cfg2 = CFG(set(non_terminals.values()), set(terminals.values()), non_terminals[0], rules2) print(sum(cfg2.contains(word) for word in words))
if rule_re.match(line): line = line.split(':') rule_no = line[0] vrs.add(Variable(rule_no)) if '"' in line[1]: # Terminal rule (a or b) t = line[1].strip().replace('"', '') print('add terminal', rule_no, t) prods.add(Production(Variable(rule_no), [Terminal(t)])) else: # Production rule (with or without |) for sub in line[1].split('|'): vs = ints_re.findall(sub) print('add variables', rule_no, vs) prods.add( Production(Variable(rule_no), [Variable(var) for var in vs])) else: # messages messages.append(line) ta = Terminal('a') tb = Terminal('b') cfg = CFG(vrs, {ta, tb}, Variable('0'), prods) n = 0 for m in messages: if cfg.contains(m): n += 1 print(n)
elif data['rules'] == '"b"': ters["b"] = Terminal("b") productions.append(Production(vars[data['id']], [ters["b"]])) elif '|' in data['rules']: for r in data['rules'].split("|"): productions.append( create_production(data['id'], [int(x) for x in r.split()])) else: productions.append( create_production(data['id'], [int(x) for x in data['rules'].split()])) while line := file.readline().strip(): tests.append(line) # Creation of the Context-Free Grammar # PART 1 cfg1 = CFG(set(vars.values()), set(ters.values()), vars[0], set(productions)) # PART 2 productions.append(create_production(8, [42, 8])) productions.append(create_production(11, [42, 11, 31])) cfg2 = CFG(set(vars.values()), set(ters.values()), vars[0], set(productions)) valids = [] for test in tests: # valids.append(cfg1.contains([ters[char] for char in test])) valids.append(cfg2.contains([ters[char] for char in test])) print(valids.count(True))
ter_re = re.compile(r'.*([ab]).*') lines = [x.strip() for x in open('part2.txt')] rules = { x[0:x.index(':')]: x[x.index(':') + 2:] for x in lines if len(x) > 1 and ':' in x } msgs = (x.strip() for x in lines if len(x) > 1 and ':' not in x) rule_vars = set() rule_prods = set() for rule in rules: subs = rules[rule].split('|') rule_vars.add(Variable(rule)) for sub in subs: if ter_re.match(sub): rule_prods.add( Production(Variable(rule), [Terminal(ter_re.match(sub).group(1))])) else: rule_prods.add( Production(Variable(rule), [Variable(x) for x in var_re.findall(sub)])) ter_a = Terminal('a') ter_b = Terminal('b') cfg = CFG(rule_vars, {ter_a, ter_b}, Variable('0'), rule_prods) print(sum([1 for msg in msgs if cfg.contains(msg)]))
def grammar(request): var1_1 = request.GET.get("numb1") var1_1=str(":" if var1_1 is None else var1_1) var1 = Variable(var1_1) str1 = request.GET.get("numb2") str1 =str(":" if str1 is None else str1) terimal=[] list1=[] for i in str1: if(i.islower()): terimal.append(Terminal(i)) if(i.islower()): list1.append(Terminal(i)) else: list1.append(Variable(i)) #print(list1) #print(var1) var2_1 = request.GET.get("numb3") var2_1=str(":" if var2_1 is None else var2_1) var2=Variable(var2_1) str2= request.GET.get("numb4") str2 =str(":" if str2 is None else str2) list2=[] for i in str2: if(i.islower()): terimal.append(Terminal(i)) if(i.islower()): list2.append(Terminal(i)) else: list2.append(Variable(i)) #print(list2) var3_1 = request.GET.get("numb5") var3_1=str(":" if var3_1 is None else var3_1) var3= Variable(var3_1) str3= request.GET.get("numb6") str3 =str(":" if str3 is None else str3) list3=[] for i in str3: if(i.islower()): terimal.append(Terminal(i)) if(i.islower()): list3.append(Terminal(i)) else: list3.append(Variable(i)) #print(list3) var4_1 = request.GET.get("numb7") var4_1=str(":" if var4_1 is None else var4_1) var4 = Variable(var4_1) str4 = request.GET.get("numb8") str4 =str(":" if str4 is None else str4) list4=[] for i in str4: if(i.islower()): terimal.append(Terminal(i)) if(i.islower()): list4.append(Terminal(i)) else: list4.append(Variable(i)) #print(list4) var5_1 = request.GET.get("numb9") var5_1=str(":" if var5_1 is None else var5_1) var5 = Variable(var5_1) str5 = request.GET.get("numb10") str5 =str(":" if str5 is None else str5) list5=[] for i in str5: if(i.islower()): terimal.append(Terminal(i)) if(i.islower()): list5.append(Terminal(i)) else: list5.append(Variable(i)) #print(list5) terminal=set(terimal) #print(terminal) p0 = Production(var1, list1) p1 = Production(var2, list2) p2 = Production(var3, list3) p4 = Production(var4, list4) p5 = Production(var5, list5) #print(p0) #print(p1) #print(p2) #print(p4) #print(p5) vari = {var1,var2,var3,var4,var5} variable=set() for var in vari: if (var): variable.add(var) #print(variable) cfg = CFG(variable, terminal, "S", {p0, p1, p2, p4, p5}) ans = request.GET.get("numb11") ans =str(":" if ans is None else ans) list_ans=[] for j in ans: list_ans.append(j) val=cfg.contains(list_ans) if(val): answer_1="YES" else: answer_1="NO" return render(request,'grammar.html',{'result':answer_1})