1 /* Bison Grammar Scanner -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 %option debug nodefault nounput noyywrap never-interactive
25 %option prefix="gram_" outfile="lex.yy.c"
28 /* Work around a bug in flex 2.5.31. See Debian bug 333231
29 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
33 #define FLEX_PREFIX(Id) gram_ ## Id
34 #include "flex-scanner.h"
46 #include "scan-gram.h"
48 #define YY_DECL GRAM_LEX_DECL
50 #define YY_USER_INIT \
51 code_start = scanner_cursor = loc->start; \
53 /* Location of scanner cursor. */
54 static boundary scanner_cursor;
56 #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
58 static size_t no_cr_read (FILE *, char *, size_t);
59 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
61 /* A string representing the most recently saved token. */
62 static char *last_string;
65 gram_scanner_last_string_free (void)
70 static void handle_syncline (char *, location);
71 static unsigned long int scan_integer (char const *p, int base, location loc);
72 static int convert_ucn_to_byte (char const *hex_text);
73 static void unexpected_eof (boundary, char const *);
74 static void unexpected_newline (boundary, char const *);
77 /* A C-like comment in directives/rules. */
79 /* Strings and characters in directives/rules. */
80 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
81 /* A identifier was just read in directives/rules. Special state
82 to capture the sequence `identifier :'. */
83 %x SC_AFTER_IDENTIFIER
85 /* Three types of user code:
86 - prologue (code between `%{' `%}' in the first section, before %%);
87 - actions, printers, union, etc, (between braced in the middle section);
88 - epilogue (everything after the second %%). */
89 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
90 /* C and C++ comments in code. */
91 %x SC_COMMENT SC_LINE_COMMENT
92 /* Strings and characters in code. */
93 %x SC_STRING SC_CHARACTER
95 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
96 id {letter}({letter}|[0-9])*
97 directive %{letter}({letter}|[0-9]|-)*
100 /* POSIX says that a tag must be both an id and a C union member, but
101 historically almost any character is allowed in a tag. We disallow
102 NUL and newline, as this simplifies our implementation. */
105 /* Zero or more instances of backslash-newline. Following GCC, allow
106 white space between the backslash and the newline. */
107 splice (\\[ \f\t\v]*\n)*
111 /* Nesting level of the current code in braces. */
112 int braces_level IF_LINT (= 0);
114 /* Parent context state, when applicable. */
115 int context_state IF_LINT (= 0);
117 /* Location of most recent identifier, when applicable. */
118 location id_loc IF_LINT (= empty_location);
120 /* Where containing code started, when applicable. Its initial
121 value is relevant only when yylex is invoked in the SC_EPILOGUE
123 boundary code_start = scanner_cursor;
125 /* Where containing comment or string or character literal started,
127 boundary token_start IF_LINT (= scanner_cursor);
131 /*-----------------------.
132 | Scanning white space. |
133 `-----------------------*/
135 <INITIAL,SC_AFTER_IDENTIFIER>
137 /* Comments and white space. */
138 "," warn_at (*loc, _("stray `,' treated as white space"));
142 token_start = loc->start;
143 context_state = YY_START;
144 BEGIN SC_YACC_COMMENT;
147 /* #line directives are not documented, and may be withdrawn or
148 modified in future versions of Bison. */
149 ^"#line "{int}" \"".*"\"\n" {
150 handle_syncline (yytext + sizeof "#line " - 1, *loc);
155 /*----------------------------.
156 | Scanning Bison directives. |
157 `----------------------------*/
160 "%binary" return PERCENT_NONASSOC;
161 "%code" return PERCENT_CODE;
162 "%code-top" return PERCENT_CODE_TOP;
163 "%debug" return PERCENT_DEBUG;
164 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
165 "%define" return PERCENT_DEFINE;
166 "%defines" return PERCENT_DEFINES;
167 "%destructor" return PERCENT_DESTRUCTOR;
168 "%dprec" return PERCENT_DPREC;
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 "%language" return PERCENT_LANGUAGE;
177 "%left" return PERCENT_LEFT;
178 "%lex-param" return PERCENT_LEX_PARAM;
179 "%locations" return PERCENT_LOCATIONS;
180 "%merge" return PERCENT_MERGE;
181 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
182 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
183 "%no"[-_]"lines" return PERCENT_NO_LINES;
184 "%nonassoc" return PERCENT_NONASSOC;
185 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
186 "%nterm" return PERCENT_NTERM;
187 "%output" return PERCENT_OUTPUT;
188 "%parse-param" return PERCENT_PARSE_PARAM;
189 "%prec" return PERCENT_PREC;
190 "%printer" return PERCENT_PRINTER;
191 "%provides" return PERCENT_PROVIDES;
192 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
193 "%push-parser" return PERCENT_PUSH_PARSER;
194 "%push-pull-parser" return PERCENT_PUSH_PULL_PARSER;
195 "%require" return PERCENT_REQUIRE;
196 "%requires" return PERCENT_REQUIRES;
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;
215 "<*>" return TYPE_TAG_ANY;
216 "<>" return TYPE_TAG_NONE;
219 val->uniqstr = uniqstr_new (yytext);
221 BEGIN SC_AFTER_IDENTIFIER;
225 val->integer = scan_integer (yytext, 10, *loc);
228 0[xX][0-9abcdefABCDEF]+ {
229 val->integer = scan_integer (yytext, 16, *loc);
233 /* Characters. We don't check there is only one. */
234 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
237 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
240 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
242 /* Code in between braces. */
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 `------------------------------------------------*/
346 if (yytext[0] == '\n')
347 unexpected_newline (token_start, "\"");
349 loc->start = token_start;
350 val->chars = last_string;
355 unexpected_eof (token_start, "\"");
357 loc->start = token_start;
358 val->chars = last_string;
364 /*----------------------------------------------------------.
365 | Scanning a Bison character literal, decoding its escapes. |
366 | The initial quote is already eaten. |
367 `----------------------------------------------------------*/
369 <SC_ESCAPED_CHARACTER>
372 if (yytext[0] == '\n')
373 unexpected_newline (token_start, "'");
376 loc->start = token_start;
377 val->character = last_string[1];
383 unexpected_eof (token_start, "'");
385 loc->start = token_start;
386 if (strlen(last_string) > 1)
387 val->character = last_string[1];
389 val->character = last_string[0];
396 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
398 \0 complain_at (*loc, _("invalid null character"));
402 /*----------------------------.
403 | Decode escaped characters. |
404 `----------------------------*/
406 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
409 unsigned long int c = strtoul (yytext + 1, NULL, 8);
411 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
413 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
415 obstack_1grow (&obstack_for_string, c);
418 \\x[0-9abcdefABCDEF]+ {
419 verify (UCHAR_MAX < ULONG_MAX);
420 unsigned long int c = strtoul (yytext + 2, NULL, 16);
422 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
424 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
426 obstack_1grow (&obstack_for_string, c);
429 \\a obstack_1grow (&obstack_for_string, '\a');
430 \\b obstack_1grow (&obstack_for_string, '\b');
431 \\f obstack_1grow (&obstack_for_string, '\f');
432 \\n obstack_1grow (&obstack_for_string, '\n');
433 \\r obstack_1grow (&obstack_for_string, '\r');
434 \\t obstack_1grow (&obstack_for_string, '\t');
435 \\v obstack_1grow (&obstack_for_string, '\v');
437 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
438 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
440 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
441 int c = convert_ucn_to_byte (yytext);
443 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
445 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
447 obstack_1grow (&obstack_for_string, c);
450 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
455 /*--------------------------------------------.
456 | Scanning user-code characters and strings. |
457 `--------------------------------------------*/
459 <SC_CHARACTER,SC_STRING>
461 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
466 "'" STRING_GROW; BEGIN context_state;
467 \n unexpected_newline (token_start, "'"); BEGIN context_state;
468 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
473 "\"" STRING_GROW; BEGIN context_state;
474 \n unexpected_newline (token_start, "\""); BEGIN context_state;
475 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
479 /*---------------------------------------------------.
480 | Strings, comments etc. can be found in user code. |
481 `---------------------------------------------------*/
483 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
487 context_state = YY_START;
488 token_start = loc->start;
493 context_state = YY_START;
494 token_start = loc->start;
499 context_state = YY_START;
500 token_start = loc->start;
505 context_state = YY_START;
506 BEGIN SC_LINE_COMMENT;
512 /*-----------------------------------------------------------.
513 | Scanning some code in braces (actions). The initial "{" is |
515 `-----------------------------------------------------------*/
519 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
520 "%"{splice}">" STRING_GROW; braces_level--;
522 obstack_1grow (&obstack_for_string, '}');
525 if (braces_level < 0)
528 loc->start = code_start;
529 val->code = last_string;
535 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
537 "<"{splice}"<" STRING_GROW;
540 unexpected_eof (code_start, "}");
542 loc->start = code_start;
543 val->code = last_string;
550 /*--------------------------------------------------------------.
551 | Scanning some prologue: from "%{" (already scanned) to "%}". |
552 `--------------------------------------------------------------*/
558 loc->start = code_start;
559 val->chars = last_string;
565 unexpected_eof (code_start, "%}");
567 loc->start = code_start;
568 val->chars = last_string;
575 /*---------------------------------------------------------------.
576 | Scanning the epilogue (everything after the second "%%", which |
577 | has already been eaten). |
578 `---------------------------------------------------------------*/
584 loc->start = code_start;
585 val->chars = last_string;
592 /*-----------------------------------------------------.
593 | By default, grow the string obstack with the input. |
594 `-----------------------------------------------------*/
596 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
597 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
601 /* Read bytes from FP into buffer BUF of size SIZE. Return the
602 number of bytes read. Remove '\r' from input, treating \r\n
603 and isolated \r as \n. */
606 no_cr_read (FILE *fp, char *buf, size_t size)
608 size_t bytes_read = fread (buf, 1, size, fp);
611 char *w = memchr (buf, '\r', bytes_read);
615 char const *lim = buf + bytes_read;
619 /* Found an '\r'. Treat it like '\n', but ignore any
620 '\n' that immediately follows. */
625 if (ch != '\n' && ungetc (ch, fp) != ch)
631 /* Copy until the next '\r'. */
637 while ((*w++ = *r++) != '\r');
649 /*------------------------------------------------------.
650 | Scan NUMBER for a base-BASE integer at location LOC. |
651 `------------------------------------------------------*/
653 static unsigned long int
654 scan_integer (char const *number, int base, location loc)
656 verify (INT_MAX < ULONG_MAX);
657 unsigned long int num = strtoul (number, NULL, base);
661 complain_at (loc, _("integer out of range: %s"), quote (number));
669 /*------------------------------------------------------------------.
670 | Convert universal character name UCN to a single-byte character, |
671 | and return that character. Return -1 if UCN does not correspond |
672 | to a single-byte character. |
673 `------------------------------------------------------------------*/
676 convert_ucn_to_byte (char const *ucn)
678 verify (UCHAR_MAX <= INT_MAX);
679 unsigned long int code = strtoul (ucn + 2, NULL, 16);
681 /* FIXME: Currently we assume Unicode-compatible unibyte characters
682 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
683 non-ASCII hosts we support only the portable C character set.
684 These limitations should be removed once we add support for
685 multibyte characters. */
687 if (UCHAR_MAX < code)
690 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
692 /* A non-ASCII host. Use CODE to index into a table of the C
693 basic execution character set, which is guaranteed to exist on
694 all Standard C platforms. This table also includes '$', '@',
695 and '`', which are not in the basic execution character set but
696 which are unibyte characters on all the platforms that we know
698 static signed char const table[] =
700 '\0', -1, -1, -1, -1, -1, -1, '\a',
701 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
702 -1, -1, -1, -1, -1, -1, -1, -1,
703 -1, -1, -1, -1, -1, -1, -1, -1,
704 ' ', '!', '"', '#', '$', '%', '&', '\'',
705 '(', ')', '*', '+', ',', '-', '.', '/',
706 '0', '1', '2', '3', '4', '5', '6', '7',
707 '8', '9', ':', ';', '<', '=', '>', '?',
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', '[', '\\', ']', '^', '_',
712 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
713 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
714 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
715 'x', 'y', 'z', '{', '|', '}', '~'
718 code = code < sizeof table ? table[code] : -1;
726 /*----------------------------------------------------------------.
727 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
728 `----------------------------------------------------------------*/
731 handle_syncline (char *args, location loc)
734 unsigned long int lineno = strtoul (args, &after_num, 10);
735 char *file = strchr (after_num, '"') + 1;
736 *strchr (file, '"') = '\0';
737 if (INT_MAX <= lineno)
739 warn_at (loc, _("line number overflow"));
742 current_file = uniqstr_new (file);
743 boundary_set (&scanner_cursor, current_file, lineno, 1);
747 /*----------------------------------------------------------------.
748 | For a token or comment starting at START, report message MSGID, |
749 | which should say that an end marker was found before |
750 | the expected TOKEN_END. |
751 `----------------------------------------------------------------*/
754 unexpected_end (boundary start, char const *msgid, char const *token_end)
758 loc.end = scanner_cursor;
759 complain_at (loc, _(msgid), token_end);
763 /*------------------------------------------------------------------------.
764 | Report an unexpected EOF in a token or comment starting at START. |
765 | An end of file was encountered and the expected TOKEN_END was missing. |
766 `------------------------------------------------------------------------*/
769 unexpected_eof (boundary start, char const *token_end)
771 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
775 /*----------------------------------------.
776 | Likewise, but for unexpected newlines. |
777 `----------------------------------------*/
780 unexpected_newline (boundary start, char const *token_end)
782 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
786 /*-------------------------.
787 | Initialize the scanner. |
788 `-------------------------*/
791 gram_scanner_initialize (void)
793 obstack_init (&obstack_for_string);
797 /*-----------------------------------------------.
798 | Free all the memory allocated to the scanner. |
799 `-----------------------------------------------*/
802 gram_scanner_free (void)
804 obstack_free (&obstack_for_string, 0);
805 /* Reclaim Flex's buffers. */