def domination(A): global count first = union([A, DFA.Universal()]) assert identity(first, DFA.Universal()) first = intersection([A, DFA.Empty()]) assert identity(first, DFA.Empty()) count += 1
def uniqueness(A): """Uniqueness of complement: A u B = Universal and A n B = Empty => A = complement B and vice versa Involution: A = complement(complement(A)) """ global count B = difference(DFA.Universal(), A) # => A u B = Universal and A n B = Empty assert identity(union([A, B]), DFA.Universal()) assert identity(intersection([A, B]), DFA.Empty()) # Uniqueness of complement assert identity(A, complement(B)) assert identity(B, complement(A)) # Involution/Double Complement assert identity(A, complement(complement(A))) assert identity(B, complement(complement(B))) count += 1
def complement_laws(A): global count first = union([A.clone(), complement(A.clone())]) assert identity(first, DFA.Universal()) first = intersection([A.clone(), complement(A.clone())]) assert identity(first, DFA.Empty()) count += 1
def identity_vs_empty_and_universal(A): global count count += 1 # if count != 3: return first = union([A.clone(), DFA.Empty()]) assert identity(first, A.clone()) first = intersection([A.clone(), DFA.Universal()]) assert identity(first, A)
def __do(SM): """Creates a state machine that matches the reverse of what 'SM' matches. """ result = DFA(InitStateIndex=SM.init_state_index) original_acceptance_state_index_list = SM.get_acceptance_state_index_list() if len(original_acceptance_state_index_list) == 0: # If there is no acceptance state in a state machine, the state machine # cannot match any pattern, it is equivalent to '\Empty'. The reverse # of \Empty is \Empty. return DFA.Empty() # Ensure that each target state index has a state inside the state machine for state_index in SM.states.keys(): result.create_new_state(StateIdx=state_index) for state_index, state in SM.states.items(): for target_state_index, trigger_set in state.target_map.get_map( ).items(): result.states[target_state_index].add_transition( trigger_set.clone(), state_index) for target_state_index in state.target_map.get_epsilon_target_state_index_list( ): result.states[ target_state_index].target_map.add_epsilon_target_state( state_index) # -- copy all origins of the original state machine # -- We need to cancel any acceptance, because the inverted engine now starts # from a combination of the acceptance states and ends at the initial state. for state_index, state in SM.states.items(): result.states[state_index].single_entry.set( cmd.clone() for cmd in state.single_entry if cmd.__class__ != SeAccept) # deepcopy implicit # -- only the ORIGINAL initial state becomes an acceptance state (end of inverse) result.states[SM.init_state_index].set_acceptance(True) # -- setup an epsilon transition from an new init state to all previous # acceptance states. new_init_state_index = result.create_new_init_state() for state_index in original_acceptance_state_index_list: result.add_epsilon_transition(new_init_state_index, state_index) # -- for uniqueness of state ids, clone the result return result.clone()
def do(SM_List): for sm in SM_List: sm.assert_consistency() if any(sm.is_Empty() for sm in SM_List): # If one state machine is '\Empty', return DFA.Empty() # then the intersection is '\Empty'. init_state_setup = tuple(sm.init_state_index for sm in SM_List) result = DFA(AcceptanceF=intersect_acceptance(init_state_setup, SM_List)) # Result state setup: A result state is setup out of a state from each DFA. # state_setup[i] is the state from DFA 'SM_List[i]'. worklist = [ (result.init_state_index, init_state_setup) ] state_setup_db = {} N = len(SM_List) while worklist: state_index, state_setup = worklist.pop() # Generate Map that shows what lexatoms trigger to what state combination. # # NumberSet Target DFA_State Combination # [0:23] --> [ State1, State24, State56 ] # [0:23] --> [ State5, State21, State55 ] # [24:60] --> [ State1, State23, State51 ] # # 'get_intersection_line_up()' only delivers those transitions where there # is a transition for each state machine's state. line_up = get_intersection_line_up([SM_List[i].states[si].target_map for i, si in enumerate(state_setup)]) for target_state_setup, trigger_set in line_up.iteritems(): assert len(target_state_setup) == N target_index, new_f = state_index_for_combination(state_setup_db, target_state_setup) acceptance_f = intersect_acceptance(target_state_setup, SM_List) result.add_transition(state_index, trigger_set, target_index, AcceptanceF = acceptance_f) if new_f: worklist.append((target_index, target_state_setup)) result.delete_hopeless_states() return result
from quex.engine.state_machine.check.superset import do as superset from quex.engine.state_machine.core import DFA import quex.input.regular_expression.engine as regex import quex.engine.state_machine.TEST_help.many_shapes as shapes shapes.set_unique_transition_f() def dfa(Str): return regex.do(Str, {}, AllowNothingIsNecessaryF=True).extract_sm() __dfa_list = [ DFA.Universal(), # Matches all lexemes DFA.Empty(), # Matches the lexeme of zero length # dfa('a'), dfa('ab'), dfa('a(b?)'), dfa('ab|abcd'), # "Branches" dfa('12|AB'), dfa('x(12|AB)'), dfa('(12|AB)x'), dfa('x(12|AB)x'), dfa('x(1?2|A?B)x'), dfa('x(1?2?|A?B?)x'), # "Loops" dfa('A+'), dfa('A(B*)'),
elif "Loops" in sys.argv: test('A+') test('A(B*)') test('A((BC)*)') test('((A+)B+)C+') test('(ABC|BC|C)+') elif "BranchesLoops" in sys.argv: test('(AB|XY)+') test('(AB|XY)((DE|FG)*)') test('(((AB|XY)+)(DE|FG)+)(HI|JK)+') test('((AB|XY)(DE|FG)(HI|JK)|(DE|FG)(HI|JK)|(HI|JK))+') elif "Misc" in sys.argv: test('((((((((p+)r)+i)+)n)+t)+e)+r)+') test('(printer|rinter|inter|nter|ter|er|r)+') test('(p?r?i?n?t?e?r|rinter|inter|nter|ter|er|r)+') test( '(((((((((p+)r)+i)+)p)+r)+i)+n)+|(priprin|riprin|iprin|prin|rin|in|n)+)x?' ) elif "Special" in sys.argv: test(DFA.Empty()) test(DFA.Universal()) sm = DFA.Universal() sm.get_init_state().set_acceptance(True) sm = beautifier.do(sm) test(sm) else: test('a|ab')
def least_and_greatest(A): global count assert superset(A, DFA.Empty()) assert superset(DFA.Universal(), A) count += 1
def snap_empty(stream, PatternDict): return DFA.Empty()