2
* Copyright (c) ? - 2000 Lehrstuhl fuer Informatik VII, RWTH Aachen
3
* Copyright (c) 2000 - 2002 Burak Emir
4
* This file is part of the libAMoRE library.
6
* libAMoRE is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
10
* You should have received a copy of the GNU Lesser General Public
11
* License along with the GNU C Library; if not, write to the Free
12
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19
#include "nfa2mnfa_help.h"
21
nfa delsta(nfa inputnfa) {
22
mrkfin infin=inputnfa->infin; /* abbreviation */
23
nfa result; /* Automaton to be returned. */
24
b_array reach1,reach2; /* mark reachable and productive states */
26
posint height=0; /* height of stack */
27
posint state1,state2,s1,s2,letter;
28
posint count=0; /* count reachable and productive states */
30
reach1=newb_array(inputnfa->qno+1);
31
reach2=newb_array(inputnfa->qno+1);
32
stack=newarray(inputnfa->qno+1);
33
/* init stack with initial states */
34
for(state1=0;state1<=inputnfa->qno;state1++)
35
if(isinit(infin[state1]))
37
stack[height++]=state1;
39
while(height) /* stack not empty */
40
{state1=stack[--height];
41
for(letter=1;letter<=inputnfa->sno;letter++)
42
for(state2=0;state2<=inputnfa->qno;state2++)
43
if(testcon(inputnfa->delta,letter,state1,state2)&&(!reach1[state2]))
45
stack[height++]=state2;
48
/* all reachable states are marked in reach1
49
* initialize stack with reachable final states
51
for(state1=0;state1<=inputnfa->qno;state1++)
52
if(isfinal(infin[state1]) && reach1[state1])
54
stack[height++]=state1;
57
while(height) /* stack not empty */
58
{state1=stack[--height];
59
for(state2=0;state2<=inputnfa->qno;state2++)
61
for(letter=1;letter<=inputnfa->sno;letter++)
62
if(testcon(inputnfa->delta,letter,state2,state1)&&(!reach2[state2]))
64
stack[height++]=state2;
68
/* in reach2 all reachable and productive states are marked
69
* there are exactly count many of these states
71
if(count==(inputnfa->qno+1)) /* No state has to be eliminated. */
72
{freebuf();return inputnfa;}
74
/* A new automaton has to be calculated. */
76
result->sno=inputnfa->sno;
78
result->minimal=FALSE;
79
if(count)result->qno=count-1;
81
result->delta=newndelta(result->sno,result->qno);
82
result->infin=newfinal(result->qno);
84
if(count==0) /* L(na)==empty */
85
{setinit(result->infin[0]);
86
setfinalF(result->infin[0]);
89
{for(state1=0,count=0;state1<=inputnfa->qno;state1++)
90
if(reach2[state1]) stack[count++]=state1;
91
/* stack contains all old states, that are reachable and productive */
92
for(state1=0;state1<count;state1++)
93
{s1=stack[state1]; /* abbreviation for old state */
94
setfinal(result->infin[state1],isfinal(infin[s1]));
95
if(isinit(infin[s1]))setinit(result->infin[state1]);
96
for(state2=0;state2<count; state2++)
97
{s2=stack[state1]; /* abbreviation for old state */
98
for (letter=1;letter<=result->sno;letter++)
99
if(testcon(inputnfa->delta,letter,s1,s2))
100
connect(result->delta,letter,state1,state2);
108
nfa invers_d(dfa inputdfa) {
113
result->is_eps=FALSE;
114
result->minimal=FALSE;
115
result->sno=inputdfa->sno;
116
result->qno=inputdfa->qno;
117
result->infin=newfinal(result->qno);
118
result->delta=newndelta(result->sno,result->qno);
119
/* Find initial and final states and compute delta. */
120
for (i=0; i<=inputdfa->qno; i++)
121
{/* Test if i is a new initial state. */
122
if (isfinal(inputdfa->final[i]))
123
setinit(result->infin[i]);
124
/* Compute delta for state i. */
125
for (j=1; j<=inputdfa->sno; j++)
126
connect(result->delta,j,inputdfa->delta[j][i],i);
128
/* Now find only final state. */
129
setfinalT(result->infin[inputdfa->init]);
132
/***************************************************************************/