1 /* Bison Grammar Scanner -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation,
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 %option debug nodefault nounput noyywrap never-interactive
22 %option prefix="gram_" outfile="lex.yy.c"
25 /* Work around a bug in flex 2.5.31. See Debian bug 333231
26 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
30 #define FLEX_PREFIX(Id) gram_ ## Id
31 #include <src/flex-scanner.h>
33 #include <src/complain.h>
34 #include <src/files.h>
37 #include <src/reader.h>
38 #include <src/uniqstr.h>
43 #include <src/scan-gram.h>
45 #define YY_DECL GRAM_LEX_DECL
47 #define YY_USER_INIT \
48 code_start = scanner_cursor = loc->start; \
50 /* Location of scanner cursor. */
51 static boundary scanner_cursor;
53 #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
55 static size_t no_cr_read (FILE *, char *, size_t);
56 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
58 /* A string representing the most recently saved token. */
59 static char *last_string;
62 gram_scanner_last_string_free (void)
67 static void handle_syncline (char *, location);
68 static unsigned long int scan_integer (char const *p, int base, location loc);
69 static int convert_ucn_to_byte (char const *hex_text);
70 static void unexpected_eof (boundary, char const *);
71 static void unexpected_newline (boundary, char const *);
74 /* A C-like comment in directives/rules. */
76 /* Strings and characters in directives/rules. */
77 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
78 /* A identifier was just read in directives/rules. Special state
79 to capture the sequence `identifier :'. */
80 %x SC_AFTER_IDENTIFIER
81 /* A complex tag, with nested angles brackets. */
84 /* Three types of user code:
85 - prologue (code between `%{' `%}' in the first section, before %%);
86 - actions, printers, union, etc, (between braced in the middle section);
87 - epilogue (everything after the second %%). */
88 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
89 /* C and C++ comments in code. */
90 %x SC_COMMENT SC_LINE_COMMENT
91 /* Strings and characters in code. */
92 %x SC_STRING SC_CHARACTER
94 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
95 id {letter}({letter}|[0-9])*
96 directive %{letter}({letter}|[0-9]|-)*
99 /* POSIX says that a tag must be both an id and a C union member, but
100 historically almost any character is allowed in a tag. We disallow
101 NUL, as this simplifies our implementation. We disallow angle
102 bracket to match them in nested pairs: several languages use them
103 for generics/template types. */
106 /* Zero or more instances of backslash-newline. Following GCC, allow
107 white space between the backslash and the newline. */
108 splice (\\[ \f\t\v]*\n)*
112 /* Nesting level. Either for nested braces, or nested angle brackets
114 int nesting IF_LINT (= 0);
116 /* Parent context state, when applicable. */
117 int context_state IF_LINT (= 0);
119 /* Location of most recent identifier, when applicable. */
120 location id_loc IF_LINT (= empty_location);
122 /* Where containing code started, when applicable. Its initial
123 value is relevant only when yylex is invoked in the SC_EPILOGUE
125 boundary code_start = scanner_cursor;
127 /* Where containing comment or string or character literal started,
129 boundary token_start IF_LINT (= scanner_cursor);
133 /*-----------------------.
134 | Scanning white space. |
135 `-----------------------*/
137 <INITIAL,SC_AFTER_IDENTIFIER>
139 /* Comments and white space. */
140 "," warn_at (*loc, _("stray `,' treated as white space"));
144 token_start = loc->start;
145 context_state = YY_START;
146 BEGIN SC_YACC_COMMENT;
149 /* #line directives are not documented, and may be withdrawn or
150 modified in future versions of Bison. */
151 ^"#line "{int}" \"".*"\"\n" {
152 handle_syncline (yytext + sizeof "#line " - 1, *loc);
157 /*----------------------------.
158 | Scanning Bison directives. |
159 `----------------------------*/
161 /* For directives that are also command line options, the regex must be
163 after "[-_]"s are removed, and the directive must match the --long
164 option name, with a single string argument. Otherwise, add exceptions
165 to ../build-aux/cross-options.pl. */
169 "%binary" return PERCENT_NONASSOC;
170 "%code" return PERCENT_CODE;
171 "%debug" return PERCENT_DEBUG;
172 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
173 "%define" return PERCENT_DEFINE;
174 "%defines" return PERCENT_DEFINES;
175 "%destructor" return PERCENT_DESTRUCTOR;
176 "%dprec" return PERCENT_DPREC;
177 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
178 "%expect" return PERCENT_EXPECT;
179 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
180 "%file-prefix" return PERCENT_FILE_PREFIX;
181 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
182 "%initial-action" return PERCENT_INITIAL_ACTION;
183 "%glr-parser" return PERCENT_GLR_PARSER;
184 "%language" return PERCENT_LANGUAGE;
185 "%left" return PERCENT_LEFT;
186 "%lex-param" return PERCENT_LEX_PARAM;
187 "%locations" return PERCENT_LOCATIONS;
188 "%merge" return PERCENT_MERGE;
189 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
190 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
191 "%no"[-_]"lines" return PERCENT_NO_LINES;
192 "%nonassoc" return PERCENT_NONASSOC;
193 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
194 "%nterm" return PERCENT_NTERM;
195 "%output" return PERCENT_OUTPUT;
196 "%parse-param" return PERCENT_PARSE_PARAM;
197 "%prec" return PERCENT_PREC;
198 "%precedence" return PERCENT_PRECEDENCE;
199 "%printer" return PERCENT_PRINTER;
200 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
201 "%require" return PERCENT_REQUIRE;
202 "%right" return PERCENT_RIGHT;
203 "%skeleton" return PERCENT_SKELETON;
204 "%start" return PERCENT_START;
205 "%term" return PERCENT_TOKEN;
206 "%token" return PERCENT_TOKEN;
207 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
208 "%type" return PERCENT_TYPE;
209 "%union" return PERCENT_UNION;
210 "%verbose" return PERCENT_VERBOSE;
211 "%yacc" return PERCENT_YACC;
214 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
219 ";" return SEMICOLON;
222 val->uniqstr = uniqstr_new (yytext);
224 BEGIN SC_AFTER_IDENTIFIER;
228 val->integer = scan_integer (yytext, 10, *loc);
231 0[xX][0-9abcdefABCDEF]+ {
232 val->integer = scan_integer (yytext, 16, *loc);
236 /* Characters. We don't check there is only one. */
237 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
240 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
243 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
245 /* Code in between braces. */
249 code_start = loc->start;
250 BEGIN SC_BRACED_CODE;
254 "<*>" return TAG_ANY;
255 "<>" return TAG_NONE;
257 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
259 val->uniqstr = uniqstr_new (last_string);
265 token_start = loc->start;
270 static int percent_percent_count;
271 if (++percent_percent_count == 2)
273 return PERCENT_PERCENT;
277 complain_at (*loc, _("invalid character: %s"), quote (yytext));
281 loc->start = loc->end = scanner_cursor;
287 /*--------------------------------------------------------------.
288 | Supporting \0 complexifies our implementation for no expected |
290 `--------------------------------------------------------------*/
292 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_TAG>
294 \0 complain_at (*loc, _("invalid null character"));
298 /*-----------------------------------------------------------------.
299 | Scanning after an identifier, checking whether a colon is next. |
300 `-----------------------------------------------------------------*/
302 <SC_AFTER_IDENTIFIER>
310 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
324 /*---------------------------------------------------------------.
325 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
326 `---------------------------------------------------------------*/
330 "*/" BEGIN context_state;
332 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
336 /*------------------------------------------------------------.
337 | Scanning a C comment. The initial `/ *' is already eaten. |
338 `------------------------------------------------------------*/
342 "*"{splice}"/" STRING_GROW; BEGIN context_state;
343 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
347 /*--------------------------------------------------------------.
348 | Scanning a line comment. The initial `//' is already eaten. |
349 `--------------------------------------------------------------*/
353 "\n" STRING_GROW; BEGIN context_state;
354 {splice} STRING_GROW;
355 <<EOF>> BEGIN context_state;
359 /*------------------------------------------------.
360 | Scanning a Bison string, including its escapes. |
361 | The initial quote is already eaten. |
362 `------------------------------------------------*/
367 if (yytext[0] == '\n')
368 unexpected_newline (token_start, "\"");
370 loc->start = token_start;
371 val->chars = last_string;
376 unexpected_eof (token_start, "\"");
378 loc->start = token_start;
379 val->chars = last_string;
385 /*----------------------------------------------------------.
386 | Scanning a Bison character literal, decoding its escapes. |
387 | The initial quote is already eaten. |
388 `----------------------------------------------------------*/
390 <SC_ESCAPED_CHARACTER>
393 if (yytext[0] == '\n')
394 unexpected_newline (token_start, "'");
397 loc->start = token_start;
398 val->character = last_string[1];
404 unexpected_eof (token_start, "'");
406 loc->start = token_start;
407 if (strlen(last_string) > 1)
408 val->character = last_string[1];
410 val->character = last_string[0];
417 /*-----------------------------------------------------------.
418 | Scanning a Bison nested tag. The initial angle bracket is |
420 `-----------------------------------------------------------*/
429 loc->start = token_start;
430 val->uniqstr = uniqstr_new (last_string);
439 "<"+ STRING_GROW; nesting += yyleng;
442 unexpected_eof (token_start, ">");
444 loc->start = token_start;
445 val->uniqstr = uniqstr_new (last_string);
452 /*----------------------------.
453 | Decode escaped characters. |
454 `----------------------------*/
456 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
459 unsigned long int c = strtoul (yytext + 1, NULL, 8);
461 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
463 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
465 obstack_1grow (&obstack_for_string, c);
468 \\x[0-9abcdefABCDEF]+ {
469 verify (UCHAR_MAX < ULONG_MAX);
470 unsigned long int c = strtoul (yytext + 2, NULL, 16);
472 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
474 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
476 obstack_1grow (&obstack_for_string, c);
479 \\a obstack_1grow (&obstack_for_string, '\a');
480 \\b obstack_1grow (&obstack_for_string, '\b');
481 \\f obstack_1grow (&obstack_for_string, '\f');
482 \\n obstack_1grow (&obstack_for_string, '\n');
483 \\r obstack_1grow (&obstack_for_string, '\r');
484 \\t obstack_1grow (&obstack_for_string, '\t');
485 \\v obstack_1grow (&obstack_for_string, '\v');
487 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
488 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
490 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
491 int c = convert_ucn_to_byte (yytext);
493 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
495 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
497 obstack_1grow (&obstack_for_string, c);
500 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
505 /*--------------------------------------------.
506 | Scanning user-code characters and strings. |
507 `--------------------------------------------*/
509 <SC_CHARACTER,SC_STRING>
511 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
516 "'" STRING_GROW; BEGIN context_state;
517 \n unexpected_newline (token_start, "'"); BEGIN context_state;
518 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
523 "\"" STRING_GROW; BEGIN context_state;
524 \n unexpected_newline (token_start, "\""); BEGIN context_state;
525 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
529 /*---------------------------------------------------.
530 | Strings, comments etc. can be found in user code. |
531 `---------------------------------------------------*/
533 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
537 context_state = YY_START;
538 token_start = loc->start;
543 context_state = YY_START;
544 token_start = loc->start;
549 context_state = YY_START;
550 token_start = loc->start;
555 context_state = YY_START;
556 BEGIN SC_LINE_COMMENT;
562 /*-----------------------------------------------------------.
563 | Scanning some code in braces (actions). The initial "{" is |
565 `-----------------------------------------------------------*/
569 "{"|"<"{splice}"%" STRING_GROW; nesting++;
570 "%"{splice}">" STRING_GROW; nesting--;
572 obstack_1grow (&obstack_for_string, '}');
578 loc->start = code_start;
579 val->code = last_string;
585 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
587 "<"{splice}"<" STRING_GROW;
590 unexpected_eof (code_start, "}");
592 loc->start = code_start;
593 val->code = last_string;
600 /*--------------------------------------------------------------.
601 | Scanning some prologue: from "%{" (already scanned) to "%}". |
602 `--------------------------------------------------------------*/
608 loc->start = code_start;
609 val->chars = last_string;
615 unexpected_eof (code_start, "%}");
617 loc->start = code_start;
618 val->chars = last_string;
625 /*---------------------------------------------------------------.
626 | Scanning the epilogue (everything after the second "%%", which |
627 | has already been eaten). |
628 `---------------------------------------------------------------*/
634 loc->start = code_start;
635 val->chars = last_string;
642 /*-----------------------------------------------------.
643 | By default, grow the string obstack with the input. |
644 `-----------------------------------------------------*/
646 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
647 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
651 /* Read bytes from FP into buffer BUF of size SIZE. Return the
652 number of bytes read. Remove '\r' from input, treating \r\n
653 and isolated \r as \n. */
656 no_cr_read (FILE *fp, char *buf, size_t size)
658 size_t bytes_read = fread (buf, 1, size, fp);
661 char *w = memchr (buf, '\r', bytes_read);
665 char const *lim = buf + bytes_read;
669 /* Found an '\r'. Treat it like '\n', but ignore any
670 '\n' that immediately follows. */
675 if (ch != '\n' && ungetc (ch, fp) != ch)
681 /* Copy until the next '\r'. */
687 while ((*w++ = *r++) != '\r');
699 /*------------------------------------------------------.
700 | Scan NUMBER for a base-BASE integer at location LOC. |
701 `------------------------------------------------------*/
703 static unsigned long int
704 scan_integer (char const *number, int base, location loc)
706 verify (INT_MAX < ULONG_MAX);
707 unsigned long int num = strtoul (number, NULL, base);
711 complain_at (loc, _("integer out of range: %s"), quote (number));
719 /*------------------------------------------------------------------.
720 | Convert universal character name UCN to a single-byte character, |
721 | and return that character. Return -1 if UCN does not correspond |
722 | to a single-byte character. |
723 `------------------------------------------------------------------*/
726 convert_ucn_to_byte (char const *ucn)
728 verify (UCHAR_MAX <= INT_MAX);
729 unsigned long int code = strtoul (ucn + 2, NULL, 16);
731 /* FIXME: Currently we assume Unicode-compatible unibyte characters
732 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
733 non-ASCII hosts we support only the portable C character set.
734 These limitations should be removed once we add support for
735 multibyte characters. */
737 if (UCHAR_MAX < code)
740 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
742 /* A non-ASCII host. Use CODE to index into a table of the C
743 basic execution character set, which is guaranteed to exist on
744 all Standard C platforms. This table also includes '$', '@',
745 and '`', which are not in the basic execution character set but
746 which are unibyte characters on all the platforms that we know
748 static signed char const table[] =
750 '\0', -1, -1, -1, -1, -1, -1, '\a',
751 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
752 -1, -1, -1, -1, -1, -1, -1, -1,
753 -1, -1, -1, -1, -1, -1, -1, -1,
754 ' ', '!', '"', '#', '$', '%', '&', '\'',
755 '(', ')', '*', '+', ',', '-', '.', '/',
756 '0', '1', '2', '3', '4', '5', '6', '7',
757 '8', '9', ':', ';', '<', '=', '>', '?',
758 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
759 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
760 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
761 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
762 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
763 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
764 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
765 'x', 'y', 'z', '{', '|', '}', '~'
768 code = code < sizeof table ? table[code] : -1;
776 /*----------------------------------------------------------------.
777 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
778 `----------------------------------------------------------------*/
781 handle_syncline (char *args, location loc)
784 unsigned long int lineno = strtoul (args, &after_num, 10);
785 char *file = strchr (after_num, '"') + 1;
786 *strchr (file, '"') = '\0';
787 if (INT_MAX <= lineno)
789 warn_at (loc, _("line number overflow"));
792 current_file = uniqstr_new (file);
793 boundary_set (&scanner_cursor, current_file, lineno, 1);
797 /*----------------------------------------------------------------.
798 | For a token or comment starting at START, report message MSGID, |
799 | which should say that an end marker was found before |
800 | the expected TOKEN_END. |
801 `----------------------------------------------------------------*/
804 unexpected_end (boundary start, char const *msgid, char const *token_end)
808 loc.end = scanner_cursor;
809 complain_at (loc, _(msgid), token_end);
813 /*------------------------------------------------------------------------.
814 | Report an unexpected EOF in a token or comment starting at START. |
815 | An end of file was encountered and the expected TOKEN_END was missing. |
816 `------------------------------------------------------------------------*/
819 unexpected_eof (boundary start, char const *token_end)
821 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
825 /*----------------------------------------.
826 | Likewise, but for unexpected newlines. |
827 `----------------------------------------*/
830 unexpected_newline (boundary start, char const *token_end)
832 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
836 /*-------------------------.
837 | Initialize the scanner. |
838 `-------------------------*/
841 gram_scanner_initialize (void)
843 obstack_init (&obstack_for_string);
847 /*-----------------------------------------------.
848 | Free all the memory allocated to the scanner. |
849 `-----------------------------------------------*/
852 gram_scanner_free (void)
854 obstack_free (&obstack_for_string, 0);
855 /* Reclaim Flex's buffers. */