%{ #include #include #include "umpf.h" static struct tree* tree_malloc(int type); %} %error-verbose %union { int n; char* str; struct tree* tr; } %token CONST %token NUM %token VAR %token KW_DISCARD %token KW_PIPE KW_MAIL KW_COPY %token '(' ')' '{' '}' ';' %nonassoc KW_IF %nonassoc KW_ELSE %left ARROW %left EQ NEQ GE LE '<' '>' RE NRE %left '=' %left '.' %left '+' '-' %left '*' '/' %left '|' %left '^' %left '&' %left '!' %type input %type command %type next %type ass %type ass_right %type cif %type arrow %type cond %type rop %type left %type right %type leaves %% input: /* empty */ { $$ = input_tree = tree_malloc(ST_EMPTY); } | command input { $$ = tree_malloc(ST_BLOCK); $$->pt.block.head = $1; $$->pt.block.tail = $2; input_tree = $$; } ; command: ';' { $$ = tree_malloc(ST_EMPTY); } | '{' command next '}' { $$ = tree_malloc(ST_BLOCK); $$->pt.block.head = $2; $$->pt.block.tail = $3; } | '{' '}' { $$ = tree_malloc(ST_EMPTY); } | cif | ass ';' { $$ = $1; } | arrow ';' { $$ = $1; } ; next: /* empty */ {$$ = tree_malloc(ST_EMPTY); } | command ; cif: KW_IF cond command KW_ELSE command { $$ = tree_malloc(ST_IF); $$->pt.tif.c = $2; $$->pt.tif.i = $3; $$->pt.tif.e = $5; } | KW_IF cond command { $$ = tree_malloc(ST_IF); $$->pt.tif.c = $2; $$->pt.tif.i = $3; $$->pt.tif.e = tree_malloc(ST_EMPTY); } ; cond: '!' cond { $$ = tree_malloc(ST_COND); $$->pt.cond.left = $2; $$->pt.cond.right = NULL; $$->pt.cond.op = $1; $$->pt.cond.type = OP_BOOL; } | cond '|' cond { $$ = tree_malloc(ST_COND); $$->pt.cond.left = $1; $$->pt.cond.right = $3; $$->pt.cond.op = $2; $$->pt.cond.type = OP_BOOL; } | cond '&' cond { $$ = tree_malloc(ST_COND); $$->pt.cond.left = $1; $$->pt.cond.right = $3; $$->pt.cond.op = $2; $$->pt.cond.type = OP_BOOL; } | cond '^' cond { $$ = tree_malloc(ST_COND); $$->pt.cond.left = $1; $$->pt.cond.right = $3; $$->pt.cond.op = $2; $$->pt.cond.type = OP_BOOL; } | '(' cond ')' { $$ = $2; } | ass_right rop ass_right { $$ = tree_malloc(ST_COND); $$->pt.cond.left = $1; $$->pt.cond.right = $3; $$->pt.cond.op = $2; $$->pt.cond.type = OP_REL; } ; rop: '>' | '<' | EQ | NEQ | LE | GE | RE | NRE ; ass: VAR '=' ass_right { $$ = tree_malloc(ST_ASS); $$->pt.ass.left = tree_malloc(ST_LEAF); $$->pt.ass.left->pt.leaf.type = L_VAR; $$->pt.ass.left->pt.leaf.value = $1; $$->pt.ass.right = $3; } ; leaves: VAR { $$ = tree_malloc(ST_LEAF); $$->pt.leaf.type = L_VAR; $$->pt.leaf.value = $1; } | CONST { $$ = tree_malloc(ST_LEAF); $$->pt.leaf.type = L_CONST; $$->pt.leaf.value = $1; } ; arrow: left ARROW right ass_right { $$ = tree_malloc(ST_ARROW); $$->pt.arrow.s = $4; $$->pt.arrow.kw_left = $1; $$->pt.arrow.kw_right = $3; } | left ARROW KW_DISCARD { //FIXME: actually left does not make sense here $$ = tree_malloc(ST_ARROW); $$->pt.arrow.s = NULL; $$->pt.arrow.kw_left = NULL; $$->pt.arrow.kw_right = "discard"; } ; left: /* empty */ { $$ = NULL;} | KW_COPY { $$ = "copy"; } ; right: /* empty */ { $$ = NULL; } | KW_PIPE { $$ = "pipe"; } | KW_MAIL { $$ = "mail"; } ; ass_right: leaves | ass_right '.' ass_right { $$ = tree_malloc(ST_OP); $$->pt.op.op = '.'; $$->pt.op.left = $1; $$->pt.op.right = $3; } ; %% struct tree* tree_malloc(int type) { struct tree* temp; temp = xmalloc(sizeof (struct tree)); temp->st=type; return temp; } void yyerror (char const *s) { fprintf (stderr, "Line %d: %s\n", line, s); }