Skip to content
Snippets Groups Projects
Commit 3cf23858 authored by Alexis Nasr's avatar Alexis Nasr
Browse files

fixed bugs in json parser

parent f1a5754c
Branches
No related tags found
No related merge requests found
...@@ -52,9 +52,12 @@ typedef struct { ...@@ -52,9 +52,12 @@ typedef struct {
}json_parser_ctx; }json_parser_ctx;
json_struct *json_parse(char *filename);
json_struct *json_parse_full(char *filename, int trace_xml);
json_struct *structure(json_parser_ctx *ctx); json_struct *structure(json_parser_ctx *ctx);
json_parser_ctx *json_parser_init(char *filename); json_parser_ctx *json_parser_init(char *filename);
json_parser_ctx *json_parser_init_full(char *filename, int trace_xml);
......
#ifndef __JSON_TREE__ #ifndef __JSON_TREE__
#define __JSON_TREE__ #define __JSON_TREE__
#define JSON_OBJECT 1 #define JSON_LIST 1
#define JSON_LIST 2 #define JSON_STRING 2
#define JSON_STRING 3 #define JSON_NUMBER 3
#define JSON_NUMBER 4 #define JSON_CONSTANT 4
#define JSON_CONSTANT 5 #define JSON_AVL 5 /* attribute value list */
#define JSON_CONST_TRUE 1 #define JSON_CONST_TRUE 1
#define JSON_CONST_FALSE 2 #define JSON_CONST_FALSE 2
...@@ -27,10 +27,11 @@ struct _json_struct_ { ...@@ -27,10 +27,11 @@ struct _json_struct_ {
struct _json_struct_ *next; struct _json_struct_ *next;
}; };
/*
typedef struct _json_object_ { typedef struct _json_object_ {
json_attr_val *list; json_attr_val *list;
} json_object; } json_object;
*/
/* /*
struct _json_struct_list_ { struct _json_struct_list_ {
int nbelem; int nbelem;
...@@ -48,12 +49,13 @@ json_struct *json_new_string(char *string); ...@@ -48,12 +49,13 @@ json_struct *json_new_string(char *string);
json_struct *json_new_number(float number); json_struct *json_new_number(float number);
json_struct *json_new_constant(int constant); json_struct *json_new_constant(int constant);
json_attr_val *json_new_attr_val(char *attr, json_struct *s, json_attr_val *next); json_attr_val *json_new_attr_val(char *attr, json_struct *s, json_attr_val *next);
json_struct *json_new_object(json_attr_val *avl); //json_struct *json_new_object(json_attr_val *avl);
json_struct *json_new_avl(json_attr_val *avl);
json_struct *json_new_list(json_struct *s); json_struct *json_new_list(json_struct *s);
void json_print_struct(FILE *f, json_struct *s); void json_print_struct(FILE *f, json_struct *s);
void json_print_object(FILE *f, json_struct *s); void json_print_avl(FILE *f, json_struct *s);
void json_print_list(FILE *f, json_struct *s); void json_print_list(FILE *f, json_struct *s);
void json_print_string(FILE *f, json_struct *s); void json_print_string(FILE *f, json_struct *s);
void json_print_number(FILE *f, json_struct *s); void json_print_number(FILE *f, json_struct *s);
......
...@@ -20,11 +20,31 @@ void initialise_premiers(json_parser_ctx *ctx); ...@@ -20,11 +20,31 @@ void initialise_premiers(json_parser_ctx *ctx);
void initialise_suivants(json_parser_ctx *ctx); void initialise_suivants(json_parser_ctx *ctx);
int yylex(json_parser_ctx *ctx); int yylex(json_parser_ctx *ctx);
json_struct *json_parse(char *filename)
{
json_parser_ctx *ctx=json_parser_init(filename);
return structure(ctx);
}
json_struct *json_parse_full(char *filename, int trace_xml)
{
json_parser_ctx *ctx=json_parser_init_full(filename, trace_xml);
json_struct *root = structure(ctx);
/* il faut libérer ctx */
return root;
}
json_parser_ctx *json_parser_init(char *filename) json_parser_ctx *json_parser_init(char *filename)
{
return json_parser_init_full(filename, 0);
}
json_parser_ctx *json_parser_init_full(char *filename, int trace_xml)
{ {
json_parser_ctx *ctx = (json_parser_ctx *) malloc(sizeof(json_parser_ctx)); json_parser_ctx *ctx = (json_parser_ctx *) malloc(sizeof(json_parser_ctx));
ctx->nb_ligne = 1; ctx->nb_ligne = 1;
ctx->trace_xml = 0; ctx->trace_xml = trace_xml;
ctx->indent_xml = 0; ctx->indent_xml = 0;
ctx->indent_step = 1; ctx->indent_step = 1;
initialise_premiers(ctx); initialise_premiers(ctx);
...@@ -413,7 +433,7 @@ json_struct *structure(json_parser_ctx *ctx) ...@@ -413,7 +433,7 @@ json_struct *structure(json_parser_ctx *ctx)
if(est_premier(ctx, ctx->uc, _object_)) { if(est_premier(ctx, ctx->uc, _object_)) {
json_attr_val *avl = object(ctx); json_attr_val *avl = object(ctx);
affiche_balise_fermante(ctx, __FUNCTION__); affiche_balise_fermante(ctx, __FUNCTION__);
return json_new_object(avl); return json_new_avl(avl);
} }
if(ctx->uc == STRING){ if(ctx->uc == STRING){
......
...@@ -50,9 +50,9 @@ json_attr_val *json_new_attr_val(char *attr, json_struct *s, json_attr_val *next ...@@ -50,9 +50,9 @@ json_attr_val *json_new_attr_val(char *attr, json_struct *s, json_attr_val *next
return av; return av;
} }
json_struct *json_new_object(json_attr_val *avl) json_struct *json_new_avl(json_attr_val *avl)
{ {
json_struct *c = json_new_struct(JSON_OBJECT); json_struct *c = json_new_struct(JSON_AVL);
c->u.attr_val_list = avl; c->u.attr_val_list = avl;
return c; return c;
} }
...@@ -60,14 +60,14 @@ json_struct *json_new_object(json_attr_val *avl) ...@@ -60,14 +60,14 @@ json_struct *json_new_object(json_attr_val *avl)
void json_print_struct(FILE *f, json_struct *s) void json_print_struct(FILE *f, json_struct *s)
{ {
if(s == NULL) return; if(s == NULL) return;
if(s->type == JSON_OBJECT) json_print_object(f, s); if(s->type == JSON_AVL) json_print_avl(f, s);
if(s->type == JSON_LIST) json_print_list(f, s); if(s->type == JSON_LIST) json_print_list(f, s);
if(s->type == JSON_STRING) json_print_string(f, s); if(s->type == JSON_STRING) json_print_string(f, s);
if(s->type == JSON_NUMBER) json_print_number(f, s); if(s->type == JSON_NUMBER) json_print_number(f, s);
if(s->type == JSON_CONSTANT) json_print_constant(f, s); if(s->type == JSON_CONSTANT) json_print_constant(f, s);
} }
void json_print_object(FILE *f, json_struct *s) void json_print_avl(FILE *f, json_struct *s)
{ {
json_attr_val *avl; json_attr_val *avl;
int first = 1; int first = 1;
...@@ -97,7 +97,10 @@ void json_print_list(FILE *f, json_struct *s) ...@@ -97,7 +97,10 @@ void json_print_list(FILE *f, json_struct *s)
void json_print_string(FILE *f, json_struct *s) void json_print_string(FILE *f, json_struct *s)
{ {
if(s->u.string)
fprintf(f, "%s", s->u.string); fprintf(f, "%s", s->u.string);
else
fprintf(f, "");
} }
void json_print_number(FILE *f, json_struct *s) void json_print_number(FILE *f, json_struct *s)
...@@ -152,7 +155,7 @@ void json_free_attr_val_list(json_attr_val *avl) ...@@ -152,7 +155,7 @@ void json_free_attr_val_list(json_attr_val *avl)
void json_free_struct(json_struct *s) void json_free_struct(json_struct *s)
{ {
if(s == NULL) return; if(s == NULL) return;
if(s->type == JSON_OBJECT) json_free_attr_val_list(s->u.attr_val_list); if(s->type == JSON_AVL) json_free_attr_val_list(s->u.attr_val_list);
if(s->type == JSON_LIST) json_free_list(s->u.first); if(s->type == JSON_LIST) json_free_list(s->u.first);
if(s->type == JSON_STRING) free(s->u.string); if(s->type == JSON_STRING) free(s->u.string);
free(s); free(s);
......
...@@ -223,18 +223,15 @@ int main(int argc, char *argv[]) ...@@ -223,18 +223,15 @@ int main(int argc, char *argv[])
int index_first_word; int index_first_word;
int index_last_word; int index_last_word;
int sentence_nb = 0; int sentence_nb = 0;
json_parser_ctx *parser_ctx = NULL;
json_struct *root = NULL; json_struct *root = NULL;
json_struct *document = NULL; json_struct *document = NULL;
json_attr_val *avl = NULL; json_attr_val *avl = NULL;
json2mcf_check_options(ctx); json2mcf_check_options(ctx);
wb = word_buffer_load_mcf(ctx->mcf_filename, ctx->mcd_struct); wb = word_buffer_load_mcf(ctx->mcf_filename, ctx->mcd_struct);
root = json_parse_full(ctx->json_filename, 0);
parser_ctx = json_parser_init(ctx->json_filename); if(root->type != JSON_AVL){
root = structure(parser_ctx);
if(root->type != JSON_OBJECT){
fprintf(stderr, "erreur le json doit être un objet\n"); fprintf(stderr, "erreur le json doit être un objet\n");
exit(1); exit(1);
} }
...@@ -245,15 +242,9 @@ int main(int argc, char *argv[]) ...@@ -245,15 +242,9 @@ int main(int argc, char *argv[])
} }
} }
/*json_print_struct(stdout, root); //json_print_struct(stdout, root);
json_free_struct(root);*/
json_free_struct(root);
json2mcf_context_free(ctx); json2mcf_context_free(ctx);
return 0; return 0;
} }
...@@ -137,7 +137,7 @@ void print_header(FILE *output_file, mcd *mcd_struct) ...@@ -137,7 +137,7 @@ void print_header(FILE *output_file, mcd *mcd_struct)
dico *dico_label = mcd_struct->dico_array[label_col]; dico *dico_label = mcd_struct->dico_array[label_col];
int i; int i;
printf("dico label : %d dico = %d\n", label_col, dico_label); // printf("dico label : %d dico = %d\n", label_col, dico_label);
fprintf(output_file, "{\n"); fprintf(output_file, "{\n");
fprintf(output_file, "\"header\":{\n"); fprintf(output_file, "\"header\":{\n");
...@@ -154,7 +154,7 @@ void print_header(FILE *output_file, mcd *mcd_struct) ...@@ -154,7 +154,7 @@ void print_header(FILE *output_file, mcd *mcd_struct)
for(i=0; i < dico_label->nbelem; i++){ for(i=0; i < dico_label->nbelem; i++){
fprintf(output_file, " %s", dico_label->array[i]); fprintf(output_file, " %s", dico_label->array[i]);
} }
fprintf(output_file, "\",\n"); fprintf(output_file, "\"\n");
fprintf(output_file, "},\n"); fprintf(output_file, "},\n");
...@@ -163,6 +163,8 @@ void print_header(FILE *output_file, mcd *mcd_struct) ...@@ -163,6 +163,8 @@ void print_header(FILE *output_file, mcd *mcd_struct)
fprintf(output_file, "\"time_start\": \"\",\n"); fprintf(output_file, "\"time_start\": \"\",\n");
fprintf(output_file, "\"time_end\": \"\"\n"); fprintf(output_file, "\"time_end\": \"\"\n");
fprintf(output_file, "},\n"); fprintf(output_file, "},\n");
fprintf(output_file, "\"documents\": [");
} }
void print_link(FILE *output_file, word *w, int index, int gov_col, int label_col) void print_link(FILE *output_file, word *w, int index, int gov_col, int label_col)
...@@ -332,7 +334,6 @@ int main(int argc, char *argv[]) ...@@ -332,7 +334,6 @@ int main(int argc, char *argv[])
wb = word_buffer_load_mcf(ctx->mcf_filename, ctx->mcd_struct); wb = word_buffer_load_mcf(ctx->mcf_filename, ctx->mcd_struct);
print_header(output_file, ctx->mcd_struct); print_header(output_file, ctx->mcd_struct);
fprintf(output_file, "\"documents\": [");
do{ do{
w = word_buffer_b0(wb); w = word_buffer_b0(wb);
if(new_sentence){ if(new_sentence){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment