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