]> mj.ucw.cz Git - umpf.git/commitdiff
lex
authorAnicka Bernathova <anicka@anicka.net>
Thu, 3 Jul 2008 14:04:08 +0000 (16:04 +0200)
committerAnicka Bernathova <anicka@anicka.net>
Thu, 3 Jul 2008 14:04:08 +0000 (16:04 +0200)
Makefile
cond.y

index 458e8e4a13a236b1adbc8e0c13de4124e848b460..a58753a02d4ed5c4d8e8bd536e0328154612ba02 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ all: bison
        gcc -Wall -O2 -o cond -g -lpcre cond.tab.c
 
 bison:
-       bison cond.y
+       bison -t cond.y
 
 clean:
        rm -rf cond.tab.c cond  
diff --git a/cond.y b/cond.y
index cb995c631ea60f9ce6e9917e1e26144342059ee1..d5b293413053b19b2d8d71ac13fdc8a65025369b 100644 (file)
--- a/cond.y
+++ b/cond.y
@@ -21,10 +21,18 @@ int regex_cmp(char* s, char* r);
 
 %token <str> CONST
 %token <str> REGEX
+%token ERR
 %token <n> NUM
-%left EQ NEQ GE LE GT LT RE NRE
+%token <str> VAR
+%token KW_IF KW_ELSE KW_PIPE KW_MAIL KW_COPY
+%token '(' ')' '{' '}'
+%left ARROW
+%left EQ NEQ GE LE '<' '>' RE NRE
+%left '+' '-' 
+%left '*' '/'
 %left '|' '&' '^'
 %left '!'
+%left '=' 
 %type <b> boo
 
 %%
@@ -45,8 +53,8 @@ boo:   CONST EQ CONST         { $$ = ! strcmp($1, $3); }
        | NUM NEQ NUM           { $$ = $1 != $3 }
        | NUM GE NUM            { $$ = $1 >= $3 }
        | NUM LE NUM            { $$ = $1 <= $3 }
-       | NUM GT NUM            { $$ = $1 > $3 }
-       | NUM LT 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) }
@@ -156,45 +164,108 @@ yylex(void)
                        return NEQ;
                else if (c == '~')
                        return NRE;
-               else
-                       if (! safe_unget(c))
-                               return c;
+               else {
+                       safe_unget(c);
+                       return '!';     
+               }
        }
 
        if (c == '<'){
                if ((c = getchar ()) == '=')
                        return LE;
-               else
-                       return (safe_unget(c) ? LT : c); 
+               else {
+                       safe_unget(c);
+                       return '<';
+               }
        }
 
        if (c == '>'){
                if ((c = getchar ()) == '=')
                        return GE;
-               else
-                       return (safe_unget(c) ? GT : c); 
+               else {
+                       safe_unget(c);
+                       return '>'; 
+               } 
        }
 
        if (c == '='){
                if ((c = getchar ()) == '=')
                        return EQ;
-               else
-                       if (! safe_unget(c))
-                               return c;
+               else {
+                       safe_unget(c);
+                       return '='; 
+               }
        }
 
        if (c == '~'){
                if ((c = getchar ()) == '~')
                        return RE;
+               else {
+                       safe_unget(c);
+                       return ERR;
+               }
+       }
+
+       if (c == '-'){
+               if ((c = getchar ()) == '>')
+                       return ARROW;
+               else {
+                       safe_unget(c);
+                       return '-';
+               }
+       }
+
+       if (c == '$'){
+               int i=0;
+       
+               if (!(yylval.str=malloc(BUFSIZE))){
+                       puts("Low memory");
+                       exit (0);
+               }
+                       
+               while (isalnum(c = getchar()) || c == '_' || c == '-'){
+                       yylval.str[i]=c;
+                       i++;
+                       if (i >= BUFSIZE)
+                               break;
+               }
+
+               return VAR;
+       }
+
+       if (c == '\n' || c == '+' || c == '*' || c == '/' ||
+               c == '(' || c == ')' || c == '{' || c == '}')
+               return c;
+
+#define KLEN 10
+
+       if (isalpha(c)){
+               char buf[KLEN]; 
+               int i=0;
+
+               ungetc(c,stdin);
+               while (isalpha(c = getchar()) && i<KLEN-1)
+                       buf[i++]=c;
+               buf[i]=0;
+               
+               if (!strcmp(buf,"if"))
+                       return KW_IF;
+               else if (!strcmp(buf,"else"))
+                       return KW_ELSE;
+               else if (!strcmp(buf,"pipe"))
+                       return KW_COPY;
+               else if (!strcmp(buf,"mail"))
+                       return KW_MAIL;
+               else if (!strcmp(buf,"copy"))
+                       return KW_COPY;
                else
-                       if (! safe_unget(c))
-                               return c;
+                       return ERR;
        }
        
        if (c == EOF)
                return 0;
        
-       return c;
+       return ERR;
 }
 
 void
@@ -206,5 +277,6 @@ yyerror (char const *s)
 int
 main(void)
 {
+//     yydebug=1;
        return yyparse ();
 }