Exemple #1
0
 def test_empty_dpda(self):
     """Should accept an empty input if the DPDA is empty."""
     dpda = DPDA(states={'q0'},
                 input_symbols=set(),
                 stack_symbols={'0'},
                 transitions=dict(),
                 initial_state='q0',
                 initial_stack_symbol='0',
                 final_states={'q0'},
                 acceptance_mode='both')
     self.assertTrue(dpda.accepts_input(''))
Exemple #2
0
 def test_init_dpda_missing_formal_params(self):
     """Should raise an error if formal DPDA parameters are missing."""
     with self.assertRaises(TypeError):
         DPDA(states={'q0', 'q1', 'q2'},
              input_symbols={'a', 'b'},
              initial_state='q0',
              final_states={'q0'})
Exemple #3
0
 def setup(self):
     """Reset test automata before every test function."""
     # DPDA which which matches zero or more 'a's, followed by the same
     # number of 'b's (accepting by final state)
     self.dpda = DPDA(
         states={'q0', 'q1', 'q2', 'q3'},
         input_symbols={'a', 'b'},
         stack_symbols={'0', '1'},
         transitions={
             'q0': {
                 'a': {'0': ('q1', ('1', '0'))}
             },
             'q1': {
                 'a': {'1': ('q1', ('1', '1'))},
                 'b': {'1': ('q2', '')}
             },
             'q2': {
                 'b': {'1': ('q2', '')},
                 '': {'0': ('q3', ('0',))}
             }
         },
         initial_state='q0',
         initial_stack_symbol='0',
         final_states={'q3'}
     )
Exemple #4
0
 def test_init_dpda_no_acceptance_mode(self):
     """Should create a new DPDA."""
     new_dpda = DPDA(states={'q0'},
                     input_symbols={'a', 'b'},
                     stack_symbols={'#'},
                     transitions={'q0': {
                         'a': {
                             '#': ('q0', '')
                         },
                     }},
                     initial_state='q0',
                     initial_stack_symbol='#',
                     final_states={'q0'})
     self.assertEqual(new_dpda.acceptance_mode, 'both')
Exemple #5
0
def creatDPDA(states, alphabet, alphabetStack, transition, initialState, symbolsStackInitial, finalStates):

    for i in range(len(alphabet)):
        if alphabet[i] == '':
            del alphabet[i]

    return DPDA(
        states=set(states),
        input_symbols=set(alphabet),
        stack_symbols=set(alphabetStack),
        transitions=transition,
        initial_state=initialState,
        initial_stack_symbol=symbolsStackInitial,
        final_states=set(finalStates)
        )
Exemple #6
0
    def setup(self):
        """Reset test automata before every test function."""
        # DPDA which which matches zero or more 'a's, followed by the same
        # number of 'b's (accepting by final state)
        self.dpda = DPDA(states={'q0', 'q1', 'q2', 'q3'},
                         input_symbols={'a', 'b'},
                         stack_symbols={'0', '1'},
                         transitions={
                             'q0': {
                                 'a': {
                                     '0': ('q1', ('1', '0'))
                                 }
                             },
                             'q1': {
                                 'a': {
                                     '1': ('q1', ('1', '1'))
                                 },
                                 'b': {
                                     '1': ('q2', '')
                                 }
                             },
                             'q2': {
                                 'b': {
                                     '1': ('q2', '')
                                 },
                                 '': {
                                     '0': ('q3', ('0', ))
                                 }
                             }
                         },
                         initial_state='q0',
                         initial_stack_symbol='0',
                         final_states={'q3'})

        # NPDA which matches palindromes consisting of 'a's and 'b's
        # (accepting by final state)
        # q0 reads the first half of the word, q1 the other half, q2 accepts.
        # But we have to guess when to switch.
        self.npda = NPDA(states={'q0', 'q1', 'q2'},
                         input_symbols={'a', 'b'},
                         stack_symbols={'A', 'B', '#'},
                         transitions={
                             'q0': {
                                 '': {
                                     '#': {('q2', '#')},
                                 },
                                 'a': {
                                     '#': {('q0', ('A', '#'))},
                                     'A': {
                                         ('q0', ('A', 'A')),
                                         ('q1', ''),
                                     },
                                     'B': {('q0', ('A', 'B'))},
                                 },
                                 'b': {
                                     '#': {('q0', ('B', '#'))},
                                     'A': {('q0', ('B', 'A'))},
                                     'B': {
                                         ('q0', ('B', 'B')),
                                         ('q1', ''),
                                     },
                                 },
                             },
                             'q1': {
                                 '': {
                                     '#': {('q2', '#')}
                                 },
                                 'a': {
                                     'A': {('q1', '')}
                                 },
                                 'b': {
                                     'B': {('q1', '')}
                                 },
                             },
                         },
                         initial_state='q0',
                         initial_stack_symbol='#',
                         final_states={'q2'})
