%{ #define _BISON_SIMPLE_INCL_ /* ===================================================================== Includes ===================================================================== */ #define YYDEBUG 1 #define _NO_YYPARSE_PROTO_ /* #define _NO_YYLEX_PROTO_ */ #include "a4gl_libaubit4gl.h" #include "a4gl_dbload_int.h" #include #include #include //int is_column_name(char *s); //int process_entry (struct s_dbloadline *dbload); //char *conv_dbl(char *s); //void a4gl_dbload_yyerror(char *s); //int a4gl_dbload_yylex (void); /* ===================================================================== Variables definitions ===================================================================== */ /* ===================================================================== Functions definitions ===================================================================== */ struct filedef *m_filedef=0; %} %start dbload %name-prefix="a4gl_dbload_yy" %union { char str[30000]; char *ptr; struct filedef *fileelem; struct insert *insertexpr; struct s_dbloadline *dbload; struct s_nposlist *nposlist; struct s_field_pos *field_pos; struct s_pos_list *pos_list; struct s_field_pos_list *field_pos_list; } %token KW_FILE KW_DELIMITER KW_STRING KW_INSERT KW_INTO %token KW_NAMED KW_FNAMED %token KW_COMMA KW_NUMBER_VALUE %token KW_SEMICOLON KW_INT_VALUE KW_OPEN_BRACKET KW_CLOSE_BRACKET KW_VALUES %token KW_NULL KW_MINUS KW_EQUAL KW_COLON %% dbload : dbload_entry | dbload dbload_entry ; dbload_entry: file_delim_entry { m_filedef=$1; } insert_entries ; insert_entries: insert_entry { $$=malloc(sizeof(struct s_dbloadline)); $$->f=m_filedef; $$->i=$1; process_entry($$); } | insert_entries insert_entry { $$=malloc(sizeof(struct s_dbloadline)); $$->f=m_filedef; $$->i=$2; process_entry($$); } ; file_delim_entry: KW_FILE fname KW_DELIMITER KW_STRING KW_INT_VALUE KW_SEMICOLON { $$=malloc(sizeof(struct filedef)); $$->type=0; $$->field_pos_list=0; $$->fname=strdup($2); $$->delim=strdup(A4GL_strip_quotes($4)); $$->nfields=atoi($5); } | KW_FILE fname KW_OPEN_BRACKET field_pos_list KW_CLOSE_BRACKET KW_SEMICOLON { $$=malloc(sizeof(struct filedef)); $$->type=1; $$->fname=strdup($2); $$->delim=0; $$->field_pos_list=$4; $$->nfields=$$->field_pos_list->npos; } ; field_pos_list : field_pos { $$=malloc(sizeof(struct s_field_pos_list)); $$->npos=1; $$->pos=malloc(sizeof(struct s_field_pos*)); $$->pos[0]=$1; } | field_pos_list KW_COMMA field_pos { $$=$1; $$->npos++; $$->pos=realloc($$->pos,sizeof(struct s_field_pos*)*$$->npos); $$->pos[$1->npos-1]=$3; } ; field_pos : field_name field_positions opt_null { $$=malloc(sizeof(struct s_field_pos)); $$->name=strdup($1); $$->null=$3; $$->pos_list=$2; } ; opt_null : { $$=0; } | KW_NULL KW_EQUAL KW_STRING { $$=strdup(A4GL_strip_quotes($3)); } ; field_name: KW_NAMED { strcpy($$,$1); } ; field_positions : field_position { $$=malloc(sizeof(struct s_nposlist)); $$->npos=1; $$->list_of_character_positions=malloc(sizeof(struct s_pos_list *)); $$->list_of_character_positions[$$->npos-1]=$1; $$->memsz=$1->sz; } | field_positions KW_COLON field_position { $1->npos++; $1->list_of_character_positions=realloc($1->list_of_character_positions,sizeof(struct s_pos_list *)*$1->npos); $1->list_of_character_positions[$1->npos-1]=$3; $$=$1; $$->memsz+=$3->sz; } ; field_position: KW_INT_VALUE { $$=malloc(sizeof(struct s_pos_list)); $$->start=atoi($1); $$->end=0; $$->sz=1; } | KW_INT_VALUE KW_MINUS KW_INT_VALUE { $$=malloc(sizeof(struct s_pos_list)); $$->start=atoi($1); $$->end=atoi($3); $$->sz=($$->end-$$->start+1); } ; fname: KW_NAMED { strcpy($$,$1); } | KW_FNAMED { strcpy($$,$1); } | KW_STRING { strcpy($$,A4GL_strip_quotes($1)); } ; insert_entry : KW_INSERT KW_INTO tabname opt_cols KW_SEMICOLON { $$=malloc(sizeof(struct insert)); $$->tabname=strdup($3); $$->colexpr=strdup($4); } ; opt_cols : { strcpy($$,""); } | KW_OPEN_BRACKET column_list KW_CLOSE_BRACKET KW_VALUES KW_OPEN_BRACKET values_list KW_CLOSE_BRACKET { sprintf($$,"(%s) VALUES (%s)",$2,$6); } | KW_VALUES KW_OPEN_BRACKET values_list KW_CLOSE_BRACKET { sprintf($$,"VALUES (%s)",$3); } ; column_list : column_name { strcpy($$,$1); } | column_list KW_COMMA column_name { sprintf($$,"%s,%s",$1,$3); } ; column_name : KW_NAMED { if (is_column_name($1)) { char *ptr; ptr=$1; ptr++; sprintf($$,"\t%d\n",atoi(ptr)); } else { strcpy($$,$1); } } ; tabname: KW_NAMED ; values_list: value_value { strcpy($$,$1); } | values_list KW_COMMA value_value { sprintf($$,"%s,%s",$1,$3); } ; value_value: KW_STRING { sprintf($$,"\"%s\"",$1); } | KW_INT_VALUE | KW_NUMBER_VALUE | KW_NAMED { if (is_column_name($1)) { char *ptr; ptr=$1; ptr++; sprintf($$,"\t%d\n",atoi(ptr)); } else { strcpy($$,$1); } } ; %% #ifdef USE_PREGEN #include "lex_pregen.yy.c" #else #include "lex.yy.c" #endif