1 /* Bison Grammar Scanner -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free
4 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 char quoted_ws[] = "` '";
564 if (isspace (*p) && isprint (*p))
570 p = quotearg_style_mem (escape_quoting_style, p, 1);
571 complain_at (*loc, _("invalid character after \\-escape: %s"), p);
575 /*--------------------------------------------.
576 | Scanning user-code characters and strings. |
577 `--------------------------------------------*/
579 <SC_CHARACTER,SC_STRING>
581 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
586 "'" STRING_GROW; BEGIN context_state;
587 \n unexpected_newline (token_start, "'"); BEGIN context_state;
588 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
593 "\"" STRING_GROW; BEGIN context_state;
594 \n unexpected_newline (token_start, "\""); BEGIN context_state;
595 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
599 /*---------------------------------------------------.
600 | Strings, comments etc. can be found in user code. |
601 `---------------------------------------------------*/
603 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
607 context_state = YY_START;
608 token_start = loc->start;
613 context_state = YY_START;
614 token_start = loc->start;
619 context_state = YY_START;
620 token_start = loc->start;
625 context_state = YY_START;
626 BEGIN SC_LINE_COMMENT;
632 /*-----------------------------------------------------------.
633 | Scanning some code in braces (actions). The initial "{" is |
635 `-----------------------------------------------------------*/
639 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
640 "%"{splice}">" STRING_GROW; braces_level--;
642 obstack_1grow (&obstack_for_string, '}');
645 if (braces_level < 0)
648 loc->start = code_start;
649 val->code = last_string;
655 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
657 "<"{splice}"<" STRING_GROW;
660 unexpected_eof (code_start, "}");
662 loc->start = code_start;
663 val->code = last_string;
670 /*--------------------------------------------------------------.
671 | Scanning some prologue: from "%{" (already scanned) to "%}". |
672 `--------------------------------------------------------------*/
678 loc->start = code_start;
679 val->chars = last_string;
685 unexpected_eof (code_start, "%}");
687 loc->start = code_start;
688 val->chars = last_string;
695 /*---------------------------------------------------------------.
696 | Scanning the epilogue (everything after the second "%%", which |
697 | has already been eaten). |
698 `---------------------------------------------------------------*/
704 loc->start = code_start;
705 val->chars = last_string;
712 /*-----------------------------------------------------.
713 | By default, grow the string obstack with the input. |
714 `-----------------------------------------------------*/
716 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
717 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
721 /* Read bytes from FP into buffer BUF of size SIZE. Return the
722 number of bytes read. Remove '\r' from input, treating \r\n
723 and isolated \r as \n. */
726 no_cr_read (FILE *fp, char *buf, size_t size)
728 size_t bytes_read = fread (buf, 1, size, fp);
731 char *w = memchr (buf, '\r', bytes_read);
735 char const *lim = buf + bytes_read;
739 /* Found an '\r'. Treat it like '\n', but ignore any
740 '\n' that immediately follows. */
745 if (ch != '\n' && ungetc (ch, fp) != ch)
751 /* Copy until the next '\r'. */
757 while ((*w++ = *r++) != '\r');
769 /*------------------------------------------------------.
770 | Scan NUMBER for a base-BASE integer at location LOC. |
771 `------------------------------------------------------*/
773 static unsigned long int
774 scan_integer (char const *number, int base, location loc)
776 verify (INT_MAX < ULONG_MAX);
777 unsigned long int num = strtoul (number, NULL, base);
781 complain_at (loc, _("integer out of range: %s"), quote (number));
789 /*------------------------------------------------------------------.
790 | Convert universal character name UCN to a single-byte character, |
791 | and return that character. Return -1 if UCN does not correspond |
792 | to a single-byte character. |
793 `------------------------------------------------------------------*/
796 convert_ucn_to_byte (char const *ucn)
798 verify (UCHAR_MAX <= INT_MAX);
799 unsigned long int code = strtoul (ucn + 2, NULL, 16);
801 /* FIXME: Currently we assume Unicode-compatible unibyte characters
802 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
803 non-ASCII hosts we support only the portable C character set.
804 These limitations should be removed once we add support for
805 multibyte characters. */
807 if (UCHAR_MAX < code)
810 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
812 /* A non-ASCII host. Use CODE to index into a table of the C
813 basic execution character set, which is guaranteed to exist on
814 all Standard C platforms. This table also includes '$', '@',
815 and '`', which are not in the basic execution character set but
816 which are unibyte characters on all the platforms that we know
818 static signed char const table[] =
820 '\0', -1, -1, -1, -1, -1, -1, '\a',
821 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
822 -1, -1, -1, -1, -1, -1, -1, -1,
823 -1, -1, -1, -1, -1, -1, -1, -1,
824 ' ', '!', '"', '#', '$', '%', '&', '\'',
825 '(', ')', '*', '+', ',', '-', '.', '/',
826 '0', '1', '2', '3', '4', '5', '6', '7',
827 '8', '9', ':', ';', '<', '=', '>', '?',
828 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
829 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
830 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
831 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
832 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
833 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
834 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
835 'x', 'y', 'z', '{', '|', '}', '~'
838 code = code < sizeof table ? table[code] : -1;
846 /*----------------------------------------------------------------.
847 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
848 `----------------------------------------------------------------*/
851 handle_syncline (char *args, location loc)
854 unsigned long int lineno = strtoul (args, &after_num, 10);
855 char *file = strchr (after_num, '"') + 1;
856 *strchr (file, '"') = '\0';
857 if (INT_MAX <= lineno)
859 warn_at (loc, _("line number overflow"));
862 current_file = uniqstr_new (file);
863 boundary_set (&scanner_cursor, current_file, lineno, 1);
867 /*----------------------------------------------------------------.
868 | For a token or comment starting at START, report message MSGID, |
869 | which should say that an end marker was found before |
870 | the expected TOKEN_END. |
871 `----------------------------------------------------------------*/
874 unexpected_end (boundary start, char const *msgid, char const *token_end)
878 loc.end = scanner_cursor;
879 complain_at (loc, _(msgid), token_end);
883 /*------------------------------------------------------------------------.
884 | Report an unexpected EOF in a token or comment starting at START. |
885 | An end of file was encountered and the expected TOKEN_END was missing. |
886 `------------------------------------------------------------------------*/
889 unexpected_eof (boundary start, char const *token_end)
891 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
895 /*----------------------------------------.
896 | Likewise, but for unexpected newlines. |
897 `----------------------------------------*/
900 unexpected_newline (boundary start, char const *token_end)
902 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
906 /*-------------------------.
907 | Initialize the scanner. |
908 `-------------------------*/
911 gram_scanner_initialize (void)
913 obstack_init (&obstack_for_string);
917 /*-----------------------------------------------.
918 | Free all the memory allocated to the scanner. |
919 `-----------------------------------------------*/
922 gram_scanner_free (void)
924 obstack_free (&obstack_for_string, 0);
925 /* Reclaim Flex's buffers. */