def get_longest_query_grammar(self, relations, uids): var_s = Variable("S") b_relation_variables = dict() l_relation_variables = dict() for relation in relations: b_relation_variables[relation] = Variable("B" + relation) l_relation_variables[relation] = Variable("L" + relation) productions = set() # For now only single query longest_query = self.get_longest_query() q = Terminal(longest_query[0]) q_minus = Terminal(get_inverse_relation(longest_query[0])) # S -> q productions.add(Production(var_s, [q])) # S -> Bq . q productions.add(Production(var_s, [b_relation_variables[q.value], q])) # S -> q Bq- q- productions.add( Production(var_s, [q, b_relation_variables[q_minus.value], q_minus])) # S -> Bq q Bq- q- productions.add( Production(var_s, [ b_relation_variables[q.value], q, b_relation_variables[q_minus.value], q_minus ])) # Br1 -> Br1 Lr2 for uid in uids: productions.add( Production(b_relation_variables[uid.get_body()], [ b_relation_variables[uid.get_body()], l_relation_variables[uid.get_head()] ])) # Br1 -> Lr2 for uid in uids: productions.add( Production(b_relation_variables[uid.get_body()], [l_relation_variables[uid.get_head()]])) # Lr -> r Br- r- for relation in relations: productions.add( Production(l_relation_variables[relation], [ Terminal(relation), b_relation_variables[get_inverse_relation(relation)], Terminal(get_inverse_relation(relation)) ])) # Lr -> r r- for relation in relations: productions.add( Production(l_relation_variables[relation], [ Terminal(relation), Terminal(get_inverse_relation(relation)) ])) return CFG(start_symbol=var_s, productions=productions)
rules, data = f.read().split("\n\n") rules = rules.split("\n") data = [item.strip() for item in data.split("\n")] variables = set() productions = set() 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:
with open('day19.txt') as fin: contents = [line.strip() for line in fin.readlines()] terminals = set() variables = set() productions = set() strings = [] cnt = 0 for line in contents: if not line: continue elif ':' in line: var, production = line.split(': ') variables.add(Variable(var)) 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]
def test_get_parsing_tree(self): self.assertTrue( self.parser.is_parsable( ["(", "int", "+", "(", "int", "*", "int", ")", ")"])) parse_tree = self.parser.get_parse_tree( ["(", "int", "+", "(", "int", "*", "int", ")", ")"]) derivation = parse_tree.get_leftmost_derivation() self.assertEqual(derivation, [ [Variable("S")], [Terminal("("), Variable("E"), Terminal(")")], [ Terminal("("), Variable("S"), Terminal("+"), Variable("S"), Terminal(")") ], [ Terminal("("), Terminal("int"), Terminal("+"), Variable("S"), Terminal(")") ], [ Terminal("("), Terminal("int"), Terminal("+"), Terminal("("), Variable("E"), Terminal(")"), Terminal(")") ], [ Terminal("("), Terminal("int"), Terminal("+"), Terminal("("), Variable("S"), Terminal("*"), Variable("S"), Terminal(")"), Terminal(")") ], [ Terminal("("), Terminal("int"), Terminal("+"), Terminal("("), Terminal("int"), Terminal("*"), Variable("S"), Terminal(")"), Terminal(")") ], [ Terminal("("), Terminal("int"), Terminal("+"), Terminal("("), Terminal("int"), Terminal("*"), Terminal("int"), Terminal(")"), Terminal(")") ], ])
def test_all(self): with open(self.GRAMMAR_PATH, 'r') as f: gram = custom_CFG.read_cfg( f.read(), start_symbol=Variable("SCRIPT"), contains_regexes=True, track_variables=True, ) gram = gram.to_normal_form() script = """ connect "ABC" """ assert (run_script(script, gram=gram)) script = """ select count edges from "g" intersect grammar """ assert (run_script(script, gram=gram)) script = """ select count edges from setStartAndFinal {3, 2, 6} 1:6 "g" """ assert (run_script(script, gram=gram)) script = """ select count edges from [term(a)?.term(b)*.(term(c)|term(d))+] """ assert (run_script(script, gram=gram)) script = """ select count edges from "g" intersect [term(a).term(b)*.(term(c)|term(d))+] """ assert (run_script(script, gram=gram)) script = """ select count edges from setStartAndFinal _ _ "g" """ assert (run_script(script, gram=gram)) script = """ select count edges from setStartAndFinal _ _ ("g" intersect grammar) """ assert (run_script(script, gram=gram)) script = """ select filter (v, e, u) -> (e hasLbl abc) : edges from "g" """ assert (run_script(script, gram=gram)) script = """ select filter (v, e, u) -> (e hasLbl abc & isStart v | !(isFinal u)) : edges from "g" """ assert (run_script(script, gram=gram)) script = """ select count edges from "g1" intersect ("g2" intersect "g3") """ assert (run_script(script, gram=gram)) script = """ s : nonterm(S).term(a).nonterm(S).term(b) s : term(eps) """ assert (run_script(script, gram=gram)) script = """ select edges from [term(a)?.term(b)*.(term(c)|term(d))+] """ assert (run_script(script, gram=gram))
# Variables vrs = set() # Productions prods = set() # Regexes ints_re = re.compile(r'([0-9]+)') rule_re = re.compile(r'^[0-9]') messages = [] for line in open(sys.argv[1]).read().rstrip().splitlines(): # rules 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
from pathlib import Path # filename = "input_19_test.txt" filename = "input_19.txt" tests = [] vars = {} ters = {} productions = [] def create_production(id, l): return Production(vars[id], [vars[x] for x in l]) for i in range(132): vars[i] = Variable(i) with Path(filename).open() as file: while line := file.readline().strip(): data = parse('{id:d}: {rules}', line) if data['rules'] == '"a"': ters["a"] = Terminal("a") productions.append(Production(vars[data['id']], [ters["a"]])) 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:
def to_cfg(self) -> CFG: """Create a context-free grammar [1]_ from Recursive State Machine [2]_. Examples -------- >>> import cfpq_data >>> rsm = cfpq_data.rsm_from_text("S -> (a S* b S*)*") >>> cfg = rsm.to_cfg() >>> [cfg.contains(word) for word in ["", "ab", "aabb"]] [True, True, True] Returns ------- cfg : CFG Context-free grammar. References ---------- .. [1] https://en.wikipedia.org/wiki/Context-free_grammar#Formal_definitions .. [2] Alur R., Etessami K., Yannakakis M. (2001) Analysis of Recursive State Machines. In: Berry G., Comon H., Finkel A. (eds) Computer Aided Verification. CAV 2001. Lecture Notes in Computer Science, vol 2102. Springer, Berlin, Heidelberg. https://doi.org/10.1007/3-540-44585-4_18 """ variables = set() terminals = set() productions = set() for symbol, dfa in self.boxes: variables.add(Variable(symbol)) naming = {dfa.start_state: Variable(symbol)} for state in dfa.states: if state not in naming: naming[state] = Variable(f"S{len(naming)}") variables.add(naming[state]) if state in dfa.final_states: productions.add(Production(naming[state], [])) for v, label, to in dfa._transition_function.get_edges(): if label.value == label.value.lower(): try: label_value = re.search('"TER:(.*)"', label.value).group(1) except AttributeError: label_value = label.value terminals.add(Terminal(label_value)) production_label = Terminal(label_value) else: try: label_value = re.search('"VAR:(.*)"', label.value).group(1) except AttributeError: label_value = label.value production_label = Variable(label_value) productions.add( Production(naming[v], [production_label, naming[to]])) return CFG( variables=variables, terminals=terminals, start_symbol=self.start_symbol, productions=productions, )
def test_change_starting_variable(self): text = """S1 -> a""" cfg = CFG.from_text(text, start_symbol="S1") self.assertEqual(Variable("S1"), cfg.start_symbol)
def test_generate_epsilon(self): var_s = Variable("S") ter_a = Terminal("a") productions = [Production(var_s, [ter_a])] cfg = CFG(productions=productions, start_symbol=var_s) self.assertFalse(cfg.generate_epsilon())
def test_string_variable(self): var = Variable("A") self.assertEqual(var.__repr__(), "Variable(A)")
var_re = re.compile(r'(\d+)') 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})