]> mj.ucw.cz Git - umpf.git/commitdiff
parsing conditions
authorAnicka Bernathova <anicka@anicka.net>
Fri, 4 Jul 2008 18:42:19 +0000 (20:42 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Fri, 4 Jul 2008 18:42:19 +0000 (20:42 +0200)
cond.y
lex.c

diff --git a/cond.y b/cond.y
index 6473557660d944e472b17612517f7440092b6a4c..34af26373f77b43e4ff1553811ff5997e183fc45 100644 (file)
--- a/cond.y
+++ b/cond.y
 #include <string.h>
 #include <pcre.h>
 
+#include "lex.h"
+
 #define OVECCOUNT 3
 
 int yylex (void);
 void yyerror (char const *);
 
 int regex_cmp(char* s, char* r);
+struct tree* tree_malloc(int type);
+
+struct tree {
+       enum {
+               ST_IF,
+               ST_COND,
+               ST_BLOCK,
+               ST_ASS,
+               ST_LEAF,
+               ST_EMPTY
+       } st;
+       union {
+               struct {
+                       struct tree* c;
+                       struct tree* i;
+                       struct tree* e; 
+               } tif;
+
+               struct {
+                       int op; 
+                       struct tree* left;
+                       struct tree* right;
+               } cond;
+
+               struct {
+                       struct tree* head;
+                       struct tree* tail;
+               } block;
+
+               struct {
+                       struct tree* left;
+                       struct tree* right;
+               } ass;
+
+               struct {
+                       enum {
+                               L_VAR,
+                               L_CONST,
+                               L_NUM
+                       } type;
+                       union {
+                               char* s;
+                               int n;
+                       } value;
+               } leaf;
+
+       } pt;
+};
 
 %}
 
 %union {
-       int b;  
        int n;
        char* str;
+       struct tree* tr;        
 }
 
 %token <str> CONST
 %token <n> NUM
 %token <str> VAR
-%token KW_IF KW_ELSE KW_PIPE KW_MAIL KW_COPY
-%token '(' ')' '{' '}'
+%token KW_PIPE KW_MAIL KW_COPY
+%token '(' ')' '{' '}' ';'
+%left KW_ELSE
+%left KW_IF
 %left ARROW
-%left EQ NEQ GE LE '<' '>' RE NRE
+%left <n> EQ NEQ GE LE '<' '>' RE NRE
 %left '+' '-' 
 %left '*' '/'
-%left '|' '&' '^'
-%left '!'
-%left '=' 
-%type <b> boo
+%left <n> '|'
+%left <n> '^'
+%left <n> '&'
+%left <n> '!'
+%left '='
+%type <tr> input 
+%type <tr> command 
+%type <tr> next 
+%type <tr> ass 
+%type <tr> ass_right 
+%type <tr> cif
+%type <tr> cond
+%type <n> rop 
 
 %%
-input: /* empty */
-       | input line
+input: /* empty */     { $$ = NULL }
+       | command input {       $$ = tree_malloc(ST_BLOCK); 
+                               $$->pt.block.head = $1;
+                               $$->pt.block.tail = $2; 
+                       } 
+;
+
+command:        ';' { $$ = tree_malloc(ST_EMPTY); }
+               | '{' command next '}'  {
+                                               $$ = tree_malloc(ST_BLOCK); 
+                                               $$->pt.block.head = $2;
+                                               $$->pt.block.tail = $3; 
+                                       }
+               | '{' '}' { $$ = NULL }
+               | cif { $$ = $1 }
+               | ass ';' { $$ = $1}
+               
+       
+;
+
+next:  /* empty */ {$$ = NULL}
+       | 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 = NULL;
+                               }
 ;
 
