/media/sda-magnetic/david/Dok-15-2023-11-27/informatik/pascal/postregs/newreg/reg7xxxsuper.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 [] = "[[[aq,f],b],[c,d]]";
//char expr [] = "[a,b]";
//char expr [] = "abcdefgh";
//char expr [] = "abcd";
int i = 0;
int x = 0;


int initstates () {
    int l;

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

}


int gettoken () {
    if (i >= (strlen (expr))) 
      return -1;
    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 fracinit ();

void debug (char fname [], struct fracture ret) {
  int k;
  printf ("=====================================\n");
  printf ("function name:\t\t%s\n", fname);
  printf ("y.begin:\t\t %3i\n", ret.begin);
  printf ("y.end:\t\t %3i\n", ret.end);
  printf ("y.d1_begin:\t\t %3i\n", ret.d1_begin);
  printf ("y.d1_end:\t\t %3i\n", ret.d1_end);
  printf ("y.d2_begin:\t\t %3i\n", ret.d2_begin);
  printf ("y.d2_end:\t\t %3i\n", ret.d2_end);
  printf ("y.exists:\t\t %2i\n", ret.exists);
  printf ("x:\t\t\t (%3i, (%i, %i))\n", x, state1 [x], state2 [x]);
  printf ("=====================================\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]);
  }
  printf ("=====================================\n");
  //getchar ();
}

struct fracture fracinit () {
  struct fracture x;
  x.begin = 0;
  x.end = 0;
  x.d1_begin = 0;
  x.d1_end = 0;
  x.d2_begin = 0;
  x.d2_end = 0;
  x.d3_begin = 0;
  x.d3_end = 0;
  x.exists = 0;
}

struct fracture or_operator () {
    int ch;
    struct fracture x1 = fracinit ();
    struct fracture x2 = fracinit ();
    struct fracture y = fracinit ();
    
    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;
        
        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;
        x++;
        
        
        state1 [y.begin] = x1.begin;
        state2 [y.begin] = x2.begin;
        state1 [x1.end] = y.end;
        state2 [x1.end] = y.end;
        state1 [x2.end] = y.end;
        state2 [x2.end] = y.end;
        
        //x++;
        //repeat_operator ();
        
        debug ("or_operator", y);
        return y;
    }
    else if (ch == -1)
      return fracinit ();
    else {
        tokenback ();
        y = repeat_operator ();
        debug ("or_operator", y);
        return y;
    }
}


struct fracture repeat_operator () {
    int ch;
    struct fracture y = fracinit ();
    struct fracture x1 = fracinit ();

    if ((ch = 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
        
        debug ("repeat_operator", y);
        return y;
    }
    if (ch == -1) 
      return fracinit ();
    else {
        tokenback ();
        y = stream ();
        debug ("repeat_operator", y);
        return y;
    }
}
struct fracture stream () {
    struct fracture x1 = fracinit ();
    struct fracture x2 = fracinit ();
    struct fracture x3 = fracinit ();
    struct fracture y = fracinit ();
  
    //x1 = compound ();
    x2 = followed ();
   
    debug ("stream", y);
  
    y = x2;
    return y;
}

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

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

struct fracture compound () {
    struct fracture x1 = fracinit ();
    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;
        
        debug ("compound", x1);
        return x1;
    }
    else if (ch == -1)
      return fracinit ();
    else {
        x1.exists = 0;
        tokenback ();
        debug ("compound", x1);
        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]);
    }
}