]> git.saurik.com Git - bison.git/blob - src/scan-gram.l
68d22afe7d5bd903306e239b122c4b176e0dbb53
[bison.git] / src / scan-gram.l
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_INIT \
35 do { \
36 LOCATION_RESET (*yylloc); \
37 /* This is only to avoid GCC warnings. */ \
38 if (yycontrol) {;}; \
39 } while (0)
40
41 #define YY_USER_ACTION LOCATION_COLUMNS (*yylloc, yyleng)
42 #define YY_LINES LOCATION_LINES (*yylloc, yyleng); lineno += yyleng;
43 #define YY_STEP LOCATION_STEP (*yylloc)
44
45 /* STRING_OBSTACK -- Used to store all the characters that we need to
46 keep (to construct ID, STRINGS etc.). Use the following macros to
47 use it.
48
49 Use YY_OBS_GROW to append what has just been matched, and
50 YY_OBS_FINISH to end the string (it puts the ending 0).
51 YY_OBS_FINISH also stores this string in LAST_STRING, which can be
52 used, and which is used by YY_OBS_FREE to free the last string. */
53
54 static struct obstack string_obstack;
55 char *last_string;
56
57 #define YY_OBS_GROW \
58 obstack_grow (&string_obstack, yytext, yyleng)
59
60 #define YY_OBS_FINISH \
61 do { \
62 obstack_1grow (&string_obstack, '\0'); \
63 last_string = obstack_finish (&string_obstack); \
64 } while (0)
65
66 #define YY_OBS_FREE \
67 do { \
68 obstack_free (&string_obstack, last_string); \
69 } while (0)
70
71 void
72 scanner_last_string_free (void)
73 {
74 YY_OBS_FREE;
75 }
76
77
78 static int braces_level = 0;
79 static int percent_percent_count = 0;
80
81 static void handle_dollar PARAMS ((braced_code_t code_kind,
82 char *cp, location_t location));
83 static void handle_at PARAMS ((braced_code_t code_kind,
84 char *cp, location_t location));
85
86 %}
87 %x SC_COMMENT
88 %x SC_STRING SC_CHARACTER
89 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
90 %x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
91
92 id [.a-zA-Z][.a-zA-Z_0-9]*
93 int [0-9]+
94 eols (\n|\r|\n\r|\r\n)+
95 blanks [ \t\f]+
96
97 %%
98 %{
99 /* At each yylex invocation, mark the current position as the
100 start of the next token. */
101 #define TR_POS 0
102 #if TR_POS
103 fprintf (stderr, "FOO1: %p: ", yylloc);
104 LOCATION_PRINT (stderr, *yylloc);
105 fprintf (stderr, "\n");
106 #endif
107 YY_STEP;
108 #if TR_POS
109 fprintf (stderr, "BAR1: ");
110 LOCATION_PRINT (stderr, *yylloc);
111 fprintf (stderr, "\n");
112 #endif
113 %}
114
115
116 /*----------------------------.
117 | Scanning Bison directives. |
118 `----------------------------*/
119 <INITIAL>
120 {
121 "%binary" return PERCENT_NONASSOC;
122 "%debug" return PERCENT_DEBUG;
123 "%define" return PERCENT_DEFINE;
124 "%defines" return PERCENT_DEFINES;
125 "%destructor" return PERCENT_DESTRUCTOR;
126 "%dprec" return PERCENT_DPREC;
127 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
128 "%expect" return PERCENT_EXPECT;
129 "%file-prefix" return PERCENT_FILE_PREFIX;
130 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
131 "%glr"[-_]"parser" return PERCENT_GLR_PARSER;
132 "%left" return PERCENT_LEFT;
133 "%locations" return PERCENT_LOCATIONS;
134 "%merge" return PERCENT_MERGE;
135 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
136 "%no"[-_]"lines" return PERCENT_NO_LINES;
137 "%nonassoc" return PERCENT_NONASSOC;
138 "%nterm" return PERCENT_NTERM;
139 "%output" return PERCENT_OUTPUT;
140 "%prec" return PERCENT_PREC;
141 "%printer" return PERCENT_PRINTER;
142 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
143 "%right" return PERCENT_RIGHT;
144 "%skeleton" return PERCENT_SKELETON;
145 "%start" return PERCENT_START;
146 "%term" return PERCENT_TOKEN;
147 "%token" return PERCENT_TOKEN;
148 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
149 "%type" return PERCENT_TYPE;
150 "%union" return PERCENT_UNION;
151 "%verbose" return PERCENT_VERBOSE;
152 "%yacc" return PERCENT_YACC;
153
154 "=" return EQUAL;
155 ":" return COLON;
156 "|" return PIPE;
157 ";" return SEMICOLON;
158
159 {eols} YY_LINES; YY_STEP;
160 {blanks} YY_STEP;
161 {id} {
162 yylval->symbol = symbol_get (yytext, *yylloc);
163 return ID;
164 }
165
166 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
167
168 /* Characters. We don't check there is only one. */
169 \' YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
170
171 /* Strings. */
172 \" YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
173
174 /* Comments. */
175 "/*" yy_push_state (SC_COMMENT);
176 "//".* YY_STEP;
177
178 /* Prologue. */
179 "%{" yy_push_state (SC_PROLOGUE);
180
181 /* Code in between braces. */
182 "{" YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
183
184 /* A type. */
185 "<"[^>]+">" {
186 obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
187 YY_OBS_FINISH;
188 yylval->string = last_string;
189 return TYPE;
190 }
191
192
193 "%%" {
194 if (++percent_percent_count == 2)
195 yy_push_state (SC_EPILOGUE);
196 return PERCENT_PERCENT;
197 }
198
199 . {
200 LOCATION_PRINT (stderr, *yylloc);
201 fprintf (stderr, ": invalid character: `%c'\n", *yytext);
202 YY_STEP;
203 }
204 }
205
206
207 /*------------------------------------------------------------.
208 | Whatever the start condition (but those which correspond to |
209 | entity `swallowed' by Bison: SC_ESCAPED_STRING and |
210 | SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
211 `------------------------------------------------------------*/
212
213 <SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
214 {
215 \[ if (YY_START != SC_COMMENT) obstack_sgrow (&string_obstack, "@<:@");
216 \] if (YY_START != SC_COMMENT) obstack_sgrow (&string_obstack, "@:>@");
217 }
218
219
220
221 /*-----------------------------------------------------------.
222 | Scanning a C comment. The initial `/ *' is already eaten. |
223 `-----------------------------------------------------------*/
224
225 <SC_COMMENT>
226 {
227 "*/" { /* End of the comment. */
228 if (yy_top_state () == INITIAL)
229 {
230 YY_STEP;
231 }
232 else
233 {
234 YY_OBS_GROW;
235 }
236 yy_pop_state ();
237 }
238
239 [^\[\]*\n\r]+ if (yy_top_state () != INITIAL) YY_OBS_GROW;
240 {eols} if (yy_top_state () != INITIAL) YY_OBS_GROW; YY_LINES;
241 . /* Stray `*'. */if (yy_top_state () != INITIAL) YY_OBS_GROW;
242
243 <<EOF>> {
244 LOCATION_PRINT (stderr, *yylloc);
245 fprintf (stderr, ": unexpected end of file in a comment\n");
246 yy_pop_state ();
247 }
248 }
249
250
251 /*----------------------------------------------------------------.
252 | Scanning a C string, including its escapes. The initial `"' is |
253 | already eaten. |
254 `----------------------------------------------------------------*/
255
256 <SC_ESCAPED_STRING>
257 {
258 \" {
259 assert (yy_top_state () == INITIAL);
260 YY_OBS_GROW;
261 YY_OBS_FINISH;
262 yylval->string = last_string;
263 yy_pop_state ();
264 return STRING;
265 }
266
267 [^\"\n\r\\]+ YY_OBS_GROW;
268
269 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
270
271 <<EOF>> {
272 LOCATION_PRINT (stderr, *yylloc);
273 fprintf (stderr, ": unexpected end of file in a string\n");
274 assert (yy_top_state () == INITIAL);
275 YY_OBS_FINISH;
276 yylval->string = last_string;
277 yy_pop_state ();
278 return STRING;
279 }
280 }
281
282 /*---------------------------------------------------------------.
283 | Scanning a C character, decoding its escapes. The initial "'" |
284 | is already eaten. |
285 `---------------------------------------------------------------*/
286
287 <SC_ESCAPED_CHARACTER>
288 {
289 \' {
290 YY_OBS_GROW;
291 assert (yy_top_state () == INITIAL);
292 {
293 YY_OBS_FINISH;
294 yylval->symbol = symbol_get (last_string, *yylloc);
295 symbol_class_set (yylval->symbol, token_sym, *yylloc);
296 symbol_user_token_number_set (yylval->symbol,
297 (unsigned char) last_string[1], *yylloc);
298 YY_OBS_FREE;
299 yy_pop_state ();
300 return ID;
301 }
302 }
303
304 [^\'\n\r\\] YY_OBS_GROW;
305
306 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
307
308 <<EOF>> {
309 LOCATION_PRINT (stderr, *yylloc);
310 fprintf (stderr, ": unexpected end of file in a character\n");
311 assert (yy_top_state () == INITIAL);
312 YY_OBS_FINISH;
313 yylval->string = last_string;
314 yy_pop_state ();
315 return CHARACTER;
316 }
317 }
318
319
320 /*----------------------------.
321 | Decode escaped characters. |
322 `----------------------------*/
323
324 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
325 {
326 \\[0-7]{3} {
327 long c = strtol (yytext + 1, 0, 8);
328 if (c > 255)
329 {
330 LOCATION_PRINT (stderr, *yylloc);
331 fprintf (stderr, ": invalid escape: %s\n", quote (yytext));
332 YY_STEP;
333 }
334 else
335 obstack_1grow (&string_obstack, c);
336 }
337
338 \\x[0-9a-fA-F]{2} {
339 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
340 }
341
342 \\a obstack_1grow (&string_obstack, '\a');
343 \\b obstack_1grow (&string_obstack, '\b');
344 \\f obstack_1grow (&string_obstack, '\f');
345 \\n obstack_1grow (&string_obstack, '\n');
346 \\r obstack_1grow (&string_obstack, '\r');
347 \\t obstack_1grow (&string_obstack, '\t');
348 \\v obstack_1grow (&string_obstack, '\v');
349 \\[\\""] obstack_1grow (&string_obstack, yytext[1]);
350 \\(.|\n) {
351 LOCATION_PRINT (stderr, *yylloc);
352 fprintf (stderr, ": unrecognized escape: %s\n", quote (yytext));
353 YY_OBS_GROW;
354 }
355 /* FLex wants this rule, in case of a `\<<EOF>>'. */
356 \\ YY_OBS_GROW;
357 }
358
359
360 /*----------------------------------------------------------.
361 | Scanning a C character without decoding its escapes. The |
362 | initial "'" is already eaten. |
363 `----------------------------------------------------------*/
364
365 <SC_CHARACTER>
366 {
367 \' {
368 YY_OBS_GROW;
369 assert (yy_top_state () != INITIAL);
370 yy_pop_state ();
371 }
372
373 [^\[\]\'\n\r\\]+ YY_OBS_GROW;
374 \\(.|\n) YY_OBS_GROW;
375 /* FLex wants this rule, in case of a `\<<EOF>>'. */
376 \\ YY_OBS_GROW;
377
378 {eols} YY_OBS_GROW; YY_LINES;
379
380 <<EOF>> {
381 LOCATION_PRINT (stderr, *yylloc);
382 fprintf (stderr, ": unexpected end of file in a character\n");
383 assert (yy_top_state () != INITIAL);
384 yy_pop_state ();
385 }
386 }
387
388
389 /*----------------------------------------------------------------.
390 | Scanning a C string, without decoding its escapes. The initial |
391 | `"' is already eaten. |
392 `----------------------------------------------------------------*/
393
394 <SC_STRING>
395 {
396 \" {
397 assert (yy_top_state () != INITIAL);
398 YY_OBS_GROW;
399 yy_pop_state ();
400 }
401
402 [^\[\]\"\n\r\\]+ YY_OBS_GROW;
403 \\(.|\n) YY_OBS_GROW;
404 /* FLex wants this rule, in case of a `\<<EOF>>'. */
405 \\ YY_OBS_GROW;
406
407 {eols} YY_OBS_GROW; YY_LINES;
408
409 <<EOF>> {
410 LOCATION_PRINT (stderr, *yylloc);
411 fprintf (stderr, ": unexpected end of file in a string\n");
412 assert (yy_top_state () != INITIAL);
413 yy_pop_state ();
414 }
415 }
416
417
418 /*---------------------------------------------------.
419 | Strings, comments etc. can be found in user code. |
420 `---------------------------------------------------*/
421
422 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
423 {
424 /* Characters. We don't check there is only one. */
425 \' YY_OBS_GROW; yy_push_state (SC_CHARACTER);
426
427 /* Strings. */
428 \" YY_OBS_GROW; yy_push_state (SC_STRING);
429
430 /* Comments. */
431 "/*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
432 "//".* YY_OBS_GROW;
433
434 /* Not comments. */
435 "/" YY_OBS_GROW;
436 }
437
438
439 /*---------------------------------------------------------------.
440 | Scanning some code in braces (%union and actions). The initial |
441 | "{" is already eaten. |
442 `---------------------------------------------------------------*/
443
444 <SC_BRACED_CODE>
445 {
446 "}" {
447 YY_OBS_GROW;
448 if (--braces_level == 0)
449 {
450 yy_pop_state ();
451 YY_OBS_FINISH;
452 yylval->string = last_string;
453 return BRACED_CODE;
454 }
455 }
456
457 "{" YY_OBS_GROW; braces_level++;
458
459 "$"("<"[^>]+">")?(-?[0-9]+|"$") { handle_dollar (current_braced_code,
460 yytext, *yylloc); }
461 "@"(-?[0-9]+|"$") { handle_at (current_braced_code,
462 yytext, *yylloc); }
463
464 [^$@\[\]/\'\"\{\}\n\r]+ YY_OBS_GROW;
465 {eols} YY_OBS_GROW; YY_LINES;
466
467 /* A lose $, or /, or etc. */
468 . YY_OBS_GROW;
469
470 <<EOF>> {
471 LOCATION_PRINT (stderr, *yylloc);
472 fprintf (stderr, ": unexpected end of file in a braced code\n");
473 yy_pop_state ();
474 YY_OBS_FINISH;
475 yylval->string = last_string;
476 return BRACED_CODE;
477 }
478
479 }
480
481
482 /*--------------------------------------------------------------.
483 | Scanning some prologue: from "%{" (already scanned) to "%}". |
484 `--------------------------------------------------------------*/
485
486 <SC_PROLOGUE>
487 {
488 "%}" {
489 yy_pop_state ();
490 YY_OBS_FINISH;
491 yylval->string = last_string;
492 return PROLOGUE;
493 }
494
495 [^%\[\]/\'\"\n\r]+ YY_OBS_GROW;
496 "%" YY_OBS_GROW;
497 {eols} YY_OBS_GROW; YY_LINES;
498
499 <<EOF>> {
500 LOCATION_PRINT (stderr, *yylloc);
501 fprintf (stderr, ": unexpected end of file in a prologue\n");
502 yy_pop_state ();
503 YY_OBS_FINISH;
504 yylval->string = last_string;
505 return PROLOGUE;
506 }
507 }
508
509
510 /*---------------------------------------------------------------.
511 | Scanning the epilogue (everything after the second "%%", which |
512 | has already been eaten. |
513 `---------------------------------------------------------------*/
514
515 <SC_EPILOGUE>
516 {
517 ([^\[\]]|{eols})+ YY_OBS_GROW;
518
519 <<EOF>> {
520 yy_pop_state ();
521 YY_OBS_FINISH;
522 yylval->string = last_string;
523 return EPILOGUE;
524 }
525 }
526
527
528 %%
529
530 /*------------------------------------------------------------------.
531 | TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
532 | |
533 | Possible inputs: $[<TYPENAME>]($|integer) |
534 | |
535 | Output to the STRING_OBSTACK a reference to this semantic value. |
536 `------------------------------------------------------------------*/
537
538 static inline void
539 handle_action_dollar (char *text, location_t location)
540 {
541 const char *type_name = NULL;
542 char *cp = text + 1;
543
544 /* Get the type name if explicit. */
545 if (*cp == '<')
546 {
547 type_name = ++cp;
548 while (*cp != '>')
549 ++cp;
550 *cp = '\0';
551 ++cp;
552 }
553
554 if (*cp == '$')
555 {
556 if (!type_name)
557 type_name = symbol_list_n_type_name_get (current_rule, location, 0);
558 if (!type_name && typed)
559 complain_at (location, _("$$ of `%s' has no declared type"),
560 current_rule->sym->tag);
561 if (!type_name)
562 type_name = "";
563 obstack_fgrow1 (&string_obstack,
564 "]b4_lhs_value([%s])[", type_name);
565 }
566 else if (isdigit (*cp) || *cp == '-')
567 {
568 /* RULE_LENGTH is the number of values in the current rule so
569 far, which says where to find `$0' with respect to the top of
570 the stack. It is not the same as the rule->length in the
571 case of mid rule actions. */
572 int rule_length = symbol_list_length (current_rule->next);
573 int n = strtol (cp, &cp, 10);
574
575 if (n > rule_length)
576 complain_at (location, _("invalid value: %s%d"), "$", n);
577 else
578 {
579 if (!type_name && n > 0)
580 type_name = symbol_list_n_type_name_get (current_rule, location,
581 n);
582 if (!type_name && typed)
583 complain_at (location, _("$%d of `%s' has no declared type"),
584 n, current_rule->sym->tag);
585 if (!type_name)
586 type_name = "";
587 obstack_fgrow3 (&string_obstack,
588 "]b4_rhs_value([%d], [%d], [%s])[",
589 rule_length, n, type_name);
590 }
591 }
592 else
593 {
594 complain_at (location, _("%s is invalid"), quote (text));
595 }
596 }
597
598
599 /*---------------------------------------------------------------.
600 | TEXT is expexted tp be $$ in some code associated to a symbol: |
601 | destructor or printer. |
602 `---------------------------------------------------------------*/
603
604 static inline void
605 handle_symbol_code_dollar (char *text, location_t location)
606 {
607 char *cp = text + 1;
608 if (*cp == '$')
609 obstack_sgrow (&string_obstack, "]b4_dollar_dollar[");
610 else
611 complain_at (location, _("%s is invalid"), quote (text));
612 }
613
614
615 /*-----------------------------------------------------------------.
616 | Dispatch onto handle_action_dollar, or handle_destructor_dollar, |
617 | depending upon CODE_KIND. |
618 `-----------------------------------------------------------------*/
619
620 static void
621 handle_dollar (braced_code_t braced_code_kind,
622 char *text, location_t location)
623 {
624 switch (braced_code_kind)
625 {
626 case action_braced_code:
627 handle_action_dollar (text, location);
628 break;
629
630 case destructor_braced_code:
631 case printer_braced_code:
632 handle_symbol_code_dollar (text, location);
633 break;
634 }
635 }
636
637
638 /*------------------------------------------------------.
639 | TEXT is a location token (i.e., a `@...'). Output to |
640 | STRING_OBSTACK a reference to this location. |
641 `------------------------------------------------------*/
642
643 static inline void
644 handle_action_at (char *text, location_t location)
645 {
646 char *cp = text + 1;
647 locations_flag = 1;
648
649 if (*cp == '$')
650 {
651 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
652 }
653 else if (isdigit (*cp) || *cp == '-')
654 {
655 /* RULE_LENGTH is the number of values in the current rule so
656 far, which says where to find `$0' with respect to the top of
657 the stack. It is not the same as the rule->length in the
658 case of mid rule actions. */
659 int rule_length = symbol_list_length (current_rule->next);
660 int n = strtol (cp, &cp, 10);
661
662 if (n > rule_length)
663 complain_at (location, _("invalid value: %s%d"), "@", n);
664 else
665 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
666 rule_length, n);
667 }
668 else
669 {
670 complain_at (location, _("%s is invalid"), quote (text));
671 }
672 }
673
674
675 /*---------------------------------------------------------------.
676 | TEXT is expexted tp be @$ in some code associated to a symbol: |
677 | destructor or printer. |
678 `---------------------------------------------------------------*/
679
680 static inline void
681 handle_symbol_code_at (char *text, location_t location)
682 {
683 char *cp = text + 1;
684 if (*cp == '$')
685 obstack_sgrow (&string_obstack, "]b4_at_dollar[");
686 else
687 complain_at (location, _("%s is invalid"), quote (text));
688 }
689
690
691 /*-------------------------------------------------------------------.
692 | Dispatch onto handle_action_at, or handle_destructor_at, depending |
693 | upon CODE_KIND. |
694 `-------------------------------------------------------------------*/
695
696 static void
697 handle_at (braced_code_t braced_code_kind,
698 char *text, location_t location)
699 {
700 switch (braced_code_kind)
701 {
702 case action_braced_code:
703 handle_action_at (text, location);
704 break;
705
706 case destructor_braced_code:
707 case printer_braced_code:
708 handle_symbol_code_at (text, location);
709 break;
710 }
711 }
712
713
714 /*-------------------------.
715 | Initialize the scanner. |
716 `-------------------------*/
717
718 void
719 scanner_initialize (void)
720 {
721 obstack_init (&string_obstack);
722 }
723
724
725 /*-----------------------------------------------.
726 | Free all the memory allocated to the scanner. |
727 `-----------------------------------------------*/
728
729 void
730 scanner_free (void)
731 {
732 obstack_free (&string_obstack, 0);
733 /* Reclaim Flex's buffers. */
734 yy_delete_buffer (YY_CURRENT_BUFFER);
735 }