def more_DFAs(A, B): """RETURNS: [0] B+ [1] B* [2] B*A """ B_plus = repeat.do(B) B_star = repeat.do(B, min_repetition_n=0) B_star_A = beautifier.do(sequentialize.do([B_star, A])) return beautifier.do(B_plus), \ beautifier.do(B_star), \ B_star_A
def __snap_repetition_range(the_state_machine, stream): """Snaps a string that represents a repetition range. The following syntaxes are supported: '?' one or none repetition '+' one or arbitrary repetition '*' arbitrary repetition (even zero) '{n}' exactly 'n' repetitions '{m,n}' from 'm' to 'n' repetitions '{n,}' arbitrary, but at least 'n' repetitions """ assert the_state_machine.__class__.__name__ == "StateMachine", \ "received object of type '%s'" % the_state_machine.__class__.__name__ + "\n" + \ repr(the_state_machine) position_0 = stream.tell() x = stream.read(1) if x == "+": result = repeat.do(the_state_machine, 1) elif x == "*": result = repeat.do(the_state_machine) elif x == "?": result = repeat.do(the_state_machine, 0, 1) elif x == "{": repetition_range_str = __snap_until(stream, "}") if len(repetition_range_str) and not repetition_range_str[0].isdigit(): # no repetition range, so everything remains as it is stream.seek(position_0) return the_state_machine try: if repetition_range_str.find(",") == -1: # no ',' thus "match exactly a certain number": # e.g. {4} = match exactly four repetitions number = int(repetition_range_str) result = repeat.do(the_state_machine, number, number) return result # a range of numbers is given fields = repetition_range_str.split(",") fields = map(lambda x: x.strip(), fields) number_1 = int(fields[0].strip()) if fields[1] == "": number_2 = -1 # e.g. {2,} else: number_2 = int(fields[1].strip()) # e.g. {2,5} # produce repeated state machine result = repeat.do(the_state_machine, number_1, number_2) return result except: raise RegularExpressionException("error while parsing repetition range expression '%s'" \ % repetition_range_str) else: # no repetition range, so everything remains as it is stream.seek(position_0) return the_state_machine return result
def __snap_repetition_range(the_state_machine, stream): """Snaps a string that represents a repetition range. The following syntaxes are supported: '?' one or none repetition '+' one or arbitrary repetition '*' arbitrary repetition (even zero) '{n}' exactly 'n' repetitions '{m,n}' from 'm' to 'n' repetitions '{n,}' arbitrary, but at least 'n' repetitions """ assert the_state_machine.__class__.__name__ == "DFA", \ "received object of type '%s'" % the_state_machine.__class__.__name__ + "\n" + \ repr(the_state_machine) position_0 = stream.tell() x = stream.read(1) if x == "+": result = repeat.do(the_state_machine, 1) elif x == "*": result = repeat.do(the_state_machine) elif x == "?": result = repeat.do(the_state_machine, 0, 1) elif x == "{": repetition_range_str = __snap_until(stream, "}") if len(repetition_range_str) and not repetition_range_str[0].isdigit(): # no repetition range, so everything remains as it is stream.seek(position_0) return the_state_machine try: if repetition_range_str.find(",") == -1: # no ',' thus "match exactly a certain number": # e.g. {4} = match exactly four repetitions number = int(repetition_range_str) result = repeat.do(the_state_machine, number, number) return result # a range of numbers is given fields = repetition_range_str.split(",") fields = map(lambda x: x.strip(), fields) number_1 = int(fields[0].strip()) if fields[1] == "": number_2 = -1 # e.g. {2,} else: number_2 = int(fields[1].strip()) # e.g. {2,5} # produce repeated state machine result = repeat.do(the_state_machine, number_1, number_2) return result except: raise RegularExpressionException("error while parsing repetition range expression '%s'" \ % repetition_range_str) else: # no repetition range, so everything remains as it is stream.seek(position_0) return the_state_machine return result
def do(SM_A, SM_B): """\NotIn{P Q} = \NotBegin{P \Any*(Q+)} """ all_star = repeat.do(special.get_any(), min_repetition_n=0) sm_b_repeated = repeat.do(SM_B, min_repetition_n=1) tmp = sequentialize.do([all_star, sm_b_repeated], MountToFirstStateMachineF=True, CloneRemainingStateMachinesF=True) tmp = beautifier.do(tmp) # There might be many paths which have no hope to reach acceptance tmp.clean_up() return complement_begin.do(SM_A, tmp)
def unary_checks(Q, operation): Q_plus = beautifier.do(repeat.do(Q)) Q_star = beautifier.do(repeat.do(Q, min_repetition_n=0)) Q_is_Q_star = identity.do(Q, Q_star) Q_is_Q_plus = identity.do(Q, Q_plus) # \Cut{Q Q} = \Nothing y = operation(Q, Q) assert y.is_Nothing() # if Q != Q+: \CutBegin{Q+ Q} = Q* if not Q_is_Q_plus: y = operation(Q_plus, Q) assert identity.do(y, Q_star) # if Q != Q*: \CutBegin{Q* Q} = Q* if not Q_is_Q_star: y = operation(Q_star, Q) assert identity.do(y, Q_star) # \Cut{Q \Nothing} = Q y = operation(Q, DFA.Nothing()) assert identity.do(y, Q) # \Cut{\Nothing Q} = \Nothing y = operation(DFA.Nothing(), Q) assert y.is_Nothing() # \Cut{Q \Universal} = \Nothing y = operation(Q, DFA.Universal()) assert y.is_Nothing() # NOT: \Cut{\Universal Q} = \Universal if not Q_is_Q_star and not Q_is_Q_plus: y = operation(Q, DFA.Universal()) assert y.is_Nothing() return Q_star, Q_plus
backup_sm = deepcopy(sm) optimal_sm = hopcroft.do(sm, CreateNewStateMachineF=CreateNewStateMachineF) print optimal_sm orphan_state_index_list = optimal_sm.get_orphaned_state_index_list() if len(orphan_state_index_list) != 0: print "ERROR: orphan states found = ", orphan_state_index_list if identity_checker.do(backup_sm, optimal_sm) == False: print "ERROR: state machines not equivalent" print "_______________________________________________________________________________" print "Example A:" sm = DFA() n0 = sm.init_state_index n1 = sm.add_transition(n0, ord('a'), AcceptanceF=True) sm = repeat.do(sm, 1) dfa = nfa_to_dfa.do(sm) test(dfa) print "_______________________________________________________________________________" print "Example B:" sm = DFA() n0 = sm.init_state_index n1 = sm.add_transition(n0, ord('a'), AcceptanceF=True) sm = repeat.do(sm) dfa = nfa_to_dfa.do(sm) test(dfa) print "_______________________________________________________________________________" print "Example C:" sm = DFA()
sys.path.insert(0, os.environ["QUEX_PATH"]) from quex.engine.state_machine.TEST.test_state_machines import * from quex.engine.state_machine.index import * import quex.engine.state_machine.construction.parallelize as parallelize import quex.engine.state_machine.construction.repeat as repeat import quex.engine.state_machine.algorithm.beautifier as beautifier if "--hwut-info" in sys.argv: print "Ranking of state machines: Filter dominated origins" sys.exit(0) #---------------------------------------------------------------------------------------- # (*) setup the state machines sm0 = repeat.do(sm0) sm0.mark_state_origins() sm1.mark_state_origins() sm2.mark_state_origins() sm3 = repeat.do(sm3, 1) sm3.mark_state_origins() # -- paralellize the patterns sm = parallelize.do([sm0, sm1, sm2, sm3]) # -- create the optimized DFA for the patterns running in paralell sm = beautifier.do(sm) #----------------------------------------------------------------------------------------
sys.path.insert(0, os.environ["QUEX_PATH"]) from quex.engine.state_machine.TEST.test_state_machines import * from quex.engine.state_machine.index import * import quex.engine.state_machine.construction.parallelize as parallelize import quex.engine.state_machine.construction.repeat as repeat import quex.engine.state_machine.algorithm.beautifier as beautifier if "--hwut-info" in sys.argv: print "Ranking of state machines: Filter dominated origins" sys.exit(0) #---------------------------------------------------------------------------------------- # (*) setup the state machines sm0 = repeat.do(sm0) sm0.mark_state_origins() sm1.mark_state_origins() sm2.mark_state_origins() sm3 = repeat.do(sm3, 1) sm3.mark_state_origins() # -- paralellize the patterns sm = parallelize.do([sm0, sm1, sm2, sm3]) # -- create the optimized DFA for the patterns running in paralell sm = beautifier.do(sm) #---------------------------------------------------------------------------------------- # (*) create some priorities in between the patterns
import os sys.path.insert(0, os.environ["QUEX_PATH"]) from quex.engine.state_machine.core import * import quex.engine.state_machine.construction.repeat as repeat from quex.engine.state_machine.TEST.test_state_machines import * if "--hwut-info" in sys.argv: print "StateMachine Operations: Repetition with min and max repetition numbers" sys.exit(0) print "-------------------------------------------------------------------------------" print "##sm0", sm3 print "## repeat.do(sm3) " sm3r = repeat.do(sm3) print "##result = ", sm3r print "-------------------------------------------------------------------------------" print "##sm0", sm3 print "## repeat.do(sm3, 2) " sm3r = repeat.do(sm3, 2) print "##result = ", sm3r print "-------------------------------------------------------------------------------" print "##sm0", sm3 print "## repeat.do(sm3, 0, 2) " sm3r = repeat.do(sm3, 0, 2) print "##result = ", sm3r print "-------------------------------------------------------------------------------"
#! /usr/bin/env python import sys import os sys.path.insert(0, os.environ["QUEX_PATH"]) import quex.engine.state_machine.construction.repeat as repeat from quex.engine.state_machine.TEST_help.some_dfas import * if "--hwut-info" in sys.argv: print "NFA: Epsilon closure (single state)" sys.exit(0) test_sm0 = repeat.do(sm0, 1) print "## state machine = ", test_sm0.get_string(NormalizeF=False) print "## compute epsilon closures of all states:" normal_epsilon_closures = [] for state_idx in test_sm0.states.keys(): ec = test_sm0.get_epsilon_closure(state_idx) ec = list(sorted(ec)) if len(ec) != 1: print "state = ", state_idx, "epsilon-closure = ", ec else: if ec[0] == state_idx: normal_epsilon_closures.append(state_idx) else: print "error: state idx", state_idx, " produces epsilon transition = ", ec normal_epsilon_closures.sort() print "## normal epsilon closures = ", normal_epsilon_closures
#! /usr/bin/env python import sys import os sys.path.insert(0, os.environ["QUEX_PATH"]) import quex.engine.state_machine.construction.repeat as repeat from quex.engine.state_machine.TEST.test_state_machines import * if "--hwut-info" in sys.argv: print "NFA: Epsilon closure (single state)" sys.exit(0) test_sm0 = repeat.do(sm0, 1) print "## state machine = ", test_sm0.get_string(NormalizeF=False) print "## compute epsilon closures of all states:" normal_epsilon_closures = [] for state_idx in test_sm0.states.keys(): ec = test_sm0.get_epsilon_closure(state_idx) ec = list(sorted(ec)) if len(ec) != 1: print "state = ", state_idx, "epsilon-closure = ", ec else: if ec[0] == state_idx: normal_epsilon_closures.append(state_idx) else: print "error: state idx", state_idx, " produces epsilon transition = ", ec normal_epsilon_closures.sort() print "## normal epsilon closures = ", normal_epsilon_closures
from quex.engine.state_machine.TEST.test_state_machines import sm3 from quex.engine.state_machine.core import * import quex.engine.state_machine.construction.repeat as repeat import quex.engine.state_machine.algorithm.nfa_to_dfa as nfa_to_dfa if "--hwut-info" in sys.argv: print "NFA: Conversion to DFA (subset construction)" sys.exit(0) print "_______________________________________________________________________________" print "Example A:" sm = StateMachine() n0 = sm.init_state_index n1 = sm.add_transition(n0, ord('a'), AcceptanceF=True) sm = repeat.do(sm, 1) dfa = nfa_to_dfa.do(sm) print dfa print "_______________________________________________________________________________" print "Example B:" sm = StateMachine() n0 = sm.init_state_index n1 = sm.add_transition(n0, ord('a'), AcceptanceF=True) sm = repeat.do(sm) dfa = nfa_to_dfa.do(sm) print dfa print "_______________________________________________________________________________" print "Example C:" # (*) create a simple state machine: