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
92 /* Three types of user code:
93 - prologue (code between `%{' `%}' in the first section, before %%);
94 - actions, printers, union, etc, (between braced in the middle section);
95 - epilogue (everything after the second %%). */
96 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
97 /* C and C++ comments in code. */
98 %x SC_COMMENT SC_LINE_COMMENT
99 /* Strings and characters in code. */
100 %x SC_STRING SC_CHARACTER
102 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
103 id {letter}({letter}|[0-9])*
104 directive %{letter}({letter}|[0-9]|-)*
107 /* POSIX says that a tag must be both an id and a C union member, but
108 historically almost any character is allowed in a tag. We disallow
109 NUL and newline, as this simplifies our implementation. */
112 /* Zero or more instances of backslash-newline. Following GCC, allow
113 white space between the backslash and the newline. */
114 splice (\\[ \f\t\v]*\n)*
118 /* Nesting level of the current code in braces. */
119 int braces_level IF_LINT (= 0);
121 /* Parent context state, when applicable. */
122 int context_state IF_LINT (= 0);
124 /* Location of most recent identifier, when applicable. */
125 location id_loc IF_LINT (= empty_location);
127 /* Where containing code started, when applicable. Its initial
128 value is relevant only when yylex is invoked in the SC_EPILOGUE
130 boundary code_start = scanner_cursor;
132 /* Where containing comment or string or character literal started,
134 boundary token_start IF_LINT (= scanner_cursor);
138 /*-----------------------.
139 | Scanning white space. |
140 `-----------------------*/
142 <INITIAL,SC_AFTER_IDENTIFIER>
144 /* Comments and white space. */
145 "," warn_at (*loc, _("stray `,' treated as white space"));
149 token_start = loc->start;
150 context_state = YY_START;
151 BEGIN SC_YACC_COMMENT;
154 /* #line directives are not documented, and may be withdrawn or
155 modified in future versions of Bison. */
156 ^"#line "{int}" \"".*"\"\n" {
157 handle_syncline (yytext + sizeof "#line " - 1, *loc);
162 /*----------------------------.
163 | Scanning Bison directives. |
164 `----------------------------*/
167 "%binary" return PERCENT_NONASSOC;
168 "%debug" return PERCENT_DEBUG;
169 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
170 "%define" return PERCENT_DEFINE;
171 "%defines" return PERCENT_DEFINES;
172 "%destructor" return PERCENT_DESTRUCTOR;
173 "%dprec" return PERCENT_DPREC;
174 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
175 "%expect" return PERCENT_EXPECT;
176 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
177 "%file-prefix" return PERCENT_FILE_PREFIX;
178 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
179 "%initial-action" return PERCENT_INITIAL_ACTION;
180 "%glr-parser" return PERCENT_GLR_PARSER;
181 "%left" return PERCENT_LEFT;
182 "%lex-param" return PERCENT_LEX_PARAM;
183 "%locations" return PERCENT_LOCATIONS;
184 "%merge" return PERCENT_MERGE;
185 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
186 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
187 "%no"[-_]"lines" return PERCENT_NO_LINES;
188 "%nonassoc" return PERCENT_NONASSOC;
189 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
190 "%nterm" return PERCENT_NTERM;
191 "%output" return PERCENT_OUTPUT;
192 "%parse-param" return PERCENT_PARSE_PARAM;
193 "%prec" return PERCENT_PREC;
194 "%printer" return PERCENT_PRINTER;
195 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
196 "%require" return PERCENT_REQUIRE;
197 "%right" return PERCENT_RIGHT;
198 "%skeleton" return PERCENT_SKELETON;
199 "%start" return PERCENT_START;
200 "%term" return PERCENT_TOKEN;
201 "%token" return PERCENT_TOKEN;
202 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
203 "%type" return PERCENT_TYPE;
204 "%union" return PERCENT_UNION;
205 "%verbose" return PERCENT_VERBOSE;
206 "%yacc" return PERCENT_YACC;
209 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
214 ";" return SEMICOLON;
217 val->uniqstr = uniqstr_new (yytext);
219 BEGIN SC_AFTER_IDENTIFIER;
223 val->integer = scan_integer (yytext, 10, *loc);
226 0[xX][0-9abcdefABCDEF]+ {
227 val->integer = scan_integer (yytext, 16, *loc);
231 /* Characters. We don't check there is only one. */
232 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
235 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
238 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
240 /* Code in between braces. */
242 if (current_rule && current_rule->action)
243 grammar_midrule_action ();
246 code_start = loc->start;
247 BEGIN SC_BRACED_CODE;
252 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
254 val->uniqstr = uniqstr_new (last_string);
260 static int percent_percent_count;
261 if (++percent_percent_count == 2)
263 return PERCENT_PERCENT;
267 complain_at (*loc, _("invalid character: %s"), quote (yytext));
271 loc->start = loc->end = scanner_cursor;
277 /*-----------------------------------------------------------------.
278 | Scanning after an identifier, checking whether a colon is next. |
279 `-----------------------------------------------------------------*/
281 <SC_AFTER_IDENTIFIER>
289 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
303 /*---------------------------------------------------------------.
304 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
305 `---------------------------------------------------------------*/
309 "*/" BEGIN context_state;
311 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
315 /*------------------------------------------------------------.
316 | Scanning a C comment. The initial `/ *' is already eaten. |
317 `------------------------------------------------------------*/
321 "*"{splice}"/" STRING_GROW; BEGIN context_state;
322 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
326 /*--------------------------------------------------------------.
327 | Scanning a line comment. The initial `//' is already eaten. |
328 `--------------------------------------------------------------*/
332 "\n" STRING_GROW; BEGIN context_state;
333 {splice} STRING_GROW;
334 <<EOF>> BEGIN context_state;
338 /*------------------------------------------------.
339 | Scanning a Bison string, including its escapes. |
340 | The initial quote is already eaten. |
341 `------------------------------------------------*/
347 loc->start = token_start;
348 val->chars = last_string;
352 \n unexpected_newline (token_start, "\""); BEGIN INITIAL;
353 <<EOF>> unexpected_eof (token_start, "\""); BEGIN INITIAL;
356 /*----------------------------------------------------------.
357 | Scanning a Bison character literal, decoding its escapes. |
358 | The initial quote is already eaten. |
359 `----------------------------------------------------------*/
361 <SC_ESCAPED_CHARACTER>
364 unsigned char last_string_1;
367 loc->start = token_start;
368 val->character = last_string[1];
373 \n unexpected_newline (token_start, "'"); BEGIN INITIAL;
374 <<EOF>> unexpected_eof (token_start, "'"); BEGIN INITIAL;
377 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
379 \0 complain_at (*loc, _("invalid null character"));
383 /*----------------------------.
384 | Decode escaped characters. |
385 `----------------------------*/
387 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
390 unsigned long int c = strtoul (yytext + 1, NULL, 8);
392 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
394 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
396 obstack_1grow (&obstack_for_string, c);
399 \\x[0-9abcdefABCDEF]+ {
400 verify (UCHAR_MAX < ULONG_MAX);
401 unsigned long int c = strtoul (yytext + 2, NULL, 16);
403 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
405 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
407 obstack_1grow (&obstack_for_string, c);
410 \\a obstack_1grow (&obstack_for_string, '\a');
411 \\b obstack_1grow (&obstack_for_string, '\b');
412 \\f obstack_1grow (&obstack_for_string, '\f');
413 \\n obstack_1grow (&obstack_for_string, '\n');
414 \\r obstack_1grow (&obstack_for_string, '\r');
415 \\t obstack_1grow (&obstack_for_string, '\t');
416 \\v obstack_1grow (&obstack_for_string, '\v');
418 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
419 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
421 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
422 int c = convert_ucn_to_byte (yytext);
424 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
426 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
428 obstack_1grow (&obstack_for_string, c);
431 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
436 /*--------------------------------------------.
437 | Scanning user-code characters and strings. |
438 `--------------------------------------------*/
440 <SC_CHARACTER,SC_STRING>
442 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
447 "'" STRING_GROW; BEGIN context_state;
448 \n unexpected_newline (token_start, "'"); BEGIN context_state;
449 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
454 "\"" STRING_GROW; BEGIN context_state;
455 \n unexpected_newline (token_start, "\""); BEGIN context_state;
456 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
460 /*---------------------------------------------------.
461 | Strings, comments etc. can be found in user code. |
462 `---------------------------------------------------*/
464 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
468 context_state = YY_START;
469 token_start = loc->start;
474 context_state = YY_START;
475 token_start = loc->start;
480 context_state = YY_START;
481 token_start = loc->start;
486 context_state = YY_START;
487 BEGIN SC_LINE_COMMENT;
493 /*-----------------------------------------------------------.
494 | Scanning some code in braces (actions). The initial "{" is |
496 `-----------------------------------------------------------*/
500 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
501 "%"{splice}">" STRING_GROW; braces_level--;
503 obstack_1grow (&obstack_for_string, '}');
506 if (braces_level < 0)
509 loc->start = code_start;
510 val->chars = last_string;
511 gram_last_braced_code_loc = *loc;
517 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
519 "<"{splice}"<" STRING_GROW;
521 <<EOF>> unexpected_eof (code_start, "}"); BEGIN INITIAL;
525 /*--------------------------------------------------------------.
526 | Scanning some prologue: from "%{" (already scanned) to "%}". |
527 `--------------------------------------------------------------*/
533 loc->start = code_start;
534 val->chars = last_string;
539 <<EOF>> unexpected_eof (code_start, "%}"); BEGIN INITIAL;
543 /*---------------------------------------------------------------.
544 | Scanning the epilogue (everything after the second "%%", which |
545 | has already been eaten). |
546 `---------------------------------------------------------------*/
552 loc->start = code_start;
553 val->chars = last_string;
560 /*-----------------------------------------------------.
561 | By default, grow the string obstack with the input. |
562 `-----------------------------------------------------*/
564 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
565 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
569 /* Read bytes from FP into buffer BUF of size SIZE. Return the
570 number of bytes read. Remove '\r' from input, treating \r\n
571 and isolated \r as \n. */
574 no_cr_read (FILE *fp, char *buf, size_t size)
576 size_t bytes_read = fread (buf, 1, size, fp);
579 char *w = memchr (buf, '\r', bytes_read);
583 char const *lim = buf + bytes_read;
587 /* Found an '\r'. Treat it like '\n', but ignore any
588 '\n' that immediately follows. */
593 if (ch != '\n' && ungetc (ch, fp) != ch)
599 /* Copy until the next '\r'. */
605 while ((*w++ = *r++) != '\r');
617 /*------------------------------------------------------.
618 | Scan NUMBER for a base-BASE integer at location LOC. |
619 `------------------------------------------------------*/
621 static unsigned long int
622 scan_integer (char const *number, int base, location loc)
624 verify (INT_MAX < ULONG_MAX);
625 unsigned long int num = strtoul (number, NULL, base);
629 complain_at (loc, _("integer out of range: %s"), quote (number));
637 /*------------------------------------------------------------------.
638 | Convert universal character name UCN to a single-byte character, |
639 | and return that character. Return -1 if UCN does not correspond |
640 | to a single-byte character. |
641 `------------------------------------------------------------------*/
644 convert_ucn_to_byte (char const *ucn)
646 verify (UCHAR_MAX <= INT_MAX);
647 unsigned long int code = strtoul (ucn + 2, NULL, 16);
649 /* FIXME: Currently we assume Unicode-compatible unibyte characters
650 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
651 non-ASCII hosts we support only the portable C character set.
652 These limitations should be removed once we add support for
653 multibyte characters. */
655 if (UCHAR_MAX < code)
658 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
660 /* A non-ASCII host. Use CODE to index into a table of the C
661 basic execution character set, which is guaranteed to exist on
662 all Standard C platforms. This table also includes '$', '@',
663 and '`', which are not in the basic execution character set but
664 which are unibyte characters on all the platforms that we know
666 static signed char const table[] =
668 '\0', -1, -1, -1, -1, -1, -1, '\a',
669 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
670 -1, -1, -1, -1, -1, -1, -1, -1,
671 -1, -1, -1, -1, -1, -1, -1, -1,
672 ' ', '!', '"', '#', '$', '%', '&', '\'',
673 '(', ')', '*', '+', ',', '-', '.', '/',
674 '0', '1', '2', '3', '4', '5', '6', '7',
675 '8', '9', ':', ';', '<', '=', '>', '?',
676 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
677 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
678 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
679 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
680 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
681 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
682 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
683 'x', 'y', 'z', '{', '|', '}', '~'
686 code = code < sizeof table ? table[code] : -1;
694 /*----------------------------------------------------------------.
695 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
696 `----------------------------------------------------------------*/
699 handle_syncline (char *args, location loc)
702 unsigned long int lineno = strtoul (args, &after_num, 10);
703 char *file = strchr (after_num, '"') + 1;
704 *strchr (file, '"') = '\0';
705 if (INT_MAX <= lineno)
707 warn_at (loc, _("line number overflow"));
710 current_file = uniqstr_new (file);
711 boundary_set (&scanner_cursor, current_file, lineno, 1);
715 /*----------------------------------------------------------------.
716 | For a token or comment starting at START, report message MSGID, |
717 | which should say that an end marker was found before |
718 | the expected TOKEN_END. |
719 `----------------------------------------------------------------*/
722 unexpected_end (boundary start, char const *msgid, char const *token_end)
726 loc.end = scanner_cursor;
727 complain_at (loc, _(msgid), token_end);
731 /*------------------------------------------------------------------------.
732 | Report an unexpected EOF in a token or comment starting at START. |
733 | An end of file was encountered and the expected TOKEN_END was missing. |
734 `------------------------------------------------------------------------*/
737 unexpected_eof (boundary start, char const *token_end)
739 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
743 /*----------------------------------------.
744 | Likewise, but for unexpected newlines. |
745 `----------------------------------------*/
748 unexpected_newline (boundary start, char const *token_end)
750 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
754 /*-------------------------.
755 | Initialize the scanner. |
756 `-------------------------*/
759 gram_scanner_initialize (void)
761 obstack_init (&obstack_for_string);
765 /*-----------------------------------------------.
766 | Free all the memory allocated to the scanner. |
767 `-----------------------------------------------*/
770 gram_scanner_free (void)
772 obstack_free (&obstack_for_string, 0);
773 /* Reclaim Flex's buffers. */
774 yy_delete_buffer (YY_CURRENT_BUFFER);