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"
27 /* Work around a bug in flex 2.5.31. See Debian bug 333231
28 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
32 #define FLEX_PREFIX(Id) gram_ ## Id
33 #include "flex-scanner.h"
45 #include "scan-gram.h"
47 #define YY_DECL GRAM_LEX_DECL
49 #define YY_USER_INIT \
50 code_start = scanner_cursor = loc->start; \
52 /* Location of scanner cursor. */
53 static boundary scanner_cursor;
55 #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
57 static size_t no_cr_read (FILE *, char *, size_t);
58 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
60 /* A string representing the most recently saved token. */
64 gram_scanner_last_string_free (void)
69 static void handle_syncline (char *, location);
70 static unsigned long int scan_integer (char const *p, int base, location loc);
71 static int convert_ucn_to_byte (char const *hex_text);
72 static void unexpected_eof (boundary, char const *);
73 static void unexpected_newline (boundary, char const *);
76 /* A C-like comment in directives/rules. */
78 /* Strings and characters in directives/rules. */
79 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
80 /* A identifier was just read in directives/rules. Special state
81 to capture the sequence `identifier :'. */
82 %x SC_AFTER_IDENTIFIER
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 and newline, as this simplifies our implementation. */
104 /* Zero or more instances of backslash-newline. Following GCC, allow
105 white space between the backslash and the newline. */
106 splice (\\[ \f\t\v]*\n)*
110 /* Nesting level of the current code in braces. */
111 int braces_level IF_LINT (= 0);
113 /* Parent context state, when applicable. */
114 int context_state IF_LINT (= 0);
116 /* Location of most recent identifier, when applicable. */
117 location id_loc IF_LINT (= empty_location);
119 /* Where containing code started, when applicable. Its initial
120 value is relevant only when yylex is invoked in the SC_EPILOGUE
122 boundary code_start = scanner_cursor;
124 /* Where containing comment or string or character literal started,
126 boundary token_start IF_LINT (= scanner_cursor);
130 /*-----------------------.
131 | Scanning white space. |
132 `-----------------------*/
134 <INITIAL,SC_AFTER_IDENTIFIER>
136 /* Comments and white space. */
137 "," warn_at (*loc, _("stray `,' treated as white space"));
141 token_start = loc->start;
142 context_state = YY_START;
143 BEGIN SC_YACC_COMMENT;
146 /* #line directives are not documented, and may be withdrawn or
147 modified in future versions of Bison. */
148 ^"#line "{int}" \"".*"\"\n" {
149 handle_syncline (yytext + sizeof "#line " - 1, *loc);
154 /*----------------------------.
155 | Scanning Bison directives. |
156 `----------------------------*/
159 "%after-header" return PERCENT_AFTER_HEADER;
160 "%before-header" return PERCENT_BEFORE_HEADER;
161 "%binary" return PERCENT_NONASSOC;
162 "%debug" return PERCENT_DEBUG;
163 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
164 "%define" return PERCENT_DEFINE;
165 "%defines" return PERCENT_DEFINES;
166 "%destructor" return PERCENT_DESTRUCTOR;
167 "%dprec" return PERCENT_DPREC;
168 "%end-header" return PERCENT_END_HEADER;
169 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
170 "%expect" return PERCENT_EXPECT;
171 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
172 "%file-prefix" return PERCENT_FILE_PREFIX;
173 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
174 "%initial-action" return PERCENT_INITIAL_ACTION;
175 "%glr-parser" return PERCENT_GLR_PARSER;
176 "%left" return PERCENT_LEFT;
177 "%lex-param" return PERCENT_LEX_PARAM;
178 "%locations" return PERCENT_LOCATIONS;
179 "%merge" return PERCENT_MERGE;
180 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
181 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
182 "%no"[-_]"lines" return PERCENT_NO_LINES;
183 "%nonassoc" return PERCENT_NONASSOC;
184 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
185 "%nterm" return PERCENT_NTERM;
186 "%output" return PERCENT_OUTPUT;
187 "%parse-param" return PERCENT_PARSE_PARAM;
188 "%prec" return PERCENT_PREC;
189 "%printer" return PERCENT_PRINTER;
190 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
191 "%push"[-_]"parser" return PERCENT_PUSH_PARSER;
192 "%require" return PERCENT_REQUIRE;
193 "%right" return PERCENT_RIGHT;
194 "%skeleton" return PERCENT_SKELETON;
195 "%start" return PERCENT_START;
196 "%start-header" return PERCENT_START_HEADER;
197 "%symbol-default" return PERCENT_SYMBOL_DEFAULT;
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;
248 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
250 val->uniqstr = uniqstr_new (last_string);
256 static int percent_percent_count;
257 if (++percent_percent_count == 2)
259 return PERCENT_PERCENT;
263 complain_at (*loc, _("invalid character: %s"), quote (yytext));
267 loc->start = loc->end = scanner_cursor;
273 /*-----------------------------------------------------------------.
274 | Scanning after an identifier, checking whether a colon is next. |
275 `-----------------------------------------------------------------*/
277 <SC_AFTER_IDENTIFIER>
285 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
299 /*---------------------------------------------------------------.
300 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
301 `---------------------------------------------------------------*/
305 "*/" BEGIN context_state;
307 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
311 /*------------------------------------------------------------.
312 | Scanning a C comment. The initial `/ *' is already eaten. |
313 `------------------------------------------------------------*/
317 "*"{splice}"/" STRING_GROW; BEGIN context_state;
318 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
322 /*--------------------------------------------------------------.
323 | Scanning a line comment. The initial `//' is already eaten. |
324 `--------------------------------------------------------------*/
328 "\n" STRING_GROW; BEGIN context_state;
329 {splice} STRING_GROW;
330 <<EOF>> BEGIN context_state;
334 /*------------------------------------------------.
335 | Scanning a Bison string, including its escapes. |
336 | The initial quote is already eaten. |
337 `------------------------------------------------*/
342 if (yytext[0] == '\n')
343 unexpected_newline (token_start, "\"");
345 loc->start = token_start;
346 val->chars = last_string;
351 unexpected_eof (token_start, "\"");
353 loc->start = token_start;
354 val->chars = last_string;
360 /*----------------------------------------------------------.
361 | Scanning a Bison character literal, decoding its escapes. |
362 | The initial quote is already eaten. |
363 `----------------------------------------------------------*/
365 <SC_ESCAPED_CHARACTER>
368 if (yytext[0] == '\n')
369 unexpected_newline (token_start, "'");
372 loc->start = token_start;
373 val->character = last_string[1];
379 unexpected_eof (token_start, "'");
381 loc->start = token_start;
382 if (strlen(last_string) > 1)
383 val->character = last_string[1];
385 val->character = last_string[0];
392 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
394 \0 complain_at (*loc, _("invalid null character"));
398 /*----------------------------.
399 | Decode escaped characters. |
400 `----------------------------*/
402 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
405 unsigned long int c = strtoul (yytext + 1, NULL, 8);
407 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
409 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
411 obstack_1grow (&obstack_for_string, c);
414 \\x[0-9abcdefABCDEF]+ {
415 verify (UCHAR_MAX < ULONG_MAX);
416 unsigned long int c = strtoul (yytext + 2, NULL, 16);
418 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
420 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
422 obstack_1grow (&obstack_for_string, c);
425 \\a obstack_1grow (&obstack_for_string, '\a');
426 \\b obstack_1grow (&obstack_for_string, '\b');
427 \\f obstack_1grow (&obstack_for_string, '\f');
428 \\n obstack_1grow (&obstack_for_string, '\n');
429 \\r obstack_1grow (&obstack_for_string, '\r');
430 \\t obstack_1grow (&obstack_for_string, '\t');
431 \\v obstack_1grow (&obstack_for_string, '\v');
433 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
434 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
436 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
437 int c = convert_ucn_to_byte (yytext);
439 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
441 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
443 obstack_1grow (&obstack_for_string, c);
446 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
451 /*--------------------------------------------.
452 | Scanning user-code characters and strings. |
453 `--------------------------------------------*/
455 <SC_CHARACTER,SC_STRING>
457 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
462 "'" STRING_GROW; BEGIN context_state;
463 \n unexpected_newline (token_start, "'"); BEGIN context_state;
464 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
469 "\"" STRING_GROW; BEGIN context_state;
470 \n unexpected_newline (token_start, "\""); BEGIN context_state;
471 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
475 /*---------------------------------------------------.
476 | Strings, comments etc. can be found in user code. |
477 `---------------------------------------------------*/
479 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
483 context_state = YY_START;
484 token_start = loc->start;
489 context_state = YY_START;
490 token_start = loc->start;
495 context_state = YY_START;
496 token_start = loc->start;
501 context_state = YY_START;
502 BEGIN SC_LINE_COMMENT;
508 /*-----------------------------------------------------------.
509 | Scanning some code in braces (actions). The initial "{" is |
511 `-----------------------------------------------------------*/
515 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
516 "%"{splice}">" STRING_GROW; braces_level--;
518 obstack_1grow (&obstack_for_string, '}');
521 if (braces_level < 0)
524 loc->start = code_start;
525 val->chars = last_string;
531 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
533 "<"{splice}"<" STRING_GROW;
536 unexpected_eof (code_start, "}");
538 loc->start = code_start;
539 val->chars = last_string;
546 /*--------------------------------------------------------------.
547 | Scanning some prologue: from "%{" (already scanned) to "%}". |
548 `--------------------------------------------------------------*/
554 loc->start = code_start;
555 val->chars = last_string;
561 unexpected_eof (code_start, "%}");
563 loc->start = code_start;
564 val->chars = last_string;
571 /*---------------------------------------------------------------.
572 | Scanning the epilogue (everything after the second "%%", which |
573 | has already been eaten). |
574 `---------------------------------------------------------------*/
580 loc->start = code_start;
581 val->chars = last_string;
588 /*-----------------------------------------------------.
589 | By default, grow the string obstack with the input. |
590 `-----------------------------------------------------*/
592 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
593 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
597 /* Read bytes from FP into buffer BUF of size SIZE. Return the
598 number of bytes read. Remove '\r' from input, treating \r\n
599 and isolated \r as \n. */
602 no_cr_read (FILE *fp, char *buf, size_t size)
604 size_t bytes_read = fread (buf, 1, size, fp);
607 char *w = memchr (buf, '\r', bytes_read);
611 char const *lim = buf + bytes_read;
615 /* Found an '\r'. Treat it like '\n', but ignore any
616 '\n' that immediately follows. */
621 if (ch != '\n' && ungetc (ch, fp) != ch)
627 /* Copy until the next '\r'. */
633 while ((*w++ = *r++) != '\r');
645 /*------------------------------------------------------.
646 | Scan NUMBER for a base-BASE integer at location LOC. |
647 `------------------------------------------------------*/
649 static unsigned long int
650 scan_integer (char const *number, int base, location loc)
652 verify (INT_MAX < ULONG_MAX);
653 unsigned long int num = strtoul (number, NULL, base);
657 complain_at (loc, _("integer out of range: %s"), quote (number));
665 /*------------------------------------------------------------------.
666 | Convert universal character name UCN to a single-byte character, |
667 | and return that character. Return -1 if UCN does not correspond |
668 | to a single-byte character. |
669 `------------------------------------------------------------------*/
672 convert_ucn_to_byte (char const *ucn)
674 verify (UCHAR_MAX <= INT_MAX);
675 unsigned long int code = strtoul (ucn + 2, NULL, 16);
677 /* FIXME: Currently we assume Unicode-compatible unibyte characters
678 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
679 non-ASCII hosts we support only the portable C character set.
680 These limitations should be removed once we add support for
681 multibyte characters. */
683 if (UCHAR_MAX < code)
686 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
688 /* A non-ASCII host. Use CODE to index into a table of the C
689 basic execution character set, which is guaranteed to exist on
690 all Standard C platforms. This table also includes '$', '@',
691 and '`', which are not in the basic execution character set but
692 which are unibyte characters on all the platforms that we know
694 static signed char const table[] =
696 '\0', -1, -1, -1, -1, -1, -1, '\a',
697 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
698 -1, -1, -1, -1, -1, -1, -1, -1,
699 -1, -1, -1, -1, -1, -1, -1, -1,
700 ' ', '!', '"', '#', '$', '%', '&', '\'',
701 '(', ')', '*', '+', ',', '-', '.', '/',
702 '0', '1', '2', '3', '4', '5', '6', '7',
703 '8', '9', ':', ';', '<', '=', '>', '?',
704 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
705 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
706 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
707 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
708 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
709 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
710 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
711 'x', 'y', 'z', '{', '|', '}', '~'
714 code = code < sizeof table ? table[code] : -1;
722 /*----------------------------------------------------------------.
723 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
724 `----------------------------------------------------------------*/
727 handle_syncline (char *args, location loc)
730 unsigned long int lineno = strtoul (args, &after_num, 10);
731 char *file = strchr (after_num, '"') + 1;
732 *strchr (file, '"') = '\0';
733 if (INT_MAX <= lineno)
735 warn_at (loc, _("line number overflow"));
738 current_file = uniqstr_new (file);
739 boundary_set (&scanner_cursor, current_file, lineno, 1);
743 /*----------------------------------------------------------------.
744 | For a token or comment starting at START, report message MSGID, |
745 | which should say that an end marker was found before |
746 | the expected TOKEN_END. |
747 `----------------------------------------------------------------*/
750 unexpected_end (boundary start, char const *msgid, char const *token_end)
754 loc.end = scanner_cursor;
755 complain_at (loc, _(msgid), token_end);
759 /*------------------------------------------------------------------------.
760 | Report an unexpected EOF in a token or comment starting at START. |
761 | An end of file was encountered and the expected TOKEN_END was missing. |
762 `------------------------------------------------------------------------*/
765 unexpected_eof (boundary start, char const *token_end)
767 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
771 /*----------------------------------------.
772 | Likewise, but for unexpected newlines. |
773 `----------------------------------------*/
776 unexpected_newline (boundary start, char const *token_end)
778 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
782 /*-------------------------.
783 | Initialize the scanner. |
784 `-------------------------*/
787 gram_scanner_initialize (void)
789 obstack_init (&obstack_for_string);
793 /*-----------------------------------------------.
794 | Free all the memory allocated to the scanner. |
795 `-----------------------------------------------*/
798 gram_scanner_free (void)
800 obstack_free (&obstack_for_string, 0);
801 /* Reclaim Flex's buffers. */
802 yy_delete_buffer (YY_CURRENT_BUFFER);