]> git.saurik.com Git - bison.git/blame - src/scan-gram.l
* src/scan-gram.l (YY_INIT, YY_GROW, YY_FINISH): Rename as...
[bison.git] / src / scan-gram.l
CommitLineData
e9955c83
AD
1/* Bison Grammar Scanner -*- C -*-
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA
20*/
21
22%option debug nodefault noyywrap nounput never-interactive stack
23%option prefix="gram_" outfile="lex.yy.c"
24
25%{
26#include "system.h"
27#include "complain.h"
28#include "quote.h"
29#include "getargs.h"
30#include "gram.h"
31#include "reader.h"
32
33/* Each time we match a string, move the end cursor to its end. */
34#define YY_USER_ACTION LOCATION_COLUMNS (*yylloc, yyleng)
35#define YY_LINES LOCATION_LINES (*yylloc, yyleng); lineno += yyleng;
36#define YY_STEP LOCATION_STEP (*yylloc)
37
44995b2e
AD
38
39/* STRING_OBSTACK -- Used to store all the characters that we need to
40 keep (to construct ID, STRINGS etc.). Use the following macros to
41 use it.
42
43 Use YY_OBS_INIT to initialize a new growing string, YY_OBS_GROW to
44 append what has just been matched, and YY_OBS_FINISH to end the
45 string (it puts the ending 0). YY_OBS_FINISH also stores this
46 string in LAST_STRING, which can be used, and which is used by
47 YY_OBS_FREE to free the last string. */
48
49static struct obstack string_obstack;
50char *last_string;
51
52#define YY_OBS_INIT \
53 obstack_init (&string_obstack)
54
55#define YY_OBS_GROW \
56 obstack_grow (&string_obstack, yytext, yyleng)
57
58#define YY_OBS_FINISH \
59 do { \
60 obstack_1grow (&string_obstack, '\0'); \
61 last_string = obstack_finish (&string_obstack); \
62 yylval->string = last_string; \
63 } while (0)
64
65#define YY_OBS_FREE \
66 do { \
67 obstack_free (&string_obstack, last_string); \
68 } while (0)
e9955c83
AD
69
70/* This is only to avoid GCC warnings. */
71#define YY_USER_INIT if (yycontrol) {;};
72
44995b2e 73
e9955c83
AD
74static int braces_level = 0;
75static int percent_percent_count = 0;
76
77static void handle_dollar PARAMS ((char *cp));
78static void handle_at PARAMS ((char *cp));
79
80%}
81%x SC_COMMENT
82%x SC_STRING SC_CHARACTER
83%x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
84%x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
85
86id [.a-zA-Z][.a-zA-Z_0-9]*
87int [0-9]+
88eols (\n|\r|\n\r|\r\n)+
89blanks [ \t\f]+
90
91%%
92%{
93 /* At each yylex invocation, mark the current position as the
94 start of the next token. */
95#define TR_POS 0
96#if TR_POS
97 fprintf (stderr, "FOO1: ");
98 LOCATION_PRINT (stderr, *yylloc);
99 fprintf (stderr, "\n");
100#endif
101 YY_STEP;
102#if TR_POS
103 fprintf (stderr, "BAR1: ");
104 LOCATION_PRINT (stderr, *yylloc);
105 fprintf (stderr, "\n");
106#endif
107%}
108
109
110 /*----------------------------.
111 | Scanning Bison directives. |
112 `----------------------------*/
113<INITIAL>
114{
115 "%binary" return PERCENT_NONASSOC;
116 "%debug" return PERCENT_DEBUG;
117 "%define" return PERCENT_DEFINE;
118 "%defines" return PERCENT_DEFINES;
119 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
120 "%expect" return PERCENT_EXPECT;
121 "%file-prefix" return PERCENT_FILE_PREFIX;
122 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
123 "%left" return PERCENT_LEFT;
124 "%locations" return PERCENT_LOCATIONS;
125 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
126 "%no"[-_]"lines" return PERCENT_NO_LINES;
127 "%nonassoc" return PERCENT_NONASSOC;
128 "%nterm" return PERCENT_NTERM;
129 "%output" return PERCENT_OUTPUT;
130 "%prec" return PERCENT_PREC;
131 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
132 "%right" return PERCENT_RIGHT;
133 "%skeleton" return PERCENT_SKELETON;
134 "%start" return PERCENT_START;
135 "%term" return PERCENT_TOKEN;
136 "%token" return PERCENT_TOKEN;
137 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
138 "%type" return PERCENT_TYPE;
139 "%union" return PERCENT_UNION;
140 "%verbose" return PERCENT_VERBOSE;
141 "%yacc" return PERCENT_YACC;
142
143 "=" return EQUAL;
144 ":" return COLON;
145 "|" return PIPE;
146 ";" return SEMICOLON;
147
148 {eols} YY_LINES; YY_STEP;
149 {blanks} YY_STEP;
150 {id} {
44995b2e
AD
151 YY_OBS_INIT; YY_OBS_GROW; YY_OBS_FINISH;
152 yylval->symbol = getsym (last_string);
153 YY_OBS_FREE;
e9955c83
AD
154 return ID;
155 }
156
157 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
158
159 /* Characters. We don't check there is only one. */
44995b2e 160 \' YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
e9955c83
AD
161
162 /* Strings. */
44995b2e 163 \" YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
e9955c83
AD
164
165 /* Comments. */
166 "/*" yy_push_state (SC_COMMENT);
167 "//".* YY_STEP;
168
169 /* Prologue. */
44995b2e 170 "%{" YY_OBS_INIT; yy_push_state (SC_PROLOGUE);
e9955c83
AD
171
172 /* Code in between braces. */
44995b2e 173 "{" YY_OBS_INIT; YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
e9955c83
AD
174
175 /* A type. */
44995b2e 176 "<"[^>]+">" YY_OBS_INIT; obstack_grow (&string_obstack, yytext + 1, yyleng - 2); YY_OBS_FINISH; return TYPE;
e9955c83
AD
177
178 "%%" {
179 if (++percent_percent_count == 2)
180 yy_push_state (SC_EPILOGUE);
181 return PERCENT_PERCENT;
182 }
183
184 . {
185 LOCATION_PRINT (stderr, *yylloc);
186 fprintf (stderr, ": invalid character: `%c'\n", *yytext);
187 YY_STEP;
188 }
189}
190
191
192 /*------------------------------------------------------------.
193 | Whatever the start condition (but those which correspond to |
194 | entity `swallowed' by Bison: SC_ESCAPED_STRING and |
195 | SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
196 `------------------------------------------------------------*/
197
198<SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
199{
200 \[ obstack_sgrow (&string_obstack, "@<:@");
201 \] obstack_sgrow (&string_obstack, "@:>@");
202}
203
204
205
206 /*-----------------------------------------------------------.
207 | Scanning a C comment. The initial `/ *' is already eaten. |
208 `-----------------------------------------------------------*/
209
210<SC_COMMENT>
211{
212 "*/" { /* End of the comment. */
213 if (yy_top_state () == INITIAL)
214 {
215 YY_STEP;
216 }
217 else
218 {
44995b2e 219 YY_OBS_GROW;
e9955c83
AD
220 }
221 yy_pop_state ();
222 }
223
44995b2e
AD
224 [^\[\]*\n\r]+ if (yy_top_state () != INITIAL) YY_OBS_GROW;
225 {eols} if (yy_top_state () != INITIAL) YY_OBS_GROW; YY_LINES;
226 . /* Stray `*'. */if (yy_top_state () != INITIAL) YY_OBS_GROW;
e9955c83
AD
227
228 <<EOF>> {
229 LOCATION_PRINT (stderr, *yylloc);
230 fprintf (stderr, ": unexpected end of file in a comment\n");
231 yy_pop_state ();
232 }
233}
234
235
236 /*----------------------------------------------------------------.
237 | Scanning a C string, including its escapes. The initial `"' is |
238 | already eaten. |
239 `----------------------------------------------------------------*/
240
241<SC_ESCAPED_STRING>
242{
243 \" {
244 assert (yy_top_state () == INITIAL);
44995b2e
AD
245 YY_OBS_GROW;
246 YY_OBS_FINISH;
e9955c83
AD
247 yy_pop_state ();
248 return STRING;
249 }
250
44995b2e 251 [^\"\n\r\\]+ YY_OBS_GROW;
e9955c83
AD
252
253 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
254
255 <<EOF>> {
256 LOCATION_PRINT (stderr, *yylloc);
257 fprintf (stderr, ": unexpected end of file in a string\n");
258 assert (yy_top_state () == INITIAL);
44995b2e 259 YY_OBS_FINISH;
e9955c83
AD
260 yy_pop_state ();
261 return STRING;
262 }
263}
264
265 /*---------------------------------------------------------------.
266 | Scanning a C character, decoding its escapes. The initial "'" |
267 | is already eaten. |
268 `---------------------------------------------------------------*/
269
270<SC_ESCAPED_CHARACTER>
271{
272 \' {
44995b2e 273 YY_OBS_GROW;
e9955c83
AD
274 assert (yy_top_state () == INITIAL);
275 {
44995b2e
AD
276 YY_OBS_FINISH;
277 yylval->symbol = getsym (last_string);
e9955c83 278 symbol_class_set (yylval->symbol, token_sym);
44995b2e
AD
279 symbol_user_token_number_set (yylval->symbol, last_string[1]);
280 YY_OBS_FREE;
e9955c83
AD
281 yy_pop_state ();
282 return ID;
283 }
284 }
285
44995b2e 286 [^\'\n\r\\] YY_OBS_GROW;
e9955c83
AD
287
288 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
289
290 <<EOF>> {
291 LOCATION_PRINT (stderr, *yylloc);
292 fprintf (stderr, ": unexpected end of file in a character\n");
293 assert (yy_top_state () == INITIAL);
44995b2e 294 YY_OBS_FINISH;
e9955c83
AD
295 yy_pop_state ();
296 return CHARACTER;
297 }
298}
299
300
301 /*----------------------------.
302 | Decode escaped characters. |
303 `----------------------------*/
304
305<SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
306{
307 \\[0-7]{3} {
308 long c = strtol (yytext + 1, 0, 8);
309 if (c > 255)
310 {
311 LOCATION_PRINT (stderr, *yylloc);
312 fprintf (stderr, ": invalid escape: %s\n", yytext);
313 YY_STEP;
314 }
315 else
316 obstack_1grow (&string_obstack, c);
317 }
318
319 \\x[0-9a-fA-F]{2} {
320 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
321 }
322
323 \\a obstack_1grow (&string_obstack, '\a');
324 \\b obstack_1grow (&string_obstack, '\b');
325 \\f obstack_1grow (&string_obstack, '\f');
326 \\n obstack_1grow (&string_obstack, '\n');
327 \\r obstack_1grow (&string_obstack, '\r');
328 \\t obstack_1grow (&string_obstack, '\t');
329 \\v obstack_1grow (&string_obstack, '\v');
330 \\[\\""] obstack_1grow (&string_obstack, yytext[1]);
331 \\. {
332 LOCATION_PRINT (stderr, *yylloc);
333 fprintf (stderr, ": unrecognized escape: %s\n", yytext);
44995b2e 334 YY_OBS_GROW;
e9955c83
AD
335 }
336}
337
338
339 /*----------------------------------------------------------.
340 | Scanning a C character without decoding its escapes. The |
341 | initial "'" is already eaten. |
342 `----------------------------------------------------------*/
343
344<SC_CHARACTER>
345{
346 \' {
44995b2e 347 YY_OBS_GROW;
e9955c83
AD
348 assert (yy_top_state () != INITIAL);
349 yy_pop_state ();
350 }
351
44995b2e
AD
352 [^\[\]\'\n\r\\] YY_OBS_GROW;
353 \\. YY_OBS_GROW;
e9955c83 354
44995b2e 355 {eols} YY_OBS_GROW; YY_LINES;
e9955c83
AD
356
357 <<EOF>> {
358 LOCATION_PRINT (stderr, *yylloc);
359 fprintf (stderr, ": unexpected end of file in a character\n");
360 assert (yy_top_state () != INITIAL);
361 yy_pop_state ();
362 }
363}
364
365
366 /*----------------------------------------------------------------.
367 | Scanning a C string, without decoding its escapes. The initial |
368 | `"' is already eaten. |
369 `----------------------------------------------------------------*/
370
371<SC_STRING>
372{
373 \" {
374 assert (yy_top_state () != INITIAL);
44995b2e 375 YY_OBS_GROW;
e9955c83
AD
376 yy_pop_state ();
377 }
378
44995b2e
AD
379 [^\[\]\"\n\r\\]+ YY_OBS_GROW;
380 \\. YY_OBS_GROW;
e9955c83 381
44995b2e 382 {eols} YY_OBS_GROW; YY_LINES;
e9955c83
AD
383
384 <<EOF>> {
385 LOCATION_PRINT (stderr, *yylloc);
386 fprintf (stderr, ": unexpected end of file in a string\n");
387 assert (yy_top_state () != INITIAL);
388 yy_pop_state ();
389 }
390}
391
392
393 /*---------------------------------------------------.
394 | Strings, comments etc. can be found in user code. |
395 `---------------------------------------------------*/
396
397<SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
398{
399 /* Characters. We don't check there is only one. */
44995b2e 400 \' YY_OBS_GROW; yy_push_state (SC_CHARACTER);
e9955c83
AD
401
402 /* Strings. */
44995b2e 403 \" YY_OBS_GROW; yy_push_state (SC_STRING);
e9955c83
AD
404
405 /* Comments. */
44995b2e
AD
406 "/*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
407 "//".* YY_OBS_GROW;
e9955c83
AD
408}
409
410
411 /*---------------------------------------------------------------.
412 | Scanning some code in braces (%union and actions). The initial |
413 | "{" is already eaten. |
414 `---------------------------------------------------------------*/
415
416<SC_BRACED_CODE>
417{
418 "}" {
44995b2e 419 YY_OBS_GROW;
e9955c83
AD
420 if (--braces_level == 0)
421 {
422 yy_pop_state ();
44995b2e 423 YY_OBS_FINISH;
e9955c83
AD
424 return BRACED_CODE;
425 }
426 }
427
44995b2e 428 "{" YY_OBS_GROW; braces_level++;
e9955c83
AD
429
430 "$"("<".*">")?(-?[0-9]+|"$") { handle_dollar (yytext); }
431 "@"(-?[0-9]+|"$") { handle_at (yytext); }
432
44995b2e
AD
433 [^\[\]$/\'\"@\{\}\n\r]+ YY_OBS_GROW;
434 {eols} YY_OBS_GROW; YY_LINES;
e9955c83
AD
435
436 /* A lose $, or /, or etc. */
44995b2e 437 . YY_OBS_GROW;
e9955c83
AD
438
439 <<EOF>> {
440 LOCATION_PRINT (stderr, *yylloc);
441 fprintf (stderr, ": unexpected end of file in a braced code\n");
442 yy_pop_state ();
44995b2e 443 YY_OBS_FINISH;
e9955c83
AD
444 return PROLOGUE;
445 }
446
447}
448
449
450 /*--------------------------------------------------------------.
451 | Scanning some prologue: from "%{" (already scanned) to "%}". |
452 `--------------------------------------------------------------*/
453
454<SC_PROLOGUE>
455{
456 "%}" {
457 yy_pop_state ();
44995b2e 458 YY_OBS_FINISH;
e9955c83
AD
459 return PROLOGUE;
460 }
461
44995b2e
AD
462 [^\[\]%\n\r]+ YY_OBS_GROW;
463 "%"+[^%\}\n\r]+ YY_OBS_GROW;
464 {eols} YY_OBS_GROW; YY_LINES;
e9955c83
AD
465
466 <<EOF>> {
467 LOCATION_PRINT (stderr, *yylloc);
468 fprintf (stderr, ": unexpected end of file in a prologue\n");
469 yy_pop_state ();
44995b2e 470 YY_OBS_FINISH;
e9955c83
AD
471 return PROLOGUE;
472 }
473
474}
475
476
477 /*---------------------------------------------------------------.
478 | Scanning the epilogue (everything after the second "%%", which |
479 | has already been eaten. |
480 `---------------------------------------------------------------*/
481
482<SC_EPILOGUE>
483{
44995b2e 484 ([^\[\]]|{eols})+ YY_OBS_GROW;
e9955c83
AD
485
486 <<EOF>> {
487 yy_pop_state ();
44995b2e 488 YY_OBS_FINISH;
e9955c83
AD
489 return EPILOGUE;
490 }
491}
492
493
494%%
495
496/*------------------------------------------------------------------.
497| CP is pointing to a wannabee semantic value (i.e., a `$'). |
498| |
499| Possible inputs: $[<TYPENAME>]($|integer) |
500| |
501| Output to the STRING_OBSTACK a reference to this semantic value. |
502`------------------------------------------------------------------*/
503
504static void
505handle_dollar (char *cp)
506{
507 const char *type_name = NULL;
508
509 /* RULE_LENGTH is the number of values in the current rule so far,
510 which says where to find `$0' with respect to the top of the
511 stack. It is not the same as the rule->length in the case of mid
512 rule actions. */
513 int rule_length = 0;
514 symbol_list *rhs;
515 for (rhs = current_rule->next; rhs; rhs = rhs->next)
516 ++rule_length;
517
518 ++cp;
519
520 /* Get the type name if explicit. */
521 if (*cp == '<')
522 {
523 type_name = ++cp;
524 while (*cp != '>')
525 ++cp;
526 *cp = '\0';
527 ++cp;
528 }
529
530 if (*cp == '$')
531 {
532 if (!type_name)
533 type_name = get_type_name (0, current_rule);
534 if (!type_name && typed)
535 complain (_("$$ of `%s' has no declared type"),
536 current_rule->sym->tag);
537 if (!type_name)
538 type_name = "";
539 obstack_fgrow1 (&string_obstack,
540 "]b4_lhs_value([%s])[", type_name);
541 }
542 else if (isdigit (*cp) || *cp == '-')
543 {
544 int n = strtol (cp, &cp, 10);
545
546 if (n > rule_length)
547 complain (_("invalid value: %s%d"), "$", n);
548 else
549 {
550 if (!type_name && n > 0)
551 type_name = get_type_name (n, current_rule);
552 if (!type_name && typed)
553 complain (_("$%d of `%s' has no declared type"),
554 n, current_rule->sym->tag);
555 if (!type_name)
556 type_name = "";
557 obstack_fgrow3 (&string_obstack,
558 "]b4_rhs_value([%d], [%d], [%s])[",
559 rule_length, n, type_name);
560 }
561 }
562 else
563 {
564 char buf[] = "$c";
565 buf[1] = *cp;
566 complain (_("%s is invalid"), quote (buf));
567 }
568}
569
570/*-------------------------------------------------------.
571| CP is pointing to a location (i.e., a `@'). Output to |
572| STRING_OBSTACK a reference to this location. |
573`-------------------------------------------------------*/
574
575static void
576handle_at (char *cp)
577{
578 /* RULE_LENGTH is the number of values in the current rule so far,
579 which says where to find `$0' with respect to the top of the
580 stack. It is not the same as the rule->length in the case of mid
581 rule actions. */
582 int rule_length = 0;
583 symbol_list *rhs;
584 for (rhs = current_rule->next; rhs; rhs = rhs->next)
585 ++rule_length;
586
587 locations_flag = 1;
588 ++cp;
589
590 if (*cp == '$')
591 {
592 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
593 }
594 else if (isdigit (*cp) || *cp == '-')
595 {
596 int n = strtol (cp, &cp, 10);
597 if (n > rule_length)
598 complain (_("invalid value: %s%d"), "@", n);
599 else
600 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
601 rule_length, n);
602 }
603 else
604 {
605 char buf[] = "@c";
606 buf[1] = *cp;
607 complain (_("%s is invalid"), quote (buf));
608 }
609}