PIPS
scanner.l
Go to the documentation of this file.
1 /*
2 
3  $Id: scanner.l 23128 2016-07-01 11:58:21Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS 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.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 %{
26 #ifdef HAVE_CONFIG_H
27  #include "pips_config.h"
28 #endif
29 
30 /* UICON and ICON must be distinguished from RCON and DCON:
31  REAL*8 D1
32  REAL*8 E2
33  Should not be parsed as real constants!
34 */
35 
36 #ifdef FLEX_SCANNER
37 
38 /* flex incompatible way of playing with input characters:-)
39  */
40 
41 #define YY_INPUT(buf,result,max_size) \
42 { int c = PipsGetc(yyin); result = (c == EOF)? YY_NULL: (buf[0] = c, 1);}
43 
44 #else /* we're not using flex... let us assume some very peculiar lex... */
45 
46 /* the portability of the following macro makes many (:-) assumptions
47  * about lex internals hence should not be very portable...
48  * POSIX states that input cannot be redefined.
49  * the only possible posix implementation would be to refilter the
50  * file through PipsGetc and then to use a standard lex/yacc.
51  */
52 #undef input()
53 #define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):PipsGetc(yyin))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
54 
55 #endif /* FLEX_SCANNER */
56 
57 #include <stdlib.h>
58 #include <stdio.h>
59 
60 #include "genC.h"
61 #include "parser_private.h"
62 #include "linear.h"
63 #include "ri.h"
64 
65 #include "misc.h"
66 #include "syntax.h"
67 
68 /* yacc generated header file
69  */
70 #include "syn_yacc.h"
71 
72 /* stdlib defines abs on solaris 2 */
73 #ifdef abs
74 #undef abs
75 #endif
76 
77 static int line = 1; /* position in pseudo-file... */
78 
79 %}
80 
81 DIG [0-9]
82 ULET [A-Z_]
83 LLET [a-z_]
84 SIG [+-]
85 UICON ({DIG}+)
86 ICON ({SIG}?{UICON})
87 MANT (({DIG}+\.{DIG}*)|({DIG}*\.{DIG}+))
88 REXP (E{ICON})
89 DEXP (D{ICON})
90 RCON (({MANT}|{UICON}){REXP}?)
91 DCON (({MANT}|{UICON}){DEXP}?)
92 
93 %start TYPEDECLARATION
94 
95 %%
96 %AND% {
97  pips_debug(9, "TK_AND\n");
98  return(TK_AND);
99  }
100 %INOUT% {
101  pips_debug(9, "TK_INOUT\n");
102  return(TK_INOUT);
103  }
104 %IN% {
105  pips_debug(9, "TK_IN\n");
106  return(TK_IN);
107  }
108 %OUT% {
109  pips_debug(9, "TK_OUT\n");
110  return(TK_OUT);
111  }
112 %EQ% {
113  pips_debug(9, "TK_EQ\n");
114  return(TK_EQ);
115  }
116 %EQV% {
117  pips_debug(9, "TK_EQV\n");
118  return(TK_EQV);
119  }
120 %GE% {
121  pips_debug(9, "TK_GE\n");
122  return(TK_GE);
123  }
124 %GT% {
125  pips_debug(9, "TK_GT\n");
126  return(TK_GT);
127  }
128 %LE% {
129  pips_debug(9, "TK_LE\n");
130  return(TK_LE);
131  }
132 %LT% {
133  pips_debug(9, "TK_LT\n");
134  return(TK_LT);
135  }
136 %NE% {
137  pips_debug(9, "TK_NE\n");
138  return(TK_NE);
139  }
140 %NEQV% {
141  pips_debug(9, "TK_NEQV\n");
142  return(TK_NEQV);
143  }
144 %NOT% {
145  pips_debug(9, "TK_NOT\n");
146  return(TK_NOT);
147  }
148 %OR% {
149  pips_debug(9, "TK_OR\n");
150  return(TK_OR);
151  }
152 %TRUE% {
153  pips_debug(9, "TK_TRUE\n");
154  return(TK_TRUE);
155  }
156 %FALSE% {
157  pips_debug(9, "TK_FALSE\n");
158  return(TK_FALSE);
159  }
160 
161 {ULET}{LLET}+ {
162  int tv = IsCapKeyword(yytext);
163  if (tv == TK_FORMAT) {
164  char * s = FormatValue;
165  while ((*s++ = input()) != '\n') ;
166  unput('\n');
167  *(s-1) = '\0';
168  }
169 
170  switch(tv) {
171  case TK_CHARACTER:
172  case TK_COMPLEX:
173  case TK_INTEGER:
174  case TK_LOGICAL:
175  case TK_REAL:
176  BEGIN TYPEDECLARATION;
177  }
178 
179  pips_debug(9, "TK_KEYWORD %s %u %d\n",
180  yytext, (unsigned int) yyleng, tv);
181  return(tv);
182  }
183 
184 {ULET}+({DIG}|{ULET})* {
185  pips_debug(9, "TK_NAME %s %u\n", yytext, (unsigned int) yyleng);
186  syn_lval.string = strdup(yytext);
187  BEGIN INITIAL;
188  return(TK_NAME);
189  }
190 
191 {UICON} {
192  pips_debug(9, "TK_ICON %s %u\n", yytext, (unsigned int) yyleng);
193  syn_lval.string = strdup(yytext);
194  return(TK_ICON);
195 }
196 
197 <INITIAL>{RCON} {
198  pips_debug(9, "TK_RCON %s %u\n", yytext, (unsigned int) yyleng);
199  syn_lval.string = strdup(yytext);
200  return(TK_RCON);
201 }
202 
203 <INITIAL>{DCON} {
204  pips_debug(9, "TK_DCON %s %u\n", yytext, (unsigned int) yyleng);
205  syn_lval.string = strdup(yytext);
206  return(TK_DCON);
207 }
208 
209 \'([^\']|(\'\')|(\\\'))*\' {
210  pips_debug(9, "TK_SCON %s %u\n", yytext, (unsigned int) yyleng);
211  syn_lval.string = strdup(yytext);
212  return(TK_SCON);
213 }
214 
215 \"([^\"]|(\"\")|(\\\"))*\" {
216  pips_debug(9, "TK_SCON %s %u\n", yytext, (unsigned int) yyleng);
217  syn_lval.string = strdup(yytext);
218  return(TK_SCON);
219 }
220 
221 "-" {
222  pips_debug(9, "TK_MINUS\n");
223  return(TK_MINUS);
224  }
225 "+" {
226  pips_debug(9, "TK_PLUS\n");
227  return(TK_PLUS);
228  }
229 "/" {
230  pips_debug(9, "TK_SLASH\n");
231  return(TK_SLASH);
232  }
233 "**" {
234  pips_debug(9, "TK_POWER\n");
235  return(TK_POWER);
236  }
237 "*" {
238  pips_debug(9, "TK_STAR\n");
239  return(TK_STAR);
240  }
241 "(" {
242  pips_debug(9, "TK_LPAR\n");
243  return(TK_LPAR);
244  }
245 ")" {
246  pips_debug(9, "TK_RPAR\n");
247  return(TK_RPAR);
248  }
249 "," {
250  pips_debug(9, "TK_COMMA\n");
251  return(TK_COMMA);
252  }
253 ":" {
254  pips_debug(9, "TK_COLON\n");
255  return(TK_COLON);
256  }
257 "=" {
258  pips_debug(9, "TK_EQUALS\n");
259  return(TK_EQUALS);
260  }
261 "$" {
262  pips_debug(9, "TK_CURRENCY\n");
263  return(TK_CURRENCY);
264  }
265 "//" {
266  pips_debug(9, "TK_CONCAT\n");
267  return(TK_CONCAT);
268  }
269 \n { line++;
270  BEGIN INITIAL;
271  pips_debug(9, "TK_EOS\n");
272  return(TK_EOS);
273  }
274 . {
275  /* Ignore carriage return from VMS and Microsoft and Apple?
276  * Not here! It must have been done earlier in the processing.
277  */
278  pips_user_warning("illegal character \"%c\" (hexadecimal %#04x)\n",
279  yytext[0], yytext[0]);
280  ParserError("Lexical Analysis",
281  "Check standard Fortran character set.\n");
282  }
283 %%
284 
285 void syn_error(const char * msg)
286 {
287  int c;
288 
289  /* Do not use pips_user_warning() to avoid portability issues */
290 
291  user_warning("syn_error", "Syntax error - %s line %d near \"%s\"\n",
292  msg, line, yytext);
293 
294  /* what does it mean? */
295  user_warning("syn_error", "Non analyzed source text stored in logfile\n");
296 
297  while ((c = getc(yyin)) != EOF)
298  putc(c, stderr);
299 
300  /* pas de recouvrement d'erreurs */
301  ParserError("syn_error", "Syntax error\n");
302 }
303 
304 void syn_reset_lex(void)
305 {
306  line = 0;
307 #ifdef FLEX_SCANNER
308  syn_restart(yyin);
309 #else
310  /* reset lex... Might be better to read the whole file?
311  */
312  syn_sptr = syn_sbuf;
313 # define MMNEWLINE 10
314  syn_previous = MMNEWLINE;
315 #endif
316 }
317 
318 /* end of it */