pure subroutine build_nfa_graph (tree, nfa, nfa_entry, nfa_exit, nfa_top, all_segments)
use :: forgex_parameters_m, only: NFA_TRANSITION_UNIT
implicit none
type(tree_t), intent(in) :: tree
type(nfa_state_node_t), intent(inout), allocatable :: nfa(:)
integer(int32), intent(inout) :: nfa_entry
integer(int32), intent(inout) :: nfa_exit
integer(int32), intent(inout) :: nfa_top
type(segment_t), intent(inout), allocatable :: all_segments(:)
integer(int32) :: i, i_begin, i_end ! index for states array
i_begin = NFA_STATE_BASE
i_end = NFA_STATE_UNIT
! initialize
nfa_top = 0
allocate(nfa(i_begin:i_end))
! Initialize
nfa(i_begin:i_end)%own_i = [(i, i =i_begin, i_end)]
nfa(:)%alloc_count_f = 0
nfa(:)%alloc_count_b = 0
nfa(:)%forward_top = 1
nfa(:)%backward_top = 1
call make_nfa_node(nfa_top)
nfa_entry = nfa_top
call make_nfa_node(nfa_top)
nfa_exit = nfa_top
call generate_nfa(tree, tree%top, nfa, nfa_top, nfa_entry, nfa_exit)
do i = 1, nfa_top
call nfa(i)%merge_segments()
end do
call disjoin_nfa(nfa, nfa_top, all_segments)
end subroutine build_nfa_graph