1 /* Bison Grammar Scanner -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 %option debug nodefault nounput noyywrap never-interactive
24 %option prefix="gram_" outfile="lex.yy.c"
30 /* Work around a bug in flex 2.5.31. See Debian bug 333231
31 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
35 #define FLEX_PREFIX(Id) gram_ ## Id
36 #include "flex-scanner.h"
40 #include "getargs.h" /* yacc_flag */
49 #include "scan-gram.h"
51 #define YY_DECL GRAM_LEX_DECL
53 #define YY_USER_INIT \
54 code_start = scanner_cursor = loc->start; \
56 /* Location of scanner cursor. */
57 boundary scanner_cursor;
59 #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
61 static size_t no_cr_read (FILE *, char *, size_t);
62 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
64 /* A string representing the most recently saved token. */
68 gram_scanner_last_string_free (void)
73 /* The location of the most recently saved token, if it was a
74 BRACED_CODE token; otherwise, this has an unspecified value. */
75 location gram_last_braced_code_loc;
77 static void handle_syncline (char *, location);
78 static unsigned long int scan_integer (char const *p, int base, location loc);
79 static int convert_ucn_to_byte (char const *hex_text);
80 static void unexpected_eof (boundary, char const *);
81 static void unexpected_newline (boundary, char const *);
84 /* A C-like comment in directives/rules. */
86 /* Strings and characters in directives/rules. */
87 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
88 /* A identifier was just read in directives/rules. Special state
89 to capture the sequence `identifier :'. */
90 %x SC_AFTER_IDENTIFIER
91 /* A keyword that should be followed by some code was read (e.g.
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
105 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
106 id {letter}({letter}|[0-9])*
107 directive %{letter}({letter}|[0-9]|-)*
110 /* POSIX says that a tag must be both an id and a C union member, but
111 historically almost any character is allowed in a tag. We disallow
112 NUL and newline, as this simplifies our implementation. */
115 /* Zero or more instances of backslash-newline. Following GCC, allow
116 white space between the backslash and the newline. */
117 splice (\\[ \f\t\v]*\n)*
121 /* Nesting level of the current code in braces. */
122 int braces_level IF_LINT (= 0);
124 /* Parent context state, when applicable. */
125 int context_state IF_LINT (= 0);
127 /* Token type to return, when applicable. */
128 int token_type IF_LINT (= 0);
130 /* Location of most recent identifier, when applicable. */
131 location id_loc IF_LINT (= empty_location);
133 /* Where containing code started, when applicable. Its initial
134 value is relevant only when yylex is invoked in the SC_EPILOGUE
136 boundary code_start = scanner_cursor;
138 /* Where containing comment or string or character literal started,
140 boundary token_start IF_LINT (= scanner_cursor);
144 /*-----------------------.
145 | Scanning white space. |
146 `-----------------------*/
148 <INITIAL,SC_AFTER_IDENTIFIER,SC_PRE_CODE>
150 /* Comments and white space. */
151 "," warn_at (*loc, _("stray `,' treated as white space"));
155 token_start = loc->start;
156 context_state = YY_START;
157 BEGIN SC_YACC_COMMENT;
160 /* #line directives are not documented, and may be withdrawn or
161 modified in future versions of Bison. */
162 ^"#line "{int}" \"".*"\"\n" {
163 handle_syncline (yytext + sizeof "#line " - 1, *loc);
168 /*----------------------------.
169 | Scanning Bison directives. |
170 `----------------------------*/
173 "%binary" return PERCENT_NONASSOC;
174 "%debug" return PERCENT_DEBUG;
175 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
176 "%define" return PERCENT_DEFINE;
177 "%defines" return PERCENT_DEFINES;
178 "%destructor" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; return PERCENT_DESTRUCTOR;
179 "%dprec" return PERCENT_DPREC;
180 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
181 "%expect" return PERCENT_EXPECT;
182 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
183 "%file-prefix" return PERCENT_FILE_PREFIX;
184 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
185 "%initial-action" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; return PERCENT_INITIAL_ACTION;
186 "%glr-parser" return PERCENT_GLR_PARSER;
187 "%left" return PERCENT_LEFT;
188 "%lex-param" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; return PERCENT_LEX_PARAM;
189 "%locations" return PERCENT_LOCATIONS;
190 "%merge" return PERCENT_MERGE;
191 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
192 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
193 "%no"[-_]"lines" return PERCENT_NO_LINES;
194 "%nonassoc" return PERCENT_NONASSOC;
195 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
196 "%nterm" return PERCENT_NTERM;
197 "%output" return PERCENT_OUTPUT;
198 "%parse-param" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; return PERCENT_PARSE_PARAM;
199 "%prec" return PERCENT_PREC;
200 "%printer" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; return PERCENT_PRINTER;
201 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
202 "%require" return PERCENT_REQUIRE;
203 "%right" return PERCENT_RIGHT;
204 "%skeleton" return PERCENT_SKELETON;
205 "%start" return PERCENT_START;
206 "%term" return PERCENT_TOKEN;
207 "%token" return PERCENT_TOKEN;
208 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
209 "%type" return PERCENT_TYPE;
210 "%union" token_type = PERCENT_UNION; BEGIN SC_PRE_CODE;
211 "%verbose" return PERCENT_VERBOSE;
212 "%yacc" return PERCENT_YACC;
215 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
220 ";" return SEMICOLON;
223 val->symbol = symbol_get (yytext, *loc);
225 BEGIN SC_AFTER_IDENTIFIER;
229 val->integer = scan_integer (yytext, 10, *loc);
232 0[xX][0-9abcdefABCDEF]+ {
233 val->integer = scan_integer (yytext, 16, *loc);
237 /* Characters. We don't check there is only one. */
238 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
241 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
244 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
246 /* Code in between braces. */
248 if (current_rule && current_rule->action)
249 grammar_midrule_action ();
251 token_type = BRACED_CODE;
253 code_start = loc->start;
254 BEGIN SC_BRACED_CODE;
259 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
261 val->uniqstr = uniqstr_new (last_string);
267 static int percent_percent_count;
268 if (++percent_percent_count == 2)
270 return PERCENT_PERCENT;
274 complain_at (*loc, _("invalid character: %s"), quote (yytext));
278 loc->start = loc->end = scanner_cursor;
284 /*-----------------------------------------------------------------.
285 | Scanning after an identifier, checking whether a colon is next. |
286 `-----------------------------------------------------------------*/
288 <SC_AFTER_IDENTIFIER>
296 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
310 /*---------------------------------------------------------------.
311 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
312 `---------------------------------------------------------------*/
316 "*/" BEGIN context_state;
318 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
322 /*------------------------------------------------------------.
323 | Scanning a C comment. The initial `/ *' is already eaten. |
324 `------------------------------------------------------------*/
328 "*"{splice}"/" STRING_GROW; BEGIN context_state;
329 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
333 /*--------------------------------------------------------------.
334 | Scanning a line comment. The initial `//' is already eaten. |
335 `--------------------------------------------------------------*/
339 "\n" STRING_GROW; BEGIN context_state;
340 {splice} STRING_GROW;
341 <<EOF>> BEGIN context_state;
345 /*------------------------------------------------.
346 | Scanning a Bison string, including its escapes. |
347 | The initial quote is already eaten. |
348 `------------------------------------------------*/
354 loc->start = token_start;
355 val->chars = last_string;
359 \n unexpected_newline (token_start, "\""); BEGIN INITIAL;
360 <<EOF>> unexpected_eof (token_start, "\""); BEGIN INITIAL;
363 /*----------------------------------------------------------.
364 | Scanning a Bison character literal, decoding its escapes. |
365 | The initial quote is already eaten. |
366 `----------------------------------------------------------*/
368 <SC_ESCAPED_CHARACTER>
371 unsigned char last_string_1;
374 loc->start = token_start;
375 val->symbol = symbol_get (quotearg_style (escape_quoting_style,
378 symbol_class_set (val->symbol, token_sym, *loc, false);
379 last_string_1 = last_string[1];
380 symbol_user_token_number_set (val->symbol, last_string_1, *loc);
385 \n unexpected_newline (token_start, "'"); BEGIN INITIAL;
386 <<EOF>> unexpected_eof (token_start, "'"); BEGIN INITIAL;
389 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
391 \0 complain_at (*loc, _("invalid null character"));
395 /*----------------------------.
396 | Decode escaped characters. |
397 `----------------------------*/
399 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
402 unsigned long int c = strtoul (yytext + 1, NULL, 8);
404 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
406 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
408 obstack_1grow (&obstack_for_string, c);
411 \\x[0-9abcdefABCDEF]+ {
412 verify (UCHAR_MAX < ULONG_MAX);
413 unsigned long int c = strtoul (yytext + 2, NULL, 16);
415 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
417 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
419 obstack_1grow (&obstack_for_string, c);
422 \\a obstack_1grow (&obstack_for_string, '\a');
423 \\b obstack_1grow (&obstack_for_string, '\b');
424 \\f obstack_1grow (&obstack_for_string, '\f');
425 \\n obstack_1grow (&obstack_for_string, '\n');
426 \\r obstack_1grow (&obstack_for_string, '\r');
427 \\t obstack_1grow (&obstack_for_string, '\t');
428 \\v obstack_1grow (&obstack_for_string, '\v');
430 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
431 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
433 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
434 int c = convert_ucn_to_byte (yytext);
436 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
438 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
440 obstack_1grow (&obstack_for_string, c);
443 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
448 /*--------------------------------------------.
449 | Scanning user-code characters and strings. |
450 `--------------------------------------------*/
452 <SC_CHARACTER,SC_STRING>
454 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
459 "'" STRING_GROW; BEGIN context_state;
460 \n unexpected_newline (token_start, "'"); BEGIN context_state;
461 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
466 "\"" STRING_GROW; BEGIN context_state;
467 \n unexpected_newline (token_start, "\""); BEGIN context_state;
468 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
472 /*---------------------------------------------------.
473 | Strings, comments etc. can be found in user code. |
474 `---------------------------------------------------*/
476 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
480 context_state = YY_START;
481 token_start = loc->start;
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 BEGIN SC_LINE_COMMENT;
504 /*---------------------------------------------------------------.
505 | Scanning after %union etc., possibly followed by white space. |
506 | For %union only, allow arbitrary C code to appear before the |
507 | following brace, as an extension to POSIX. |
508 `---------------------------------------------------------------*/
513 bool valid = yytext[0] == '{' || token_type == PERCENT_UNION;
514 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
520 code_start = loc->start;
521 BEGIN SC_BRACED_CODE;
525 complain_at (*loc, _("missing `{' in %s"),
526 token_name (token_type));
527 obstack_sgrow (&obstack_for_string, "{}");
529 val->chars = last_string;
535 <<EOF>> unexpected_eof (scanner_cursor, "{}"); BEGIN INITIAL;
539 /*---------------------------------------------------------------.
540 | Scanning some code in braces (%union and actions). The initial |
541 | "{" is already eaten. |
542 `---------------------------------------------------------------*/
546 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
547 "%"{splice}">" STRING_GROW; braces_level--;
549 obstack_1grow (&obstack_for_string, '}');
552 if (braces_level < 0)
555 loc->start = code_start;
556 val->chars = last_string;
557 gram_last_braced_code_loc = *loc;
563 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
565 "<"{splice}"<" STRING_GROW;
567 <<EOF>> unexpected_eof (code_start, "}"); BEGIN INITIAL;
571 /*--------------------------------------------------------------.
572 | Scanning some prologue: from "%{" (already scanned) to "%}". |
573 `--------------------------------------------------------------*/
579 loc->start = code_start;
580 val->chars = last_string;
585 <<EOF>> unexpected_eof (code_start, "%}"); BEGIN INITIAL;
589 /*---------------------------------------------------------------.
590 | Scanning the epilogue (everything after the second "%%", which |
591 | has already been eaten). |
592 `---------------------------------------------------------------*/
598 loc->start = code_start;
599 val->chars = last_string;
606 /*-----------------------------------------------------.
607 | By default, grow the string obstack with the input. |
608 `-----------------------------------------------------*/
610 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
611 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
615 /* Read bytes from FP into buffer BUF of size SIZE. Return the
616 number of bytes read. Remove '\r' from input, treating \r\n
617 and isolated \r as \n. */
620 no_cr_read (FILE *fp, char *buf, size_t size)
622 size_t bytes_read = fread (buf, 1, size, fp);
625 char *w = memchr (buf, '\r', bytes_read);
629 char const *lim = buf + bytes_read;
633 /* Found an '\r'. Treat it like '\n', but ignore any
634 '\n' that immediately follows. */
639 if (ch != '\n' && ungetc (ch, fp) != ch)
645 /* Copy until the next '\r'. */
651 while ((*w++ = *r++) != '\r');
663 /*------------------------------------------------------.
664 | Scan NUMBER for a base-BASE integer at location LOC. |
665 `------------------------------------------------------*/
667 static unsigned long int
668 scan_integer (char const *number, int base, location loc)
670 verify (INT_MAX < ULONG_MAX);
671 unsigned long int num = strtoul (number, NULL, base);
675 complain_at (loc, _("integer out of range: %s"), quote (number));
683 /*------------------------------------------------------------------.
684 | Convert universal character name UCN to a single-byte character, |
685 | and return that character. Return -1 if UCN does not correspond |
686 | to a single-byte character. |
687 `------------------------------------------------------------------*/
690 convert_ucn_to_byte (char const *ucn)
692 verify (UCHAR_MAX <= INT_MAX);
693 unsigned long int code = strtoul (ucn + 2, NULL, 16);
695 /* FIXME: Currently we assume Unicode-compatible unibyte characters
696 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
697 non-ASCII hosts we support only the portable C character set.
698 These limitations should be removed once we add support for
699 multibyte characters. */
701 if (UCHAR_MAX < code)
704 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
706 /* A non-ASCII host. Use CODE to index into a table of the C
707 basic execution character set, which is guaranteed to exist on
708 all Standard C platforms. This table also includes '$', '@',
709 and '`', which are not in the basic execution character set but
710 which are unibyte characters on all the platforms that we know
712 static signed char const table[] =
714 '\0', -1, -1, -1, -1, -1, -1, '\a',
715 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
716 -1, -1, -1, -1, -1, -1, -1, -1,
717 -1, -1, -1, -1, -1, -1, -1, -1,
718 ' ', '!', '"', '#', '$', '%', '&', '\'',
719 '(', ')', '*', '+', ',', '-', '.', '/',
720 '0', '1', '2', '3', '4', '5', '6', '7',
721 '8', '9', ':', ';', '<', '=', '>', '?',
722 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
723 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
724 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
725 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
726 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
727 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
728 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
729 'x', 'y', 'z', '{', '|', '}', '~'
732 code = code < sizeof table ? table[code] : -1;
740 /*----------------------------------------------------------------.
741 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
742 `----------------------------------------------------------------*/
745 handle_syncline (char *args, location loc)
748 unsigned long int lineno = strtoul (args, &after_num, 10);
749 char *file = strchr (after_num, '"') + 1;
750 *strchr (file, '"') = '\0';
751 if (INT_MAX <= lineno)
753 warn_at (loc, _("line number overflow"));
756 current_file = uniqstr_new (file);
757 boundary_set (&scanner_cursor, current_file, lineno, 1);
761 /*----------------------------------------------------------------.
762 | For a token or comment starting at START, report message MSGID, |
763 | which should say that an end marker was found before |
764 | the expected TOKEN_END. |
765 `----------------------------------------------------------------*/
768 unexpected_end (boundary start, char const *msgid, char const *token_end)
772 loc.end = scanner_cursor;
773 complain_at (loc, _(msgid), token_end);
777 /*------------------------------------------------------------------------.
778 | Report an unexpected EOF in a token or comment starting at START. |
779 | An end of file was encountered and the expected TOKEN_END was missing. |
780 `------------------------------------------------------------------------*/
783 unexpected_eof (boundary start, char const *token_end)
785 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
789 /*----------------------------------------.
790 | Likewise, but for unexpected newlines. |
791 `----------------------------------------*/
794 unexpected_newline (boundary start, char const *token_end)
796 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
800 /*-------------------------.
801 | Initialize the scanner. |
802 `-------------------------*/
805 gram_scanner_initialize (void)
807 obstack_init (&obstack_for_string);
811 /*-----------------------------------------------.
812 | Free all the memory allocated to the scanner. |
813 `-----------------------------------------------*/
816 gram_scanner_free (void)
818 obstack_free (&obstack_for_string, 0);
819 /* Reclaim Flex's buffers. */
820 yy_delete_buffer (YY_CURRENT_BUFFER);