/*
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>
int line = 0;
char *opstr [] = {"+", "-", "<<", ">>", "\&\&", "||", "!"};
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++)
printf (" ");
}
void registr (void) {
printf (" %c ", (rand () % 4) + 'a');
return;
}
void cnst (void) {
printf (" %i ", rand () % 128);
return;
}
void operator (void) {
printf (" %s ", opstr [rand () % 6]);
return;
}
void cmp (void) {
printf (" %s ", cmpstr [rand () % 5]);
return;
}
void operand (void) {
if ((rand () % 2) == 0)
cnst ();
else
registr ();
return;
}
void expr (int emptyspacen) {
printf ("%4i:", line++);
printemptyspace (emptyspacen);
registr ();
printf (" <- ");
operand ();
if ((rand () % 2) == 0) {
operator ();
operand ();
}
printf ("n");
return;
}
void condition (int emptyspacen, int depth) {
printf ("%4i:", line++);
printemptyspace (emptyspacen);
printf (" IF ", line++);
registr ();
cmp ();
operand ();
printf (" THEN n", line++);
program (2, 0, emptyspacen+1, depth+1);
printf ("%4i:", line++);
printemptyspace (emptyspacen);
printf (" ELSE n", line++);
program (2, 0, emptyspacen+1, depth+1);
printf ("%4c ", ' ');
printemptyspace (emptyspacen);
printf (" FI n", ' ');
return;
}
void program (int n, int i, int emptyspacen, int depth) {
if (((rand () % 4) == 0) \&\& (depth < 3))
condition (emptyspacen, depth);
else
expr (emptyspacen);
if (i < n) {
program (n, i+1, emptyspacen, depth);
}
return;
}
int main (void) {
srand (time(NULL));
program (5, 0, 0, 0);
return 0;
}