#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 [] = "[ag,[b,c]]";
//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;
printf ("-------------------------------\n");
printf ("%c\n", expr [i]);
printf ("-------------------------------\n");
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 ();
getchar ();
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 ();
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 ();
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;
}
else
return fracinit ();
debug ("stream", y);
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 ();
y.d1_begin = x1.begin;
y.end = x1.end;
y.exists = 1;
state1 [y.begin] = x1.begin;
state2 [y.begin] = x1.begin;
//state1 [x] = x+1;
//state2 [x] = x+1;
debug ("followed", y);
return y;
}
else if (ch == -1)
return fracinit ();
else {
y.exists = 0;
tokenback ();
debug ("followed", y);
return fracinit ();
}
}
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]);
}
}