%{ #include %} %token S %token K %token OPBRACK %token CLBRACK %start comm %% comm: comm '\n' { return $$;} | /* empty */ | comm exp '\n' {$$ = $2; return $$;} | comm error '\n' {printf("Invalid combinator sequence\n"); return 1;} ; exp: exp2 {$$ = $1; } | exp exp2 {$$ = (int)mknode((Tree*)$1,(Tree*)$2); } ; exp2: OPBRACK exp CLBRACK {$$ = $2; } | K {$$ = (int)mkK(); } | S {$$ = (int)mkS(); } ; %% #include "lex.yy.c" enum combinator { s , k }; typedef union Tree Tree; typedef struct Node { Tree* left; Tree* right; } Node; union Tree { Node* N; int C; }; Tree* mkK() { Tree* t = (Tree*) malloc(sizeof(Tree)); t->C = k; return t; } Tree* mkS() { Tree* t = (Tree*) malloc(sizeof(Tree)); t->C = s; return t; } Tree* mknode(Tree* t1, Tree* t2) { Tree* t = (Tree*) malloc(sizeof(Tree)); Node* n = (Node*) malloc(sizeof(Node)); n->left = t1; n->right = t2; t->N = n; return t; } yyerror(s) char *s; { printf("%s\n", s); }