]> mj.ucw.cz Git - umpf.git/commitdiff
add arrow and .
authorAnicka Bernathova <anicka@anicka.net>
Tue, 8 Jul 2008 14:08:27 +0000 (16:08 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Tue, 8 Jul 2008 14:08:27 +0000 (16:08 +0200)
cond.y
lex.c
lex.h

diff --git a/cond.y b/cond.y
index e69668b30b7fc2a94264b545b21c3e4bf1cfe781..8baaf2163306d7d1f58f2eae1a5061c15d1e20e1 100644 (file)
--- 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 <str> CONST
 %token <n> NUM
 %token <str> VAR
-%token KW_PIPE KW_MAIL KW_COPY
+%token <str> KW_PIPE KW_MAIL KW_COPY
 %token '(' ')' '{' '}' ';'
 %nonassoc KW_IF
 %nonassoc KW_ELSE
@@ -95,8 +111,12 @@ struct tree* input_tree;
 %type <tr> ass 
 %type <tr> ass_right 
 %type <tr> cif
+%type <tr> arrow 
 %type <tr> cond
 %type <n> rop 
+%type <str> left
+%type <str> right 
+%type <tr> 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 35b5b00f67073f012ad45ad9f68e72fb75610a88..9b48b680868efbd977aaee773d7c5dd31ca6c816 100644 (file)
--- a/lex.c
+++ b/lex.c
@@ -5,7 +5,7 @@
 #include <stdarg.h>
 
 #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 71799df9f8e16c446a8b39a5c9b39072070a2641..6e5e5245cba722f858d5bb2242baa081f79dc44a 100644 (file)
--- a/lex.h
+++ b/lex.h
@@ -1 +1,2 @@
 void* xmalloc(size_t size);
+int line;