#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct tree {
struct tree *l;
struct tree *r;
int ch;
};
char expr [] = "(a(((bc(d))lll)de(faaa)))zzz";
int i = 0;
char gettoken () {
return expr [i++];
}
void tokenback () {
i--;
}
/*
aaaa
aaaaaa
aaaaaaa
aaaaaaaa()
*/
struct tree *stream ();
struct tree *followed ();
struct tree *compound ();
struct tree *or_operator ();
struct tree *repeat_operator ();
struct tree *or_operator () {
struct tree *n;
if ((n = malloc (sizeof(struct tree))) == NULL) {
perror ("Not enough memory");
exit (1);
}
n -> ch = -1;
n -> l = n -> r = repeat_operator ();
if (gettoken () == '+') {
n -> r = repeat_operator ();
}
else
tokenback ();
return n;
}
int repeat_operator () {
struct tree *n;
if ((n = malloc (sizeof(struct tree))) == NULL) {
perror ("Not enough memory");
exit (1);
}
n -> ch = -1;
if (gettoken () == '*') {
stream ();
}
else {
tokenback ();
stream ();
}
}
int stream () {
compound ();
followed ();
}
int followed () {
int ch = gettoken ();
if ((ch >= 'a') && (ch <= 'z')) {
printf ("%c ", ch);
or_operator ();
}
else
tokenback ();
}
int compound () {
if (gettoken () == '(') {
or_operator ();
if (gettoken () != ')') {
fprintf (stderr, "fehler klammer vergessen %c %i\n", expr [i], i);
exit (1);
}
}
else
tokenback ();
}
int main (void) {
or_operator ();
}