]> matita.cs.unibo.it Git - helm.git/blob - helm/metadata/extractor/meta_lex_ind.l
1. change_tac moved from PrimitiveTactics to ReductionTactics
[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 = 1;}
160                     else if (position == INHYP) no_open_source++;
161                    }
162
163 "</decl>"          |
164 "</def>"           {
165                     if (position == INHYP)
166                      {
167                       no_open_source--;
168                       if (no_open_source == 0) 
169                         {
170                          position = MAINHYP;
171                          depth++;
172                          first_child = HERE;
173                         }
174                      }
175                     else if (position == MAINHYP)
176                       {
177                        position = INTYPE;
178                        spine_depth++;
179                        depth = 0;
180                        first_child = HERE;
181                       }
182                    }
183
184
185 .|\n               {
186                    }
187
188 "<LAMBDA"          |
189 "<MUTCASE"         |
190 "<FIX"             |
191 "<COFIX"           { 
192                           first_child = AFTER;
193                    }
194
195 "<REL"             {
196                     if (((position == INTYPE) | (position == MAINHYP)) &&
197                        (first_child == HERE))
198                      {
199                        if (position == INTYPE) /* REL on the spine */
200                          {
201                            position = INCONCL;
202                            search("Rel",first_child,position,spine_depth);
203                          }
204                        else search("Rel",first_child,position,depth);
205                        first_child = AFTER;
206                      }
207                    }
208
209 "<SORT"(" "|\n)+"value=\""{value}   {         
210                     if (((position == INTYPE) | (position == MAINHYP)) &&
211                        (first_child == HERE))
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 "name=\""{id} {
306                  if (waiting_for_name == 1) {
307                     waiting_for_name = 0;
308                     strsep(&yytext,&sep);
309                     tmp=(char *)malloc((sizeof(sep)*(strlen(yytext)+1)));
310                     strcpy(tmp,yytext);
311                     print_name(tmp,source_uri);
312                     free(tmp);
313                  }
314               } 
315
316
317 %%
318
319  /***************************************************************/
320  /* 6. Auxiliary functions.                                     */
321  /***************************************************************/
322
323 int main(int argc, char *argv[])
324 {
325     /* FILE *debug; */
326
327     if (argc != 3)
328     {
329         fprintf(stderr, "Usage: meta_ind <object_uri> <inductive_type_file>\n");
330         exit(1);
331     }
332
333     source_uri = malloc((sizeof('a')*2000));
334     source_uri_prefix=argv[1];
335     /* fprintf(stderr,"qua"); */
336     yyin = fopen(argv[2], "r");
337     yylex();
338
339     return 0;
340 }
341
342 void search(uri,first_child,position,depth)
343 char               *uri;
344 int                first_child;
345 int                position; 
346 {                  
347                    if (position == MAINHYP)
348                       { 
349                        if (first_child == HERE) 
350                            found = search_bucket(uri,MAINHYP,depth);
351                        else 
352                            found = search_bucket(uri,INHYP,0);
353                       }
354                    else if (position == INCONCL)
355                       { 
356                        if (first_child == HERE) 
357                            found = search_bucket(uri,MAINCONCL,depth);
358                        else
359                            found = search_bucket(uri,INCONCL,0);
360                       }
361                         
362                    else 
363                       found = search_bucket(uri,position,depth);
364                    /* if (found == NOTFOUND)
365                          printf( "pos = %d, uri = %s\n", position, uri);  */
366 }
367
368
369 int yywrap() {
370                return 1;
371              }
372
373
374
375