]> matita.cs.unibo.it Git - helm.git/blob - helm/metadata/create_V7_mowgli/METADATA/meta_lex_ind.l
a7a62666f64219d6ea4ea293935f50645e2682e2
[helm.git] / helm / metadata / create_V7_mowgli / METADATA / meta_lex_ind.l
1  /******************************************************************/
2  /*  Copyright (C) 2000, HELM Team                                 */ 
3  /*                                                                */
4  /* This file is part of HELM, an Hypertextual, Electronic         */
5  /* Library of Mathematics, developed at the Computer Science      */
6  /* Department, University of Bologna, Italy.                      */
7  /*                                                                */
8  /* HELM is free software; you can redistribute it and/or          */
9  /* modify it under the terms of the GNU General Public License    */
10  /* as published by the Free Software Foundation; either version   */
11  /* 2 of the License, or (at your option) any later version.       */
12  /*                                                                */
13  /* HELM is distributed in the hope that it will be useful,        */
14  /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15  /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the   */
16  /* GNU General Public License for more details.                   */
17  /*                                                                */
18  /* You should have received a copy of the GNU General Public      */
19  /* License along with HELM; if not, write to the Free Software    */
20  /* Foundation, Inc., 59 Temple Place - Suite 330, Boston,         */
21  /* MA  02111-1307, USA.                                           */
22  /*                                                                */
23  /* For details, see the HELM World-Wide-Web page,                 */
24  /* http://cs.unibo.it/helm/.                                      */
25  /******************************************************************/
26
27  /***************************************************************/
28  /*                        META_LEXAN                           */
29  /*                 Automatic Metadata Extractor                */
30  /*           First draft 11/12/2001, by Andrea Asperti         */
31  /*      more bugs added by domenico lordi on mon 12/17/2001    */
32  /***************************************************************/
33
34  /***************************************************************/
35  /* 1. Inclusion of header files.                               */
36  /***************************************************************/
37
38 %{
39 #include                <string.h>
40 #include                <stdlib.h>
41 #include                <sys/stat.h>
42 #include                "sthandler_ind.h"
43 %}
44
45  /***************************************************************/
46  /* 2. Constants and Variables Definitions                      */
47  /***************************************************************/
48
49 %{
50 #define                 NOWHERE   0
51 #define                 CONST     1
52 #define                 MUTIND    2
53 #define                 MUTCONSTRUCT  3
54 #define                 SORT      4
55
56 #define                 INBODY    0
57 #define                 MAINHYP   1
58 #define                 INHYP     2
59 #define                 INCONCL   3
60 #define                 MAINCONCL 4
61 #define                 INTYPE    5
62 #define                 NOTFOUND  6
63
64 #define                 HERE      0     
65 #define                 AFTER     1
66
67
68 int                     where = NOWHERE;
69 int                     found = NOTFOUND;
70 int                     position = INBODY;
71 int                     first_child = HERE;
72 int                     skip = 0;     // boolean to skip the insertion of a URI
73 int                     no_open_source =0;
74 int                     spine_depth = 0;
75 int                     depth = 0;
76 int                     tmp_n;
77 int                     inductive_type = 0;
78 int                     constructor = 0;
79 int                     deep_type = 0;
80 char                    sep = '"';
81 char                    *xpointer = "#xpointer(1/";
82 char                    *uri;
83 char                    *tmp;
84 char                    *filename;
85 char                    *file_uri; 
86 char                    *inductive_uri;
87 char                    *filename_prefix;
88 char                    *file_uri_prefix;
89 %}
90
91  /***************************************************************/
92  /* 3. Regular definitions.                                     */
93  /***************************************************************/
94
95 uri                     [^"]+
96 digits                  [0-9]+ 
97 value                   [^"]+                  
98
99  /***************************************************************/
100  /* 4. Rules.                                                   */
101  /***************************************************************/
102
103
104 %%
105
106 "<InductiveType"   { 
107                      /* fprintf(stderr,"uno"); */
108                      init_symbol_table();
109                      no_open_source = 0;
110                      depth = 0;
111                      spine_depth = 0;
112                      /* fprintf(stderr,"due"); */
113                      inductive_type++;
114                      constructor=0;
115                      position = INTYPE;
116                      first_child = HERE;
117                    }
118
119 "</arity>"         { tmp = (char *)malloc(sizeof('a')*128);
120                      strcpy(filename,filename_prefix);
121                      /* fprintf(stderr,"tre"); */
122                      strcpy(file_uri,file_uri_prefix);
123                      sprintf(tmp,",%d.xml", inductive_type);
124                      /* fprintf(stderr,"quattro"); */
125                      strcat(filename,tmp);
126                      sprintf(tmp,"#xpointer(1/%d)", inductive_type);
127                      strcat(file_uri,tmp);
128                      /* fprintf(stderr,"cinque"); */
129                      free(tmp);
130                      print_file();
131                    }
132
133 "<Constructor"     { init_symbol_table();
134                      no_open_source = 0;
135                      depth = 0;
136                      spine_depth = 0;
137                      constructor++;
138                      strcpy(inductive_uri,file_uri_prefix);
139                      position = INTYPE;
140                      first_child = HERE;
141                    }
142
143 "</Constructor>"   { tmp = (char *)malloc(sizeof('a')*128);
144                      strcpy(filename,filename_prefix);
145                      strcpy(file_uri,file_uri_prefix);
146                      strcpy(inductive_uri,file_uri_prefix);
147                      sprintf(tmp,",%d,%d.xml", inductive_type,constructor);
148                      strcat(filename,tmp);
149                      sprintf(tmp,"#xpointer(1/%d/%d)",inductive_type,constructor);
150                      strcat(file_uri,tmp);
151                      free(tmp);
152                      print_file();
153                    }
154
155 "<decl"            |
156 "<def"             {
157                     if (position == INTYPE)
158                        position = MAINHYP;
159                     else if (position == MAINHYP)
160                         { position = INHYP;
161                           no_open_source++;};
162                    }
163
164 "</decl>"          |
165 "</def"            {
166                     if (position == INHYP)
167                      {
168                       no_open_source--;
169                       if (no_open_source == 0) 
170                         {
171                          position = MAINHYP;
172                          depth++;
173                          first_child = HERE;
174                         }
175                      }
176                     else if (position == MAINHYP)
177                       {
178                        position = INTYPE;
179                        spine_depth++;
180                        depth = 0;
181                       }
182                     first_child = HERE;
183                    }
184
185
186 .|\n               {
187                    }
188
189 "<LAMBDA"          |
190 "<MUTCASE"         |
191 "<FIX"             |
192 "<COFIX"           { 
193                           first_child = AFTER;
194                    }
195
196 "<REL"             {
197                     if (((position == INTYPE) | (position == MAINHYP)) &&
198                        (first_child == HERE))
199                      {
200                        if (position == INTYPE) /* REL on the spine */
201                          {
202                            position = INCONCL;
203                            search("Rel",first_child,position,spine_depth);
204                          }
205                        else search("Rel",first_child,position,depth);
206                        first_child = AFTER;
207                      }
208                    }
209
210 "<SORT"(" "|\n)+"value=\""{value}   {         
211                     if ((position == INTYPE) | (position == MAINHYP))
212                      {
213                        tmp=(char *)malloc((sizeof('a')*200)); 
214                        strcpy(tmp,yytext);
215                        strsep(&tmp,&sep); 
216                        if (position == INTYPE) /* SORT on the spine */
217                          { 
218                            position = INCONCL;
219                            search(tmp,first_child,position,spine_depth);
220                          }
221                        else search(tmp,first_child,position,depth);
222                        first_child = AFTER;
223                      }
224                    }
225
226 "<VAR"             {
227                      skip = 1;
228                      first_child = AFTER;
229                    }
230
231 "<CONST"           { 
232                      if (position == INTYPE) /* CONST on the spine */
233                         position = INCONCL;
234                      where = CONST;
235                    }
236
237 "<MUTIND"          { 
238                      if (position == INTYPE) /* MUTIND on the spine */
239                         position = INCONCL;
240                      where = MUTIND;
241                    }
242
243 "<MUTCONSTRUCT"    { 
244                      if (position == INTYPE) /* MUTCONSTRUCT on the spine */
245                         position = INCONCL;
246                      where = MUTCONSTRUCT;
247                    }
248
249 "uri=\""{uri}      {     
250                          if (!skip) {
251                             uri=(char *)malloc((sizeof('a')*200)); 
252                             strcpy(uri,yytext);
253                             strsep(&uri,&sep);
254                             if (where == CONST)
255                               {
256                                 if (position == INCONCL)
257                                   search(uri,first_child,position,spine_depth);
258                                 else search(uri,first_child,position,depth);
259                                 where = NOWHERE;
260                                 first_child = AFTER;
261                                 free(uri); 
262                               };
263                          } else skip = 0;
264                    } 
265
266 "noType=\""{digits} {
267                          if ((where == MUTIND) || (where == MUTCONSTRUCT))
268                           { strsep(&yytext,&sep);
269                             tmp=(char *)malloc((sizeof(sep)*(strlen(yytext)+1)));
270                             strcpy(tmp,yytext);
271                             tmp_n = atoi(tmp)+1;
272                             sprintf(tmp,"%d",tmp_n);
273                             strcat(uri,"#xpointer(1/"); 
274                             strcat(uri,tmp); 
275                           };
276                          if (where == MUTIND) 
277                              { 
278                                strcat(uri,")");
279                                if (position == INCONCL)
280                                   search(uri,first_child,position,spine_depth);
281                                else search(uri,first_child,position,depth);
282                                free(uri);
283                                free(tmp);
284                                where = NOWHERE; 
285                                first_child = AFTER;};
286                    } 
287
288 "noConstr=\""{digits} {
289                          if (where == MUTCONSTRUCT)
290                           { strsep(&yytext,&sep);
291                             tmp=(char *)malloc((sizeof(sep)*(strlen(yytext)+1)));
292                             strcpy(tmp,yytext);
293                             strcat(uri,"/");
294                             strcat(uri,tmp);
295                             strcat(uri,")");
296                             if (position == INCONCL)
297                               search(uri,first_child,position,spine_depth);
298                             else search(uri,first_child,position,depth);
299                             free(uri);
300                             free(tmp);
301                             where = NOWHERE; 
302                             first_child = AFTER;};
303                    } 
304
305
306
307 %%
308
309  /***************************************************************/
310  /* 6. Auxiliary functions.                                     */
311  /***************************************************************/
312
313 main(int argc, char *argv[])
314 {
315                    filename = malloc((sizeof('a')*2000));
316                    file_uri = malloc((sizeof('a')*2000));
317                    inductive_uri = malloc((sizeof('a')*2000));
318                    filename_prefix=argv[1];
319                    file_uri_prefix=argv[2];
320                    /* fprintf(stderr,"qua"); */
321                    yyin = fopen("tmp/inductive_type.xml", "r");
322                    yylex();
323 }
324
325 print_file()
326 {                  
327                    FILE *out, *outrel, *outsort;
328
329                    if (!(out = fopen(filename,"w"))) 
330                      {
331                       fprintf(stderr, "error in openinf file %s\n", filename);
332                       exit(-1);
333                      } 
334                    if (!(outrel = fopen("forward_rel.rdf","a"))) 
335                      {
336                       fprintf(stderr, "error in openinf file forward_rel.rdf\n");
337                       exit(-1);
338                      }
339                    if (!(outsort = fopen("forward_sort.rdf","a"))) 
340                      {
341                       fprintf(stderr, "error in openinf file forward_rel.rdf\n");
342                       exit(-1);
343                      }
344                  
345                    // We process the type
346
347                    fprintf(out,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n");
348 fprintf(out,"<!DOCTYPE rdf:RDF [
349         <!ENTITY rdfns 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
350         <!ENTITY hthns 'http://www.cs.unibo.it/helm/schemas/schema-helmth#'>
351         <!ENTITY hns 'http://www.cs.unibo.it/helm/schemas/schema-helm#'>
352
353    ]>\n");
354                    fprintf(out,"<rdf:RDF xml:lang=\"en\" xmlns:rdf=\"&rdfns;\" xmlns:h=\"&hns;\" xmlns:hth=\"&hthns;\">\n");
355                    fprintf(out,"\t<h:Object rdf:about=\"");
356                    fprintf(out,"%s",file_uri);
357                    fprintf(out,"\">\n");
358                    print_all(file_uri,out,outrel,outsort);
359                    fprintf(out,"\t</h:Object>\n");
360                    fprintf(out,"</rdf:RDF>\n");
361                    fclose(out);
362                    fclose(outrel);
363                    fclose(outsort);
364 }
365
366 search(uri,first_child,position,depth)
367 char               *uri;
368 int                first_child;
369 int                position; 
370 {                  
371                    if (position == MAINHYP)
372                       { 
373                        if (first_child == HERE) 
374                            found = search_bucket(uri,MAINHYP,depth);
375                        else 
376                            found = search_bucket(uri,INHYP,0);
377                       }
378                    else if (position == INCONCL)
379                       { 
380                        if (first_child == HERE) 
381                            found = search_bucket(uri,MAINCONCL,depth);
382                        else
383                            found = search_bucket(uri,INCONCL,0);
384                       }
385                         
386                    else 
387                       found = search_bucket(uri,position,depth);
388                    /* if (found == NOTFOUND)
389                          printf( "pos = %d, uri = %s\n", position, uri); */
390 }
391
392
393 int yywrap() {
394                return 1;
395              }
396
397
398
399