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 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 "flex-scanner.h"
43 #include "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
82 /* Three types of user code:
83 - prologue (code between `%{' `%}' in the first section, before %%);
84 - actions, printers, union, etc, (between braced in the middle section);
85 - epilogue (everything after the second %%). */
86 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
87 /* C and C++ comments in code. */
88 %x SC_COMMENT SC_LINE_COMMENT
89 /* Strings and characters in code. */
90 %x SC_STRING SC_CHARACTER
92 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
93 id {letter}({letter}|[0-9])*
94 directive %{letter}({letter}|[0-9]|-)*
97 /* POSIX says that a tag must be both an id and a C union member, but
98 historically almost any character is allowed in a tag. We disallow
99 NUL and newline, as this simplifies our implementation. */
102 /* Zero or more instances of backslash-newline. Following GCC, allow
103 white space between the backslash and the newline. */
104 splice (\\[ \f\t\v]*\n)*
108 /* Nesting level of the current code in braces. */
109 int braces_level IF_LINT (= 0);
111 /* Parent context state, when applicable. */
112 int context_state IF_LINT (= 0);
114 /* Location of most recent identifier, when applicable. */
115 location id_loc IF_LINT (= empty_location);
117 /* Where containing code started, when applicable. Its initial
118 value is relevant only when yylex is invoked in the SC_EPILOGUE
120 boundary code_start = scanner_cursor;
122 /* Where containing comment or string or character literal started,
124 boundary token_start IF_LINT (= scanner_cursor);
128 /*-----------------------.
129 | Scanning white space. |
130 `-----------------------*/
132 <INITIAL,SC_AFTER_IDENTIFIER>
134 /* Comments and white space. */
135 "," warn_at (*loc, _("stray `,' treated as white space"));
139 token_start = loc->start;
140 context_state = YY_START;
141 BEGIN SC_YACC_COMMENT;
144 /* #line directives are not documented, and may be withdrawn or
145 modified in future versions of Bison. */
146 ^"#line "{int}" \"".*"\"\n" {
147 handle_syncline (yytext + sizeof "#line " - 1, *loc);
152 /*----------------------------.
153 | Scanning Bison directives. |
154 `----------------------------*/
157 "%binary" return PERCENT_NONASSOC;
158 "%code" return PERCENT_CODE;
159 "%debug" return PERCENT_DEBUG;
160 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
161 "%define" return PERCENT_DEFINE;
162 "%defines" return PERCENT_DEFINES;
163 "%destructor" return PERCENT_DESTRUCTOR;
164 "%dprec" return PERCENT_DPREC;
165 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
166 "%expect" return PERCENT_EXPECT;
167 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
168 "%file-prefix" return PERCENT_FILE_PREFIX;
169 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
170 "%initial-action" return PERCENT_INITIAL_ACTION;
171 "%glr-parser" return PERCENT_GLR_PARSER;
172 "%language" return PERCENT_LANGUAGE;
173 "%left" return PERCENT_LEFT;
174 "%lex-param" return PERCENT_LEX_PARAM;
175 "%locations" return PERCENT_LOCATIONS;
176 "%merge" return PERCENT_MERGE;
177 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
178 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
179 "%no"[-_]"lines" return PERCENT_NO_LINES;
180 "%nonassoc" return PERCENT_NONASSOC;
181 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
182 "%nterm" return PERCENT_NTERM;
183 "%output" return PERCENT_OUTPUT;
184 "%parse-param" return PERCENT_PARSE_PARAM;
185 "%prec" return PERCENT_PREC;
186 "%printer" return PERCENT_PRINTER;
187 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
188 "%push-parser" return PERCENT_PUSH_PARSER;
189 "%push-pull-parser" return PERCENT_PUSH_PULL_PARSER;
190 "%require" return PERCENT_REQUIRE;
191 "%right" return PERCENT_RIGHT;
192 "%skeleton" return PERCENT_SKELETON;
193 "%start" return PERCENT_START;
194 "%term" return PERCENT_TOKEN;
195 "%token" return PERCENT_TOKEN;
196 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
197 "%type" return PERCENT_TYPE;
198 "%union" return PERCENT_UNION;
199 "%verbose" return PERCENT_VERBOSE;
200 "%yacc" return PERCENT_YACC;
203 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
208 ";" return SEMICOLON;
209 "<*>" return TYPE_TAG_ANY;
210 "<>" return TYPE_TAG_NONE;
213 val->uniqstr = uniqstr_new (yytext);
215 BEGIN SC_AFTER_IDENTIFIER;
219 val->integer = scan_integer (yytext, 10, *loc);
222 0[xX][0-9abcdefABCDEF]+ {
223 val->integer = scan_integer (yytext, 16, *loc);
227 /* Characters. We don't check there is only one. */
228 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
231 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
234 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
236 /* Code in between braces. */
240 code_start = loc->start;
241 BEGIN SC_BRACED_CODE;
246 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
248 val->uniqstr = uniqstr_new (last_string);
254 static int percent_percent_count;
255 if (++percent_percent_count == 2)
257 return PERCENT_PERCENT;
261 complain_at (*loc, _("invalid character: %s"), quote (yytext));
265 loc->start = loc->end = scanner_cursor;
271 /*-----------------------------------------------------------------.
272 | Scanning after an identifier, checking whether a colon is next. |
273 `-----------------------------------------------------------------*/
275 <SC_AFTER_IDENTIFIER>
283 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
297 /*---------------------------------------------------------------.
298 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
299 `---------------------------------------------------------------*/
303 "*/" BEGIN context_state;
305 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
309 /*------------------------------------------------------------.
310 | Scanning a C comment. The initial `/ *' is already eaten. |
311 `------------------------------------------------------------*/
315 "*"{splice}"/" STRING_GROW; BEGIN context_state;
316 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
320 /*--------------------------------------------------------------.
321 | Scanning a line comment. The initial `//' is already eaten. |
322 `--------------------------------------------------------------*/
326 "\n" STRING_GROW; BEGIN context_state;
327 {splice} STRING_GROW;
328 <<EOF>> BEGIN context_state;
332 /*------------------------------------------------.
333 | Scanning a Bison string, including its escapes. |
334 | The initial quote is already eaten. |
335 `------------------------------------------------*/
340 if (yytext[0] == '\n')
341 unexpected_newline (token_start, "\"");
343 loc->start = token_start;
344 val->chars = last_string;
349 unexpected_eof (token_start, "\"");
351 loc->start = token_start;
352 val->chars = last_string;
358 /*----------------------------------------------------------.
359 | Scanning a Bison character literal, decoding its escapes. |
360 | The initial quote is already eaten. |
361 `----------------------------------------------------------*/
363 <SC_ESCAPED_CHARACTER>
366 if (yytext[0] == '\n')
367 unexpected_newline (token_start, "'");
370 loc->start = token_start;
371 val->character = last_string[1];
377 unexpected_eof (token_start, "'");
379 loc->start = token_start;
380 if (strlen(last_string) > 1)
381 val->character = last_string[1];
383 val->character = last_string[0];
390 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
392 \0 complain_at (*loc, _("invalid null character"));
396 /*----------------------------.
397 | Decode escaped characters. |
398 `----------------------------*/
400 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
403 unsigned long int c = strtoul (yytext + 1, NULL, 8);
405 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
407 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
409 obstack_1grow (&obstack_for_string, c);
412 \\x[0-9abcdefABCDEF]+ {
413 verify (UCHAR_MAX < ULONG_MAX);
414 unsigned long int c = strtoul (yytext + 2, NULL, 16);
416 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
418 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
420 obstack_1grow (&obstack_for_string, c);
423 \\a obstack_1grow (&obstack_for_string, '\a');
424 \\b obstack_1grow (&obstack_for_string, '\b');
425 \\f obstack_1grow (&obstack_for_string, '\f');
426 \\n obstack_1grow (&obstack_for_string, '\n');
427 \\r obstack_1grow (&obstack_for_string, '\r');
428 \\t obstack_1grow (&obstack_for_string, '\t');
429 \\v obstack_1grow (&obstack_for_string, '\v');
431 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
432 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
434 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
435 int c = convert_ucn_to_byte (yytext);
437 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
439 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
441 obstack_1grow (&obstack_for_string, c);
444 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
449 /*--------------------------------------------.
450 | Scanning user-code characters and strings. |
451 `--------------------------------------------*/
453 <SC_CHARACTER,SC_STRING>
455 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
460 "'" STRING_GROW; BEGIN context_state;
461 \n unexpected_newline (token_start, "'"); BEGIN context_state;
462 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
467 "\"" STRING_GROW; BEGIN context_state;
468 \n unexpected_newline (token_start, "\""); BEGIN context_state;
469 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
473 /*---------------------------------------------------.
474 | Strings, comments etc. can be found in user code. |
475 `---------------------------------------------------*/
477 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
481 context_state = YY_START;
482 token_start = loc->start;
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 BEGIN SC_LINE_COMMENT;
506 /*-----------------------------------------------------------.
507 | Scanning some code in braces (actions). The initial "{" is |
509 `-----------------------------------------------------------*/
513 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
514 "%"{splice}">" STRING_GROW; braces_level--;
516 obstack_1grow (&obstack_for_string, '}');
519 if (braces_level < 0)
522 loc->start = code_start;
523 val->code = last_string;
529 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
531 "<"{splice}"<" STRING_GROW;
534 unexpected_eof (code_start, "}");
536 loc->start = code_start;
537 val->code = last_string;
544 /*--------------------------------------------------------------.
545 | Scanning some prologue: from "%{" (already scanned) to "%}". |
546 `--------------------------------------------------------------*/
552 loc->start = code_start;
553 val->chars = last_string;
559 unexpected_eof (code_start, "%}");
561 loc->start = code_start;
562 val->chars = last_string;
569 /*---------------------------------------------------------------.
570 | Scanning the epilogue (everything after the second "%%", which |
571 | has already been eaten). |
572 `---------------------------------------------------------------*/
578 loc->start = code_start;
579 val->chars = last_string;
586 /*-----------------------------------------------------.
587 | By default, grow the string obstack with the input. |
588 `-----------------------------------------------------*/
590 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
591 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
595 /* Read bytes from FP into buffer BUF of size SIZE. Return the
596 number of bytes read. Remove '\r' from input, treating \r\n
597 and isolated \r as \n. */
600 no_cr_read (FILE *fp, char *buf, size_t size)
602 size_t bytes_read = fread (buf, 1, size, fp);
605 char *w = memchr (buf, '\r', bytes_read);
609 char const *lim = buf + bytes_read;
613 /* Found an '\r'. Treat it like '\n', but ignore any
614 '\n' that immediately follows. */
619 if (ch != '\n' && ungetc (ch, fp) != ch)
625 /* Copy until the next '\r'. */
631 while ((*w++ = *r++) != '\r');
643 /*------------------------------------------------------.
644 | Scan NUMBER for a base-BASE integer at location LOC. |
645 `------------------------------------------------------*/
647 static unsigned long int
648 scan_integer (char const *number, int base, location loc)
650 verify (INT_MAX < ULONG_MAX);
651 unsigned long int num = strtoul (number, NULL, base);
655 complain_at (loc, _("integer out of range: %s"), quote (number));
663 /*------------------------------------------------------------------.
664 | Convert universal character name UCN to a single-byte character, |
665 | and return that character. Return -1 if UCN does not correspond |
666 | to a single-byte character. |
667 `------------------------------------------------------------------*/
670 convert_ucn_to_byte (char const *ucn)
672 verify (UCHAR_MAX <= INT_MAX);
673 unsigned long int code = strtoul (ucn + 2, NULL, 16);
675 /* FIXME: Currently we assume Unicode-compatible unibyte characters
676 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
677 non-ASCII hosts we support only the portable C character set.
678 These limitations should be removed once we add support for
679 multibyte characters. */
681 if (UCHAR_MAX < code)
684 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
686 /* A non-ASCII host. Use CODE to index into a table of the C
687 basic execution character set, which is guaranteed to exist on
688 all Standard C platforms. This table also includes '$', '@',
689 and '`', which are not in the basic execution character set but
690 which are unibyte characters on all the platforms that we know
692 static signed char const table[] =
694 '\0', -1, -1, -1, -1, -1, -1, '\a',
695 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
696 -1, -1, -1, -1, -1, -1, -1, -1,
697 -1, -1, -1, -1, -1, -1, -1, -1,
698 ' ', '!', '"', '#', '$', '%', '&', '\'',
699 '(', ')', '*', '+', ',', '-', '.', '/',
700 '0', '1', '2', '3', '4', '5', '6', '7',
701 '8', '9', ':', ';', '<', '=', '>', '?',
702 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
703 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
704 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
705 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
706 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
707 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
708 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
709 'x', 'y', 'z', '{', '|', '}', '~'
712 code = code < sizeof table ? table[code] : -1;
720 /*----------------------------------------------------------------.
721 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
722 `----------------------------------------------------------------*/
725 handle_syncline (char *args, location loc)
728 unsigned long int lineno = strtoul (args, &after_num, 10);
729 char *file = strchr (after_num, '"') + 1;
730 *strchr (file, '"') = '\0';
731 if (INT_MAX <= lineno)
733 warn_at (loc, _("line number overflow"));
736 current_file = uniqstr_new (file);
737 boundary_set (&scanner_cursor, current_file, lineno, 1);
741 /*----------------------------------------------------------------.
742 | For a token or comment starting at START, report message MSGID, |
743 | which should say that an end marker was found before |
744 | the expected TOKEN_END. |
745 `----------------------------------------------------------------*/
748 unexpected_end (boundary start, char const *msgid, char const *token_end)
752 loc.end = scanner_cursor;
753 complain_at (loc, _(msgid), token_end);
757 /*------------------------------------------------------------------------.
758 | Report an unexpected EOF in a token or comment starting at START. |
759 | An end of file was encountered and the expected TOKEN_END was missing. |
760 `------------------------------------------------------------------------*/
763 unexpected_eof (boundary start, char const *token_end)
765 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
769 /*----------------------------------------.
770 | Likewise, but for unexpected newlines. |
771 `----------------------------------------*/
774 unexpected_newline (boundary start, char const *token_end)
776 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
780 /*-------------------------.
781 | Initialize the scanner. |
782 `-------------------------*/
785 gram_scanner_initialize (void)
787 obstack_init (&obstack_for_string);
791 /*-----------------------------------------------.
792 | Free all the memory allocated to the scanner. |
793 `-----------------------------------------------*/
796 gram_scanner_free (void)
798 obstack_free (&obstack_for_string, 0);
799 /* Reclaim Flex's buffers. */