i386.md: Simplify certain comparisons of const_int.
[gcc.git] / gcc / treelang / lex.l
1 /* -*- c -*- = mode for emacs editor
2
3 TREELANG lexical analysis
4
5 ---------------------------------------------------------------------
6
7 Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002, 2003
8 Free Software Foundation, Inc.
9
10 This program is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
13 later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
24
25 In other words, you are welcome to use, share and improve this program.
26 You are forbidden to forbid anyone else to use, share and improve
27 what you give them. Help stamp out software-hoarding!
28
29 ---------------------------------------------------------------------
30
31 Written by Tim Josling 1999-2001, based in part on other parts of
32 the GCC compiler.
33
34 */
35
36 %{
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "diagnostic.h"
42 #include "tree.h"
43
44 /* Token defs. */
45 #include "treelang.h"
46 #include "parse.h"
47 #include "treetree.h"
48
49 extern int option_lexer_trace;
50
51 int yylex (void);
52 void update_yylval (int a);
53
54 static int next_tree_charno=1;
55
56 static void update_lineno_charno (void);
57 static void dump_lex_value (int lexret);
58
59 #define SAVE_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
60 {fprintf (stderr, "\nlexer returning"); dump_lex_value (a);} return a;}
61 #define NOT_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
62 {fprintf (stderr, "\nlexer swallowing"); dump_lex_value (a);}}
63
64 %}
65
66 %option nostack
67 %option nounput
68 %option noyywrap
69 %option pointer
70 %option nodefault
71
72 %%
73
74 {
75 /* Should really allocate only what we need. lll;. */
76 yylval = my_malloc (sizeof (struct prod_token_parm_item));
77 ((struct prod_token_parm_item *)yylval)->tp.tok.location = input_location;
78 ((struct prod_token_parm_item *)yylval)->tp.tok.charno = next_tree_charno;
79 }
80
81 [ \n\t]+ {
82 update_lineno_charno ();
83 NOT_RETURN (WHITESPACE);
84 }
85
86 "//".* {
87 /* Comment. */
88 update_lineno_charno ();
89 NOT_RETURN (COMMENT);
90 }
91
92 "{" {
93 update_lineno_charno ();
94 SAVE_RETURN (LEFT_BRACE);
95 }
96
97 "}" {
98 update_lineno_charno ();
99 SAVE_RETURN (RIGHT_BRACE);
100 }
101
102 "(" {
103 update_lineno_charno ();
104 SAVE_RETURN (LEFT_PARENTHESIS);
105 }
106
107 ")" {
108 update_lineno_charno ();
109 SAVE_RETURN (RIGHT_PARENTHESIS);
110 }
111
112 "," {
113 update_lineno_charno ();
114 SAVE_RETURN (COMMA);
115 }
116
117 ";" {
118 update_lineno_charno ();
119 SAVE_RETURN (SEMICOLON);
120 }
121
122 "+" {
123 update_lineno_charno ();
124 SAVE_RETURN (tl_PLUS);
125 }
126
127 "-" {
128 update_lineno_charno ();
129 SAVE_RETURN (tl_MINUS);
130 }
131
132 "=" {
133 update_lineno_charno ();
134 SAVE_RETURN (ASSIGN);
135 }
136
137 "==" {
138 update_lineno_charno ();
139 SAVE_RETURN (EQUALS);
140 }
141
142 [+-]?[0-9]+ {
143 update_lineno_charno ();
144 SAVE_RETURN (INTEGER);
145 }
146
147 "external_reference" {
148 update_lineno_charno ();
149 SAVE_RETURN (EXTERNAL_REFERENCE);
150 }
151
152 "external_definition" {
153 update_lineno_charno ();
154 SAVE_RETURN (EXTERNAL_DEFINITION);
155 }
156
157 "static" {
158 update_lineno_charno ();
159 SAVE_RETURN (STATIC);
160 }
161
162 "automatic" {
163 update_lineno_charno ();
164 SAVE_RETURN (AUTOMATIC);
165 }
166
167 "int" {
168 update_lineno_charno ();
169 SAVE_RETURN (INT);
170 }
171
172 "char" {
173 update_lineno_charno ();
174 SAVE_RETURN (CHAR);
175 }
176
177 "void" {
178 update_lineno_charno ();
179 SAVE_RETURN (VOID);
180 }
181
182 "unsigned" {
183 update_lineno_charno ();
184 SAVE_RETURN (UNSIGNED);
185 }
186
187 "return" {
188 update_lineno_charno ();
189 SAVE_RETURN (tl_RETURN);
190 }
191
192 "if" {
193 update_lineno_charno ();
194 SAVE_RETURN (IF);
195 }
196
197 "else" {
198 update_lineno_charno ();
199 SAVE_RETURN (ELSE);
200 }
201
202 [A-Za-z_]+[A-Za-z_0-9]* {
203 update_lineno_charno ();
204 update_yylval (NAME);
205 if (option_lexer_trace)
206 {
207 fprintf (stderr, "\nlexer returning");
208 dump_lex_value (NAME);
209 }
210 return NAME;
211 }
212
213 [^\n] {
214 update_lineno_charno ();
215 fprintf (stderr, "%s:%i:%i: Unrecognized character %c\n",
216 ((struct prod_token_parm_item *)yylval)->tp.tok.location.file,
217 ((struct prod_token_parm_item *)yylval)->tp.tok.location.line,
218 ((struct prod_token_parm_item *)yylval)->tp.tok.charno, yytext[0]);
219 errorcount++;
220 }
221
222 %%
223
224 /*
225 Update line number (1-) and character number (1-). Call this
226 before processing the token. */
227
228 static void
229 update_lineno_charno (void)
230 {
231 /* Update the values we send to caller in case we sometimes don't
232 tell them about all the 'tokens' eg comments etc. */
233 int yyl;
234 ((struct prod_token_parm_item *)yylval)->tp.tok.location = input_location;
235 ((struct prod_token_parm_item *)yylval)->tp.tok.charno = next_tree_charno;
236 for ( yyl = 0; yyl < yyleng; ++yyl )
237 {
238 if ( yytext[yyl] == '\n' )
239 {
240 ++input_line;
241 next_tree_charno = 1;
242 }
243 else
244 next_tree_charno++;
245 }
246 }
247
248 /* Fill in the fields of yylval - the value of the token. The token
249 type is A. */
250 void
251 update_yylval (int a)
252 {
253 struct prod_token_parm_item * tok;
254 tok=yylval;
255
256 tok->category = token_category;
257 tok->type = a;
258 tok->tp.tok.length = yyleng;
259 /* Have to copy yytext as it is just a ptr into the buffer at the
260 moment. */
261 tok->tp.tok.chars = (unsigned char*) get_string (yytext, yyleng);
262 }
263
264 /* Trace the value LEXRET and the position and token details being
265 returned by the lexical analyser. */
266
267 static void
268 dump_lex_value (int lexret)
269 {
270 int ix;
271 fprintf (stderr, " %d l:%d c:%d ln:%d text=", lexret,
272 ((struct prod_token_parm_item *) yylval)->tp.tok.location.line,
273 ((struct prod_token_parm_item *) yylval)->tp.tok.charno,
274 ((struct prod_token_parm_item *) yylval)->tp.tok.length);
275 for (ix = 0; ix < yyleng; ix++)
276 {
277 fprintf (stderr, "%c", yytext[ix]);
278 }
279 fprintf (stderr, " in hex:");
280 for (ix = 0; ix < yyleng; ix++)
281 {
282 fprintf (stderr, " %2.2x", yytext[ix]);
283 }
284 fprintf (stderr, "\n");
285 }
286