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