Beispiel #1
0
def test5():
    rules = {'S': ['AB', 'aS'], 'A': ['aAA', 'ε'], 'B': ['bBB', 'ε']}
    c = CFG(rules)
    c.start = 'S'
    c.elim_null()
    c.print_cfg()
    """
Beispiel #2
0
def test8():
    rules = {'S': ['aB', 'a'], 'B': ['Ba', 'b']}
    c = CFG(rules)
    c.start = 'S'
    c.convert_to_cnf()
    c.print_cfg()
    """
Beispiel #3
0
def test1():
    rules = {'S': ['AC', 'B'], 'A': ['a'], 'C': ['c', 'BC'], 'E': ['aA', 'e']}
    c = CFG(rules)
    c.start = 'S'
    c.reduce()
    c.print_cfg()
    """
Beispiel #4
0
def test9():
    rules = {'S': ['CA', 'BB'], 'B': ['b', 'SB'], 'C': ['b'], 'A': ['a']}
    c = CFG(rules)
    c.start = 'S'
    c.convert_to_gnf()
    c.print_cfg()
    """
Beispiel #5
0
def test7():
    rules = {'S': ['aXbX'], 'X': ['aY', 'bY', 'ε'], 'Y': ['X', 'c']}
    c = CFG(rules)
    c.start = 'S'
    c.convert_to_cnf()
    c.print_cfg()
    """
Beispiel #6
0
def test6():
    rules = {'S': ['ABCd'], 'A': ['BC'], 'B': ['bB', 'ε'], 'C': ['cC', 'ε']}
    c = CFG(rules)
    c.start = 'S'
    c.elim_null()
    c.print_cfg()
    """
Beispiel #7
0
def test3():
    rules = {
        'S': ['Aa', 'B', 'c'],
        'B': ['A', 'bb', 'aD'],
        'A': ['a', 'bc', 'B'],
        'D': ['d']
    }
    c = CFG(rules)
    c.start = 'S'
    c.elim_unit()
    c.print_cfg()
    """
Beispiel #8
0
def test2():
    rules = {
        'T': ['aaB', 'abA', 'aaT'],
        'A': ['aA'],
        'B': ['ab', 'b'],
        'C': ['ad']
    }
    c = CFG(rules)
    c.start = 'T'
    c.reduce()
    c.print_cfg()
    """
Beispiel #9
0
def test4():
    rules = {
        'S': ['XY'],
        'X': ['a'],
        'Y': ['Z', 'b'],
        'Z': ['M'],
        'M': ['N'],
        'N': ['a']
    }
    c = CFG(rules)
    c.start = 'S'
    c.elim_unit()
    c.print_cfg()
    """
Beispiel #10
0
    def to_cfg(self):
        from automata.cfg import CFG
        self.my_cfg = CFG({})

        self.count = 0
        self.state_names = {}
        new_rules = {'S': []}
        self.flatten_transitions()

        #1. all start state combos
        for st in self.states:
            n = self.namify(self.start_state, self.init_stack_symbol, st)
            self.state_names[n] = 'A' + str(self.count)
            new_rules['S'].append(['A' + str(self.count)])
            self.count += 1
        #2. look at all transitions
        for s1, inp, sym1, s2, sym2 in self.transitions:
            if sym2 == 'ε':
                n = self.namify(s1, sym1, s2)
                if n not in self.state_names:
                    self.state_names[n] = 'A' + str(self.count)
                    self.count += 1
                nt = self.state_names[n]
                if nt not in new_rules:
                    new_rules[nt] = []
                new_rules[nt].append([inp])
            else:
                for s3 in self.states:
                    n = self.namify(s1, sym1, s3)
                    if n not in self.state_names:
                        self.state_names[n] = 'A' + str(self.count)
                        self.count += 1
                    nt = self.state_names[n]
                    if nt not in new_rules:
                        new_rules[nt] = []
                    #get all combos for one [pXq]
                    lst = self.enumerate_nts(s2, sym2, s3)
                    if inp != 'ε':
                        lst = [[inp] + seq for seq in lst]
                    new_rules[nt] += lst

        self.my_cfg.rules = new_rules
        self.my_cfg.start = 'S'
        self.my_cfg.reduce()
Beispiel #11
0
def test1():
    rules = {'S': ['aSb', 'A'], 'A': ['ε']}
    c = CFG(rules)
    c.start = 'S'
    c.convert_to_pda()
    c.my_pda.print_pda()
Beispiel #12
0
def test2():
    rules = {'S': ['aSa', 'bSb', 'ε', 'b', 'a']}
    c = CFG(rules)
    c.start = 'S'
    c.convert_to_pda()
    c.my_pda.print_pda()