sxatcpp.c

Go to the documentation of this file.
00001 /******************************************************************************
00002  *                                S Y N T A X
00003  *-----------------------------------------------------------------------------
00004  *   Copyright (C) 1972-2008 INRIA (Institut National de Recherche en
00005  *   Informatique et Automatique)
00006  *-----------------------------------------------------------------------------
00007  *   URL: http://syntax.gforge.inria.fr
00008  *-----------------------------------------------------------------------------
00009  *   The source code of SYNTAX is distributed with two different licenses,
00010  *   depending on the files:
00011  *   - The recursive content of src/ and incl/ and the non-recursive content
00012  *     of SYNTAX's root directory are distributed under the CeCILL-C license
00013  *   - The recursive content of all other repertories is distributed under
00014  *     the CeCILL license
00015  *   All code produced by SYNTAX must be considered as being under the
00016  *   CeCILL-C license. Information about the CeCILL and CeCILL-C licenses
00017  *   can be found at, e.g., http://www.cecill.fr
00018  *****************************************************************************/
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #include "sxversion.h"
00031 #include "sxunix.h"
00032 
00033 char WHAT_SXATCPP[] = "@(#)SYNTAX - $Id: sxatcpp.c 1711 2009-03-11 14:56:21Z sagot $" WHAT_DEBUG;
00034 
00035 /* Gestion dans le tas : */
00036 
00037 static SXINT    stack_size;
00038 struct tree_node_stack {
00039     struct sxnode_pp    *head, *tail;
00040 };
00041 static struct tree_node_stack   *tree_node_stack /* 1:stack_size */ ;
00042 
00043 #define allocate_node() (node == area_end ? allocate_area () : ++node)
00044 
00045 #ifndef NBNODES
00046 # define NBNODES 128
00047 #endif
00048 
00049 #ifdef SXRESIZE
00050   /* Increase area_size, to minimize number of allocations */
00051 # define NEW_AREA_SIZE (area_size += NBNODES)
00052 #else
00053   /* Do not grow area_size, as it cannot be reallocated */
00054 # define NEW_AREA_SIZE (area_size = NBNODES)
00055 #endif
00056 
00057 struct sxatcpp_area {
00058     struct sxatcpp_area *next;
00059     struct sxnode_pp    zone [1];
00060 };
00061 static long area_size;
00062 static struct sxatcpp_area  *area;
00063 static struct sxnode_pp *area_end, *node;
00064 
00065 
00066 
00067 static struct sxnode_pp *allocate_area (void)
00068 {
00069     struct sxatcpp_area *old, *new;
00070 
00071     old = area;
00072     area = new = (struct sxatcpp_area*) sxcalloc (1, sizeof (struct sxatcpp_area) + NEW_AREA_SIZE * sizeof (struct sxnode_pp));
00073     new->next = old;
00074     area_end = (node = new->zone) + area_size;
00075     return node;
00076 }
00077 
00078 
00079 
00080 static SXVOID   free_areas (void)
00081 {
00082     struct sxatcpp_area *prev, *next;
00083     next = area;
00084 
00085     while ((prev = next) != NULL) {
00086     next = next->next;
00087     sxfree (prev);
00088     }
00089 }
00090 
00091 
00092 
00093 static struct sxnode_pp *father_ptr, *prv_ptr;
00094 static char *com_ref;
00095 
00096 
00097 
00098 
00099 #define cons_com(c1,c2) (c1==NULL?c2:(c2==NULL?c1:string_cat(c1,c2)))
00100 
00101 static char *string_cat (char *s1, char *s2)
00102 {
00103     char    *t1, *t2;
00104     size_t  l1;
00105 
00106     s1 = (char*) sxrealloc (s1, (l1 = strlen (s1)) + strlen (s2) + 1, sizeof (char));
00107     t1 = s1 + l1, t2 = s2;
00108 
00109     while ((*t1++ = *t2++) != SXNUL) {
00110     /* null; */
00111     }
00112 
00113     sxfree (s2);
00114     return s1;
00115 }
00116 
00117 
00118 
00119 static SXVOID   initialize (void)
00120 {
00121     stack_size = 128;
00122     area_size = 0;
00123     area = NULL;
00124     node = area_end = NULL;
00125     father_ptr = NULL;
00126     tree_node_stack = (struct tree_node_stack*) sxalloc (stack_size + 1, sizeof (struct tree_node_stack));
00127 }
00128 
00129 
00130 
00131 static SXVOID   finalize (void)
00132 {
00133 /* le commentaire eventuel se trouvant en fin de  */
00134 /* programme devient le post_comment de la racine */
00135     father_ptr->post_comment = cons_com (father_ptr->post_comment, sxsvar.sxlv.terminal_token.comment);
00136 
00137 #ifdef SXRESIZE
00138     if (area != NULL) {
00139     SXRESIZE (area, 1, sizeof (struct sxatcpp_area) + (area_size = (area_end = node) - area->zone) * sizeof (struct sxnode_pp));
00140     }
00141 #endif
00142 
00143     sxfree (tree_node_stack);
00144 }
00145 
00146 
00147 
00148 static SXVOID   action (void)
00149 {
00150     SXINT   top, new_top, xs;
00151     struct sxtoken  *token;
00152     struct sxnode_pp    *brother, *son, *father;
00153     SXBOOLEAN   is_first = SXTRUE;
00154 
00155     top = SXSTACKtop ();
00156     new_top = SXSTACKnewtop ();
00157 
00158     if (top >= stack_size)
00159     tree_node_stack = (struct tree_node_stack*) sxrealloc (tree_node_stack, (stack_size *= 2) + 1, sizeof (struct
00160          tree_node_stack));
00161 
00162     son = father = allocate_node ();
00163     father->name = SXSTACKreduce ();
00164 
00165 /* Initialisation: on met a priori le source_index du look_ahead */
00166 
00167     father->terminal_token.source_index = SXGET_TOKEN (sxplocals.atok_no).source_index;
00168     com_ref = NULL;
00169     prv_ptr = NULL;
00170 
00171     for (xs = new_top; xs <= top; xs++) {
00172     token = &(SXSTACKtoken (xs));
00173 
00174     if (token->lahead >= 0 /* nt or t */ ) {
00175         if (is_first)
00176         father->terminal_token.source_index = token->source_index;
00177 
00178         if (token->lahead == 0) {
00179         /* non-terminal */
00180         brother = son->next = tree_node_stack [xs].head;
00181         son = tree_node_stack [xs].tail;
00182         }
00183         else if (sxgenericp (sxplocals.sxtables, token->lahead)) {
00184         son = son->next = brother = allocate_node ();
00185         brother->terminal_token = *token;
00186         brother->name = -token->lahead;
00187         }
00188         else
00189         brother = NULL;
00190 
00191         if (brother != NULL) {
00192         brother->terminal_token.comment = cons_com (com_ref, brother->terminal_token.comment);
00193         com_ref = NULL;
00194         }
00195         else if (is_first)
00196         father->terminal_token.comment = token->comment;
00197         else if (prv_ptr != NULL)
00198         prv_ptr->post_comment = cons_com (prv_ptr->post_comment, token->comment);
00199         else
00200         com_ref = cons_com (com_ref, token->comment);
00201 
00202         prv_ptr = brother;
00203         is_first = SXFALSE;
00204     }
00205     }
00206 
00207     father->post_comment = com_ref;
00208     tree_node_stack [new_top].head = father_ptr = father;
00209     tree_node_stack [new_top].tail = son;
00210 }
00211 
00212 
00213 
00214 static SXVOID   error (void)
00215 {
00216     father_ptr = allocate_node ();
00217     father_ptr->name = 0 /* error node */ ;
00218     tree_node_stack [SXSTACKnewtop ()].head = tree_node_stack [SXSTACKnewtop ()].tail = father_ptr;
00219 }
00220 
00221 
00222 
00223 SXVOID  sxatcpp (SXINT sxatcpp_what, struct sxtables *arg /* or action number */)
00224 {
00225     switch (sxatcpp_what) {
00226     case SXOPEN:
00227     sxcheck_magic_number (arg->magic, SXMAGIC_NUMBER, "atcpp");
00228 
00229     case SXCLOSE:
00230     sxppp (sxatcpp_what, arg);
00231     break;
00232 
00233     case SXINIT:
00234     initialize ();
00235     break;
00236 
00237     case SXFINAL:
00238     finalize ();
00239     break;
00240 
00241     case SXACTION:
00242     if ((long) arg != 0) {  /* 0 when simple reduction */
00243         action ();
00244     }
00245     break;
00246 
00247     case SXSEMPASS:
00248         if (sxerrmngr.nbmess [2] != 0)
00249       break;
00250 
00251     if (sxverbosep) {
00252         fputs ("Pretty Printer ", sxtty);
00253     }
00254 
00255     /* dans le cas ou sxppp() est appelee avec un premier parametre valant
00256         * SXACTION, le deuxieme parametre, formellement declare avec le type
00257         * "(struct sxtables *)", doit etre de type "(struct sxnode_pp *)" en
00258         * realite : l'interieur de la fonction sxppp() contient un cast dans
00259         * ce cas */
00260     sxppp (SXACTION, (struct sxtables *) father_ptr);
00261     free_areas ();
00262     break;
00263 
00264     case SXERROR:
00265     error ();
00266     break;
00267 
00268     default:
00269     fprintf (sxstderr, "The function \"sxatcpp\" is out of date with respect to its specification.\n");
00270     sxexit(1);
00271     }
00272 }

Generated on Wed Apr 21 16:39:34 2010 for syntax-6.0b7 by  doxygen 1.6.1