Re: Quine Mc Cluskey and Automat

/*
Grammatik

Sporadische Sammlung
- Zuweisung
- Addition
if
- Vergleiche
    - <=, >=, ==, !=, <, >
- Subtraktion
- Shift
    << >>
- Null setzen

Operationen
    - Mathematische:
        + (Addition)
        - (Subtraktion)
    - Verschieben
        >> Rechtsshift
        << Linksshift
    - 0 setzen
Vergleiche
    - <=, >=, ==, !=, <, >
Zuweisung
    <-

Zeichensatz: Variablen, Register, Operatoren und Konstante Werte

Operand ::= <Register> | <Const>
CMP ::= <= | >= | == | != | < | >
MathOperator ::= + | - | << | >>
BitBooleanOperator ::= '\&amp;\&amp;' | '||' | '!'
Operator ::= <MathOperator> | <BitBooleanOperator>
Expr ::= <Register> <- <Operand> | <Operand> <Operator> <Operand> | 0
Condition ::= IF <Register> <CMP> <Operand> THEN <Program> FI

Programm ::= <Expr> | <Condition> <Program>
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

FILE *fout = NULL;

int lineelse [1024];
int lineif [1024];
int linegoto1 [1024];
int linegoto2 [1024];
int stateif = 0;
int stateelse = 0;
int stategoto1 = 0;
int stategoto2 = 0;
int stategotodst = 0;
int gotodst [1024];

int line = 0;
int nline = 0;
int maxstate = 0;

char *opstr [] = {"+", "-", "<<", ">>", "\&amp;\&amp;", "||", "!"};
char *cmpstr [] = {"<=", ">=", "==", "!=", "<", ">"};

void operator (void);
void cmp (void);
void operand (void);
void expr (int);
void condition (int, int);
void program (int, int, int, int);
void registr (void);
void cnst (void);
void operator (void);
void printemptyspace (int);

void printemptyspace (int n) {
    int i;

    for (i = 0;  i < n*2;  i++)
        fprintf (fout, " ");
}


void registr (void) {
    fprintf (fout, " %c ", (rand () % 4) + 'a');
return;
}

void cnst (void) {
    fprintf (fout, " %i ", rand () % 128);
return;
}

void operator (void) {
    fprintf (fout, " %s ", opstr [rand () % 6]);
return;
}

void cmp (void) {
    fprintf (fout, " %s ", cmpstr [rand () % 5]);
return;
}

void operand (void) {
    if ((rand () % 2) == 0)
        cnst ();
    else
        registr ();
return;
}

void expr (int emptyspacen) {
    fprintf (fout, "%4i:", line++);
    printemptyspace (emptyspacen);
    registr ();
    fprintf (fout, " <- ");
    operand ();
    if ((rand () % 2) == 0) {
        operator ();
        operand ();
    }
    fprintf (fout, "n");
return;
}

void condition (int emptyspacen, int depth) {
    int goto1or2;

    goto1or2 = rand () % 2;


    lineif [stateif++] = line;
    fprintf (fout, "%4i:", line++);
    printemptyspace (emptyspacen);
    fprintf (fout, " IF ", line);
    registr ();
    cmp ();
    operand ();
    fprintf (fout, " THEN n", line);
    program (2, 0, emptyspacen+1, depth+1);
    linegoto1 [stategoto1++] = line;
    fprintf (fout, "%4i:", line++);
    printemptyspace (emptyspacen+1);
    if( goto1or2 )
        fprintf (fout, " GOTO %in", lineif [gotodst[stategotodst++]]);
    else
        fprintf (fout, " GOTO %in", rand () % (nline+1));
    lineelse [stateelse++] = line;
    fprintf (fout, "%4i:", line++);
    printemptyspace (emptyspacen);
    fprintf (fout, " ELSE n", line);
    program (2, 0, emptyspacen+1, depth+1);
    linegoto2 [stategoto2++] = line;
    fprintf (fout, "%4i:", line++);
    printemptyspace (emptyspacen+1);
    if ( !goto1or2)
        fprintf (fout, " GOTO %in", lineif [gotodst[stategotodst++]]);
    else
        fprintf (fout, " GOTO %in", rand () % (nline+1));
    fprintf (fout, "%4c ", ' ');
    printemptyspace (emptyspacen);
    fprintf (fout, " FI n", ' ');
return;
}

void program (int n, int i, int emptyspacen, int depth) {
    if (((rand () % 4) == 0) \&amp;\&amp; (depth < 3))
        condition (emptyspacen, depth);
    else
        expr (emptyspacen);
    if (i < n) {
        program (n, i+1, emptyspacen, depth);
    }
return;
}

void connect (int n) {
    int i;
    int j;
    int t;

    gotodst [0] = rand () % n;
    for (i = 1;  i < n; ) {
        t = rand () % n;
        for (j = 0;  j < i;  j++) {
            if (gotodst [j] == t) {
                t = rand () % n;
                j = 0;
            }
        }
        if (t != i) {
            gotodst [i] = t;
            i++;
        }
    }

return;
}

int main (void) {
    time_t t;
    int j;
    srand (t = time(NULL));
    fout = stdout;
    program (5, 0, 0, 0);
    maxstate = line;
    srand (t);
    fout = stderr;
    for (j = 0;  j < stateif; j++) {
        fprintf (fout, "If %in", lineif [j]);
        fprintf (fout, "Else %in", lineelse [j]);
        fprintf (fout, "Goto 1 %in", linegoto1 [j]);
        fprintf (fout, "Goto 2 %in", linegoto2 [j]);
    }
    if (stateif != 0) {
        connect (stateif);
    }
    nline = line;
    line = 0;
    stategotodst = 0;
    srand (t);
    program (5, 0, 0, 0);
    printf ("%i n", nline);
return 0;
}