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 `----------------------------*/
162 "%binary" return PERCENT_NONASSOC;
163 "%code" return PERCENT_CODE;
164 "%debug" return PERCENT_DEBUG;
165 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
166 "%define" return PERCENT_DEFINE;
167 "%defines" return PERCENT_DEFINES;
168 "%destructor" return PERCENT_DESTRUCTOR;
169 "%dprec" return PERCENT_DPREC;
170 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
171 "%expect" return PERCENT_EXPECT;
172 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
173 "%file-prefix" return PERCENT_FILE_PREFIX;
174 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
175 "%initial-action" return PERCENT_INITIAL_ACTION;
176 "%glr-parser" return PERCENT_GLR_PARSER;
177 "%language" return PERCENT_LANGUAGE;
178 "%left" return PERCENT_LEFT;
179 "%lex-param" return PERCENT_LEX_PARAM;
180 "%locations" return PERCENT_LOCATIONS;
181 "%merge" return PERCENT_MERGE;
182 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
183 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
184 "%no"[-_]"lines" return PERCENT_NO_LINES;
185 "%nonassoc" return PERCENT_NONASSOC;
186 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
187 "%nterm" return PERCENT_NTERM;
188 "%output" return PERCENT_OUTPUT;
189 "%parse-param" return PERCENT_PARSE_PARAM;
190 "%prec" return PERCENT_PREC;
191 "%precedence" return PERCENT_PRECEDENCE;
192 "%printer" return PERCENT_PRINTER;
193 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
194 "%require" return PERCENT_REQUIRE;
195 "%right" return PERCENT_RIGHT;
196 "%skeleton" return PERCENT_SKELETON;
197 "%start" return PERCENT_START;
198 "%term" return PERCENT_TOKEN;
199 "%token" return PERCENT_TOKEN;
200 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
201 "%type" return PERCENT_TYPE;
202 "%union" return PERCENT_UNION;
203 "%verbose" return PERCENT_VERBOSE;
204 "%yacc" return PERCENT_YACC;
207 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
212 ";" return SEMICOLON;
215 val->uniqstr = uniqstr_new (yytext);
217 BEGIN SC_AFTER_IDENTIFIER;
221 val->integer = scan_integer (yytext, 10, *loc);
224 0[xX][0-9abcdefABCDEF]+ {
225 val->integer = scan_integer (yytext, 16, *loc);
229 /* Characters. We don't check there is only one. */
230 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
233 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
236 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
238 /* Code in between braces. */
242 code_start = loc->start;
243 BEGIN SC_BRACED_CODE;
247 "<*>" return TAG_ANY;
248 "<>" return TAG_NONE;
250 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
252 val->uniqstr = uniqstr_new (last_string);
258 token_start = loc->start;
263 static int percent_percent_count;
264 if (++percent_percent_count == 2)
266 return PERCENT_PERCENT;
270 complain_at (*loc, _("invalid character: %s"), quote (yytext));
274 loc->start = loc->end = scanner_cursor;
280 /*--------------------------------------------------------------.
281 | Supporting \0 complexifies our implementation for no expected |
283 `--------------------------------------------------------------*/
285 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_TAG>
287 \0 complain_at (*loc, _("invalid null character"));
291 /*-----------------------------------------------------------------.
292 | Scanning after an identifier, checking whether a colon is next. |
293 `-----------------------------------------------------------------*/
295 <SC_AFTER_IDENTIFIER>
303 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
317 /*---------------------------------------------------------------.
318 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
319 `---------------------------------------------------------------*/
323 "*/" BEGIN context_state;
325 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
329 /*------------------------------------------------------------.
330 | Scanning a C comment. The initial `/ *' is already eaten. |
331 `------------------------------------------------------------*/
335 "*"{splice}"/" STRING_GROW; BEGIN context_state;
336 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
340 /*--------------------------------------------------------------.
341 | Scanning a line comment. The initial `//' is already eaten. |
342 `--------------------------------------------------------------*/
346 "\n" STRING_GROW; BEGIN context_state;
347 {splice} STRING_GROW;
348 <<EOF>> BEGIN context_state;
352 /*------------------------------------------------.
353 | Scanning a Bison string, including its escapes. |
354 | The initial quote is already eaten. |
355 `------------------------------------------------*/
360 if (yytext[0] == '\n')
361 unexpected_newline (token_start, "\"");
363 loc->start = token_start;
364 val->chars = last_string;
369 unexpected_eof (token_start, "\"");
371 loc->start = token_start;
372 val->chars = last_string;
378 /*----------------------------------------------------------.
379 | Scanning a Bison character literal, decoding its escapes. |
380 | The initial quote is already eaten. |
381 `----------------------------------------------------------*/
383 <SC_ESCAPED_CHARACTER>
386 if (yytext[0] == '\n')
387 unexpected_newline (token_start, "'");
390 loc->start = token_start;
391 val->character = last_string[1];
397 unexpected_eof (token_start, "'");
399 loc->start = token_start;
400 if (strlen(last_string) > 1)
401 val->character = last_string[1];
403 val->character = last_string[0];
410 /*-----------------------------------------------------------.
411 | Scanning a Bison nested tag. The initial angle bracket is |
413 `-----------------------------------------------------------*/
422 loc->start = token_start;
423 val->uniqstr = uniqstr_new (last_string);
432 "<"+ STRING_GROW; nesting += yyleng;
435 unexpected_eof (token_start, ">");
437 loc->start = token_start;
438 val->uniqstr = uniqstr_new (last_string);
445 /*----------------------------.
446 | Decode escaped characters. |
447 `----------------------------*/
449 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
452 unsigned long int c = strtoul (yytext + 1, NULL, 8);
454 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
456 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
458 obstack_1grow (&obstack_for_string, c);
461 \\x[0-9abcdefABCDEF]+ {
462 verify (UCHAR_MAX < ULONG_MAX);
463 unsigned long int c = strtoul (yytext + 2, NULL, 16);
465 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
467 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
469 obstack_1grow (&obstack_for_string, c);
472 \\a obstack_1grow (&obstack_for_string, '\a');
473 \\b obstack_1grow (&obstack_for_string, '\b');
474 \\f obstack_1grow (&obstack_for_string, '\f');
475 \\n obstack_1grow (&obstack_for_string, '\n');
476 \\r obstack_1grow (&obstack_for_string, '\r');
477 \\t obstack_1grow (&obstack_for_string, '\t');
478 \\v obstack_1grow (&obstack_for_string, '\v');
480 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
481 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
483 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
484 int c = convert_ucn_to_byte (yytext);
486 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
488 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
490 obstack_1grow (&obstack_for_string, c);
493 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
498 /*--------------------------------------------.
499 | Scanning user-code characters and strings. |
500 `--------------------------------------------*/
502 <SC_CHARACTER,SC_STRING>
504 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
509 "'" STRING_GROW; BEGIN context_state;
510 \n unexpected_newline (token_start, "'"); BEGIN context_state;
511 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
516 "\"" STRING_GROW; BEGIN context_state;
517 \n unexpected_newline (token_start, "\""); BEGIN context_state;
518 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
522 /*---------------------------------------------------.
523 | Strings, comments etc. can be found in user code. |
524 `---------------------------------------------------*/
526 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
530 context_state = YY_START;
531 token_start = loc->start;
536 context_state = YY_START;
537 token_start = loc->start;
542 context_state = YY_START;
543 token_start = loc->start;
548 context_state = YY_START;
549 BEGIN SC_LINE_COMMENT;
555 /*-----------------------------------------------------------.
556 | Scanning some code in braces (actions). The initial "{" is |
558 `-----------------------------------------------------------*/
562 "{"|"<"{splice}"%" STRING_GROW; nesting++;
563 "%"{splice}">" STRING_GROW; nesting--;
565 obstack_1grow (&obstack_for_string, '}');
571 loc->start = code_start;
572 val->code = last_string;
578 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
580 "<"{splice}"<" STRING_GROW;
583 unexpected_eof (code_start, "}");
585 loc->start = code_start;
586 val->code = last_string;
593 /*--------------------------------------------------------------.
594 | Scanning some prologue: from "%{" (already scanned) to "%}". |
595 `--------------------------------------------------------------*/
601 loc->start = code_start;
602 val->chars = last_string;
608 unexpected_eof (code_start, "%}");
610 loc->start = code_start;
611 val->chars = last_string;
618 /*---------------------------------------------------------------.
619 | Scanning the epilogue (everything after the second "%%", which |
620 | has already been eaten). |
621 `---------------------------------------------------------------*/
627 loc->start = code_start;
628 val->chars = last_string;
635 /*-----------------------------------------------------.
636 | By default, grow the string obstack with the input. |
637 `-----------------------------------------------------*/
639 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
640 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
644 /* Read bytes from FP into buffer BUF of size SIZE. Return the
645 number of bytes read. Remove '\r' from input, treating \r\n
646 and isolated \r as \n. */
649 no_cr_read (FILE *fp, char *buf, size_t size)
651 size_t bytes_read = fread (buf, 1, size, fp);
654 char *w = memchr (buf, '\r', bytes_read);
658 char const *lim = buf + bytes_read;
662 /* Found an '\r'. Treat it like '\n', but ignore any
663 '\n' that immediately follows. */
668 if (ch != '\n' && ungetc (ch, fp) != ch)
674 /* Copy until the next '\r'. */
680 while ((*w++ = *r++) != '\r');
692 /*------------------------------------------------------.
693 | Scan NUMBER for a base-BASE integer at location LOC. |
694 `------------------------------------------------------*/
696 static unsigned long int
697 scan_integer (char const *number, int base, location loc)
699 verify (INT_MAX < ULONG_MAX);
700 unsigned long int num = strtoul (number, NULL, base);
704 complain_at (loc, _("integer out of range: %s"), quote (number));
712 /*------------------------------------------------------------------.
713 | Convert universal character name UCN to a single-byte character, |
714 | and return that character. Return -1 if UCN does not correspond |
715 | to a single-byte character. |
716 `------------------------------------------------------------------*/
719 convert_ucn_to_byte (char const *ucn)
721 verify (UCHAR_MAX <= INT_MAX);
722 unsigned long int code = strtoul (ucn + 2, NULL, 16);
724 /* FIXME: Currently we assume Unicode-compatible unibyte characters
725 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
726 non-ASCII hosts we support only the portable C character set.
727 These limitations should be removed once we add support for
728 multibyte characters. */
730 if (UCHAR_MAX < code)
733 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
735 /* A non-ASCII host. Use CODE to index into a table of the C
736 basic execution character set, which is guaranteed to exist on
737 all Standard C platforms. This table also includes '$', '@',
738 and '`', which are not in the basic execution character set but
739 which are unibyte characters on all the platforms that we know
741 static signed char const table[] =
743 '\0', -1, -1, -1, -1, -1, -1, '\a',
744 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
745 -1, -1, -1, -1, -1, -1, -1, -1,
746 -1, -1, -1, -1, -1, -1, -1, -1,
747 ' ', '!', '"', '#', '$', '%', '&', '\'',
748 '(', ')', '*', '+', ',', '-', '.', '/',
749 '0', '1', '2', '3', '4', '5', '6', '7',
750 '8', '9', ':', ';', '<', '=', '>', '?',
751 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
752 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
753 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
754 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
755 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
756 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
757 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
758 'x', 'y', 'z', '{', '|', '}', '~'
761 code = code < sizeof table ? table[code] : -1;
769 /*----------------------------------------------------------------.
770 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
771 `----------------------------------------------------------------*/
774 handle_syncline (char *args, location loc)
777 unsigned long int lineno = strtoul (args, &after_num, 10);
778 char *file = strchr (after_num, '"') + 1;
779 *strchr (file, '"') = '\0';
780 if (INT_MAX <= lineno)
782 warn_at (loc, _("line number overflow"));
785 current_file = uniqstr_new (file);
786 boundary_set (&scanner_cursor, current_file, lineno, 1);
790 /*----------------------------------------------------------------.
791 | For a token or comment starting at START, report message MSGID, |
792 | which should say that an end marker was found before |
793 | the expected TOKEN_END. |
794 `----------------------------------------------------------------*/
797 unexpected_end (boundary start, char const *msgid, char const *token_end)
801 loc.end = scanner_cursor;
802 complain_at (loc, _(msgid), token_end);
806 /*------------------------------------------------------------------------.
807 | Report an unexpected EOF in a token or comment starting at START. |
808 | An end of file was encountered and the expected TOKEN_END was missing. |
809 `------------------------------------------------------------------------*/
812 unexpected_eof (boundary start, char const *token_end)
814 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
818 /*----------------------------------------.
819 | Likewise, but for unexpected newlines. |
820 `----------------------------------------*/
823 unexpected_newline (boundary start, char const *token_end)
825 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
829 /*-------------------------.
830 | Initialize the scanner. |
831 `-------------------------*/
834 gram_scanner_initialize (void)
836 obstack_init (&obstack_for_string);
840 /*-----------------------------------------------.
841 | Free all the memory allocated to the scanner. |
842 `-----------------------------------------------*/
845 gram_scanner_free (void)
847 obstack_free (&obstack_for_string, 0);
848 /* Reclaim Flex's buffers. */