/media/sda-magnetic/david/Dokumente-16-2024-08-01/informatikUmathematik/graph20240815html/david4/regexpr-unshrink/reg707.c


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define X   1024
#define Y   1024
#define END -1

char statechar [Y][X];
char statelevel [Y][X];
int x;
int y;

int initstates () {
    int k, l;

    for (k = 0;  k < Y;  k++) {
        for (l = 0;  l < X;  l++) {
            statechar [k][l] = ' ';
        }
    }

}

int jx = 0;

char text [] = "abcdefgh";

//char expr [] = "abc*de[fasd,asddsr]qdsda*ghijk";
//char expr [] = "[*([[[([a,[[[p,q*],ca],[d,*(ead)]]]f*(mmm(nnn)mm)),a],asd],semdu]),*poller]";
//char expr [] = "[*([[[([a,[[[p,*(q)],ca],[d,*(ead)]]]f*(mmm(nnn)mm)),a],asd],semdu]),*poller]";
//char expr [] = "*(mmm(nnn)mm)";
char expr [] = "a(b(cd))(efg)";
//char expr [] = "abcd";
int i = 0;

char gettoken () {
    return expr [i++];
}
void tokenback () {
    i--;
}

/*
aaaa
aaaaaa
aaaaaaa
aaaaaaaa()
*/

int stream (int);
int followed (int);
int compound (int);
int or_operator (int);
int repeat_operator (int);

int or_operator (int l) {
    int ch;
    if ((ch = gettoken ()) == '[') {
        or_operator (l+1);

        if (gettoken () != ',') {
            fprintf (stderr, "Komma vergessen");
            exit (1);
        }
        statechar [y][x] = '$';
        statelevel [y][x] = l;
        x++;
        y++;
        or_operator (l+1);
        if ((ch = gettoken ()) != ']') {
            fprintf (stderr, "Klammer vergessen ]");
            exit (1);
        }
        repeat_operator (l);
    }
    else {
        tokenback ();
        repeat_operator (l);
    }

}


int repeat_operator (int l) {
    if (gettoken () == '*') {
        statechar [y][x] = '#';
        statelevel [y][x] = l;
        x++;
        stream (l);
    }
    else {
        tokenback ();
        stream (l);
    }

}

int stream (int l) {
    int r = 0;
    r = compound (l);
    r |= followed (l);
    if (r) {
        or_operator(l);
    }
}

int followed (int l) {
    int ch = gettoken ();
    int st, xtmp;
    if ((ch >= 'a') && (ch <= 'z')) {
        statechar [y][x] = ch;
        statelevel [y][x] = l;
        x = x+1;
        or_operator (l);
        return 1;
    }
    else {
        tokenback ();
        return 0;
    }
}

int compound (int l) {
    int ch;
    if (gettoken () == '(') {
        statechar [y][x] = '@';
        statelevel [y][x] = l;
        x++;
        y++;
        or_operator (l+1);
        if ((ch = gettoken ()) != ')') {
            fprintf (stderr, "fehler klammer vergessen %c %i\n", expr [i], i);
            exit (1);
        }
        return 1;
    }
    else {
        tokenback ();
        return 0;
    }
}

int realstates1 [1024];
int realstates2 [1024];
int realstateschar [1024];


#define STACKLASTWASLABEL           0
#define STACKXREALLOOPLABEL         1
#define ITWASLABELNOTHING           0
#define ITWASLABELLOOP              1


#define REALEND                 -1

int stacks [32][1024];
int stacksp [32];

void push (int stackid, int state) {
  stacks [stackid][stacksp[stackid]++] = state;
}

int pop (int stackid) {
  stacksp [stackid]--;
  return stacks [stackid][stacksp[stackid]];
}

int emptystack (int stackid) {
    return (stacksp[stackid] == 0);
}

void automatgen ( ) {
    int xi = 0;
    int yi = 0;
    int xreal = 0;


    for (xi = 0, yi = 0; xi <= x;  xi++) {
        if
        else if ((statechar [yi][xi] >= 'a') && (statechar [yi][xi] <= 'z')) {
            realstates1 [xreal] = xreal+1;
            realstates2 [xreal] = xreal+1;
            realstateschar [xreal] = statechar [yi][xi];
            xreal++;
        }
        else if (statechar [yi][xi] == '@') {
            push (STACKLASTWASLABEL, ITWASLABELNOTHING)
            yi++;
        }
        else if (statechar [yi][xi] == '#') {
            xi++;
            if (statechar [yi][xi] == '@') {
                push (STACKXREALLOOPLABEL, xreal);
                push (STACKLASTWASLABEL, ITWASLABELLOOP);
                level = statelevel [yi][xi];
                yi++;
            }
        }
    }
    realstateschar [xreal] = '$';
    realstates1 [xreal] = REALEND;
    realstates2 [xreal] = REALEND;

    for (xreal = 0;  xreal <= x;  xreal++) {
        printf ("(%c, ", realstateschar [xreal]);
        printf ("%i, ", realstates1 [xreal]);
        printf ("%i)\n", realstates2 [xreal]);
    }

}

void automat () {
    int xreal;
    int xtext;

    for (xreal = 0, xtext = 0;  (realstates1 [xreal] != REALEND) && (realstates2 [xreal] != REALEND);  xreal = realstates1 [xreal], xtext++)
        if (realstateschar [xreal] == text [xtext])
            printf ("succeded: %c %s\n", text [xtext], text + xtext);

}

int main (void) {
    int k, l;
    initstates ();
    or_operator (0);

    for (l = 0;  l <= y;  l++) {
        for (k = 0;  k <= x;  k++)
            printf ("%2c%2i ", statechar [l][k], statelevel[l][k]);
        printf ("\n");
    }
    automatgen ();
    automat ();
}