v=set() rules=set() alpha=set() s='' line=input('Enter your Varibles:\n') v=line.split(',') line=input('Enter your alphebet:\n') alpha=line.split(',') s=input('Enter your start var:\n') print('Enter your rules') while True: line=input('') if line.upper()=='END': break else: tmpr=r.Rule(line) rules.add(tmpr) c1=c.CFG(v,alpha,rules,s) b=c1.accept('aaaaaaaaaacccccccc') for i in range(10): print('\n') if b==True: print('yes') else: print('no')
def test_cfg(): P = CFG.P({ "S": "aA | bB", "A": "aAB | aa | AC | AE", "B": "bBA | bb | CB | BF", "C": "DE", "D": "cc | DD", "E": "FF | FE", "F": "EcE" }) A = CFG(Set("S", "A", "B", "C", "D", "E", "F"), Set("a", "b", "c"), P, "S") B = A.reduce() ###################################################################### P = CFG.P({ "S": "ABC", "A": "Ab | BC", "B": "bB | b | Ab | ε", "C": "cD | c | Ac | ε", "D": "SSD | cSAc" }) A = CFG(Set("S", "A", "B", "C", "D"), Set("b", "c"), P, "S") B = A.remove_ε() ###################################################################### P = CFG.P({ "S": "X | Y", "A": "bS | D", "D": "bA", "B": "Sa | a", "X": "aAS | C", "C": "aD | S", "Y": "SBb" }) A = CFG(Set("S", "A", "B", "C", "D", "X", "Y"), Set("a", "b"), P, "S") B = A.remove_primitive_rules() ###################################################################### P = CFG.P({ "S": "SaSbS | aAa | bBb", "A": "aA | aaa | B | ε", "B": "Bb | bb | b" }) A = CFG(Set("S", "A", "B"), Set("a", "b"), P, "S") B = A.toOwn() C = B.toCNF() ###################################################################### P = CFG.P({ "S": "YZ | aXZa", "X": "YX | bY | aYZ", "Y": "ε | c | YZ", "Z": "a | Xb | ε | c" }) G1 = CFG(Set("S", "X", "Y", "Z"), Set("a", "b", "c"), P, "S") G1_1 = G1.remove_ε() G1_2 = G1_1.remove_primitive_rules() # print("---[ G1 ]---") # print(G1, end="\n" * 3) # print(G1.Nε, end="\n" * 3) # print(G1_1, end="\n" * 3) # print(G1_1.NS, G1_1.NX, G1_1.NY, G1_1.NZ, end="\n" * 3) # print(G1_2, end="\n" * 3) P = CFG.P({ "S": "Aa | a | Eb | abbc | aDD", "A": "Aab | b | SEE | baD", "B": "DaS | BaaC | a", "C": "Da | a | bB | Db | SaD", "D": "Da | DBc | bDb | DEaD", "E": "Aa | a | bca" }) G2 = CFG(Set("S", "A", "B", "C", "D", "E"), Set("a", "b", "c"), P, "S") G2_1 = G2.toOwn() G2_2 = G2.toCNF() # print("---[ G2 ]---") # print(G2, end="\n" * 3) # print("Nε=", G2.Nε, " NA={", G2.NS, G2.NA, # G2.NB, G2.NC, G2.ND, G2.NE, "} V=", G2.V) # print(G2_1, end="\n" * 3) # print(G2_2, end="\n" * 3) P = CFG.P({"S": "Xc | Yd | Yb", "X": "Xb | a", "Y": "SaS | Xa"}) G = CFG(Set("S", "X", "Y"), Set("a", "b", "c", "d"), P, "S") G1 = G.remove_left_recursion() G2 = G1.toGNF() P = CFG.P({"S": "ε | abSA", "A": "AaB | aB | a", "B": "aSS | bA | aB"}) G = CFG(Set("S", "A", "B"), Set("a", "b"), P, "S")
def genNonRecGrammar(g, n): """ Return an non recursive grammar with n unrolls """ # step 1: generate rules from 0..n-1 unrools global rs rs = reachableSymbols(g) print "Reachablity relation:" print rs nextSymbolIndex = {} # dictionary non-term -> last-index lastGenSymbolIndex = {} # last index of generated rule # put recursive symbols in symbolIndex dictionary for sym in rs.keys(): if sym in rs[sym]: nextSymbolIndex[sym] = 0 lastGenSymbolIndex[sym] = 0 # invariant: symbolIndex[sym] == next index to generate print "\nRecursive rules:" print nextSymbolIndex print "\nGenerating non recursive grammar:" rules = [] generated = set() while not unrools(nextSymbolIndex, n): for r in g.rules: if r.name in rs[r.name]: # the rule is recursive i = nextSymbolIndex[r.name] newrule = CFG.Rule(r.name + str(i), []) nextSymbolIndex[r.name] = i + 1 for seq in r.seqs: newseq = [] for sym in seq: if isinstance(sym, CFG.Non_Term_Ref ) and sym.name in nextSymbolIndex.keys(): # sym is recursive: generate indexed symbol j = nextSymbolIndex[sym.name] lastGenSymbolIndex[sym.name] = j newseq.append(CFG.Non_Term_Ref(sym.name + str(j))) else: # sym is not recursive: generate original symbol newseq.append(sym) newrule.seqs.append(newseq) else: if r.name in generated: continue newrule = CFG.Rule(r.name, []) for seq in r.seqs: newseq = [] for sym in seq: if isinstance( sym, CFG.Non_Term_Ref) and sym.name in rs.keys(): name = sym.name + "0" newseq.append(CFG.Non_Term_Ref(name)) else: newseq.append(sym) newrule.seqs.append(newseq) generated.add(newrule.name) rules.append(newrule) # step 2: generate indexed rules with only terminal sequences as rhs # step 2.1: Generate only s : rhs width rhs containing only non-rec symbols for name in lastGenSymbolIndex.keys(): if lastGenSymbolIndex[name] == nextSymbolIndex[name]: r = g.get_rule(name) newrule = CFG.Rule(name + str(lastGenSymbolIndex[name]), []) nextSymbolIndex[name] = nextSymbolIndex[name] + 1 for seq in r.seqs: if nonrec(seq): newrule.seqs.append(seq) rules.append(newrule) # step 2.2: Generate not yet generated rules for name in lastGenSymbolIndex.keys(): if lastGenSymbolIndex[name] == nextSymbolIndex[name]: r = g.get_rule(name) newrule = CFG.Rule(name + str(lastGenSymbolIndex[name]), []) for seq in r.seqs: newseq = [] for sym in seq: if isinstance(sym, CFG.Non_Term_Ref) and sym.name in rs.keys(): name = sym.name + lastGenSymbolIndex[name] newseq.append(CFG.Non_Term_Ref(name)) else: newseq.append(sym) rules.append(newrule) generated.add(newrule.name) return CFG.CFG(g.tokens, rules)
return der_list + labels else: return labels def derivable(self, max_steps): return self.derivable_from([self.start], max_steps) # Example CFGs cfg1 = CFG(start=NT("S"), rules=[ Rule(NT("S"), [NT("NP"), NT("VP")]), Rule(NT("NP"), [NT("D"), NT("N")]), Rule(NT("VP"), [NT("V"), NT("NP")]), Rule(NT("NP"), [T("John")]), Rule(NT("NP"), [T("Mary")]), Rule(NT("D"), [T("the")]), Rule(NT("D"), [T("a")]), Rule(NT("N"), [T("cat")]), Rule(NT("N"), [T("dog")]), Rule(NT("V"), [T("saw")]), Rule(NT("V"), [T("likes")]) ]) cfg_anbn = CFG(start=NT(0), rules=[ Rule(NT(0), [NT(10), NT(1)]), Rule(NT(1), [NT(0), NT(11)]), Rule(NT(0), [NT(10), NT(11)]), Rule(NT(10), [T('a')]), Rule(NT(11), [T('b')]) ])