1 /* Bison Grammar Scanner -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
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 #define RETURN_PERCENT_FLAG(Value) \
60 val->uniqstr = uniqstr_new (Value); \
61 return PERCENT_FLAG; \
65 /* A string representing the most recently saved token. */
66 static char *last_string;
69 gram_scanner_last_string_free (void)
74 static void handle_syncline (char *, location);
75 static unsigned long int scan_integer (char const *p, int base, location loc);
76 static int convert_ucn_to_byte (char const *hex_text);
77 static void unexpected_eof (boundary, char const *);
78 static void unexpected_newline (boundary, char const *);
81 /* A C-like comment in directives/rules. */
83 /* Strings and characters in directives/rules. */
84 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
85 /* A identifier was just read in directives/rules. Special state
86 to capture the sequence `identifier :'. */
87 %x SC_AFTER_IDENTIFIER
88 /* A complex tag, with nested angles brackets. */
91 /* Three types of user code:
92 - prologue (code between `%{' `%}' in the first section, before %%);
93 - actions, printers, union, etc, (between braced in the middle section);
94 - epilogue (everything after the second %%). */
95 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
96 /* C and C++ comments in code. */
97 %x SC_COMMENT SC_LINE_COMMENT
98 /* Strings and characters in code. */
99 %x SC_STRING SC_CHARACTER
101 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
102 id {letter}({letter}|[0-9]|-)*
106 /* POSIX says that a tag must be both an id and a C union member, but
107 historically almost any character is allowed in a tag. We disallow
108 NUL, as this simplifies our implementation. We disallow angle
109 bracket to match them in nested pairs: several languages use them
110 for generics/template types. */
113 /* Zero or more instances of backslash-newline. Following GCC, allow
114 white space between the backslash and the newline. */
115 splice (\\[ \f\t\v]*\n)*
119 /* Nesting level. Either for nested braces, or nested angle brackets
121 int nesting IF_LINT (= 0);
123 /* Parent context state, when applicable. */
124 int context_state IF_LINT (= 0);
126 /* Location of most recent identifier, when applicable. */
127 location id_loc IF_LINT (= empty_location);
129 /* Where containing code started, when applicable. Its initial
130 value is relevant only when yylex is invoked in the SC_EPILOGUE
132 boundary code_start = scanner_cursor;
134 /* Where containing comment or string or character literal started,
136 boundary token_start IF_LINT (= scanner_cursor);
140 /*-----------------------.
141 | Scanning white space. |
142 `-----------------------*/
144 <INITIAL,SC_AFTER_IDENTIFIER>
146 /* Comments and white space. */
147 "," warn_at (*loc, _("stray `,' treated as white space"));
151 token_start = loc->start;
152 context_state = YY_START;
153 BEGIN SC_YACC_COMMENT;
156 /* #line directives are not documented, and may be withdrawn or
157 modified in future versions of Bison. */
158 ^"#line "{int}" \"".*"\"\n" {
159 handle_syncline (yytext + sizeof "#line " - 1, *loc);
164 /*----------------------------.
165 | Scanning Bison directives. |
166 `----------------------------*/
168 /* For directives that are also command line options, the regex must be
170 after "[-_]"s are removed, and the directive must match the --long
171 option name, with a single string argument. Otherwise, add exceptions
172 to ../build-aux/cross-options.pl. */
176 "%binary" return PERCENT_NONASSOC;
177 "%code" return PERCENT_CODE;
178 "%debug" RETURN_PERCENT_FLAG("parse.trace");
179 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
180 "%define" return PERCENT_DEFINE;
181 "%defines" return PERCENT_DEFINES;
182 "%destructor" return PERCENT_DESTRUCTOR;
183 "%dprec" return PERCENT_DPREC;
184 "%error"[-_]"verbose" RETURN_PERCENT_FLAG("error-verbose");
185 "%expect" return PERCENT_EXPECT;
186 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
187 "%file-prefix" return PERCENT_FILE_PREFIX;
188 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
189 "%initial-action" return PERCENT_INITIAL_ACTION;
190 "%glr-parser" return PERCENT_GLR_PARSER;
191 "%language" return PERCENT_LANGUAGE;
192 "%left" return PERCENT_LEFT;
193 "%lex-param" return PERCENT_LEX_PARAM;
194 "%locations" RETURN_PERCENT_FLAG("locations");
195 "%merge" return PERCENT_MERGE;
196 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
197 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
198 "%no"[-_]"lines" return PERCENT_NO_LINES;
199 "%nonassoc" return PERCENT_NONASSOC;
200 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
201 "%nterm" return PERCENT_NTERM;
202 "%output" return PERCENT_OUTPUT;
203 "%parse-param" return PERCENT_PARSE_PARAM;
204 "%prec" return PERCENT_PREC;
205 "%precedence" return PERCENT_PRECEDENCE;
206 "%printer" return PERCENT_PRINTER;
207 "%pure"[-_]"parser" RETURN_PERCENT_FLAG("api.pure");
208 "%require" return PERCENT_REQUIRE;
209 "%right" return PERCENT_RIGHT;
210 "%skeleton" return PERCENT_SKELETON;
211 "%start" return PERCENT_START;
212 "%term" return PERCENT_TOKEN;
213 "%token" return PERCENT_TOKEN;
214 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
215 "%type" return PERCENT_TYPE;
216 "%union" return PERCENT_UNION;
217 "%verbose" return PERCENT_VERBOSE;
218 "%yacc" return PERCENT_YACC;
221 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
226 ";" return SEMICOLON;
229 val->uniqstr = uniqstr_new (yytext);
231 BEGIN SC_AFTER_IDENTIFIER;
235 val->integer = scan_integer (yytext, 10, *loc);
238 0[xX][0-9abcdefABCDEF]+ {
239 val->integer = scan_integer (yytext, 16, *loc);
243 /* Characters. We don't check there is only one. */
244 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
247 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
250 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
252 /* Code in between braces. */
256 code_start = loc->start;
257 BEGIN SC_BRACED_CODE;
261 "<*>" return TAG_ANY;
262 "<>" return TAG_NONE;
264 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
266 val->uniqstr = uniqstr_new (last_string);
272 token_start = loc->start;
277 static int percent_percent_count;
278 if (++percent_percent_count == 2)
280 return PERCENT_PERCENT;
284 complain_at (*loc, _("invalid character: %s"), quote (yytext));
288 loc->start = loc->end = scanner_cursor;
294 /*--------------------------------------------------------------.
295 | Supporting \0 complexifies our implementation for no expected |
297 `--------------------------------------------------------------*/
299 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_TAG>
301 \0 complain_at (*loc, _("invalid null character"));
305 /*-----------------------------------------------------------------.
306 | Scanning after an identifier, checking whether a colon is next. |
307 `-----------------------------------------------------------------*/
309 <SC_AFTER_IDENTIFIER>
317 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
331 /*---------------------------------------------------------------.
332 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
333 `---------------------------------------------------------------*/
337 "*/" BEGIN context_state;
339 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
343 /*------------------------------------------------------------.
344 | Scanning a C comment. The initial `/ *' is already eaten. |
345 `------------------------------------------------------------*/
349 "*"{splice}"/" STRING_GROW; BEGIN context_state;
350 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
354 /*--------------------------------------------------------------.
355 | Scanning a line comment. The initial `//' is already eaten. |
356 `--------------------------------------------------------------*/
360 "\n" STRING_GROW; BEGIN context_state;
361 {splice} STRING_GROW;
362 <<EOF>> BEGIN context_state;
366 /*------------------------------------------------.
367 | Scanning a Bison string, including its escapes. |
368 | The initial quote is already eaten. |
369 `------------------------------------------------*/
374 if (yytext[0] == '\n')
375 unexpected_newline (token_start, "\"");
377 loc->start = token_start;
378 val->chars = last_string;
383 unexpected_eof (token_start, "\"");
385 loc->start = token_start;
386 val->chars = last_string;
392 /*----------------------------------------------------------.
393 | Scanning a Bison character literal, decoding its escapes. |
394 | The initial quote is already eaten. |
395 `----------------------------------------------------------*/
397 <SC_ESCAPED_CHARACTER>
400 if (yytext[0] == '\n')
401 unexpected_newline (token_start, "'");
404 loc->start = token_start;
405 val->character = last_string[1];
411 unexpected_eof (token_start, "'");
413 loc->start = token_start;
414 if (strlen (last_string) > 1)
415 val->character = last_string[1];
417 val->character = last_string[0];
424 /*-----------------------------------------------------------.
425 | Scanning a Bison nested tag. The initial angle bracket is |
427 `-----------------------------------------------------------*/
436 loc->start = token_start;
437 val->uniqstr = uniqstr_new (last_string);
446 "<"+ STRING_GROW; nesting += yyleng;
449 unexpected_eof (token_start, ">");
451 loc->start = token_start;
452 val->uniqstr = uniqstr_new (last_string);
459 /*----------------------------.
460 | Decode escaped characters. |
461 `----------------------------*/
463 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
466 unsigned long int c = strtoul (yytext + 1, NULL, 8);
468 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
470 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
472 obstack_1grow (&obstack_for_string, c);
475 \\x[0-9abcdefABCDEF]+ {
476 verify (UCHAR_MAX < ULONG_MAX);
477 unsigned long int c = strtoul (yytext + 2, NULL, 16);
479 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
481 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
483 obstack_1grow (&obstack_for_string, c);
486 \\a obstack_1grow (&obstack_for_string, '\a');
487 \\b obstack_1grow (&obstack_for_string, '\b');
488 \\f obstack_1grow (&obstack_for_string, '\f');
489 \\n obstack_1grow (&obstack_for_string, '\n');
490 \\r obstack_1grow (&obstack_for_string, '\r');
491 \\t obstack_1grow (&obstack_for_string, '\t');
492 \\v obstack_1grow (&obstack_for_string, '\v');
494 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
495 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
497 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
498 int c = convert_ucn_to_byte (yytext);
500 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
502 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
504 obstack_1grow (&obstack_for_string, c);
507 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
512 /*--------------------------------------------.
513 | Scanning user-code characters and strings. |
514 `--------------------------------------------*/
516 <SC_CHARACTER,SC_STRING>
518 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
523 "'" STRING_GROW; BEGIN context_state;
524 \n unexpected_newline (token_start, "'"); BEGIN context_state;
525 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
530 "\"" STRING_GROW; BEGIN context_state;
531 \n unexpected_newline (token_start, "\""); BEGIN context_state;
532 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
536 /*---------------------------------------------------.
537 | Strings, comments etc. can be found in user code. |
538 `---------------------------------------------------*/
540 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
544 context_state = YY_START;
545 token_start = loc->start;
550 context_state = YY_START;
551 token_start = loc->start;
556 context_state = YY_START;
557 token_start = loc->start;
562 context_state = YY_START;
563 BEGIN SC_LINE_COMMENT;
569 /*-----------------------------------------------------------.
570 | Scanning some code in braces (actions). The initial "{" is |
572 `-----------------------------------------------------------*/
576 "{"|"<"{splice}"%" STRING_GROW; nesting++;
577 "%"{splice}">" STRING_GROW; nesting--;
579 obstack_1grow (&obstack_for_string, '}');
585 loc->start = code_start;
586 val->code = last_string;
592 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
594 "<"{splice}"<" STRING_GROW;
597 unexpected_eof (code_start, "}");
599 loc->start = code_start;
600 val->code = last_string;
607 /*--------------------------------------------------------------.
608 | Scanning some prologue: from "%{" (already scanned) to "%}". |
609 `--------------------------------------------------------------*/
615 loc->start = code_start;
616 val->chars = last_string;
622 unexpected_eof (code_start, "%}");
624 loc->start = code_start;
625 val->chars = last_string;
632 /*---------------------------------------------------------------.
633 | Scanning the epilogue (everything after the second "%%", which |
634 | has already been eaten). |
635 `---------------------------------------------------------------*/
641 loc->start = code_start;
642 val->chars = last_string;
649 /*-----------------------------------------------------.
650 | By default, grow the string obstack with the input. |
651 `-----------------------------------------------------*/
653 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
654 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
658 /* Read bytes from FP into buffer BUF of size SIZE. Return the
659 number of bytes read. Remove '\r' from input, treating \r\n
660 and isolated \r as \n. */
663 no_cr_read (FILE *fp, char *buf, size_t size)
665 size_t bytes_read = fread (buf, 1, size, fp);
668 char *w = memchr (buf, '\r', bytes_read);
672 char const *lim = buf + bytes_read;
676 /* Found an '\r'. Treat it like '\n', but ignore any
677 '\n' that immediately follows. */
682 if (ch != '\n' && ungetc (ch, fp) != ch)
688 /* Copy until the next '\r'. */
694 while ((*w++ = *r++) != '\r');
706 /*------------------------------------------------------.
707 | Scan NUMBER for a base-BASE integer at location LOC. |
708 `------------------------------------------------------*/
710 static unsigned long int
711 scan_integer (char const *number, int base, location loc)
713 verify (INT_MAX < ULONG_MAX);
714 unsigned long int num = strtoul (number, NULL, base);
718 complain_at (loc, _("integer out of range: %s"), quote (number));
726 /*------------------------------------------------------------------.
727 | Convert universal character name UCN to a single-byte character, |
728 | and return that character. Return -1 if UCN does not correspond |
729 | to a single-byte character. |
730 `------------------------------------------------------------------*/
733 convert_ucn_to_byte (char const *ucn)
735 verify (UCHAR_MAX <= INT_MAX);
736 unsigned long int code = strtoul (ucn + 2, NULL, 16);
738 /* FIXME: Currently we assume Unicode-compatible unibyte characters
739 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
740 non-ASCII hosts we support only the portable C character set.
741 These limitations should be removed once we add support for
742 multibyte characters. */
744 if (UCHAR_MAX < code)
747 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
749 /* A non-ASCII host. Use CODE to index into a table of the C
750 basic execution character set, which is guaranteed to exist on
751 all Standard C platforms. This table also includes '$', '@',
752 and '`', which are not in the basic execution character set but
753 which are unibyte characters on all the platforms that we know
755 static signed char const table[] =
757 '\0', -1, -1, -1, -1, -1, -1, '\a',
758 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
759 -1, -1, -1, -1, -1, -1, -1, -1,
760 -1, -1, -1, -1, -1, -1, -1, -1,
761 ' ', '!', '"', '#', '$', '%', '&', '\'',
762 '(', ')', '*', '+', ',', '-', '.', '/',
763 '0', '1', '2', '3', '4', '5', '6', '7',
764 '8', '9', ':', ';', '<', '=', '>', '?',
765 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
766 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
767 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
768 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
769 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
770 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
771 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
772 'x', 'y', 'z', '{', '|', '}', '~'
775 code = code < sizeof table ? table[code] : -1;
783 /*----------------------------------------------------------------.
784 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
785 `----------------------------------------------------------------*/
788 handle_syncline (char *args, location loc)
791 unsigned long int lineno = strtoul (args, &after_num, 10);
792 char *file = strchr (after_num, '"') + 1;
793 *strchr (file, '"') = '\0';
794 if (INT_MAX <= lineno)
796 warn_at (loc, _("line number overflow"));
799 current_file = uniqstr_new (file);
800 boundary_set (&scanner_cursor, current_file, lineno, 1);
804 /*----------------------------------------------------------------.
805 | For a token or comment starting at START, report message MSGID, |
806 | which should say that an end marker was found before |
807 | the expected TOKEN_END. |
808 `----------------------------------------------------------------*/
811 unexpected_end (boundary start, char const *msgid, char const *token_end)
815 loc.end = scanner_cursor;
816 complain_at (loc, _(msgid), token_end);
820 /*------------------------------------------------------------------------.
821 | Report an unexpected EOF in a token or comment starting at START. |
822 | An end of file was encountered and the expected TOKEN_END was missing. |
823 `------------------------------------------------------------------------*/
826 unexpected_eof (boundary start, char const *token_end)
828 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
832 /*----------------------------------------.
833 | Likewise, but for unexpected newlines. |
834 `----------------------------------------*/
837 unexpected_newline (boundary start, char const *token_end)
839 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
843 /*-------------------------.
844 | Initialize the scanner. |
845 `-------------------------*/
848 gram_scanner_initialize (void)
850 obstack_init (&obstack_for_string);
854 /*-----------------------------------------------.
855 | Free all the memory allocated to the scanner. |
856 `-----------------------------------------------*/
859 gram_scanner_free (void)
861 obstack_free (&obstack_for_string, 0);
862 /* Reclaim Flex's buffers. */