>IF-ELSEeinfach ein Zufallswert, der die Anzahl an Zeilen nicht überschreitet. Hier der Code
/* 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 ::= '\&\&' | '||' | '!' 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> #define MAX_STATES 1024 #define MAX_EXPR_CONST_VAL 128 #define MAX_EXPR_REG_VAL 4 #define FIRST_REG_VAL 'a' #define MAX_EXPR_OPERATOR_VAL 6 #define MAX_EXPR_CMP_OPERATOR_VAL 5 #define RAND_OPERAND_CONST_REGISTER 2 #define RAND_EXPR_OPERATOR_OPERAND_FOLLOW 2 #define RAND_COND_TRUE_FALSE_DESICION 2 #define RAND_PROGRAM_COND_EXPR_DESICION 4 #define IF_ELSE_DEPTH 3 #define STD_PROGRAM_N 6 #define STD_PROGRAM2_N 4 #define RAND_COND_END_OR_GO_ON 3 FILE *fout = NULL; int line = 0; int nline = 1; int maxstate = 0; char *opstr [] = {"+", "-", "<<", ">>", "\&\&", "||", "!"}; char *cmpstr [] = {"<=", ">=", "==", "!=", "<", ">"}; void operator (void); void cmp (void); void operand (void); void expr (int); void condition (int, int, int, int); void condition2 (int, int, int, int); void program (int, int, int, int); void program2 (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 () % MAX_EXPR_REG_VAL) + FIRST_REG_VAL); return; } void cnst (void) { fprintf (fout, " %i ", rand () % MAX_EXPR_CONST_VAL); return; } void operator (void) { fprintf (fout, " %s ", opstr [rand () % MAX_EXPR_OPERATOR_VAL]); return; } void cmp (void) { fprintf (fout, " %s ", cmpstr [rand () % MAX_EXPR_CMP_OPERATOR_VAL]); return; } void operand (void) { if ((rand () % RAND_OPERAND_CONST_REGISTER) == 0) cnst (); else registr (); return; } void expr (int emptyspacen) { fprintf (fout, "%4i:", line++); printemptyspace (emptyspacen); registr (); fprintf (fout, " <- "); operand (); if ((rand () % RAND_EXPR_OPERATOR_OPERAND_FOLLOW) == 0) { operator (); operand (); } fprintf (fout, "n"); return; } void condition (int n, int i, int emptyspacen, int depth) { int goto1or2; fprintf (fout, "%4i:", line++); printemptyspace (emptyspacen); fprintf (fout, " IF ", line); registr (); cmp (); operand (); fprintf (fout, " THEN n", line); program (n, i+1, emptyspacen+1, depth+1); fprintf (fout, "%4c ", ' '); printemptyspace (emptyspacen+1); fprintf (fout, " %4cn", ' '); fprintf (fout, "%4c ", ' '); printemptyspace (emptyspacen); fprintf (fout, " ELSE n", line); program (n, i+1, emptyspacen+1, depth+1); fprintf (fout, "%4c ", ' '); printemptyspace (emptyspacen+1); fprintf (fout, " %4cn", ' '); fprintf (fout, "%4c ", ' '); printemptyspace (emptyspacen); fprintf (fout, " FI n", ' '); return; } void condition2 (int n, int i, int emptyspacen, int depth) { int goto1or2; fprintf (fout, "%4i:", line++); printemptyspace (emptyspacen); fprintf (fout, " IF ", line); registr (); cmp (); operand (); fprintf (fout, " THEN n", line); program2 (n, i+1, emptyspacen+1, depth+1); fprintf (fout, "%4c ", ' '); printemptyspace (emptyspacen+1); fprintf (fout, " GOTO %in", rand () % nline); fprintf (fout, "%4c ", ' '); printemptyspace (emptyspacen); fprintf (fout, " ELSE n", line); program2 (n, i+1, emptyspacen+1, depth+1); fprintf (fout, "%4c ", ' '); printemptyspace (emptyspacen+1); fprintf (fout, " GOTO %in", rand () % nline); fprintf (fout, "%4c ", ' '); printemptyspace (emptyspacen); fprintf (fout, " FI n", ' '); return; } void program2 (int n, int i, int emptyspacen, int depth) { if (((rand () % RAND_PROGRAM_COND_EXPR_DESICION) == 0)) expr (emptyspacen); if (i < n) { program2 (n, i+1, emptyspacen, depth); } return; } void program (int n, int i, int emptyspacen, int depth) { program2 (STD_PROGRAM2_N, 0, emptyspacen, depth); if ((rand () % RAND_COND_END_OR_GO_ON) == 0) condition2 (n, i+1, emptyspacen, depth+1); else if ((i < n) \&\& (depth < IF_ELSE_DEPTH)) condition (n, i+1, emptyspacen, depth+1); else { fprintf (fout, "%4c ", ' '); printemptyspace (emptyspacen); fprintf (fout, " GOTO %in", rand () % nline); return; } } int main (void) { time_t t; int j; srand (t = time(NULL)); fout = stderr; program (STD_PROGRAM_N, 0, 0, 0); maxstate = line; srand (t); fout = stdout; nline = line; line = 0; program (STD_PROGRAM_N, 0, 0, 0); return 0; }