From 24967a3dcde5a22b89e88abc4554735718fb4e08 Mon Sep 17 00:00:00 2001 From: Anicka Bernathova Date: Tue, 8 Jul 2008 16:08:27 +0200 Subject: [PATCH] add arrow and . --- cond.y | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- lex.c | 12 ++++----- lex.h | 1 + 3 files changed, 79 insertions(+), 13 deletions(-) diff --git a/cond.y b/cond.y index e69668b..8baaf21 100644 --- a/cond.y +++ b/cond.y @@ -22,7 +22,9 @@ struct tree { ST_BLOCK, ST_ASS, ST_LEAF, - ST_EMPTY + ST_EMPTY, + ST_ARROW, + ST_OP } st; /* subtree type */ union { struct { @@ -59,6 +61,18 @@ struct tree { } value; } leaf; + struct { + char* kw_left; + char* kw_right; + struct tree* s; + } arrow; + + struct { + int op; + struct tree* left; + struct tree* right; + } op; + } pt; }; @@ -66,6 +80,8 @@ struct tree* input_tree; %} +%error-verbose + %union { int n; char* str; @@ -75,7 +91,7 @@ struct tree* input_tree; %token CONST %token NUM %token VAR -%token KW_PIPE KW_MAIL KW_COPY +%token KW_PIPE KW_MAIL KW_COPY %token '(' ')' '{' '}' ';' %nonassoc KW_IF %nonassoc KW_ELSE @@ -95,8 +111,12 @@ struct tree* input_tree; %type ass %type ass_right %type cif +%type arrow %type cond %type rop +%type left +%type right +%type leaves %% input: /* empty */ { $$ = input_tree = NULL; } @@ -117,6 +137,7 @@ command: ';' { $$ = tree_malloc(ST_EMPTY); } | '{' '}' { $$ = tree_malloc(ST_EMPTY); } | cif | ass ';' { $$ = $1} + | arrow ';' { $$ = $1} ; @@ -200,7 +221,7 @@ ass: } ; -ass_right: NUM { +leaves: NUM { $$ = tree_malloc(ST_LEAF); $$->pt.leaf.type = L_NUM; $$->pt.leaf.value.n = $1; @@ -210,12 +231,38 @@ ass_right: NUM { $$->pt.leaf.type = L_VAR; $$->pt.leaf.value.s = $1; } - | CONST { + | CONST { $$ = tree_malloc(ST_LEAF); $$->pt.leaf.type = L_CONST; $$->pt.leaf.value.s = $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: /* 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; + } ; %% @@ -251,7 +298,7 @@ regex_cmp(char* s, char* r) void yyerror (char const *s) { - fprintf (stderr, "%s\n", s); + fprintf (stderr, "Line %d: %s\n", line, s); } void @@ -317,6 +364,26 @@ print_tree(struct tree* t, int ind) break; } break; + case ST_ARROW: + if (t->pt.arrow.kw_left){ + print_ind(ind+1, ' '); + puts(t->pt.arrow.kw_left); + } + print_ind(ind, ' '); + printf("->\n"); + if (t->pt.arrow.kw_right){ + print_ind(ind+1, ' '); + puts(t->pt.arrow.kw_right); + } + print_tree(t->pt.arrow.s,ind+1); + break; + case ST_OP: + print_tree(t->pt.op.left, ind+1); + print_ind(ind,' '); + putchar(t->pt.op.op); + putchar('\n'); + print_tree(t->pt.op.right, ind+1); + break; case ST_EMPTY: break; @@ -328,7 +395,7 @@ print_tree(struct tree* t, int ind) int main(void) { - yydebug=1; +// yydebug=1; yyparse (); print_tree(input_tree,0); diff --git a/lex.c b/lex.c index 35b5b00..9b48b68 100644 --- a/lex.c +++ b/lex.c @@ -5,7 +5,7 @@ #include #include "cond.tab.h" - +#include "lex.h" #define BUFSIZE 4096 #define KLEN 10 @@ -14,9 +14,7 @@ struct keys { enum yytokentype keytoks; }; -static int line; - -static struct keys k[] = +static struct keys kwds[] = { {"copy", KW_COPY}, {"else", KW_ELSE}, {"if", KW_IF}, @@ -200,10 +198,10 @@ yylex(void) buf[i] = 0; ungetc(c,stdin); - n = (sizeof(k)/sizeof(struct keys)); + n = (sizeof(kwds)/sizeof(struct keys)); for (i = 0; i < n; i++){ - if (!strcmp(buf,k[i].keywords)) - return k[i].keytoks; + if (!strcmp(buf,kwds[i].keywords)) + return kwds[i].keytoks; } parse_err("Unknown keyword %s", buf); diff --git a/lex.h b/lex.h index 71799df..6e5e524 100644 --- a/lex.h +++ b/lex.h @@ -1 +1,2 @@ void* xmalloc(size_t size); +int line; -- 2.39.2