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