[augeas-devel] [PATCH 1/3] Indicate whether an FA uses character ranges or regexps on transitions

lutter at redhat.com lutter at redhat.com
Thu Jan 14 01:23:08 UTC 2010


From: David Lutterkort <lutter at redhat.com>

  * src/fa.c (struct fa): new field trans_re; (fa_as_regexp): set trans_re;
    (fa_dot): print regexp rather than char range if trans_re is set
---
 src/fa.c |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/fa.c b/src/fa.c
index c8d87fb..a929887 100644
--- a/src/fa.c
+++ b/src/fa.c
@@ -56,11 +56,17 @@ int fa_minimization_algorithm = FA_MIN_HOPCROFT;
  * is put on this list. Dead/unreachable states are cleared from the list
  * at opportune times (e.g., during minimization) It's poor man's garbage
  * collection
+ *
+ * Normally, transitions are on a character range [min..max]; in
+ * fa_as_regexp, we store regexps on transitions in the re field of each
+ * transition. TRANS_RE indicates that we do that, and is used by fa_dot to
+ * produce proper graphs of an automaton transitioning on regexps.
  */
 struct fa {
     struct state *initial;
     int           deterministic : 1;
     int           minimal : 1;
+    int           trans_re : 1;
 };
 
 /* A state in a finite automaton. Transitions are never shared between
@@ -3812,6 +3818,8 @@ int fa_as_regexp(struct fa *fa, char **regexp, size_t *regexp_len) {
     if (fin == NULL)
         goto error;
 
+    fa->trans_re = 1;
+
     list_for_each(s, fa->initial) {
         r = convert_trans_to_re(s);
         if (r < 0)
@@ -4011,13 +4019,23 @@ void fa_dot(FILE *out, struct fa *fa) {
     fprintf(out, "%s -> \"%p\";\n", fa->deterministic ? "dfa" : "nfa",
             fa->initial);
 
+    struct re_str str;
+    MEMZERO(&str, 1);
     list_for_each(s, fa->initial) {
         for_each_trans(t, s) {
             fprintf(out, "\"%p\" -> \"%p\" [ label = \"", s, t->to);
-            print_char(out, t->min);
-            if (t->min != t->max) {
-                fputc('-', out);
-                print_char(out, t->max);
+            if (fa->trans_re) {
+                re_as_string(t->re, &str);
+                for (int i=0; i < str.len; i++) {
+                    print_char(out, str.rx[i]);
+                }
+                release_re_str(&str);
+            } else {
+                print_char(out, t->min);
+                if (t->min != t->max) {
+                    fputc('-', out);
+                    print_char(out, t->max);
+                }
             }
             fprintf(out, "\" ];\n");
         }
-- 
1.6.5.2




More information about the augeas-devel mailing list