OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / a60 / a60-lex.l
1 %{
2 /*
3  * Copyright (C) 1991,1992 Erik Schoenfelder (schoenfr@ibr.cs.tu-bs.de)
4  *
5  * This file is part of NASE A60.
6  * 
7  * NASE A60 is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * NASE A60 is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with NASE A60; see the file COPYING.  If not, write to the Free
19  * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * a60-lex.l:                                   aug '90
22  *
23  * Erik Schoenfelder (schoenfr@ibr.cs.tu-bs.de)
24  *
25  * this part is obsolete and replaced by a60-scan.[ch]
26  * if you like to use this, complete the keyword scanning (only
27  * 'begin' and 'end' are enclosed)
28  */
29
30 /*
31  * special for including "comm.h" :
32  */
33 #define THIS_IS_A60_LEX
34 #include "comm.h"
35 #undef THIS_IS_A60_LEX
36
37 #include "a60.h"
38 #include "util.h"
39 /*
40  * this should be ``a60-parse.tab.h'' but there are some systems ...
41  * so the names are still short...
42  */
43 #include "a60-ptab.h"
44
45
46 /*
47  * better use ctype.h ...
48  */
49 #define misupper(c)     ((c) >= 'A' && (c) <= 'Z')
50 #define mtolower(c)     ((c) - 'A' + 'a')
51
52 /* current line number. */
53 int lineno;
54
55 /*
56  * flex vs. lex:
57  */
58 #ifdef FLEX_SCANNER
59
60 /* forwards: */
61 static int input();
62 extern FILE *yyin;
63
64 #else /* ! FLEX_SCANNER */
65
66 /* forwards: */
67 static int yywrap ();
68
69 #endif /* ! FLEX_SCANNER */
70
71
72 #ifdef lint
73 /* if they use it - i'll provide it: */
74 extern _flsbuf ();
75 #endif
76
77
78 /*
79  * initalize lex-module.
80  */
81 void
82 init_lex ()
83 {
84         lineno = 1;
85
86         yyin = infile;
87 }
88
89
90 /*
91  * scan the real number from the buffer and return them.
92  */
93
94 static double
95 scan_real (s)
96 char *s;
97 {
98         char *fmt = "%le";
99         double x;
100
101 #ifdef pyr
102         fmt = "%lf";
103 #endif
104
105         if (sscanf (s, fmt, &x) < 1) {
106                 a60_error (infname, lineno,
107                    "INTERNAL: cannot scan from `%s'\n", s);
108                 xabort ("INTERNAL ERROR");
109         }
110
111         if (do_debug)
112                 printf ("** do_scan_real: `%s' -> %.10g\n", s, x);
113
114         return x;
115 }
116
117
118 /*
119  * convert the scanned string from c-like notation.
120  */
121
122 static char *
123 scan_string (s)
124 char *s;
125 {
126         int i, len;
127         char *tmp, *ptr;
128
129         len = strlen(s);
130         tmp = NTALLOC(len - 1, char);
131
132         if (*s == '\'')
133                 s += 2, len -= 4;
134
135         for (i=1, ptr=tmp; i < len-1; i++, ptr++) {
136                 if (s[i] == '\\') {
137                         i++;
138                         if (s[i] == 'n')
139                                 *ptr = '\n';
140                         else if (s[i] == '\\')
141                                 *ptr = '\\';
142                         else if (s[i] == '\0')
143                                 break;
144                         else
145                                 *ptr = s[i];
146                 }
147                 else
148                         *ptr = s[i];
149         }
150         *ptr = '\0';
151
152         return tmp;
153 }
154
155
156 /*
157  * handle this unknown char; skip input til end-of-line.
158  */
159
160 void
161 handle_unknown (ch)
162 int ch;
163 {
164         static int last_line = -1;
165         
166         if (last_line == lineno)
167                 return;
168         else
169                 last_line = lineno;
170
171         if (ch >= ' ' && ch <= 'Z')
172                  a60_error (infname, lineno,
173                     "unknown char `%c' found - skipping to next line\n",
174                             ch);
175         else
176                 a60_error (infname, lineno,
177                    "unknown char 0x%02x found - skipping to next line\n",
178                            ch);
179         nerrors++;
180
181         do {
182                 ch = input();
183         } while (ch && ch != '\n');
184
185         lineno++;
186 }
187
188 /* onk. */
189
190 %}
191
192 %e 2000
193 %p 3000
194
195 word            [a-zA-Z][a-zA-Z0-9 ]*
196
197 string          `[^']*'|"'('"[^']*"')'"|\"[^"]*\"
198
199 uint            [0-9][0-9]*
200
201 ureal           {uint}"."{uint}|"."{uint}
202
203 white           " "|"\t"|"\f"|"\r"|"\n"
204
205 single          "+"|"-"|"*"|"/"|","|"."|";"|"("|")"
206
207 %%
208
209 'begin'                         return TBEGIN;
210 '10'                            return TTEN;
211
212 'end'                           return TEND;
213
214 {single}                        return *yytext;
215
216 ":"|".."                        return ':';
217 ";"|".,"                        return ';';
218 "["|"(/"                        return '[';
219 "]"|"/)"                        return ']';
220 "**"                            return TPOW;
221 ":="|".="                       return TASSIGN;
222
223 "<"                             return TLESS;
224 ">"                             return TGREATER;
225 "="                             return TEQUAL;
226 "<="                            return TNOTGREATER;
227 ">="                            return TNOTLESS;
228
229 {white}*                        { char *s = (char *) yytext;
230                                   for (; *s; s++)
231                                           lineno += (*s == '\n');
232                                 }
233
234 {string}                        { yylval.str = scan_string (yytext);
235                                   return STRING;
236                                 }
237
238 {ureal}                         {  yylval.rtype = scan_real (yytext);
239                                    return RNUM;
240                                 }
241
242 {uint}                          { extern long atol ();
243                                   yylval.itype = atol (yytext);
244                                   return INUM;
245                                 }
246
247 {word}                          { char *str = xstrdup ((char *) yytext);
248                                   yylval.str = str;
249                                   return NAME;
250                                 }
251
252 ^#!.*                           { if (lineno != 1)
253                                           yywarning ("directive ignored");
254                                 }
255
256 .                               handle_unknown (*yytext & 0xff);
257
258 %%
259
260
261 /*
262  * error reporting:
263  */
264
265 void
266 yyerror(s)
267 char *s;
268 {
269         nerrors++;
270         /*
271          * if there is a ``parse error'' or a ``syntax error''
272          * reported from the skeleton, print the scanned string too.
273          */
274         if (! strcmp (s, "parse error")
275             || ! strcmp (s, "syntax error")) {
276                 a60_error (infname, lineno, "%s (scanned `%s')\n",
277                            s, (yytext && *yytext) ? (char *) yytext : "EOF");
278                 return;
279         }
280         a60_error (infname, lineno, "%s\n", s);
281 }
282
283 void
284 yywarning(s)
285 char *s;
286 {
287         a60_error (infname, lineno, "warning: %s\n", s);
288 }
289
290
291 #ifndef FLEX_SCANNER
292
293 /*
294  * EOF is real EOF.
295  */
296 static int
297 yywrap()
298 {
299         return 1;
300 }
301
302 #endif /* ! FLEX_SCANNER */
303
304 /* end of a60-lex.l */