pure recursive subroutine generate_nfa_closure(tree, idx, nfa_graph, nfa_top, entry, exit)
implicit none
type(tree_t), intent(in) :: tree
type(nfa_state_node_t), allocatable, intent(inout) :: nfa_graph(:)
integer(int32), intent(in) :: idx
integer(int32), intent(inout) :: nfa_top
integer(int32), intent(in) :: entry
integer(int32), intent(in) :: exit
integer(int32) :: node1, node2
call make_nfa_node(nfa_top)
if (is_exceeded(nfa_top, nfa_graph)) then
call reallocate_nfa(nfa_graph)
end if
node1 = nfa_top
call make_nfa_node(nfa_top)
if (is_exceeded(nfa_top, nfa_graph)) then
call reallocate_nfa(nfa_graph)
end if
node2 = nfa_top
call nfa_graph(entry)%add_transition(nfa_graph, entry, node1, SEG_EPSILON)
call generate_nfa(tree, tree%nodes(idx)%left_i, nfa_graph, nfa_top, node1, node2)
call nfa_graph(node2)%add_transition(nfa_graph, node2, node1, SEG_EPSILON)
call nfa_graph(node1)%add_transition(nfa_graph, node1, exit, SEG_EPSILON)
end subroutine generate_nfa_closure