From 136ebc22c9f3c19012b5b7adde1cd099aa399842 Mon Sep 17 00:00:00 2001 From: Anicka Bernathova Date: Thu, 3 Jul 2008 16:04:08 +0200 Subject: [PATCH] lex --- Makefile | 2 +- cond.y | 104 ++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 89 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 458e8e4..a58753a 100644 --- 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 cb995c6..d5b2934 100644 --- a/cond.y +++ b/cond.y @@ -21,10 +21,18 @@ int regex_cmp(char* s, char* r); %token CONST %token REGEX +%token ERR %token NUM -%left EQ NEQ GE LE GT LT RE NRE +%token 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 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