PIPS
spec.l
Go to the documentation of this file.
1 %{
2 /*
3 
4  $Id: spec.l 1381 2018-07-04 04:41:34Z coelho $
5 
6  Copyright 1989-2016 MINES ParisTech
7 
8  This file is part of NewGen.
9 
10  NewGen is free software: you can redistribute it and/or modify it under the
11  terms of the GNU General Public License as published by the Free Software
12  Foundation, either version 3 of the License, or any later version.
13 
14  NewGen is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17  License for more details.
18 
19  You should have received a copy of the GNU General Public License along with
20  NewGen. If not, see <http://www.gnu.org/licenses/>.
21 
22 */
23 /*
24  When a new keyword is added, update the KEYWORDS array in build.c
25 */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <math.h>
31 
32 #include "genC.h"
33 #include "genspec.h"
34 
35 #ifndef yylval
36 #define yylval genspec_lval
37 #endif // yylval
38 
39 extern int Current_start;
40 
41 #ifdef FLEX_SCANNER
42 
43 /* We may parse strings or files...
44  */
45 static char * string_to_parse = (char*) 0; /* shared pointer! */
46 
47 #define YY_INPUT(buffer, result, max_size) \
48  { \
49  int i = 0; \
50  if (string_to_parse) /* we're parsing a string */ \
51  { \
52  while(i < (int) max_size && *string_to_parse) \
53  buffer[i++]=*string_to_parse++; \
54  } \
55  else /* it's a file */ \
56  { \
57  int c; \
58  while (i < (int) max_size && (c=getc(yyin))!=EOF) \
59  buffer[i++]=(char) c; \
60  } \
61  result = i==0? YY_NULL: i; \
62 }
63 
64 void genspec_set_string_to_parse(char *s) { string_to_parse = s;}
65 void genspec_reset_string_to_parse() { string_to_parse = (char *) 0;}
66 
67 #else /* ATT/POSIX just seem to like parsing yyin... */
68 // I guess this code won't be use again anyway... RK
69 void genspec_set_string_to_parse(char *s)
70 {
71  if ((genspec_in = tmpfile()) == NULL) {
72  user( "Cannot create temp spec file in read/write mode\n" ) ;
73  return ;
74  }
75  fprintf( genspec_in , "%s", s ) ;
76 }
77 
78 void genspec_reset_string_to_parse()
79 {
80  // The file created by tmpfile() is automatically deleted on close:
81  fclose(genspec_in);
82 }
83 #endif // FLEX_SCANNER
84 
85 /* cache parsed string instead of strduplicating them
86  * valid because those strign are never released
87  * SG, feb. 2009
88  */
89 #ifndef DISABLE_NEWGEN_PARSER_CACHE
90 
91  #include <search.h>
92  char* strcache(char *s)
93  {
94  static void* rootp = NULL;
95  char ** res = NULL;
96  if( !(res = tfind(s,&rootp,(int(*)(const void*,const void*))strcmp)) )
97  {
98  char * dup = strdup(s);
99  res = tsearch(dup,&rootp,(int(*)(const void*,const void*))strcmp);
100  assert(*res == dup);
101  }
102  return *res;
103  }
104 
105 #else
106 
107  #define strcache strdup
108 
109 #endif
110 
111 %}
112 %option nounput
113 
114 %%
115 external {return GRAM_EXTERNAL ;}
116 import {return GRAM_IMPORT ;}
117 tabulated {return TABULATED ;}
118 from {return FROM ;}
119 persistant {return PERSISTANT ;}
120 persistent {return PERSISTANT ;}
121 \, {return COMMA ;}
122 \: {return COLUMN ;}
123 \; {return SEMI_COLUMN ;}
124 x {return AND ;}
125 \+ {return OR ;}
126 "->" {return ARROW ;}
127 \* {return STAR ;}
128 \[ {return LB ;}
129 \] {return RB ;}
130 \{ {return LR ;}
131 \} {return RR ;}
132 \"[_a-zA-Z0-9/\.~]+\" { ++yytext; yytext[+strlen(yytext)-1] = '\0';
133  yylval.name = strcache(yytext);
134  return GRAM_FILE ;}
135 = {return EQUAL ;}
136 ^--NEWGEN-START\ [0-9]*\n {
137  Current_start =
138  atoi( yytext+strlen( "--NEWGEN-START " )) ;
139  }
140 ^--NEWGEN-INDEX\ [0-9]*\n ;
141 --.*\n {}
142 [_a-zA-Z][_a-zA-Z0-9]* { yylval.name = strcache(yytext); return IDENT ;}
143 [0-9]+ { yylval.val = atoi(yytext); return GRAM_INT ;}
144 [ \t\n]* ;
145 %%
146 
147 int yywrap()
148 {
149 #ifdef FLEX_SCANNER
150  yy_init = 1;
151 #endif
152  return 1;
153 }
154 
155 #ifdef FLEX_SCANNER
156 int genspec_input()
157 {
158  return input();
159 }
160 #endif