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