]> matita.cs.unibo.it Git - helm.git/blob - helm/metadata/create_V7_mowgli/METADATA/meta_lex.l
Initial revision
[helm.git] / helm / metadata / create_V7_mowgli / METADATA / meta_lex.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.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 char                    sep = '"';
78 char                    *xpointer = "#xpointer(1/";
79 char                    *uri;
80 char                    *tmp;
81 %}
82
83  /***************************************************************/
84  /* 3. Regular definitions.                                     */
85  /***************************************************************/
86
87 uri                     [^"]+
88 digits                  [0-9]+ 
89 value                   [^"]+                  
90
91  /***************************************************************/
92  /* 4. Rules.                                                   */
93  /***************************************************************/
94
95
96 %%
97
98 "<Variable"[^>]*">"(" "|\n)*"<body" {
99                     position = INBODY; // Variables have both a body and a type
100                    }
101
102 "</body>"(" "|\n)*"<type" {
103                     position = INTYPE; // Variables have both a body and a type
104                    }
105
106 "<decl"            |
107 "<def"             {
108                     if (position == INTYPE)
109                        position = MAINHYP;
110                     else if (position == MAINHYP)
111                         { position = INHYP;
112                           no_open_source++;};
113                    }
114
115 "</decl>"          |
116 "</def"            {
117                     if (position == INHYP)
118                      {
119                       no_open_source--;
120                       if (no_open_source == 0) 
121                         {
122                          position = MAINHYP;
123                          depth++;
124                          first_child = HERE;
125                         }
126                      }
127                     else if (position == MAINHYP)
128                       {
129                        position = INTYPE;
130                        spine_depth++;
131                        depth = 0;
132                       }
133                     first_child = HERE;
134                    }
135
136
137 .|\n               {
138                    }
139
140 "<LAMBDA"          |
141 "<MUTCASE"         |
142 "<FIX"             |
143 "<COFIX"           { 
144                           first_child = AFTER;
145                    }
146
147 "<REL"             {
148                     if (((position == INTYPE) | (position == MAINHYP)) &&
149                        (first_child == HERE))
150                      {
151                        if (position == INTYPE) /* REL on the spine */
152                          {
153                            position = INCONCL;
154                            search("Rel",first_child,position,spine_depth);
155                          }
156                        else search("Rel",first_child,position,depth);
157                        first_child = AFTER;
158                      }
159                    }
160
161 "<SORT"(" "|\n)+"value=\""{value}   {         
162                     if (((position == INTYPE) | (position == MAINHYP)) &&
163                        (first_child == HERE))
164                      {
165                        tmp=(char *)malloc((sizeof('a')*200)); 
166                        strcpy(tmp,yytext);
167                        strsep(&tmp,&sep); 
168                        if (position == INTYPE) /* SORT on the spine */
169                          { 
170                            position = INCONCL;
171                            search(tmp,first_child,position,spine_depth);
172                          }
173                        else search(tmp,first_child,position,depth);
174                        first_child = AFTER;
175                      }
176                    }
177
178 "<VAR"             {
179                      skip = 1;
180                      first_child = AFTER;
181                    }
182
183 "<CONST"           { 
184                      if (position == INTYPE) /* CONST on the spine */
185                         position = INCONCL;
186                      where = CONST;
187                    }
188
189 "<MUTIND"          { 
190                      if (position == INTYPE) /* MUTIND on the spine */
191                         position = INCONCL;
192                      where = MUTIND;
193                    }
194
195 "<MUTCONSTRUCT"    { 
196                      if (position == INTYPE) /* MUTCONSTRUCT on the spine */
197                         position = INCONCL;
198                      where = MUTCONSTRUCT;
199                    }
200
201 "uri=\""{uri}      {     
202                          if (!skip) {
203                             uri=(char *)malloc((sizeof('a')*200)); 
204                             strcpy(uri,yytext);
205                             strsep(&uri,&sep);
206                             if (where == CONST)
207                               {
208                                 if (position == INCONCL)
209                                   search(uri,first_child,position,spine_depth);
210                                 else search(uri,first_child,position,depth);
211                                 where = NOWHERE;
212                                 first_child = AFTER;
213                                 free(uri); 
214                               };
215                          } else skip = 0;
216                    } 
217
218 "noType=\""{digits} {
219                          if ((where == MUTIND) || (where == MUTCONSTRUCT))
220                           { strsep(&yytext,&sep);
221                             tmp=(char *)malloc((sizeof(sep)*(strlen(yytext)+1)));
222                             strcpy(tmp,yytext);
223                             tmp_n = atoi(tmp)+1;
224                             sprintf(tmp,"%d",tmp_n);
225                             strcat(uri,"#xpointer(1/"); 
226                             strcat(uri,tmp); 
227                           };
228                          if (where == MUTIND) 
229                              { 
230                                strcat(uri,")");
231                                if (position == INCONCL)
232                                   search(uri,first_child,position,spine_depth);
233                                else search(uri,first_child,position,depth);
234                                free(uri);
235                                free(tmp);
236                                where = NOWHERE; 
237                                first_child = AFTER;};
238                    } 
239
240 "noConstr=\""{digits} {
241                          if (where == MUTCONSTRUCT)
242                           { strsep(&yytext,&sep);
243                             tmp=(char *)malloc((sizeof(sep)*(strlen(yytext)+1)));
244                             strcpy(tmp,yytext);
245                             strcat(uri,"/");
246                             strcat(uri,tmp);
247                             strcat(uri,")");
248                             if (position == INCONCL)
249                               search(uri,first_child,position,spine_depth);
250                             else search(uri,first_child,position,depth);
251                             free(uri);
252                             free(tmp);
253                             where = NOWHERE; 
254                             first_child = AFTER;};
255                    } 
256
257
258
259 %%
260
261  /***************************************************************/
262  /* 6. Auxiliary functions.                                     */
263  /***************************************************************/
264
265 main(int argc, char *argv[])
266 {                  
267                    struct stat buf;
268                    FILE *outrel, *outsort;
269
270                    init_symbol_table();
271                    if (!(outrel = fopen("forward_rel.xml","a"))) 
272                      {
273                       fprintf(stderr, "error in openinf file forward_rel.xml\n");
274                       exit(-1);
275                      }
276                    if (!(outsort = fopen("forward_sort.xml","a"))) 
277                      {
278                       fprintf(stderr, "error in openinf file forward_rel.xml\n");
279                       exit(-1);
280                      }
281                    // We process the body
282                    if (!stat("tmp/body.xml",&buf)) {
283                       yyin = fopen("tmp/body.xml", "r");
284                       position = INBODY;
285                       yylex();
286                       fclose(yyin);
287                    }
288
289                    // We process the type
290                    yyin = fopen("tmp/type.xml", "r");
291                    position = INTYPE;
292                    first_child = HERE;
293                    no_open_source = 0;
294                    spine_depth = 0;
295                    depth = 0;
296                    yylex(); 
297
298                    printf("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n");
299 printf("<!DOCTYPE rdf:RDF [
300         <!ENTITY rdfns 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
301         <!ENTITY hthns 'http://www.cs.unibo.it/helm/schemas/schema-helmth#'>
302         <!ENTITY hns 'http://www.cs.unibo.it/helm/schemas/schema-helm#'>
303
304    ]>\n");
305
306                    printf("<rdf:RDF xml:lang=\"en\" xmlns:rdf=\"&rdfns;\" xmlns:h=\"&hns;\" xmlns:hth=\"&hthns;\">\n");
307                    printf("\t<h:Object rdf:about=\"");
308                    printf("%s",argv[1]);
309                    printf("\">\n");
310                    print_all(argv[1],outrel,outsort);
311                    printf("\t</h:Object>\n");
312                    printf("</rdf:RDF>\n");
313                    fclose(yyin);
314                    } 
315
316 search(uri,first_child,position,depth)
317 char               *uri;
318 int                first_child;
319 int                position; 
320 {                  
321                    if (position == MAINHYP)
322                       { 
323                        if (first_child == HERE) 
324                            found = search_bucket(uri,MAINHYP,depth);
325                        else 
326                            found = search_bucket(uri,INHYP,0);
327                       }
328                    else if (position == INCONCL)
329                       { 
330                        if (first_child == HERE) 
331                            found = search_bucket(uri,MAINCONCL,depth);
332                        else
333                            found = search_bucket(uri,INCONCL,0);
334                       }
335                         
336                    else 
337                       found = search_bucket(uri,position,depth);
338                    /* if (found == NOTFOUND)
339                          fprintf(stderr,"pos = %d, uri = %s\n", position, uri); */
340
341 /*                  
342                       (first_child == HERE) 
343                       {
344                        if (position == MAINHYP)
345                           found = search_bucket(uri,MAINHYP,depth);
346                        else if (position == INCONCL)
347                           found = search_bucket(uri,MAINCONCL,0);
348                        else if (position == INHYP)
349                           found = search_bucket(uri,INHYP,0);
350                           if (found == NOTFOUND)
351                           printf( "pos = %d, uri = %s\n", MAINCONCL, uri); 
352                        }
353                    else if ((position == MAINHYP) && (first_child == AFTER))
354                         found = search_bucket(uri,INHYP,0);
355                    else found = search_bucket(uri,position,0);
356                    if (found == NOTFOUND)
357                          printf( "pos = %d, uri = %s\n", position, uri); 
358                    } */
359
360 int yywrap() {
361                return 1;
362              }
363