1 %{
2 /*********************************************************************
3  *   Copyright 1993, UCAR/Unidata
4  *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
5  *   $Id: ncgenyy.l,v 1.3 2005-06-22 21:02:11 bert Exp $
6  *********************************************************************/
7 
8 /* lex specification for tokens for ncgen */
9 
10 /* Fill value used by ncdump from version 2.4 and later.  Should match
11    definition of FILL_STRING in ../ncdump/vardata.h */
12 #define FILL_STRING "_"
13 #define XDR_INT_MIN (-2147483647-1)
14 #define XDR_INT_MAX 2147483647
15 
16 char errstr[100];		/* for short error messages */
17 
18 #include <string.h>
19 #include <ctype.h>
20 #include <minc.h>
21 #include "ncgen.h"
22 #include "genlib.h"
23 #include "ncgentab.h"
24 
25 extern int int_val;
26 extern float float_val;
27 #if !defined (YYSTYPE) && !defined(YYSTYPE_IS_DECLARED)
28 extern void * yylval;
29 #define YYLVAL_NULL ((void *) NULL)
30 #else
31 #define YYLVAL_NULL ((YYSTYPE) NULL)
32 #endif /* YYSTYPE defined */
33 extern int lineno;
34 extern signed char byte_val;
35 extern short short_val;
36 extern double double_val;
37 extern char *netcdfname;
38 extern char termstring[];
39 
40 #define YY_BREAK                /* defining as nothing eliminates unreachable
41 				   statement warnings from flex output,
42                                    but make sure every action ends with
43                                    "return" or "break"! */
44 
45 %}
46 
47 %p 6000
48 
49 escaped		\\.
50 nonquotes	([^"\\]|{escaped})*
51 exp		([eE][+-]?[0-9]+)
52 %%
53 \/\/.*		        { /* comment */
54                           break;
55                         }
56 
57 \"{nonquotes}\"		{
58 			 if(yyleng > MAXTRST) {
59 				yyerror("string too long, truncated\n");
60 			        yytext[MAXTRST-1] = '\0';
61 			 }
62 			 expand_escapes(termstring,(char *)yytext,yyleng);
63 		 	 return (TERMSTRING);
64 		        }
65 
66 float|FLOAT|real|REAL	{return (FLOAT_K);}
67 char|CHAR		{return (CHAR_K);}
68 byte|BYTE		{return (BYTE_K);}
69 short|SHORT		{return (SHORT_K);}
70 long|LONG|int|INT|integer|INTEGER	{return (INT_K);}
71 double|DOUBLE		{return (DOUBLE_K);}
72 unlimited|UNLIMITED	{int_val = -1;
73 			 return (NC_UNLIMITED_K);}
74 
75 dimensions:|DIMENSIONS:	{return (DIMENSIONS);}
76 variables:|VARIABLES:	{return (VARIABLES);}
77 data:|DATA:		{return (DATA);}
78 (netcdf|NETCDF|netCDF)[ \t]+[^\{]+	{
79 		char *s = (char*)yytext+strlen("netcdf");
80 		char *t = (char*)yytext+yyleng-1;
81 		while (isspace(*s))
82 			s++;
83 		while (isspace(*t))
84 			t--;
85 		t++;
86                 if (t-s+1 < 1) {
87                         yyerror("netCDF name required");
88                         return (DATA); /* generate syntax error */
89                 }
90 		netcdfname = (char *) emalloc(t-s+1);
91 		(void) strncpy(netcdfname, s, t-s);
92 		netcdfname[t-s] = '\0';
93 		return (NETCDF);
94 		}
95 (hdf5|HDF5)[ \t]+[^\{]+	{
96 		char *s = (char*)yytext+strlen("hdf5");
97 		char *t = (char*)yytext+yyleng-1;
98 		while (isspace(*s))
99 			s++;
100 		while (isspace(*t))
101 			t--;
102 		t++;
103                 if (t-s+1 < 1) {
104                         yyerror("HDF5 name required");
105                         return (DATA); /* generate syntax error */
106                 }
107 		netcdfname = (char *) emalloc(t-s+1);
108 		(void) strncpy(netcdfname, s, t-s);
109 		netcdfname[t-s] = '\0';
110 		return (HDF5);
111 		}
112 DoubleInf|NaN|-?Infinity { /* missing value (pre-2.4 backward compatibility) */
113                 if (yytext[0] == '-') {
114 		    double_val = -NC_FILL_DOUBLE;
115                 } else {
116 		    double_val = NC_FILL_DOUBLE;
117                 }
118 		return (DOUBLE_CONST);
119 		}
120 FloatInf|-?Inff	{ /* missing value (pre-2.4 backward compatibility) */
121                 if (yytext[0] == '-') {
122 		    float_val = -NC_FILL_FLOAT;
123                 } else {
124 		    float_val = NC_FILL_FLOAT;
125                 }
126 		return (FLOAT_CONST);
127 		}
128 [A-Za-z_][A-Z.@#\[\]a-z_0-9-]*	{
129                 if (STREQ((char *)yytext, FILL_STRING))
130 		        return (FILLVALUE);
131 		if ((yylval = lookup((char *)yytext)) == YYLVAL_NULL) {
132 			yylval = install((char *)yytext);
133 			}
134 		return (IDENT);
135 		}
136 
137 \n		{
138 		lineno++ ;
139                 break;
140 		}
141 
142 [+-]?[0-9]*[0-9][Bb]  {
143                 int ii;
144 		if (sscanf((char*)yytext, "%d", &ii) != 1) {
145 		    sprintf(errstr,"bad byte constant: %s",(char*)yytext);
146 		    yyerror(errstr);
147 		}
148                 byte_val = ii;
149 		if (ii != (int)byte_val) {
150 		    sprintf(errstr,"byte constant out of range (-128,127): %s",(char*)yytext);
151 		    yyerror(errstr);
152 		}
153 		return (BYTE_CONST);
154                 }
155 
156 [+-]?[0-9]*\.[0-9]*{exp}?[LlDd]?|[+-]?[0-9]*{exp}[LlDd]? {
157 		if (sscanf((char*)yytext, "%le", &double_val) != 1) {
158 		    sprintf(errstr,"bad long or double constant: %s",(char*)yytext);
159 		    yyerror(errstr);
160 		}
161                 return (DOUBLE_CONST);
162                 }
163 [+-]?[0-9]*\.[0-9]*{exp}?[Ff]|[+-]?[0-9]*{exp}[Ff] {
164 		if (sscanf((char*)yytext, "%e", &float_val) != 1) {
165 		    sprintf(errstr,"bad float constant: %s",(char*)yytext);
166 		    yyerror(errstr);
167 		}
168                 return (FLOAT_CONST);
169                 }
170 [+-]?[0-9]+[sS]|0[xX][0-9a-fA-F]+[sS] {
171 		if (sscanf((char*)yytext, "%hd", &short_val) != 1) {
172 		    sprintf(errstr,"bad short constant: %s",(char*)yytext);
173 		    yyerror(errstr);
174 		}
175 		return (SHORT_CONST);
176 	        }
177 [+-]?([1-9][0-9]*|0)[lL]? {
178     		char *ptr;
179                 errno = 0;
180 		double_val = strtod((char*)yytext, &ptr);
181 		if (errno != 0 && double_val == 0.0) {
182 		    sprintf(errstr,"bad numerical constant: %s",(char*)yytext);
183 		    yyerror(errstr);
184 		}
185                 if (double_val < XDR_INT_MIN ||double_val > XDR_INT_MAX) {
186                     return DOUBLE_CONST;
187                 } else {
188                     int_val = (int) double_val;
189                     return INT_CONST;
190                 }
191 	        }
192 0[xX]?[0-9a-fA-F]+[lL]? {
193     		char *ptr;
194                 long long_val;
195                 errno = 0;
196 		long_val = strtol((char*)yytext, &ptr, 0);
197 		if (errno != 0) {
198 		    sprintf(errstr,"bad long constant: %s",(char*)yytext);
199 		    yyerror(errstr);
200 		}
201                 if (long_val < XDR_INT_MIN || long_val > XDR_INT_MAX) {
202                     double_val = (double) long_val;
203                     return DOUBLE_CONST;
204                 } else {
205                     int_val = (int) long_val;
206                     return INT_CONST;
207                 }
208 	        }
209 \'[^\\]\'          {
210 	        (void) sscanf((char*)&yytext[1],"%c",&byte_val);
211 		return (BYTE_CONST);
212                 }
213 \'\\[0-7][0-7]?[0-7]?\'  {
214 		byte_val = (char) strtol((char*)&yytext[2], (char **) 0, 8);
215 		return (BYTE_CONST);
216                 }
217 \'\\[xX][0-9a-fA-F][0-9a-fA-F]?\'  {
218 		byte_val = (char) strtol((char*)&yytext[3], (char **) 0, 16);
219 		return (BYTE_CONST);
220                 }
221 \'\\.\'        {
222 	       switch ((char)yytext[2]) {
223 	          case 'a': byte_val = '\007'; break; /* not everyone under-
224 						       * stands '\a' yet */
225      	          case 'b': byte_val = '\b'; break;
226 		  case 'f': byte_val = '\f'; break;
227 		  case 'n': byte_val = '\n'; break;
228 		  case 'r': byte_val = '\r'; break;
229 		  case 't': byte_val = '\t'; break;
230 		  case 'v': byte_val = '\v'; break;
231 		  case '\\': byte_val = '\\'; break;
232 		  case '?': byte_val = '\177'; break;
233 		  case '\'': byte_val = '\''; break;
234 		  default: byte_val = (char)yytext[2];
235 	           }
236 		return (BYTE_CONST);
237                 }
238 
239 [ \t\f]+	{ /* whitespace */
240 		  break;
241 		}
242 .		return (yytext[0]) ;
243