From 5e7ff22cf35cad138fa7b5d71ed3ba5f2aaf8ae8 Mon Sep 17 00:00:00 2001
From: Alexis Nasr <alexis.nasr@lif.univ-mrs.fr>
Date: Sun, 3 Dec 2017 10:29:54 +0100
Subject: [PATCH] code refactoring in json_parser

---
 maca_common/include/json_parser.h |  16 ++---
 maca_common/src/json_parser.c     | 103 +++++-------------------------
 2 files changed, 25 insertions(+), 94 deletions(-)

diff --git a/maca_common/include/json_parser.h b/maca_common/include/json_parser.h
index 6b4814d..82664e5 100644
--- a/maca_common/include/json_parser.h
+++ b/maca_common/include/json_parser.h
@@ -7,16 +7,16 @@
 #define EPSILON 0
 
 /* symboles non terminaux */
-#define NB_NON_TERMINAUX 8
+#define NB_NON_TERMINAUX 6
 
 #define _structure_ 1
-#define _list_ 2
-#define _object_ 3 
-#define _list_structure_ 4 
-#define _list_structure2_ 5
-#define _attr_val_ 6
-#define _list_attr_val_ 7 
-#define _list_attr_val2_ 8
+#define _list_structure_ 2 
+#define _list_structure2_ 3
+#define _attr_val_ 4
+#define _list_attr_val_ 5
+#define _list_attr_val2_ 6
+//#define _list_ 2
+//#define _object_ 3 
 
 /* symboles terminaux */
 #define NB_TERMINAUX 10
diff --git a/maca_common/src/json_parser.c b/maca_common/src/json_parser.c
index 6f8875a..f1b7fd4 100644
--- a/maca_common/src/json_parser.c
+++ b/maca_common/src/json_parser.c
@@ -14,8 +14,6 @@
 #define is_alpha(c)(is_maj(c) || is_min(c) || (c) == '_' || (c) == '$')
 #define is_alphanum(c)(is_num((c)) || is_alpha((c)))
 
-
-
 void initialise_premiers(json_parser_ctx *ctx);
 void initialise_suivants(json_parser_ctx *ctx);
 int yylex(json_parser_ctx *ctx);
@@ -23,15 +21,15 @@ int yylex(json_parser_ctx *ctx);
 
 json_struct *json_parse(char *filename)
 {
-  json_parser_ctx *ctx=json_parser_init(filename);
-  return structure(ctx);
+  return json_parse_full(filename, 0);
 }
 
 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 */
+  fclose(ctx->yyin);
+  free(ctx);
   return root;
 }
 
