#include <stdio.h>
#include <string.h>
-#include <pcre.h>
-#define OVECCOUNT 3
+#include "umpf.h"
-int yylex (void);
-void yyerror (char const *);
-
-int regex_cmp(char* s, char* r);
+static struct tree* tree_malloc(int type);
%}
+%error-verbose
%union {
- int b;
int n;
char* str;
+ struct tree* tr;
}
%token <str> CONST
-%token <str> REGEX
%token <n> NUM
-%left EQ NEQ GE LE GT LT RE NRE
-%left '|' '&' '^'
-%left '!'
-%type <b> boo
+%token <str> VAR
+%token <str> KW_DISCARD
+%token <str> KW_PIPE KW_MAIL KW_COPY
+%token '(' ')' '{' '}' ';'
+%nonassoc KW_IF
+%nonassoc KW_ELSE
+%left ARROW
+%left <n> EQ NEQ GE LE '<' '>' RE NRE
+%left '='
+%left '.'
+%left '+' '-'
+%left '*' '/'
+%left <n> '|'
+%left <n> '^'
+%left <n> '&'
+%left <n> '!'
+%type <tr> input
+%type <tr> command
+%type <tr> next
+%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 line
+input: /* empty */ { $$ = input_tree = tree_malloc(ST_EMPTY); }
+ | command input { $$ = tree_malloc(ST_BLOCK);
+ $$->pt.block.head = $1;
+ $$->pt.block.tail = $2;
+
+ input_tree = $$;
+ }
;
-line: '\n'
- | boo '\n' { printf("%s\n",$1?"true":"false"); }
- | error '\n' { yyerrok; }
+command: ';' { $$ = tree_malloc(ST_EMPTY); }
+ | '{' command next '}' {
+ $$ = tree_malloc(ST_BLOCK);
+ $$->pt.block.head = $2;
+ $$->pt.block.tail = $3;
+ }
+ | '{' '}' { $$ = tree_malloc(ST_EMPTY); }
+ | cif
+ | ass ';' { $$ = $1; }
+ | arrow ';' { $$ = $1; }
+
+
;
-boo: CONST EQ CONST { $$ = ! strcmp($1, $3); }
- | CONST NEQ CONST { $$ = !! strcmp($1, $3); }
- | CONST RE REGEX { $$ = regex_cmp($1,$3) >= 0 }
- | CONST NRE REGEX { $$ = 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 GT NUM { $$ = $1 > $3 }
- | NUM LT NUM { $$ = $1 < $3 }
- | boo '|' boo { $$ = $1 || $3 }
- | boo '&' boo { $$ = $1 && $3 }
- | boo '^' boo { $$ = ($1 || $3) && !($1 && $3) }
- | '!' boo { $$ = ! $2 }
+next: /* empty */ {$$ = tree_malloc(ST_EMPTY); }
+ | 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 = tree_malloc(ST_EMPTY);
+ }
;
-%%
-#include <ctype.h>
-#include <stdlib.h>
+cond: '!' cond {
+ $$ = tree_malloc(ST_COND);
+ $$->pt.cond.left = $2;
+ $$->pt.cond.right = NULL;
+ $$->pt.cond.op = $1;
+ $$->pt.cond.type = OP_BOOL;
+ }
+ | cond '|' cond {
+ $$ = tree_malloc(ST_COND);
+ $$->pt.cond.left = $1;
+ $$->pt.cond.right = $3;
+ $$->pt.cond.op = $2;
+ $$->pt.cond.type = OP_BOOL;
+ }
+ | cond '&' cond {
+ $$ = tree_malloc(ST_COND);
+ $$->pt.cond.left = $1;
+ $$->pt.cond.right = $3;
+ $$->pt.cond.op = $2;
+ $$->pt.cond.type = OP_BOOL;
+ }
+ | cond '^' cond {
+ $$ = tree_malloc(ST_COND);
+ $$->pt.cond.left = $1;
+ $$->pt.cond.right = $3;
+ $$->pt.cond.op = $2;
+ $$->pt.cond.type = OP_BOOL;
+
+ }
+ | '(' cond ')' { $$ = $2; }
+ | ass_right rop ass_right {
+ $$ = tree_malloc(ST_COND);
+ $$->pt.cond.left = $1;
+ $$->pt.cond.right = $3;
+ $$->pt.cond.op = $2;
+ $$->pt.cond.type = OP_REL;
+ }
+ | ass_right {
+ $$ = tree_malloc(ST_COND);
+ $$->pt.cond.left = $1;
+ $$->pt.cond.type = JUST_BOOL;
+ }
+;
-#define BUFSIZE 4096
+rop: '>'
+ | '<'
+ | EQ
+ | NEQ
+ | LE
+ | GE
+ | RE
+ | NRE
+;
-int
-regex_cmp(char* s, char* r)
-{
- pcre *brum;
- int erroroffset;
- const char* error;
- int ovector[OVECCOUNT];
-
- brum=pcre_compile(r,0,&error,&erroroffset,NULL);
- if (!brum)
- return -1;
-
- int res=pcre_exec(brum,NULL,s,strlen(s),0,0,ovector,OVECCOUNT);
-
- pcre_free(brum);
+ass:
+ VAR '=' ass_right {
+ $$ = tree_malloc(ST_ASS);
- return res;
-}
+ $$->pt.ass.left = tree_malloc(ST_LEAF);
+ $$->pt.ass.left->pt.leaf.type = L_VAR;
+ $$->pt.ass.left->pt.leaf.value = $1;
+ $$->pt.ass.left->pt.leaf.n = find_var($1, var_hash);
+ $$->pt.ass.right = $3;
+ }
+;
-char*
-get_string_out(char delim)
-{
- int last = delim;
- int i = 0;
- char* s;
- int c;
-
- if (!(s = malloc(BUFSIZE))){
- puts("Low memory");
- exit(0);
- }
-
- while ((c = getchar()) != delim || last == '\\'){
- if (last=='\\' && c != delim)
- s[i-1] = c;
- else {
- s[i] = c;
- i++;
- }
- last = c;
- if (i >= BUFSIZE-1)
- break;
- }
- s[i] = '\0';
+leaves: VAR {
+ $$ = tree_malloc(ST_LEAF);
+ $$->pt.leaf.type = L_VAR;
+ $$->pt.leaf.value = $1;
+ $$->pt.leaf.n = find_var($1, var_hash);
+ }
+ | CONST {
+ $$ = tree_malloc(ST_LEAF);
+ $$->pt.leaf.type = L_CONST;
+ $$->pt.leaf.value = $1;
+ $$->pt.leaf.n = store_const($1);
+ }
+;
- return s;
-}
+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 ARROW KW_DISCARD { //FIXME: actually left does not make sense here
+ $$ = tree_malloc(ST_ARROW);
+ $$->pt.arrow.s = NULL;
+ $$->pt.arrow.kw_left = NULL;
+ $$->pt.arrow.kw_right = "discard";
+ }
+;
-int
-safe_unget(char c)
-{
- if (c==EOF)
- return 0;
+left: /* empty */ { $$ = NULL;}
+ | KW_COPY { $$ = "copy"; }
- ungetc(c,stdin);
- return 1;
-}
+;
+
+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;
+ }
+;
-int
-yylex(void)
+%%
+
+struct tree*
+tree_malloc(int type)
{
+ struct tree* temp;
+ temp = xmalloc(sizeof (struct tree));
+ temp->st=type;
- int c, last;
-
- while ((c = getchar ()) == ' ' || c == '\t');
-
- if (c == '"'){
- last = '"';
- yylval.str = get_string_out('"');
- return CONST;
- }
-
- if (c == '\''){
- last = '\'';
- yylval.str = get_string_out('\'');
- return CONST;
- }
-
- if (c == '/'){
- last = '/';
- yylval.str = get_string_out('/');
- return REGEX;
- }
-
- if (isdigit(c)){
- ungetc(c,stdin);
- scanf("%d",&yylval.n);
- return NUM;
- }
-
- if (c == '!'){
- if ((c = getchar ()) == '=')
- return NEQ;
- else if (c == '~')
- return NRE;
- else
- if (! safe_unget(c))
- return c;
- }
-
- if (c == '<'){
- if ((c = getchar ()) == '=')
- return LE;
- else
- return (safe_unget(c) ? LT : c);
- }
-
- if (c == '>'){
- if ((c = getchar ()) == '=')
- return GE;
- else
- return (safe_unget(c) ? GT : c);
- }
-
- if (c == '='){
- if ((c = getchar ()) == '=')
- return EQ;
- else
- if (! safe_unget(c))
- return c;
- }
-
- if (c == '~'){
- if ((c = getchar ()) == '~')
- return RE;
- else
- if (! safe_unget(c))
- return c;
- }
-
- if (c == EOF)
- return 0;
-
- return c;
+ return temp;
}
void
yyerror (char const *s)
{
- fprintf (stderr, "%s\n", s);
-}
-
-int
-main(void)
-{
- return yyparse ();
+ fprintf (stderr, "Line %d: %s\n", line, s);
}