2 * fa.h: finite automata
4 * Copyright (C) 2007-2016 David Lutterkort
6 * This library 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.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Author: David Lutterkort <dlutter@redhat.com>
31 /* The type for a finite automaton. */
34 /* The type of a state of a finite automaton. The fa_state functions return
35 * pointers to this struct. Those pointers are only valid as long as the
36 * only fa_* functions that are called are fa_state_* functions. For
37 * example, the following code will almost certainly result in a crash (or
40 * struct state *s = fa_state_initial(fa);
42 * // Crashes as S will likely have been freed
43 * s = fa_state_next(s)
47 /* Denote some basic automata, used by fa_is_basic and fa_make_basic */
49 FA_EMPTY, /* Accepts the empty language, i.e. no strings */
50 FA_EPSILON, /* Accepts only the empty word */
51 FA_TOTAL /* Accepts all words */
54 /* Choice of minimization algorithm to use; either Hopcroft's O(n log(n))
55 * algorithm or Brzozowski's reverse-determinize-reverse-determinize
56 * algorithm. While the latter has exponential complexity in theory, it
57 * works quite well for some cases.
59 enum fa_minimization_algorithms {
64 /* Which minimization algorithm to use in FA_MINIMIZE. The library
65 * minimizes internally at certain points, too.
67 * Defaults to FA_MIN_HOPCROFT
69 extern int fa_minimization_algorithm;
71 /* Unless otherwise mentioned, automata passed into routines are never
72 * modified. It is the responsibility of the caller to free automata
73 * returned by any of these routines when they are no longer needed.
77 * Compile the regular expression RE of length SIZE into an automaton. The
78 * return value is the same as the return value for the POSIX function
79 * regcomp. The syntax for regular expressions is extended POSIX syntax,
80 * with the difference that '.' does not match newlines.
82 * On success, FA points to the newly allocated automaton constructed for
83 * RE, and the function returns REG_NOERROR. Otherwise, FA is NULL, and the
84 * return value indicates the error.
86 * The FA is case sensitive. Call FA_NOCASE to switch it to
89 int fa_compile(const char *re, size_t size, struct fa **fa);
91 /* Make a new automaton that accepts one of the basic languages defined in
94 struct fa *fa_make_basic(unsigned int basic);
96 /* Return 1 if FA accepts the basic language BASIC, which must be one of
97 * the constants from enum FA_BASIC.
99 int fa_is_basic(struct fa *fa, unsigned int basic);
101 /* Minimize FA using the currently-set fa_minimization_algorithm.
102 * As a side effect, the automaton will also be deterministic after being
103 * minimized. Modifies the automaton in place.
105 int fa_minimize(struct fa *fa);
107 /* Return a finite automaton that accepts the concatenation of the
108 * languages for FA1 and FA2, i.e. L(FA1).L(FA2)
110 struct fa *fa_concat(struct fa *fa1, struct fa *fa2);
112 /* Return a finite automaton that accepts the union of the languages that
113 * FA1 and FA2 accept (the '|' operator in regular expressions).
115 struct fa *fa_union(struct fa *fa1, struct fa *fa2);
117 /* Return a finite automaton that accepts the intersection of the languages
120 struct fa *fa_intersect(struct fa *fa1, struct fa *fa2);
122 /* Return a finite automaton that accepts the complement of the language of
123 * FA, i.e. the set of all words not accepted by FA
125 struct fa *fa_complement(struct fa *fa);
127 /* Return a finite automaton that accepts the set difference of the
128 * languages of FA1 and FA2, i.e. L(FA1)\L(FA2)
130 struct fa *fa_minus(struct fa *fa1, struct fa *fa2);
132 /* Return a finite automaton that accepts a repetition of the language that
133 * FA accepts. If MAX == -1, the returned automaton accepts arbitrarily
134 * long repetitions. MIN must be 0 or bigger, and unless MAX == -1, MIN
135 * must be less or equal to MAX. If MIN is greater than 0, the returned
136 * automaton accepts only words that have at least MIN repetitions of words
139 * The following common regexp repetitios are achieved by the following
140 * calls (using a lose notation equating automata and their languages):
142 * - FA* = FA_ITER(FA, 0, -1)
143 * - FA+ = FA_ITER(FA, 1, -1)
144 * - FA? = FA_ITER(FA, 0, 1)
145 * - FA{n,m} = FA_ITER(FA, n, m) with 0 <= n and m = -1 or n <= m
147 struct fa *fa_iter(struct fa *fa, int min, int max);
149 /* If successful, returns 1 if the language of FA1 is contained in the language
150 * of FA2, 0 otherwise. Returns a negative number if an error occurred.
152 int fa_contains(struct fa *fa1, struct fa *fa2);
154 /* If successful, returns 1 if the language of FA1 equals the language of FA2,
155 * 0 otherwise. Returns a negative number if an error occurred.
157 int fa_equals(struct fa *fa1, struct fa *fa2);
159 /* Free all memory used by FA */
160 void fa_free(struct fa *fa);
162 /* Print FA to OUT as a graphviz dot file */
163 void fa_dot(FILE *out, struct fa *fa);
165 /* Return a finite automaton that accepts the overlap of the languages of
166 * FA1 and FA2. The overlap of two languages is the set of strings that can
167 * be split in more than one way into a left part accepted by FA1 and a
168 * right part accepted by FA2.
170 struct fa *fa_overlap(struct fa *fa1, struct fa *fa2);
172 /* Produce an example for the language of FA. The example is not
173 * necessarily the shortest possible. The implementation works very hard to
174 * have printable characters (preferably alphanumeric) in the example, and
175 * to avoid just an empty word.
177 * *EXAMPLE will be the example, which may be NULL. If it is non-NULL,
178 * EXAMPLE_LEN will hold the length of the example.
180 * Return 0 on success, and a negative numer on error. On error, *EXAMPLE
183 * If *EXAMPLE is set, it is the caller's responsibility to free the string
186 int fa_example(struct fa *fa, char **example, size_t *example_len);
188 /* Produce an example of an ambiguous word for the concatenation of the
189 * languages of FA1 and FA2. The return value is such a word (which must be
190 * freed by the caller) if it exists. If none exists, NULL is returned.
192 * The returned word is of the form UPV and PV and V are set to the first
193 * character of P and V in the returned word. The word UPV has the property
194 * that U and UP are accepted by FA1 and that PV and V are accepted by FA2.
196 * Neither the language of FA1 or of FA2 may contain words with the
197 * characters '\001' and '\002', as they are used during construction of
198 * the ambiguous word.
200 * UPV_LEN will be set to the length of the entire string UPV
202 * Returns 0 on success, and a negative number on failure. On failure, UPV,
203 * PV, and V will be NULL
205 int fa_ambig_example(struct fa *fa1, struct fa *fa2,
206 char **upv, size_t *upv_len,
207 char **pv, char **v);
209 /* Convert the finite automaton FA into a regular expression and set REGEXP
210 * to point to that. When REGEXP is compiled into another automaton, it is
211 * guaranteed that that automaton and FA accept the same language.
213 * The code tries to be semi-clever about keeping the generated regular
214 * expression short; to guarantee reasonably short regexps, the automaton
215 * should be minimized before passing it to this routine.
217 * On success, REGEXP_LEN is set to the length of REGEXP
219 * Return 0 on success, and a negative number on failure. The only reason
220 * for FA_AS_REGEXP to fail is running out of memory.
222 int fa_as_regexp(struct fa *fa, char **regexp, size_t *regexp_len);
224 /* Given the regular expression REGEXP construct a new regular expression
225 * NEWREGEXP that does not match strings containing any of the characters
226 * in the range FROM to TO, with the endpoints included.
228 * The new regular expression is constructed by removing the range FROM to
229 * TO from all character sets in REGEXP; if any of the characters [FROM,
230 * TO] appear outside a character set in REGEXP, return -2.
232 * Return 0 if NEWREGEXP was constructed successfully, -1 if an internal
233 * error happened (e.g., an allocation failed) and -2 if NEWREGEXP can not
234 * be constructed because any character in the range [FROM, TO] appears
235 * outside of a character set.
237 * Return a positive value if REGEXP is not syntactically valid; the value
238 * returned is one of the REG_ERRCODE_T POSIX error codes. Return 0 on
239 * success and -1 if an allocation fails.
241 int fa_restrict_alphabet(const char *regexp, size_t regexp_len,
242 char **newregexp, size_t *newregexp_len,
245 /* Convert REGEXP into one that does not use ranges inside character
248 * Return a positive value if REGEXP is not syntactically valid; the value
249 * returned is one of the REG_ERRCODE_T POSIX error codes. Return 0 on
250 * success and -1 if an allocation fails.
252 int fa_expand_char_ranges(const char *regexp, size_t regexp_len,
253 char **newregexp, size_t *newregexp_len);
255 /* Modify FA so that it matches ignoring case.
257 * Returns 0 on success, and -1 if an allocation fails. On failure, the
258 * automaton is not guaranteed to represent anything sensible.
260 int fa_nocase(struct fa *fa);
262 /* Return 1 if FA matches ignoring case, 0 if matches are case sensitive */
263 int fa_is_nocase(struct fa *fa);
265 /* Assume REGEXP is a case-insensitive regular expression, and convert it
266 * to one that matches the same strings when used case sensitively. All
267 * occurrences of individual letters c in the regular expression will be
268 * replaced by character sets [cC], and lower/upper case characters are
269 * added to character sets as needed.
271 * Return a positive value if REGEXP is not syntactically valid; the value
272 * returned is one of the REG_ERRCODE_T POSIX error codes. Return 0 on
273 * success and -1 if an allocation fails.
275 int fa_expand_nocase(const char *regexp, size_t regexp_len,
276 char **newregexp, size_t *newregexp_len);
278 /* Generate up to LIMIT words from the language of FA, which is assumed to
279 * be finite. The words are returned in WORDS, which is allocated by this
280 * function and must be freed by the caller.
282 * If FA accepts the empty word, the empty string will be included in
285 * Return the number of generated words on success, -1 if we run out of
286 * memory, and -2 if FA has more than LIMIT words.
288 int fa_enumerate(struct fa *fa, int limit, char ***words);
290 /* Print FA to OUT as a JSON file. State 0 is always the initial one.
291 * Returns 0 on success, and -1 on failure.
293 int fa_json(FILE *out, struct fa *fa);
295 /* Returns true if the FA is deterministic and 0 otherwise */
296 bool fa_is_deterministic(struct fa *fa);
298 /* Return the initial state */
299 struct state *fa_state_initial(struct fa *fa);
301 /* Return true if this is an accepting state */
302 bool fa_state_is_accepting(struct state *st);
304 /* Return the next state; return NULL if there are no more states */
305 struct state* fa_state_next(struct state *st);
307 /* Return the number of transitions for a state */
308 size_t fa_state_num_trans(struct state *st);
310 /* Produce details about the i-th transition.
312 * On success, *to points to the destination state of the transition and
313 * the interval [min-max] is the label of the transition.
315 * On failure, *to, min and max are not modified.
317 * Return 0 on success and -1 when I is larger than the number of
320 int fa_state_trans(struct state *st, size_t i,
321 struct state **to, unsigned char *min, unsigned char *max);
328 * indent-tabs-mode: nil