@@ -311,8 +309,6 @@ void initialise_premiers(json_parser_ctx *ctx){
   ctx->premiers[_structure_][CROCHET_OUVRANT] = 1;
   ctx->premiers[_structure_][ACCOLADE_OUVRANTE] = 1;
 
-  ctx->premiers[_list_][CROCHET_OUVRANT] = 1;
-
   ctx->premiers[_list_structure_][EPSILON] = 1;
   ctx->premiers[_list_structure_][CONSTANT] = 1;
   ctx->premiers[_list_structure_][STRING] = 1;
@@ -323,8 +319,6 @@ void initialise_premiers(json_parser_ctx *ctx){
   ctx->premiers[_list_structure2_][EPSILON] = 1;
   ctx->premiers[_list_structure2_][VIRGULE] = 1;
 
-  ctx->premiers[_object_][ACCOLADE_OUVRANTE] = 1;
-
   ctx->premiers[_list_attr_val_][EPSILON] = 1;
   ctx->premiers[_list_attr_val_][STRING] = 1;
 
@@ -337,10 +331,8 @@ void initialise_premiers(json_parser_ctx *ctx){
 
 /* ################################################################
 Follow(structure) =  '}'  ']'  '.'  ',' 
-Follow(list) =  ']'  '}'  '.'  ',' 
 Follow(list_structure) =  ']' 
 Follow(list_structure2) =  ']' 
-Follow(object) =  ']'  '}'  '.'  ',' 
 Follow(list_attr_val) =  '}' 
 Follow(list_attr_val2) =  '}' 
 Follow(attr_val) =  '}'  ',' 
@@ -357,18 +349,10 @@ void initialise_suivants(json_parser_ctx *ctx){
   ctx->suivants[_structure_][CROCHET_FERMANT] = 1;
   ctx->suivants[_structure_][VIRGULE] = 1;
 
-  ctx->suivants[_list_][CROCHET_FERMANT] = 1;
-  ctx->suivants[_list_][ACCOLADE_FERMANTE] = 1;
-  ctx->suivants[_list_][VIRGULE] = 1;
-
   ctx->suivants[_list_structure_][CROCHET_FERMANT] = 1;
 
   ctx->suivants[_list_structure2_][CROCHET_FERMANT] = 1;
 
-  ctx->suivants[_object_][CROCHET_FERMANT] = 1;
-  ctx->suivants[_object_][ACCOLADE_FERMANTE] = 1;
-  ctx->suivants[_object_][VIRGULE] = 1;
-
   ctx->suivants[_list_attr_val_][ACCOLADE_FERMANTE] = 1;
 
   ctx->suivants[_list_attr_val2_][ACCOLADE_FERMANTE] = 1;
@@ -377,45 +361,16 @@ void initialise_suivants(json_parser_ctx *ctx){
   ctx->suivants[_attr_val_][VIRGULE] = 1;
 }
 
-
-/*
-
-list, '[' =  list -> '[' list_structure ']'  
-
-list_structure, STRING =  list_structure -> structure list_structure2  
-list_structure, NUMBER =  list_structure -> structure list_structure2  
-list_structure, CONSTANT =  list_structure -> structure list_structure2  
-list_structure, '[' =  list_structure -> structure list_structure2  
-list_structure, ']' =  list_structure -> ε  
-list_structure, '{' =  list_structure -> structure list_structure2  
-
-list_structure2, ']' =  list_structure2 -> ε  
-list_structure2, ',' =  list_structure2 -> ',' structure  
-
-object, '{' =  object -> '{' list_attr_val '}'  
-
-list_attr_val, '}' =  list_attr_val -> ε  
-list_attr_val, STRING =  list_attr_val -> attr_val list_attr_val2  
-
-list_attr_val2, '}' =  list_attr_val2 -> ε  
-list_attr_val2, ',' =  list_attr_val2 -> ',' attr_val  
-
-attr_val, STRING =  attr_val -> STRING ':' structure  
-*/
-
-
 json_struct *structure(json_parser_ctx *ctx);
-
 json_struct *list (json_parser_ctx *ctx);
 json_struct *list_structure(json_parser_ctx *ctx);
 json_struct *list_structure2(json_parser_ctx *ctx);
 json_attr_val *list_attr_val(json_parser_ctx *ctx);
 json_attr_val *list_attr_val2(json_parser_ctx *ctx);
-json_attr_val *object(json_parser_ctx *ctx);
 json_attr_val *attr_val(json_parser_ctx *ctx);
 
 /*---------------------------------------------------------*/
-/* structure -> list | object | STRING | NUMBER | CONSTANT */
+/* structure -> '[' list_structure ']' | '{' list_attr_val '}' | STRING | NUMBER | CONSTANT */
 /*---------------------------------------------------------*/
 json_struct *structure(json_parser_ctx *ctx)
 {
@@ -424,18 +379,22 @@ json_struct *structure(json_parser_ctx *ctx)
   int constant;
   affiche_balise_ouvrante(ctx, __FUNCTION__);
 
-  if(est_premier(ctx, ctx->uc, _list_)) {
-    json_struct *s = list(ctx);
+  if(ctx->uc == CROCHET_OUVRANT){
+    consommer(ctx, CROCHET_OUVRANT);
+    json_struct *s = list_structure(ctx);
+    consommer(ctx, CROCHET_FERMANT);
     affiche_balise_fermante(ctx, __FUNCTION__);
-    return s;
+    return json_new_list(s);
   }
-
-  if(est_premier(ctx, ctx->uc, _object_)) {
-    json_attr_val *avl = object(ctx);
+  
+  if(ctx->uc == ACCOLADE_OUVRANTE){
+    consommer(ctx, ACCOLADE_OUVRANTE);
+    json_attr_val *l = list_attr_val(ctx);
+    consommer(ctx, ACCOLADE_FERMANTE);
     affiche_balise_fermante(ctx, __FUNCTION__);
-    return json_new_avl(avl);
+    return json_new_avl(l);
   }
-
+  
   if(ctx->uc == STRING){
     string = (ctx->yyleng == 0)? NULL : strdup(ctx->yytext);
     consommer(ctx, STRING);
@@ -463,21 +422,6 @@ json_struct *structure(json_parser_ctx *ctx)
   return NULL;
 }
 
-/*---------------------------------------------------------*/
-/* list -> '[' list_structure ']'*/
-/*---------------------------------------------------------*/
-
-json_struct *list (json_parser_ctx *ctx)
-{
-  affiche_balise_ouvrante(ctx, __FUNCTION__);
-  consommer(ctx, CROCHET_OUVRANT);
-  json_struct *s = list_structure(ctx);
-  consommer(ctx, CROCHET_FERMANT);
-  affiche_balise_fermante(ctx, __FUNCTION__);
-  return json_new_list(s);
-  erreur(ctx, (char *)"");
-}
-
 /*---------------------------------------------------------*/
 /* list_structure -> structure list_structure2 | epsilon */
 /*---------------------------------------------------------*/
@@ -521,20 +465,7 @@ json_struct *list_structure2(json_parser_ctx *ctx)
     return NULL;
   }
   erreur(ctx, (char *)"");
-}
-
-/*---------------------------------------------------------*/
-/* object -> '{' list_attr_val '}' */
-/*---------------------------------------------------------*/
-
-json_attr_val *object(json_parser_ctx *ctx)
-{
-  affiche_balise_ouvrante(ctx, __FUNCTION__);
-  consommer(ctx, ACCOLADE_OUVRANTE);
-  json_attr_val *l = list_attr_val(ctx);
-  consommer(ctx, ACCOLADE_FERMANTE);
-  affiche_balise_fermante(ctx, __FUNCTION__);
-  return l;
+  return NULL;
 }
 
 /*---------------------------------------------------------*/
-- 
GitLab