本文整理汇总了Python中quex.engine.state_machine.core.StateMachine.add_epsilon_transition方法的典型用法代码示例。如果您正苦于以下问题:Python StateMachine.add_epsilon_transition方法的具体用法?Python StateMachine.add_epsilon_transition怎么用?Python StateMachine.add_epsilon_transition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类quex.engine.state_machine.core.StateMachine
的用法示例。
在下文中一共展示了StateMachine.add_epsilon_transition方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: do
# 需要导入模块: from quex.engine.state_machine.core import StateMachine [as 别名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_epsilon_transition [as 别名]
def do(SM):
"""Creates a state machine that matches the reverse of what 'SM' matches.
"""
result = StateMachine(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 '\None'. The reverse
# of \None is \None.
return special.get_none()
# 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():
original_origin_list = [origin.clone() for origin in state.origins()]
for origin in original_origin_list:
origin.set_input_position_restore_f(False)
origin.set_pre_context_id(E_PreContextIDs.NONE)
origin.set_acceptance_f(False)
result.states[state_index].origins().set(original_origin_list) # 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()
示例2: do
# 需要导入模块: from quex.engine.state_machine.core import StateMachine [as 别名]
# 或者: from quex.engine.state_machine.core.StateMachine import add_epsilon_transition [as 别名]
def do(SM_List):
"""Intersection:
Only match on patterns which are matched by all state machines
in 'SM_List'.
(C) 2013 Frank-Rene Schaefer
________________________________________________________________________
A lexeme which matches all patterns must reach an acceptance in each
given state machine. That is,
For each state machine; there is a path from the init
state to an acceptance state triggered along the by
the characters of the lexeme.
We cannot go forward, since we cannot omit a path upon non-fit.
Now, consider the super-state consisting of all acceptance states
of all state machines. There there must be a way backward from the
super-acceptance-state to the init state states. As soon, as a
path is interupted, it can be thrown away. This can be achieved
by reversed state machines which are combined into a single one.
Reverse all state machines; The epsilon closure of the init state
corresponds to the super acceptance state. The transitions in the
super-state machine correspond to the way backwards in the state
machine. For each feasible state in the super-state machine create
a new state.
The acceptance state of the reversed state machines correspond to
the init state of the original state machines. If the super state
contains an acceptance state of the original state, it can become
an acceptance state of the intersection, because we now found a
path. The found state machine must be reversed at the end.
"""
for sm in SM_List:
if special.is_none(sm): # If one state machine is '\None'
return special.get_none() # then, the intersection is '\None'
reverse_sm_list = [ reverse.do(sm) for sm in SM_List ]
state_id_set_list = [ set(sm.states.iterkeys()) for sm in reverse_sm_list ]
acceptance_state_id_list = [ set(sm.get_acceptance_state_index_list()) for sm in reverse_sm_list ]
def has_one_from_each(StateIDSet_List, StateIDSet):
"""StateIDSet_List[i] is the set of state indices from state
machine 'i' in 'reverse_sm_list'.
RETURNS: True -- If the StateIDSet has at least one state
from every state machine.
False -- If there is at least one state machine
that has no state in 'StateIDSet'.
"""
for state_id_set in StateIDSet_List:
if state_id_set.isdisjoint(StateIDSet):
return False
return True
def get_merged_state(AcceptanceStateIndexList, EpsilonClosure):
"""Create the new target state in the state machine
Accept only if all accept.
"""
acceptance_f = has_one_from_each(AcceptanceStateIndexList,
EpsilonClosure)
return State(AcceptanceF=acceptance_f)
# Plain merge of all states of all state machines with an
# epsilon transition from the init state to all init states
# of the reverse_sm
sm = StateMachine()
for rsm in reverse_sm_list:
sm.states.update(rsm.states)
sm.add_epsilon_transition(sm.init_state_index, rsm.init_state_index)
initial_state_epsilon_closure = sm.get_epsilon_closure(sm.init_state_index)
InitState = get_merged_state(acceptance_state_id_list,
initial_state_epsilon_closure)
result = StateMachine(InitStateIndex=index.get(), InitState=InitState)
# (*) prepare the initial worklist
worklist = [ ( result.init_state_index, initial_state_epsilon_closure) ]
epsilon_closure_db = sm.get_epsilon_closure_db()
while len(worklist) != 0:
# 'start_state_index' is the index of an **existing** state in the state machine.
# It was either created above, in StateMachine's constructor, or as a target
# state index.
start_state_index, start_state_combination = worklist.pop()
# (*) compute the elementary trigger sets together with the
# epsilon closure of target state combinations that they trigger to.
# In other words: find the ranges of characters where the state triggers to
# a unique state combination. E.g:
# Range Target State Combination
# [0:23] --> [ State1, State2, State10 ]
# [24:60] --> [ State1 ]
#.........这里部分代码省略.........