1 /* Bison Grammar Scanner -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 %option debug nodefault nounput noyywrap never-interactive
24 %option prefix="gram_" outfile="lex.yy.c"
27 /* Work around a bug in flex 2.5.31. See Debian bug 333231
28 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
32 #define FLEX_PREFIX(Id) gram_ ## Id
33 #include "flex-scanner.h"
45 #include "scan-gram.h"
47 #define YY_DECL GRAM_LEX_DECL
49 #define YY_USER_INIT \
50 code_start = scanner_cursor = loc->start; \
52 /* Location of scanner cursor. */
53 static boundary scanner_cursor;
55 #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
57 static size_t no_cr_read (FILE *, char *, size_t);
58 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
60 /* A string representing the most recently saved token. */
64 gram_scanner_last_string_free (void)
69 /* The location of the most recently saved token, if it was a
70 BRACED_CODE token; otherwise, this has an unspecified value. */
71 location gram_last_braced_code_loc;
73 static void handle_syncline (char *, location);
74 static unsigned long int scan_integer (char const *p, int base, location loc);
75 static int convert_ucn_to_byte (char const *hex_text);
76 static void unexpected_eof (boundary, char const *);
77 static void unexpected_newline (boundary, char const *);
80 /* A C-like comment in directives/rules. */
82 /* Strings and characters in directives/rules. */
83 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
84 /* A identifier was just read in directives/rules. Special state
85 to capture the sequence `identifier :'. */
86 %x SC_AFTER_IDENTIFIER
88 /* Three types of user code:
89 - prologue (code between `%{' `%}' in the first section, before %%);
90 - actions, printers, union, etc, (between braced in the middle section);
91 - epilogue (everything after the second %%). */
92 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
93 /* C and C++ comments in code. */
94 %x SC_COMMENT SC_LINE_COMMENT
95 /* Strings and characters in code. */
96 %x SC_STRING SC_CHARACTER
98 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
99 id {letter}({letter}|[0-9])*
100 directive %{letter}({letter}|[0-9]|-)*
103 /* POSIX says that a tag must be both an id and a C union member, but
104 historically almost any character is allowed in a tag. We disallow
105 NUL and newline, as this simplifies our implementation. */
108 /* Zero or more instances of backslash-newline. Following GCC, allow
109 white space between the backslash and the newline. */
110 splice (\\[ \f\t\v]*\n)*
114 /* Nesting level of the current code in braces. */
115 int braces_level IF_LINT (= 0);
117 /* Parent context state, when applicable. */
118 int context_state IF_LINT (= 0);
120 /* Location of most recent identifier, when applicable. */
121 location id_loc IF_LINT (= empty_location);
123 /* Where containing code started, when applicable. Its initial
124 value is relevant only when yylex is invoked in the SC_EPILOGUE
126 boundary code_start = scanner_cursor;
128 /* Where containing comment or string or character literal started,
130 boundary token_start IF_LINT (= scanner_cursor);
134 /*-----------------------.
135 | Scanning white space. |
136 `-----------------------*/
138 <INITIAL,SC_AFTER_IDENTIFIER>
140 /* Comments and white space. */
141 "," warn_at (*loc, _("stray `,' treated as white space"));
145 token_start = loc->start;
146 context_state = YY_START;
147 BEGIN SC_YACC_COMMENT;
150 /* #line directives are not documented, and may be withdrawn or
151 modified in future versions of Bison. */
152 ^"#line "{int}" \"".*"\"\n" {
153 handle_syncline (yytext + sizeof "#line " - 1, *loc);
158 /*----------------------------.
159 | Scanning Bison directives. |
160 `----------------------------*/
163 "%after-header" return PERCENT_AFTER_HEADER;
164 "%before-header" return PERCENT_BEFORE_HEADER;
165 "%binary" return PERCENT_NONASSOC;
166 "%debug" return PERCENT_DEBUG;
167 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
168 "%define" return PERCENT_DEFINE;
169 "%defines" return PERCENT_DEFINES;
170 "%destructor" return PERCENT_DESTRUCTOR;
171 "%dprec" return PERCENT_DPREC;
172 "%end-header" return PERCENT_END_HEADER;
173 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
174 "%expect" return PERCENT_EXPECT;
175 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
176 "%file-prefix" return PERCENT_FILE_PREFIX;
177 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
178 "%initial-action" return PERCENT_INITIAL_ACTION;
179 "%glr-parser" return PERCENT_GLR_PARSER;
180 "%left" return PERCENT_LEFT;
181 "%lex-param" return PERCENT_LEX_PARAM;
182 "%locations" return PERCENT_LOCATIONS;
183 "%merge" return PERCENT_MERGE;
184 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
185 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
186 "%no"[-_]"lines" return PERCENT_NO_LINES;
187 "%nonassoc" return PERCENT_NONASSOC;
188 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
189 "%nterm" return PERCENT_NTERM;
190 "%output" return PERCENT_OUTPUT;
191 "%parse-param" return PERCENT_PARSE_PARAM;
192 "%prec" return PERCENT_PREC;
193 "%printer" return PERCENT_PRINTER;
194 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
195 "%require" return PERCENT_REQUIRE;
196 "%right" return PERCENT_RIGHT;
197 "%skeleton" return PERCENT_SKELETON;
198 "%start" return PERCENT_START;
199 "%start-header" return PERCENT_START_HEADER;
200 "%term" return PERCENT_TOKEN;
201 "%token" return PERCENT_TOKEN;
202 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
203 "%type" return PERCENT_TYPE;
204 "%union" return PERCENT_UNION;
205 "%verbose" return PERCENT_VERBOSE;
206 "%yacc" return PERCENT_YACC;
209 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
214 ";" return SEMICOLON;
217 val->uniqstr = uniqstr_new (yytext);
219 BEGIN SC_AFTER_IDENTIFIER;
223 val->integer = scan_integer (yytext, 10, *loc);
226 0[xX][0-9abcdefABCDEF]+ {
227 val->integer = scan_integer (yytext, 16, *loc);
231 /* Characters. We don't check there is only one. */
232 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
235 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
238 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
240 /* Code in between braces. */
244 code_start = loc->start;
245 BEGIN SC_BRACED_CODE;
250 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
252 val->uniqstr = uniqstr_new (last_string);
258 static int percent_percent_count;
259 if (++percent_percent_count == 2)
261 return PERCENT_PERCENT;
265 complain_at (*loc, _("invalid character: %s"), quote (yytext));
269 loc->start = loc->end = scanner_cursor;
275 /*-----------------------------------------------------------------.
276 | Scanning after an identifier, checking whether a colon is next. |
277 `-----------------------------------------------------------------*/
279 <SC_AFTER_IDENTIFIER>
287 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
301 /*---------------------------------------------------------------.
302 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
303 `---------------------------------------------------------------*/
307 "*/" BEGIN context_state;
309 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
313 /*------------------------------------------------------------.
314 | Scanning a C comment. The initial `/ *' is already eaten. |
315 `------------------------------------------------------------*/
319 "*"{splice}"/" STRING_GROW; BEGIN context_state;
320 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
324 /*--------------------------------------------------------------.
325 | Scanning a line comment. The initial `//' is already eaten. |
326 `--------------------------------------------------------------*/
330 "\n" STRING_GROW; BEGIN context_state;
331 {splice} STRING_GROW;
332 <<EOF>> BEGIN context_state;
336 /*------------------------------------------------.
337 | Scanning a Bison string, including its escapes. |
338 | The initial quote is already eaten. |
339 `------------------------------------------------*/
345 loc->start = token_start;
346 val->chars = last_string;
350 \n unexpected_newline (token_start, "\""); BEGIN INITIAL;
351 <<EOF>> unexpected_eof (token_start, "\""); BEGIN INITIAL;
354 /*----------------------------------------------------------.
355 | Scanning a Bison character literal, decoding its escapes. |
356 | The initial quote is already eaten. |
357 `----------------------------------------------------------*/
359 <SC_ESCAPED_CHARACTER>
364 loc->start = token_start;
365 val->character = last_string[1];
370 \n unexpected_newline (token_start, "'"); BEGIN INITIAL;
371 <<EOF>> unexpected_eof (token_start, "'"); BEGIN INITIAL;
374 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
376 \0 complain_at (*loc, _("invalid null character"));
380 /*----------------------------.
381 | Decode escaped characters. |
382 `----------------------------*/
384 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
387 unsigned long int c = strtoul (yytext + 1, NULL, 8);
389 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
391 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
393 obstack_1grow (&obstack_for_string, c);
396 \\x[0-9abcdefABCDEF]+ {
397 verify (UCHAR_MAX < ULONG_MAX);
398 unsigned long int c = strtoul (yytext + 2, NULL, 16);
400 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
402 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
404 obstack_1grow (&obstack_for_string, c);
407 \\a obstack_1grow (&obstack_for_string, '\a');
408 \\b obstack_1grow (&obstack_for_string, '\b');
409 \\f obstack_1grow (&obstack_for_string, '\f');
410 \\n obstack_1grow (&obstack_for_string, '\n');
411 \\r obstack_1grow (&obstack_for_string, '\r');
412 \\t obstack_1grow (&obstack_for_string, '\t');
413 \\v obstack_1grow (&obstack_for_string, '\v');
415 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
416 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
418 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
419 int c = convert_ucn_to_byte (yytext);
421 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
423 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
425 obstack_1grow (&obstack_for_string, c);
428 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
433 /*--------------------------------------------.
434 | Scanning user-code characters and strings. |
435 `--------------------------------------------*/
437 <SC_CHARACTER,SC_STRING>
439 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
444 "'" STRING_GROW; BEGIN context_state;
445 \n unexpected_newline (token_start, "'"); BEGIN context_state;
446 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
451 "\"" STRING_GROW; BEGIN context_state;
452 \n unexpected_newline (token_start, "\""); BEGIN context_state;
453 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
457 /*---------------------------------------------------.
458 | Strings, comments etc. can be found in user code. |
459 `---------------------------------------------------*/
461 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
465 context_state = YY_START;
466 token_start = loc->start;
471 context_state = YY_START;
472 token_start = loc->start;
477 context_state = YY_START;
478 token_start = loc->start;
483 context_state = YY_START;
484 BEGIN SC_LINE_COMMENT;
490 /*-----------------------------------------------------------.
491 | Scanning some code in braces (actions). The initial "{" is |
493 `-----------------------------------------------------------*/
497 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
498 "%"{splice}">" STRING_GROW; braces_level--;
500 obstack_1grow (&obstack_for_string, '}');
503 if (braces_level < 0)
506 loc->start = code_start;
507 val->chars = last_string;
508 gram_last_braced_code_loc = *loc;
514 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
516 "<"{splice}"<" STRING_GROW;
518 <<EOF>> unexpected_eof (code_start, "}"); BEGIN INITIAL;
522 /*--------------------------------------------------------------.
523 | Scanning some prologue: from "%{" (already scanned) to "%}". |
524 `--------------------------------------------------------------*/
530 loc->start = code_start;
531 val->chars = last_string;
536 <<EOF>> unexpected_eof (code_start, "%}"); BEGIN INITIAL;
540 /*---------------------------------------------------------------.
541 | Scanning the epilogue (everything after the second "%%", which |
542 | has already been eaten). |
543 `---------------------------------------------------------------*/
549 loc->start = code_start;
550 val->chars = last_string;
557 /*-----------------------------------------------------.
558 | By default, grow the string obstack with the input. |
559 `-----------------------------------------------------*/
561 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
562 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
566 /* Read bytes from FP into buffer BUF of size SIZE. Return the
567 number of bytes read. Remove '\r' from input, treating \r\n
568 and isolated \r as \n. */
571 no_cr_read (FILE *fp, char *buf, size_t size)
573 size_t bytes_read = fread (buf, 1, size, fp);
576 char *w = memchr (buf, '\r', bytes_read);
580 char const *lim = buf + bytes_read;
584 /* Found an '\r'. Treat it like '\n', but ignore any
585 '\n' that immediately follows. */
590 if (ch != '\n' && ungetc (ch, fp) != ch)
596 /* Copy until the next '\r'. */
602 while ((*w++ = *r++) != '\r');
614 /*------------------------------------------------------.
615 | Scan NUMBER for a base-BASE integer at location LOC. |
616 `------------------------------------------------------*/
618 static unsigned long int
619 scan_integer (char const *number, int base, location loc)
621 verify (INT_MAX < ULONG_MAX);
622 unsigned long int num = strtoul (number, NULL, base);
626 complain_at (loc, _("integer out of range: %s"), quote (number));
634 /*------------------------------------------------------------------.
635 | Convert universal character name UCN to a single-byte character, |
636 | and return that character. Return -1 if UCN does not correspond |
637 | to a single-byte character. |
638 `------------------------------------------------------------------*/
641 convert_ucn_to_byte (char const *ucn)
643 verify (UCHAR_MAX <= INT_MAX);
644 unsigned long int code = strtoul (ucn + 2, NULL, 16);
646 /* FIXME: Currently we assume Unicode-compatible unibyte characters
647 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
648 non-ASCII hosts we support only the portable C character set.
649 These limitations should be removed once we add support for
650 multibyte characters. */
652 if (UCHAR_MAX < code)
655 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
657 /* A non-ASCII host. Use CODE to index into a table of the C
658 basic execution character set, which is guaranteed to exist on
659 all Standard C platforms. This table also includes '$', '@',
660 and '`', which are not in the basic execution character set but
661 which are unibyte characters on all the platforms that we know
663 static signed char const table[] =
665 '\0', -1, -1, -1, -1, -1, -1, '\a',
666 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
667 -1, -1, -1, -1, -1, -1, -1, -1,
668 -1, -1, -1, -1, -1, -1, -1, -1,
669 ' ', '!', '"', '#', '$', '%', '&', '\'',
670 '(', ')', '*', '+', ',', '-', '.', '/',
671 '0', '1', '2', '3', '4', '5', '6', '7',
672 '8', '9', ':', ';', '<', '=', '>', '?',
673 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
674 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
675 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
676 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
677 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
678 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
679 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
680 'x', 'y', 'z', '{', '|', '}', '~'
683 code = code < sizeof table ? table[code] : -1;
691 /*----------------------------------------------------------------.
692 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
693 `----------------------------------------------------------------*/
696 handle_syncline (char *args, location loc)
699 unsigned long int lineno = strtoul (args, &after_num, 10);
700 char *file = strchr (after_num, '"') + 1;
701 *strchr (file, '"') = '\0';
702 if (INT_MAX <= lineno)
704 warn_at (loc, _("line number overflow"));
707 current_file = uniqstr_new (file);
708 boundary_set (&scanner_cursor, current_file, lineno, 1);
712 /*----------------------------------------------------------------.
713 | For a token or comment starting at START, report message MSGID, |
714 | which should say that an end marker was found before |
715 | the expected TOKEN_END. |
716 `----------------------------------------------------------------*/
719 unexpected_end (boundary start, char const *msgid, char const *token_end)
723 loc.end = scanner_cursor;
724 complain_at (loc, _(msgid), token_end);
728 /*------------------------------------------------------------------------.
729 | Report an unexpected EOF in a token or comment starting at START. |
730 | An end of file was encountered and the expected TOKEN_END was missing. |
731 `------------------------------------------------------------------------*/
734 unexpected_eof (boundary start, char const *token_end)
736 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
740 /*----------------------------------------.
741 | Likewise, but for unexpected newlines. |
742 `----------------------------------------*/
745 unexpected_newline (boundary start, char const *token_end)
747 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
751 /*-------------------------.
752 | Initialize the scanner. |
753 `-------------------------*/
756 gram_scanner_initialize (void)
758 obstack_init (&obstack_for_string);
762 /*-----------------------------------------------.
763 | Free all the memory allocated to the scanner. |
764 `-----------------------------------------------*/
767 gram_scanner_free (void)
769 obstack_free (&obstack_for_string, 0);
770 /* Reclaim Flex's buffers. */
771 yy_delete_buffer (YY_CURRENT_BUFFER);