/media/sda-magnetic/david/Dokumente-15-2023-11-22/informatik/compilerbau/pattern-matching/superreg/reg777.c


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

#define X   1024
#define END -1
#define CONTROLSTATE    '#'

int state1 [X];
int state2 [X];
int v[X];
char text [] = "d";
//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)aaa)(efg))";
char expr [] = "[a,[b,c]]";
//char expr [] = "abcd";
int i = 0;
int x = 0;


int initstates () {
    int l;

    for (l = 0;  l < X;  l++) {
        v [l] = ' ';
    }

}


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

struct fracture {
    int begin;
    int end;
    int d1_begin;
    int d2_begin; 
    int d3_begin;
    int d1_end;
    int d2_end;
    int d3_end;
    int exists;
};

struct fracture stream ();
struct fracture followed ();
struct fracture compound ();
struct fracture or_operator ();
struct fracture repeat_operator ();

struct fracture or_operator () {
    int ch;
    struct fracture x1;
    struct fracture x2;
    struct fracture y;
    
    if ((ch = gettoken ()) == '[') {
        y.begin = x;
        x++;
        x1 = or_operator ();
        if ((ch = gettoken ()) != ',') {
            fprintf (stderr, "Komma vergessen %c", ch);
            exit (1);
        }
        y.d1_begin = x1.begin;
        y.d1_end = x1.end;
        
        x++;
        x2 = or_operator ();
        y.d2_begin = x2.begin;
        y.d2_end = x2.end;
        if ((ch = gettoken ()) != ']') {
            fprintf (stderr, "Klammer vergessen ]");
            exit (1);
        }
        y.end = x;
        
        state1 [x1.end] = y.end;
        state2 [x1.end] = y.end;
        state1 [x2.end] = y.end;
        state2 [x2.end] = y.end;
        state1 [y.begin] = x1.begin;
        state2 [y.begin] = x2.begin;
        
        //x++;
        //repeat_operator ();
        
        return y;
    }
    else {
        tokenback ();
        return repeat_operator ();
    }
}


struct fracture repeat_operator () {
    struct fracture y;
    struct fracture x1;

    if (gettoken () == '*') {
        x1 = stream ();
        x++;
        y.d1_begin = x1.begin;
        y.d1_end = x1.end;
        y.begin = y.end = x;
        
        state1 [x1.end] = y.begin;
        state2 [x1.end] = y.begin;
        
        state1 [y.begin] = x1.begin;
        state2 [y.begin] = x1.begin+1;  //ACCCCCCCCCCCCCCCCCCHTTTTTTTTTUNG
    }
    else {
        tokenback ();
        return stream ();
    }
}
struct fracture stream () {
    struct fracture x1;
    struct fracture x2;
    struct fracture x3;
    struct fracture y;
    
    x1 = compound ();
    x2 = followed ();
    
    if (x1.exists && x2.exists) {
      x1.d1_begin = x2.begin;
      y = x1;
      state1 [x1.end] = x2.begin;
      state2 [x1.end] = x2.begin;
    }
    else if(x1.exists && !x2.exists) {
      y = x1;
    }
    else if (!x1.exists && x2.exists) {
      y = x2;
    }
    if (x1.exists || x2.exists) {
        x3 = or_operator();
        y.d1_begin = x3.begin;
        state1 [y.end] = x3.begin;
        state2 [y.end] = x3.begin;
    }
    return y;
}

struct fracture followed () {
    struct fracture x1;
    struct fracture y;
    
    int ch = gettoken ();

    if ((ch >= 'a') && (ch <= 'z')) {
        v [x] = ch;
        y.begin = x;
        x = x++;
        x1 = or_operator ();
        y.d1_begin = x1.begin;
        y.end = x1.end;
        y.exists = 1;
        state1 [y.begin] = x1.begin;
        state2 [y.begin] = x1.begin;
        return y;
    }
    else {
        y.exists = 0;
        tokenback ();
        return y;
    }
}

struct fracture compound () {
    struct fracture x1;
    int ch;
    if (gettoken () == '(') {
        x1 = or_operator ();
        if ((ch = gettoken ()) != ')') {
            fprintf (stderr, "fehler klammer vergessen %c %i\n", expr [i], i);
            exit (1);
        }
        x1.exists = 1;
        return x1;
    }
    else {
        x1.exists = 0;
        tokenback ();
        return x1;
    }
}


/*
int automat (int xreal, int xtext) {
    int exists = 0;
    
    if (strlen (text) <= xtext) 
      return 1;
    if (realstateschar [xreal] == CONTROLSTATE) {
      if (realstates1 [xreal] == realstates2 [xreal])
        exists = automat (realstates1 [xreal], xtext);
      else
        exists = automat (realstates1 [xreal], xtext) || automat (realstates2 [xreal], xtext);
    }
    else if ((realstateschar [xreal] >= 'a') && (realstateschar [xreal] <= 'z'))  {
      if (realstateschar [xreal] == text [xtext])
        exists = 1;
      if (realstates1 [xreal] == realstates2 [xreal]) 
        exists &= automat (realstates1 [xreal], xtext+1);
      else
        exists &= (automat (realstates1 [xreal], xtext+1) || automat (realstates2 [xreal], xtext+1));
    }
    return exists;
}*/
    
      

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

    printf ("success\n");
    
    for (k = 0;  k <= x;  k++) {
      if ((v[k] >= 'a') && (v[k] <= 'z'))
        printf ("(%i, (%c, %i, %i))\n", k, v[k], state1 [k], state2 [k]);
      else
        printf ("(%i, (%i, %i))\n", k, state1 [k], state2 [k]);
    }
}