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 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"
43 #include "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
82 /* Three types of user code:
83 - prologue (code between `%{' `%}' in the first section, before %%);
84 - actions, printers, union, etc, (between braced in the middle section);
85 - epilogue (everything after the second %%). */
86 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
87 /* C and C++ comments in code. */
88 %x SC_COMMENT SC_LINE_COMMENT
89 /* Strings and characters in code. */
90 %x SC_STRING SC_CHARACTER
92 letter [-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
93 id {letter}({letter}|[0-9])*
97 /* POSIX says that a tag must be both an id and a C union member, but
98 historically almost any character is allowed in a tag. We disallow
99 NUL and newline, as this simplifies our implementation. */
102 /* Zero or more instances of backslash-newline. Following GCC, allow
103 white space between the backslash and the newline. */
104 splice (\\[ \f\t\v]*\n)*
108 /* Nesting level of the current code in braces. */
109 int braces_level IF_LINT (= 0);
111 /* Parent context state, when applicable. */
112 int context_state IF_LINT (= 0);
114 /* Location of most recent identifier, when applicable. */
115 location id_loc IF_LINT (= empty_location);
117 /* Where containing code started, when applicable. Its initial
118 value is relevant only when yylex is invoked in the SC_EPILOGUE
120 boundary code_start = scanner_cursor;
122 /* Where containing comment or string or character literal started,
124 boundary token_start IF_LINT (= scanner_cursor);
128 /*-----------------------.
129 | Scanning white space. |
130 `-----------------------*/
132 <INITIAL,SC_AFTER_IDENTIFIER>
134 /* Comments and white space. */
135 "," warn_at (*loc, _("stray `,' treated as white space"));
139 token_start = loc->start;
140 context_state = YY_START;
141 BEGIN SC_YACC_COMMENT;
144 /* #line directives are not documented, and may be withdrawn or
145 modified in future versions of Bison. */
146 ^"#line "{int}" \"".*"\"\n" {
147 handle_syncline (yytext + sizeof "#line " - 1, *loc);
152 /*----------------------------.
153 | Scanning Bison directives. |
154 `----------------------------*/
156 /* For directives that are also command line options, the regex must be
158 after "[-_]"s are removed, and the directive must match the --long
159 option name, with a single string argument. Otherwise, add exceptions
160 to ../build-aux/cross-options.pl. */
164 "%binary" return PERCENT_NONASSOC;
165 "%code" return PERCENT_CODE;
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 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
173 "%expect" return PERCENT_EXPECT;
174 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
175 "%file-prefix" return PERCENT_FILE_PREFIX;
176 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
177 "%initial-action" return PERCENT_INITIAL_ACTION;
178 "%glr-parser" return PERCENT_GLR_PARSER;
179 "%language" return PERCENT_LANGUAGE;
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 "%term" return PERCENT_TOKEN;
200 "%token" return PERCENT_TOKEN;
201 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
202 "%type" return PERCENT_TYPE;
203 "%union" return PERCENT_UNION;
204 "%verbose" return PERCENT_VERBOSE;
205 "%yacc" return PERCENT_YACC;
208 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
213 ";" return SEMICOLON;
214 "<*>" return TYPE_TAG_ANY;
215 "<>" return TYPE_TAG_NONE;
218 val->uniqstr = uniqstr_new (yytext);
220 BEGIN SC_AFTER_IDENTIFIER;
224 val->integer = scan_integer (yytext, 10, *loc);
227 0[xX][0-9abcdefABCDEF]+ {
228 val->integer = scan_integer (yytext, 16, *loc);
232 /* Characters. We don't check there is only one. */
233 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
236 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
239 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
241 /* Code in between braces. */
245 code_start = loc->start;
246 BEGIN SC_BRACED_CODE;
251 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
253 val->uniqstr = uniqstr_new (last_string);
259 static int percent_percent_count;
260 if (++percent_percent_count == 2)
262 return PERCENT_PERCENT;
266 complain_at (*loc, _("invalid character: %s"), quote (yytext));
270 loc->start = loc->end = scanner_cursor;
276 /*-----------------------------------------------------------------.
277 | Scanning after an identifier, checking whether a colon is next. |
278 `-----------------------------------------------------------------*/
280 <SC_AFTER_IDENTIFIER>
288 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
302 /*---------------------------------------------------------------.
303 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
304 `---------------------------------------------------------------*/
308 "*/" BEGIN context_state;
310 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
314 /*------------------------------------------------------------.
315 | Scanning a C comment. The initial `/ *' is already eaten. |
316 `------------------------------------------------------------*/
320 "*"{splice}"/" STRING_GROW; BEGIN context_state;
321 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
325 /*--------------------------------------------------------------.
326 | Scanning a line comment. The initial `//' is already eaten. |
327 `--------------------------------------------------------------*/
331 "\n" STRING_GROW; BEGIN context_state;
332 {splice} STRING_GROW;
333 <<EOF>> BEGIN context_state;
337 /*------------------------------------------------.
338 | Scanning a Bison string, including its escapes. |
339 | The initial quote is already eaten. |
340 `------------------------------------------------*/
345 if (yytext[0] == '\n')
346 unexpected_newline (token_start, "\"");
348 loc->start = token_start;
349 val->chars = last_string;
354 unexpected_eof (token_start, "\"");
356 loc->start = token_start;
357 val->chars = last_string;
363 /*----------------------------------------------------------.
364 | Scanning a Bison character literal, decoding its escapes. |
365 | The initial quote is already eaten. |
366 `----------------------------------------------------------*/
368 <SC_ESCAPED_CHARACTER>
371 if (yytext[0] == '\n')
372 unexpected_newline (token_start, "'");
375 loc->start = token_start;
376 val->character = last_string[1];
382 unexpected_eof (token_start, "'");
384 loc->start = token_start;
385 if (strlen (last_string) > 1)
386 val->character = last_string[1];
388 val->character = last_string[0];
395 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
397 \0 complain_at (*loc, _("invalid null character"));
401 /*----------------------------.
402 | Decode escaped characters. |
403 `----------------------------*/
405 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
408 unsigned long int c = strtoul (yytext + 1, NULL, 8);
410 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
412 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
414 obstack_1grow (&obstack_for_string, c);
417 \\x[0-9abcdefABCDEF]+ {
418 verify (UCHAR_MAX < ULONG_MAX);
419 unsigned long int c = strtoul (yytext + 2, NULL, 16);
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 \\a obstack_1grow (&obstack_for_string, '\a');
429 \\b obstack_1grow (&obstack_for_string, '\b');
430 \\f obstack_1grow (&obstack_for_string, '\f');
431 \\n obstack_1grow (&obstack_for_string, '\n');
432 \\r obstack_1grow (&obstack_for_string, '\r');
433 \\t obstack_1grow (&obstack_for_string, '\t');
434 \\v obstack_1grow (&obstack_for_string, '\v');
436 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
437 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
439 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
440 int c = convert_ucn_to_byte (yytext);
442 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
444 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
446 obstack_1grow (&obstack_for_string, c);
449 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
454 /*--------------------------------------------.
455 | Scanning user-code characters and strings. |
456 `--------------------------------------------*/
458 <SC_CHARACTER,SC_STRING>
460 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
465 "'" STRING_GROW; BEGIN context_state;
466 \n unexpected_newline (token_start, "'"); BEGIN context_state;
467 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
472 "\"" STRING_GROW; BEGIN context_state;
473 \n unexpected_newline (token_start, "\""); BEGIN context_state;
474 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
478 /*---------------------------------------------------.
479 | Strings, comments etc. can be found in user code. |
480 `---------------------------------------------------*/
482 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
486 context_state = YY_START;
487 token_start = loc->start;
492 context_state = YY_START;
493 token_start = loc->start;
498 context_state = YY_START;
499 token_start = loc->start;
504 context_state = YY_START;
505 BEGIN SC_LINE_COMMENT;
511 /*-----------------------------------------------------------.
512 | Scanning some code in braces (actions). The initial "{" is |
514 `-----------------------------------------------------------*/
518 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
519 "%"{splice}">" STRING_GROW; braces_level--;
521 obstack_1grow (&obstack_for_string, '}');
524 if (braces_level < 0)
527 loc->start = code_start;
528 val->code = last_string;
534 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
536 "<"{splice}"<" STRING_GROW;
539 unexpected_eof (code_start, "}");
541 loc->start = code_start;
542 val->code = last_string;
549 /*--------------------------------------------------------------.
550 | Scanning some prologue: from "%{" (already scanned) to "%}". |
551 `--------------------------------------------------------------*/
557 loc->start = code_start;
558 val->chars = last_string;
564 unexpected_eof (code_start, "%}");
566 loc->start = code_start;
567 val->chars = last_string;
574 /*---------------------------------------------------------------.
575 | Scanning the epilogue (everything after the second "%%", which |
576 | has already been eaten). |
577 `---------------------------------------------------------------*/
583 loc->start = code_start;
584 val->chars = last_string;
591 /*-----------------------------------------------------.
592 | By default, grow the string obstack with the input. |
593 `-----------------------------------------------------*/
595 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
596 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
600 /* Read bytes from FP into buffer BUF of size SIZE. Return the
601 number of bytes read. Remove '\r' from input, treating \r\n
602 and isolated \r as \n. */
605 no_cr_read (FILE *fp, char *buf, size_t size)
607 size_t bytes_read = fread (buf, 1, size, fp);
610 char *w = memchr (buf, '\r', bytes_read);
614 char const *lim = buf + bytes_read;
618 /* Found an '\r'. Treat it like '\n', but ignore any
619 '\n' that immediately follows. */
624 if (ch != '\n' && ungetc (ch, fp) != ch)
630 /* Copy until the next '\r'. */
636 while ((*w++ = *r++) != '\r');
648 /*------------------------------------------------------.
649 | Scan NUMBER for a base-BASE integer at location LOC. |
650 `------------------------------------------------------*/
652 static unsigned long int
653 scan_integer (char const *number, int base, location loc)
655 verify (INT_MAX < ULONG_MAX);
656 unsigned long int num = strtoul (number, NULL, base);
660 complain_at (loc, _("integer out of range: %s"), quote (number));
668 /*------------------------------------------------------------------.
669 | Convert universal character name UCN to a single-byte character, |
670 | and return that character. Return -1 if UCN does not correspond |
671 | to a single-byte character. |
672 `------------------------------------------------------------------*/
675 convert_ucn_to_byte (char const *ucn)
677 verify (UCHAR_MAX <= INT_MAX);
678 unsigned long int code = strtoul (ucn + 2, NULL, 16);
680 /* FIXME: Currently we assume Unicode-compatible unibyte characters
681 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
682 non-ASCII hosts we support only the portable C character set.
683 These limitations should be removed once we add support for
684 multibyte characters. */
686 if (UCHAR_MAX < code)
689 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
691 /* A non-ASCII host. Use CODE to index into a table of the C
692 basic execution character set, which is guaranteed to exist on
693 all Standard C platforms. This table also includes '$', '@',
694 and '`', which are not in the basic execution character set but
695 which are unibyte characters on all the platforms that we know
697 static signed char const table[] =
699 '\0', -1, -1, -1, -1, -1, -1, '\a',
700 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
701 -1, -1, -1, -1, -1, -1, -1, -1,
702 -1, -1, -1, -1, -1, -1, -1, -1,
703 ' ', '!', '"', '#', '$', '%', '&', '\'',
704 '(', ')', '*', '+', ',', '-', '.', '/',
705 '0', '1', '2', '3', '4', '5', '6', '7',
706 '8', '9', ':', ';', '<', '=', '>', '?',
707 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
708 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
709 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
710 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
711 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
712 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
713 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
714 'x', 'y', 'z', '{', '|', '}', '~'
717 code = code < sizeof table ? table[code] : -1;
725 /*----------------------------------------------------------------.
726 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
727 `----------------------------------------------------------------*/
730 handle_syncline (char *args, location loc)
733 unsigned long int lineno = strtoul (args, &after_num, 10);
734 char *file = strchr (after_num, '"') + 1;
735 *strchr (file, '"') = '\0';
736 if (INT_MAX <= lineno)
738 warn_at (loc, _("line number overflow"));
741 current_file = uniqstr_new (file);
742 boundary_set (&scanner_cursor, current_file, lineno, 1);
746 /*----------------------------------------------------------------.
747 | For a token or comment starting at START, report message MSGID, |
748 | which should say that an end marker was found before |
749 | the expected TOKEN_END. |
750 `----------------------------------------------------------------*/
753 unexpected_end (boundary start, char const *msgid, char const *token_end)
757 loc.end = scanner_cursor;
758 complain_at (loc, _(msgid), token_end);
762 /*------------------------------------------------------------------------.
763 | Report an unexpected EOF in a token or comment starting at START. |
764 | An end of file was encountered and the expected TOKEN_END was missing. |
765 `------------------------------------------------------------------------*/
768 unexpected_eof (boundary start, char const *token_end)
770 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
774 /*----------------------------------------.
775 | Likewise, but for unexpected newlines. |
776 `----------------------------------------*/
779 unexpected_newline (boundary start, char const *token_end)
781 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
785 /*-------------------------.
786 | Initialize the scanner. |
787 `-------------------------*/
790 gram_scanner_initialize (void)
792 obstack_init (&obstack_for_string);
796 /*-----------------------------------------------.
797 | Free all the memory allocated to the scanner. |
798 `-----------------------------------------------*/
801 gram_scanner_free (void)
803 obstack_free (&obstack_for_string, 0);
804 /* Reclaim Flex's buffers. */