-line:  '\n'
-       | boo '\n'              { printf("%s\n",$1?"true":"false"); }
-       | error '\n'            { yyerrok; }
+cond:  '!' cond {
+                               $$ = tree_malloc(ST_COND);
+                               $$->pt.cond.left = $2;  
+                               $$->pt.cond.right = NULL;       
+                               $$->pt.cond.op = $1;    
+
+               }
+       | cond '|' cond {
+                               $$ = tree_malloc(ST_COND);
+                               $$->pt.cond.left = $1;  
+                               $$->pt.cond.right = $3; 
+                               $$->pt.cond.op = $2;    
+
+                       }
+       | cond '&' cond {
+                               $$ = tree_malloc(ST_COND);
+                               $$->pt.cond.left = $1;  
+                               $$->pt.cond.right = $3; 
+                               $$->pt.cond.op = $2;    
+
+                       }
+       | cond '^' cond {
+                               $$ = tree_malloc(ST_COND);
+                               $$->pt.cond.left = $1;  
+                               $$->pt.cond.right = $3; 
+                               $$->pt.cond.op = $2;    
+
+                       }
+       | '(' cond ')' { $$ = $2; }
+       | ass_right rop ass_right       {
+                                               $$ = tree_malloc(ST_COND);
+                                               $$->pt.cond.left = $1;  
+                                               $$->pt.cond.right = $3; 
+                                               $$->pt.cond.op = $2;    
+                                       }
 ;
 
-boo:    CONST EQ CONST         { $$ = ! strcmp($1, $3); }
-       | CONST NEQ CONST       { $$ = !! strcmp($1, $3); }
-       | CONST RE CONST        { $$ = regex_cmp($1,$3) >= 0 }
-       | CONST NRE CONST       { $$ = regex_cmp($1,$3) < 0 }
-       | NUM EQ NUM            { $$ = $1 == $3 }
-       | NUM NEQ NUM           { $$ = $1 != $3 }
-       | NUM GE NUM            { $$ = $1 >= $3 }
-       | NUM LE NUM            { $$ = $1 <= $3 }
-       | NUM '>' NUM           { $$ = $1 > $3 }
-       | NUM '<' NUM           { $$ = $1 < $3 }
-       | boo '|' boo           { $$ = $1 || $3 }
-       | boo '&' boo           { $$ = $1 && $3 }
-       | boo '^' boo           { $$ = ($1 || $3) && !($1 && $3) }
-       | '!' boo               { $$ = ! $2 }
+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.s = $1;
+
+                                       $$->pt.ass.right = $3;
+                               }
 ;
+
+ass_right:     NUM     { 
+                               $$ = tree_malloc(ST_LEAF);
+                               $$->pt.leaf.type = L_NUM;
+                               $$->pt.leaf.value.n = $1;
+                       }
+               | VAR   {
+                               $$ = tree_malloc(ST_LEAF);
+                               $$->pt.leaf.type = L_VAR;
+                               $$->pt.leaf.value.s = $1;
+                       }
+               | CONST {
+                               $$ = tree_malloc(ST_LEAF);
+                               $$->pt.leaf.type = L_CONST;
+                               $$->pt.leaf.value.s = $1;
+                       }
+
+;
+
 %%
 
+struct tree* 
+tree_malloc(int type)
+{
+       struct tree* temp;
+       temp = xmalloc(sizeof (struct tree));
+       temp->st=type;
+
+       return temp;
+}
+
 int 
 regex_cmp(char* s, char* r)
 {
@@ -75,7 +237,6 @@ regex_cmp(char* s, char* r)
                return -1;
        
        int res = pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
-       
        pcre_free(brum);
 
        return res;
diff --git a/lex.c b/lex.c
index 4ee876d0e2f7d995b0e5559f08ca1b8db19b27fd..ba892dbdf29789b18e073cd936e85a415c279a11 100644 (file)
--- a/lex.c
+++ b/lex.c
@@ -115,18 +115,13 @@ is_alpha(int c)
 int
 yylex(void)
 {
-       int c, nl = 0;
+       int c;
        
        while ((c = getchar ()) == ' ' || c == '\t' || c =='\n'){
-               if (c == '\n'){
-                       nl = 1;
+               if (c == '\n')
                        line++;
-               }
        }
        
-       if (nl)
-               return '\n';
-       
        if (c == EOF)
                return 0;
        
@@ -158,6 +153,7 @@ yylex(void)
                case '<':
                case '>':
                case '=':
+               case ';':
                        return c;
                
                case '"':