From 1ee14507b37bd7cb252341e7f6bdb6398407f1fd Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Sat, 2 Jul 2011 12:39:53 +0200 Subject: milestone: grammar for objects and cfsysline created --- grammar/makefile | 11 ++++--- grammar/rscript.l | 88 +++++++++++++++++++++++++++++++------------------------ grammar/rscript.y | 57 +++++++++++++++++++++++++++++++++++ grammar/utils.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++- grammar/utils.h | 52 ++++++++++++++++++++++++++++++++ 5 files changed, 242 insertions(+), 43 deletions(-) create mode 100644 grammar/rscript.y create mode 100644 grammar/utils.h (limited to 'grammar') diff --git a/grammar/makefile b/grammar/makefile index 520d836b..29aab217 100644 --- a/grammar/makefile +++ b/grammar/makefile @@ -1,8 +1,11 @@ -rscript: lex.yy.c utils.o - gcc -o rscript lex.yy.c utils.o -lestr -lfl +rscript: lex.yy.c utils.o rscript.tab.h utils.h + gcc -o rscript lex.yy.c rscript.tab.c utils.o -lestr -lex.yy.c: rscript.l +lex.yy.c: rscript.l rscript.tab.h flex rscript.l -utils.o: utils.c +rscript.tab.h: rscript.y + bison -d rscript.y + +utils.o: utils.c utils.h gcc -c utils.c diff --git a/grammar/rscript.l b/grammar/rscript.l index f94ad43a..73843692 100644 --- a/grammar/rscript.l +++ b/grammar/rscript.l @@ -33,7 +33,10 @@ * compatible to the previous format. */ %{ +#include #include +#include "utils.h" +#include "rscript.tab.h" static int preCommentState; %} @@ -72,68 +75,76 @@ static int preCommentState; "&" { printf("AMPER\n"); } "ruleset" { printf("RULESET\n"); } -"global"[ \n\t]*"(" { printf("OBJ GLOBAL begin\n"); - BEGIN INOBJ; - } -"input"[ \n\t]*"(" { printf("OBJ INPUT begin\n"); - BEGIN INOBJ; - } -"module"[ \n\t]*"(" { printf("OBJ MODULE begin\n"); - BEGIN INOBJ; - } -"action"[ \n\t]*"(" { printf("OBJ ACTION begin\n"); - BEGIN INOBJ; - } +"global"[ \n\t]*"(" { yylval.objType = CNFOBJ_GLOBAL; + BEGIN INOBJ; return BEGINOBJ; } +"input"[ \n\t]*"(" { yylval.objType = CNFOBJ_INPUT; + BEGIN INOBJ; return BEGINOBJ; } +"module"[ \n\t]*"(" { yylval.objType = CNFOBJ_MODULE; + BEGIN INOBJ; return BEGINOBJ; } +"action"[ \n\t]*"(" { yylval.objType = CNFOBJ_ACTION; + BEGIN INOBJ; return BEGINOBJ; } ^[ \t]*:\$?[a-z]+[ ]*,[ ]*!?[a-z]+[ ]*,[ ]*\".*\" { printf("PROP-FILT: '%s'\n", yytext); } -^[ \t]*[,\*a-z]+\.[,!=;\.\*a-z]+ { printf("PRI-FILT: '%s'\n", yytext); - } +^[ \t]*[,\*a-z]+\.[,!=;\.\*a-z]+ { printf("PRI-FILT: '%s'\n", yytext); + } "*" | \/[^*][^\n]* | -[\|\.\-:][^\n]+ { printf("old style action: '%s'\n", yytext); - } +[\|\.\-:][^\n]+ { printf("old style action: '%s'\n", yytext); + } -[a-z0-9_\-\+]+ { printf("name: '%s'\n", yytext); } +[a-z0-9_\-\+]+ { printf("name: '%s'\n", yytext); } -")" { printf("OBJ end\n"); - BEGIN INITIAL; - } +")" { printf("OBJ end\n"); + BEGIN INITIAL; + return ENDOBJ; + } [a-z][a-z0-9_\.]* { printf("INOBJ: name '%s'\n", yytext); - } -"=" { printf("INOBJ: equals\n"); - } + yylval.estr = es_newStrFromCStr(yytext, yyleng); + return NAME; + } +"=" { printf("INOBJ: equals (%s)\n", yytext); + return(yytext[0]); + } \"([^"\\]|\\['"?\\abfnrtv]|\\[0-7]{1,3})*\" { - printf("INOBJ: value '%s'\n", yytext); - BEGIN INOBJ; - } -"/*" { preCommentState = YY_START; - BEGIN COMMENT; - } -"/*" { preCommentState = YY_START; - BEGIN COMMENT; - } -"*/" { BEGIN preCommentState; } + printf("INOBJ: value '%s'\n", yytext); + yylval.estr = es_newStrFromCStr(yytext+1, yyleng-2); + return VALUE; + } +"/*" { preCommentState = YY_START; + BEGIN COMMENT; + } +"/*" { preCommentState = YY_START; + BEGIN COMMENT; + } +"*/" { BEGIN preCommentState; } ([^*]|\n)+|. #.*\n /* skip comments in input */ [ \n\t] -. { printf("INOBJ: invalid char '%s'\n", yytext); } +. { printf("INOBJ: invalid char '%s'\n", yytext); } /* CFSYSLINE is valid in all modes */ -\$[a-z]+.*$ { printf("CFSYSLINE: '%s'\n", yytext); } -\$[a-z]+.*$ { printf("CFSYSLINE: '%s'\n", yytext); } +\$[a-z]+.*$ { printf("CFSYSLINE: '%s'\n", yytext); + yylval.s = yytext; + return CFSYSLINE; + } +\$[a-z]+.*$ { printf("CFSYSLINE: '%s'\n", yytext); + yylval.s = yytext; + return CFSYSLINE; + } \#.*\n /* skip comments in input */ [\n\t ] /* drop whitespace */ -. { printf("invalid char: %s\n", yytext); - } +. { printf("invalid char: %s\n", yytext); + } /*<> { printf("EOF reached\n"); }*/ %% + /* int main(int argc, char *argv[]) { @@ -147,3 +158,4 @@ main(int argc, char *argv[]) //yy_switch_to_buffer(bp); yylex(); } +*/ diff --git a/grammar/rscript.y b/grammar/rscript.y new file mode 100644 index 00000000..6954d38d --- /dev/null +++ b/grammar/rscript.y @@ -0,0 +1,57 @@ + +%{ +#include +#include +#include "utils.h" +#define YYDEBUG 1 +%} + +%union { + char *s; + es_str_t *estr; + enum cnfobjType objType; + struct cnfobj *obj; + struct nvlst *nvlst; +} + +%token NAME +%token VALUE +%token BEGINOBJ +%token ENDOBJ +%token CFSYSLINE + +%type nv nvlst +%type obj + +%% + /* conf: | conf global | conf action*/ +conf: + | obj conf + | cfsysline conf + +obj: BEGINOBJ nvlst ENDOBJ { printf("XXXX: global processed\n"); + $$ = cnfobjNew($1, $2); + cnfobjPrint($$); + cnfobjDestruct($$); + } +cfsysline: CFSYSLINE { printf("XXXX: processing CFSYSLINE: %s\n", $1); + } +nvlst: { $$ = NULL; } + | nvlst nv { printf("XXXX: nvlst $1: %p, $2 %p\n", $1,$2); + $2->next = $1; + $$ = $2; + } +nv: NAME '=' VALUE { $$ = nvlstNew($1, $3); } + +%% +int yyerror(char *s) +{ + printf("yyerror called: %s\n", s); +} + +int main() +{ + yydebug = 0; + return yyparse(); +} + diff --git a/grammar/utils.c b/grammar/utils.c index f9c50bc9..ccc9fbc7 100644 --- a/grammar/utils.c +++ b/grammar/utils.c @@ -1,7 +1,9 @@ #include +#include #include #include -#include "libestr.h" +#include +#include "utils.h" void readConfFile(FILE *fp, es_str_t **str) @@ -43,3 +45,76 @@ readConfFile(FILE *fp, es_str_t **str) es_addChar(str, '\0'); es_addChar(str, '\0'); } + +struct nvlst* +nvlstNew(es_str_t *name, es_str_t *value) +{ + struct nvlst *lst; + + if((lst = malloc(sizeof(struct nvlst))) != NULL) { + lst->next = NULL; + lst->name = name; + lst->value = value; + } + + return lst; +} + +void +nvlstDestruct(struct nvlst *lst) +{ + struct nvlst *toDel; + + while(lst != NULL) { + toDel = lst; + lst = lst->next; + es_deleteStr(toDel->name); + es_deleteStr(toDel->value); + free(toDel); + } +} + + +void +nvlstPrint(struct nvlst *lst) +{ + char *name, *value; + printf("nvlst %p:\n", lst); + while(lst != NULL) { + name = es_str2cstr(lst->name, NULL); + value = es_str2cstr(lst->value, NULL); + printf("\tname: '%s', value '%s'\n", name, value); + free(name); + free(value); + lst = lst->next; + } +} + +struct cnfobj* +cnfobjNew(enum cnfobjType objType, struct nvlst *lst) +{ + struct cnfobj *o; + + if((o = malloc(sizeof(struct nvlst))) != NULL) { + o->objType = objType; + o->nvlst = lst; + } + + return o; +} + +void +cnfobjDestruct(struct cnfobj *o) +{ + if(o != NULL) { + nvlstDestruct(o->nvlst); + free(o); + } +} + +void +cnfobjPrint(struct cnfobj *o) +{ + printf("obj: '%s'\n", cnfobjType2str(o->objType)); + nvlstPrint(o->nvlst); +} diff --git a/grammar/utils.h b/grammar/utils.h new file mode 100644 index 00000000..45176a50 --- /dev/null +++ b/grammar/utils.h @@ -0,0 +1,52 @@ +#ifndef INC_UTILS_H +#define INC_UTILS_H +#include + +enum cnfobjType { + CNFOBJ_ACTION, + CNFOBJ_GLOBAL, + CNFOBJ_INPUT, + CNFOBJ_MODULE, + CNFOBJ_INVALID = 0 +}; + +static inline char* +cnfobjType2str(enum cnfobjType ot) +{ + switch(ot) { + case CNFOBJ_ACTION: + return "action"; + break; + case CNFOBJ_GLOBAL: + return "global"; + break; + case CNFOBJ_INPUT: + return "input"; + break; + case CNFOBJ_MODULE: + return "module"; + break; + default:return "error: invalid cnfobjType"; + } +} + +struct cnfobj { + enum cnfobjType objType; + struct nvlst *nvlst; +}; + +struct nvlst { + struct nvlst *next; + es_str_t *name; + es_str_t *value; +}; + + +void readConfFile(FILE *fp, es_str_t **str); +struct nvlst* nvlstNew(es_str_t *name, es_str_t *value); +void nvlstDestruct(struct nvlst *lst); +void nvlstPrint(struct nvlst *lst); +struct cnfobj* cnfobjNew(enum cnfobjType objType, struct nvlst *lst); +void cnfobjDestruct(struct cnfobj *o); +void cnfobjPrint(struct cnfobj *o); +#endif -- cgit