1 /* Bison Grammar Scanner -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 noinput 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 "flex-scanner.h"
44 #include "scan-gram.h"
46 #define YY_DECL GRAM_LEX_DECL
48 #define YY_USER_INIT \
49 code_start = scanner_cursor = loc->start; \
51 /* Location of scanner cursor. */
52 static boundary scanner_cursor;
54 #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
56 static size_t no_cr_read (FILE *, char *, size_t);
57 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
59 #define ROLLBACK_CURRENT_TOKEN \
61 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0); \
65 /* A string representing the most recently saved token. */
66 static char *last_string;
68 /* Bracketed identifier. */
69 static uniqstr bracketed_id_str = 0;
70 static location bracketed_id_loc;
71 static boundary bracketed_id_start;
72 static int bracketed_id_context_state = 0;
75 gram_scanner_last_string_free (void)
80 static void handle_syncline (char *, location);
81 static unsigned long int scan_integer (char const *p, int base, location loc);
82 static int convert_ucn_to_byte (char const *hex_text);
83 static void unexpected_eof (boundary, char const *);
84 static void unexpected_newline (boundary, char const *);
87 /* A C-like comment in directives/rules. */
89 /* Strings and characters in directives/rules. */
90 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
91 /* A identifier was just read in directives/rules. Special state
92 to capture the sequence `identifier :'. */
93 %x SC_AFTER_IDENTIFIER
95 /* Three types of user code:
96 - prologue (code between `%{' `%}' in the first section, before %%);
97 - actions, printers, union, etc, (between braced in the middle section);
98 - epilogue (everything after the second %%). */
99 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
100 /* C and C++ comments in code. */
101 %x SC_COMMENT SC_LINE_COMMENT
102 /* Strings and characters in code. */
103 %x SC_STRING SC_CHARACTER
104 /* Bracketed identifiers support. */
105 %x SC_BRACKETED_ID SC_RETURN_BRACKETED_ID
107 letter [-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
108 id {letter}({letter}|[0-9])*
112 /* POSIX says that a tag must be both an id and a C union member, but
113 historically almost any character is allowed in a tag. We disallow
114 NUL and newline, as this simplifies our implementation. */
117 /* Zero or more instances of backslash-newline. Following GCC, allow
118 white space between the backslash and the newline. */
119 splice (\\[ \f\t\v]*\n)*
123 /* Nesting level of the current code in braces. */
124 int braces_level IF_LINT (= 0);
126 /* Parent context state, when applicable. */
127 int context_state IF_LINT (= 0);
129 /* Location of most recent identifier, when applicable. */
130 location id_loc IF_LINT (= empty_location);
132 /* Where containing code started, when applicable. Its initial
133 value is relevant only when yylex is invoked in the SC_EPILOGUE
135 boundary code_start = scanner_cursor;
137 /* Where containing comment or string or character literal started,
139 boundary token_start IF_LINT (= scanner_cursor);
143 /*-----------------------.
144 | Scanning white space. |
145 `-----------------------*/
147 <INITIAL,SC_AFTER_IDENTIFIER,SC_BRACKETED_ID,SC_RETURN_BRACKETED_ID>
149 /* Comments and white space. */
150 "," warn_at (*loc, _("stray `,' treated as white space"));
154 token_start = loc->start;
155 context_state = YY_START;
156 BEGIN SC_YACC_COMMENT;
159 /* #line directives are not documented, and may be withdrawn or
160 modified in future versions of Bison. */
161 ^"#line "{int}" \"".*"\"\n" {
162 handle_syncline (yytext + sizeof "#line " - 1, *loc);
167 /*----------------------------.
168 | Scanning Bison directives. |
169 `----------------------------*/
171 /* For directives that are also command line options, the regex must be
173 after "[-_]"s are removed, and the directive must match the --long
174 option name, with a single string argument. Otherwise, add exceptions
175 to ../build-aux/cross-options.pl. */
179 "%binary" return PERCENT_NONASSOC;
180 "%code" return PERCENT_CODE;
181 "%debug" return PERCENT_DEBUG;
182 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
183 "%define" return PERCENT_DEFINE;
184 "%defines" return PERCENT_DEFINES;
185 "%destructor" return PERCENT_DESTRUCTOR;
186 "%dprec" return PERCENT_DPREC;
187 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
188 "%expect" return PERCENT_EXPECT;
189 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
190 "%file-prefix" return PERCENT_FILE_PREFIX;
191 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
192 "%initial-action" return PERCENT_INITIAL_ACTION;
193 "%glr-parser" return PERCENT_GLR_PARSER;
194 "%language" return PERCENT_LANGUAGE;
195 "%left" return PERCENT_LEFT;
196 "%lex-param" return PERCENT_LEX_PARAM;
197 "%locations" return PERCENT_LOCATIONS;
198 "%merge" return PERCENT_MERGE;
199 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
200 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
201 "%no"[-_]"lines" return PERCENT_NO_LINES;
202 "%nonassoc" return PERCENT_NONASSOC;
203 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
204 "%nterm" return PERCENT_NTERM;
205 "%output" return PERCENT_OUTPUT;
206 "%parse-param" return PERCENT_PARSE_PARAM;
207 "%prec" return PERCENT_PREC;
208 "%printer" return PERCENT_PRINTER;
209 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
210 "%require" return PERCENT_REQUIRE;
211 "%right" return PERCENT_RIGHT;
212 "%skeleton" return PERCENT_SKELETON;
213 "%start" return PERCENT_START;
214 "%term" return PERCENT_TOKEN;
215 "%token" return PERCENT_TOKEN;
216 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
217 "%type" return PERCENT_TYPE;
218 "%union" return PERCENT_UNION;
219 "%verbose" return PERCENT_VERBOSE;
220 "%yacc" return PERCENT_YACC;
223 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
228 ";" return SEMICOLON;
229 "<*>" return TYPE_TAG_ANY;
230 "<>" return TYPE_TAG_NONE;
233 val->uniqstr = uniqstr_new (yytext);
235 bracketed_id_str = NULL;
236 BEGIN SC_AFTER_IDENTIFIER;
240 val->integer = scan_integer (yytext, 10, *loc);
243 0[xX][0-9abcdefABCDEF]+ {
244 val->integer = scan_integer (yytext, 16, *loc);
248 /* Identifiers may not start with a digit. Yet, don't silently
249 accept "1FOO" as "1 FOO". */
251 complain_at (*loc, _("invalid identifier: %s"), quote (yytext));
255 "'" token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
258 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
261 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
263 /* Code in between braces. */
267 code_start = loc->start;
268 BEGIN SC_BRACED_CODE;
273 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
275 val->uniqstr = uniqstr_new (last_string);
281 static int percent_percent_count;
282 if (++percent_percent_count == 2)
284 return PERCENT_PERCENT;
288 bracketed_id_str = NULL;
289 bracketed_id_start = loc->start;
290 bracketed_id_context_state = YY_START;
291 BEGIN SC_BRACKETED_ID;
295 complain_at (*loc, _("invalid character: %s"), quote (yytext));
299 loc->start = loc->end = scanner_cursor;
305 /*-----------------------------------------------------------------.
306 | Scanning after an identifier, checking whether a colon is next. |
307 `-----------------------------------------------------------------*/
309 <SC_AFTER_IDENTIFIER>
312 if (bracketed_id_str)
314 ROLLBACK_CURRENT_TOKEN;
315 BEGIN SC_RETURN_BRACKETED_ID;
321 bracketed_id_start = loc->start;
322 bracketed_id_context_state = YY_START;
323 BEGIN SC_BRACKETED_ID;
327 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
332 ROLLBACK_CURRENT_TOKEN;
333 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
338 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
344 /*--------------------------------.
345 | Scanning bracketed identifiers. |
346 `--------------------------------*/
351 if (bracketed_id_str)
353 complain_at (*loc, _("unexpected identifier in bracketed name: %s"),
358 bracketed_id_str = uniqstr_new (yytext);
359 bracketed_id_loc = *loc;
363 BEGIN bracketed_id_context_state;
364 if (bracketed_id_str)
366 if (INITIAL == bracketed_id_context_state)
368 val->uniqstr = bracketed_id_str;
369 bracketed_id_str = 0;
370 *loc = bracketed_id_loc;
375 complain_at (*loc, _("an identifier expected"));
378 complain_at (*loc, _("invalid character in bracketed name: %s"),
382 BEGIN bracketed_id_context_state;
383 unexpected_eof (bracketed_id_start, "]");
387 <SC_RETURN_BRACKETED_ID>
390 ROLLBACK_CURRENT_TOKEN;
391 val->uniqstr = bracketed_id_str;
392 bracketed_id_str = 0;
393 *loc = bracketed_id_loc;
400 /*---------------------------------------------------------------.
401 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
402 `---------------------------------------------------------------*/
406 "*/" BEGIN context_state;
408 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
412 /*------------------------------------------------------------.
413 | Scanning a C comment. The initial `/ *' is already eaten. |
414 `------------------------------------------------------------*/
418 "*"{splice}"/" STRING_GROW; BEGIN context_state;
419 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
423 /*--------------------------------------------------------------.
424 | Scanning a line comment. The initial `//' is already eaten. |
425 `--------------------------------------------------------------*/
429 "\n" STRING_GROW; BEGIN context_state;
430 {splice} STRING_GROW;
431 <<EOF>> BEGIN context_state;
435 /*------------------------------------------------.
436 | Scanning a Bison string, including its escapes. |
437 | The initial quote is already eaten. |
438 `------------------------------------------------*/
443 if (yytext[0] == '\n')
444 unexpected_newline (token_start, "\"");
446 loc->start = token_start;
447 val->chars = last_string;
452 unexpected_eof (token_start, "\"");
454 loc->start = token_start;
455 val->chars = last_string;
461 /*----------------------------------------------------------.
462 | Scanning a Bison character literal, decoding its escapes. |
463 | The initial quote is already eaten. |
464 `----------------------------------------------------------*/
466 <SC_ESCAPED_CHARACTER>
470 loc->start = token_start;
471 val->character = last_string[0];
473 /* FIXME: Eventually, make these errors. */
474 if (last_string[0] == '\0')
476 warn_at (*loc, _("empty character literal"));
477 /* '\0' seems dangerous even if we are about to complain. */
478 val->character = '\'';
480 else if (last_string[1] != '\0')
481 warn_at (*loc, _("extra characters in character literal"));
483 if (yytext[0] == '\n')
484 unexpected_newline (token_start, "'");
491 loc->start = token_start;
492 val->character = last_string[0];
494 /* FIXME: Eventually, make these errors. */
495 if (last_string[0] == '\0')
497 warn_at (*loc, _("empty character literal"));
498 /* '\0' seems dangerous even if we are about to complain. */
499 val->character = '\'';
501 else if (last_string[1] != '\0')
502 warn_at (*loc, _("extra characters in character literal"));
504 unexpected_eof (token_start, "'");
511 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
513 \0 complain_at (*loc, _("invalid null character"));
517 /*----------------------------.
518 | Decode escaped characters. |
519 `----------------------------*/
521 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
524 unsigned long int c = strtoul (yytext + 1, NULL, 8);
525 if (!c || UCHAR_MAX < c)
526 complain_at (*loc, _("invalid number after \\-escape: %s"),
529 obstack_1grow (&obstack_for_string, c);
532 \\x[0-9abcdefABCDEF]+ {
533 verify (UCHAR_MAX < ULONG_MAX);
534 unsigned long int c = strtoul (yytext + 2, NULL, 16);
535 if (!c || UCHAR_MAX < c)
536 complain_at (*loc, _("invalid number after \\-escape: %s"),
539 obstack_1grow (&obstack_for_string, c);
542 \\a obstack_1grow (&obstack_for_string, '\a');
543 \\b obstack_1grow (&obstack_for_string, '\b');
544 \\f obstack_1grow (&obstack_for_string, '\f');
545 \\n obstack_1grow (&obstack_for_string, '\n');
546 \\r obstack_1grow (&obstack_for_string, '\r');
547 \\t obstack_1grow (&obstack_for_string, '\t');
548 \\v obstack_1grow (&obstack_for_string, '\v');
550 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
551 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
553 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
554 int c = convert_ucn_to_byte (yytext);
556 complain_at (*loc, _("invalid number after \\-escape: %s"),
559 obstack_1grow (&obstack_for_string, c);
562 char const *p = yytext + 1;
563 /* Quote only if escaping won't make the character visible. */
564 if (isspace ((unsigned char) *p) && isprint ((unsigned char) *p))
567 p = quotearg_style_mem (escape_quoting_style, p, 1);
568 complain_at (*loc, _("invalid character after \\-escape: %s"), p);
572 /*--------------------------------------------.
573 | Scanning user-code characters and strings. |
574 `--------------------------------------------*/
576 <SC_CHARACTER,SC_STRING>
578 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
583 "'" STRING_GROW; BEGIN context_state;
584 \n unexpected_newline (token_start, "'"); BEGIN context_state;
585 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
590 "\"" STRING_GROW; BEGIN context_state;
591 \n unexpected_newline (token_start, "\""); BEGIN context_state;
592 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
596 /*---------------------------------------------------.
597 | Strings, comments etc. can be found in user code. |
598 `---------------------------------------------------*/
600 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
604 context_state = YY_START;
605 token_start = loc->start;
610 context_state = YY_START;
611 token_start = loc->start;
616 context_state = YY_START;
617 token_start = loc->start;
622 context_state = YY_START;
623 BEGIN SC_LINE_COMMENT;
629 /*-----------------------------------------------------------.
630 | Scanning some code in braces (actions). The initial "{" is |
632 `-----------------------------------------------------------*/
636 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
637 "%"{splice}">" STRING_GROW; braces_level--;
639 obstack_1grow (&obstack_for_string, '}');
642 if (braces_level < 0)
645 loc->start = code_start;
646 val->code = last_string;
652 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
654 "<"{splice}"<" STRING_GROW;
657 unexpected_eof (code_start, "}");
659 loc->start = code_start;
660 val->code = last_string;
667 /*--------------------------------------------------------------.
668 | Scanning some prologue: from "%{" (already scanned) to "%}". |
669 `--------------------------------------------------------------*/
675 loc->start = code_start;
676 val->chars = last_string;
682 unexpected_eof (code_start, "%}");
684 loc->start = code_start;
685 val->chars = last_string;
692 /*---------------------------------------------------------------.
693 | Scanning the epilogue (everything after the second "%%", which |
694 | has already been eaten). |
695 `---------------------------------------------------------------*/
701 loc->start = code_start;
702 val->chars = last_string;
709 /*-----------------------------------------------------.
710 | By default, grow the string obstack with the input. |
711 `-----------------------------------------------------*/
713 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
714 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
718 /* Read bytes from FP into buffer BUF of size SIZE. Return the
719 number of bytes read. Remove '\r' from input, treating \r\n
720 and isolated \r as \n. */
723 no_cr_read (FILE *fp, char *buf, size_t size)
725 size_t bytes_read = fread (buf, 1, size, fp);
728 char *w = memchr (buf, '\r', bytes_read);
732 char const *lim = buf + bytes_read;
736 /* Found an '\r'. Treat it like '\n', but ignore any
737 '\n' that immediately follows. */
742 if (ch != '\n' && ungetc (ch, fp) != ch)
748 /* Copy until the next '\r'. */
754 while ((*w++ = *r++) != '\r');
766 /*------------------------------------------------------.
767 | Scan NUMBER for a base-BASE integer at location LOC. |
768 `------------------------------------------------------*/
770 static unsigned long int
771 scan_integer (char const *number, int base, location loc)
773 verify (INT_MAX < ULONG_MAX);
774 unsigned long int num = strtoul (number, NULL, base);
778 complain_at (loc, _("integer out of range: %s"), quote (number));
786 /*------------------------------------------------------------------.
787 | Convert universal character name UCN to a single-byte character, |
788 | and return that character. Return -1 if UCN does not correspond |
789 | to a single-byte character. |
790 `------------------------------------------------------------------*/
793 convert_ucn_to_byte (char const *ucn)
795 verify (UCHAR_MAX <= INT_MAX);
796 unsigned long int code = strtoul (ucn + 2, NULL, 16);
798 /* FIXME: Currently we assume Unicode-compatible unibyte characters
799 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
800 non-ASCII hosts we support only the portable C character set.
801 These limitations should be removed once we add support for
802 multibyte characters. */
804 if (UCHAR_MAX < code)
807 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
809 /* A non-ASCII host. Use CODE to index into a table of the C
810 basic execution character set, which is guaranteed to exist on
811 all Standard C platforms. This table also includes '$', '@',
812 and '`', which are not in the basic execution character set but
813 which are unibyte characters on all the platforms that we know
815 static signed char const table[] =
817 '\0', -1, -1, -1, -1, -1, -1, '\a',
818 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
819 -1, -1, -1, -1, -1, -1, -1, -1,
820 -1, -1, -1, -1, -1, -1, -1, -1,
821 ' ', '!', '"', '#', '$', '%', '&', '\'',
822 '(', ')', '*', '+', ',', '-', '.', '/',
823 '0', '1', '2', '3', '4', '5', '6', '7',
824 '8', '9', ':', ';', '<', '=', '>', '?',
825 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
826 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
827 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
828 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
829 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
830 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
831 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
832 'x', 'y', 'z', '{', '|', '}', '~'
835 code = code < sizeof table ? table[code] : -1;
843 /*----------------------------------------------------------------.
844 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
845 `----------------------------------------------------------------*/
848 handle_syncline (char *args, location loc)
851 unsigned long int lineno = strtoul (args, &after_num, 10);
852 char *file = strchr (after_num, '"') + 1;
853 *strchr (file, '"') = '\0';
854 if (INT_MAX <= lineno)
856 warn_at (loc, _("line number overflow"));
859 current_file = uniqstr_new (file);
860 boundary_set (&scanner_cursor, current_file, lineno, 1);
864 /*----------------------------------------------------------------.
865 | For a token or comment starting at START, report message MSGID, |
866 | which should say that an end marker was found before |
867 | the expected TOKEN_END. |
868 `----------------------------------------------------------------*/
871 unexpected_end (boundary start, char const *msgid, char const *token_end)
875 loc.end = scanner_cursor;
876 complain_at (loc, _(msgid), token_end);
880 /*------------------------------------------------------------------------.
881 | Report an unexpected EOF in a token or comment starting at START. |
882 | An end of file was encountered and the expected TOKEN_END was missing. |
883 `------------------------------------------------------------------------*/
886 unexpected_eof (boundary start, char const *token_end)
888 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
892 /*----------------------------------------.
893 | Likewise, but for unexpected newlines. |
894 `----------------------------------------*/
897 unexpected_newline (boundary start, char const *token_end)
899 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
903 /*-------------------------.
904 | Initialize the scanner. |
905 `-------------------------*/
908 gram_scanner_initialize (void)
910 obstack_init (&obstack_for_string);
914 /*-----------------------------------------------.
915 | Free all the memory allocated to the scanner. |
916 `-----------------------------------------------*/
919 gram_scanner_free (void)
921 obstack_free (&obstack_for_string, 0);
922 /* Reclaim Flex's buffers. */