Exemple #7
0
 def test_init_dpda(self):
     """Should copy DPDA if passed into DPDA constructor."""
     new_dpda = DPDA.copy(self.dpda)
     self.assert_is_copy(new_dpda, self.dpda)
Exemple #8
0
dpda = DPDA(
    states={'q0', 'q1', 'q2', 'q3', 'q4', 'q5'},
    input_symbols={'g', 'c', 'a', 'u'},
    stack_symbols={'S', '1', '2', '3', 'g', 'c', 'a', 'u'},
    transitions={
        'q0': {
            'g': {
                'S': ('q1', ('1', 'c'))
            },  # gが観測されたらSをpopし,g1cをpush,さらにgをpopして次の状態へ
            'c': {
                'S': ('q1', ('1', 'g'))
            },
            'a': {
                'S': ('q1', ('1', 'u'))
            },
            'u': {
                'S': ('q1', ('1', 'a'))
            }
        },
        'q1': {
            'g': {
                '1': ('q2', ('2', 'c'))
            },
            'c': {
                '1': ('q2', ('2', 'g'))
            },
            'a': {
                '1': ('q2', ('2', 'u'))
            },
            'u': {
                '1': ('q2', ('2', 'a'))
            }
        },
        'q2': {
            'g': {
                '2': ('q3', ('3', 'c'))
            },
            'c': {
                '2': ('q3', ('3', 'g'))
            },
            'a': {
                '2': ('q3', ('3', 'u'))
            },
            'u': {
                '2': ('q3', ('3', 'a'))
            }
        },
        'q3': {
            'g': {
                '3': ('q4', ('a', 'a', 'a'))
            }  #gが観測されたらgaaaをpush,さらにgをpopし次の状態へ
        },
        'q4': {
            'g': {
                'g': ('q4', (''))
            },  #gをpop
            'c': {
                'c': ('q4', (''))
            },
            'a': {
                'a': ('q4', (''))
            },
            'u': {
                'u': ('q4', (''))
            },
            '': {
                'S': ('q5', ('S', ))
            }  #スタックと入力が共に空になれば正常終了
        }
    },
    initial_state='q0',
    initial_stack_symbol='S',
    final_states={'q5'})
Exemple #9
0
from automata.fa.dfa import DFA
from automata.fa.nfa import NFA
from automata.pda.dpda import DPDA
# DPDA which which matches zero or more 'a's, followed by the same
# number of 'b's (accepting by final state)
dpda = DPDA(
    states={'q0', 'q1'},
    input_symbols={'x', 'y'},
    stack_symbols={'0', '1'},
    transitions={
        'q0': {
            'x': {'0': ('q0', ('1', '0'))},  # transition pushes '1' to stack
            'x': {'1': ('q0', ('1', '1'))},  # transition pushes '1' to stack
             '': {'1': ('q1', ('1',))},
             '': {'0': ('q1', ('0',))}
            
        },
        
        'q1': {
            'y': {'1': ('q1', '')}
        }
    },
    initial_state='q0',
    initial_stack_symbol='0',
    final_states={'q1'}
)
#test = ["01001","10101","10100","10001","1000111110111","1110110","101010101","101","1010101111100","1111000011101010","1100001010101101","1011100001010","11111000101","11110001010","1111000000101","00111101","1101","0","","10010","1110","111100","0","1","00","11","111","11100","0011010","1100000000000000111111111","11001000110010110","111","000","1111100000","111110011"]

test= ["xyxyx","xyxyxyyx","xyx","xyxyxyxy","xyxyxyxyx","xyyyxx","xxxxyyy","xxxyyy","xy","","xxyy","xxxxxyyyyy"]
for i in range(0,len(test)):
    #print(i)