1 /* Bison Grammar Scanner -*- C -*-
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 %option debug nodefault noyywrap never-interactive
23 %option prefix="gram_" outfile="lex.yy.c"
35 /* Each time we match a string, move the end cursor to its end. */
36 #define YY_USER_INIT \
38 LOCATION_RESET (*yylloc); \
39 yylloc->file = infile; \
40 /* This is only to avoid GCC warnings. */ \
44 #define YY_USER_ACTION extend_location (yylloc, yytext, yyleng);
45 #define YY_STEP LOCATION_STEP (*yylloc)
47 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
50 /* Read bytes from FP into buffer BUF of size SIZE. Return the
51 number of bytes read. Remove '\r' from input, treating \r\n
52 and isolated \r as \n. */
55 no_cr_read (FILE *fp, char *buf, size_t size)
57 size_t s = fread (buf, 1, size, fp);
60 char *w = memchr (buf, '\r', s);
64 char const *lim = buf + s;
68 /* Found an '\r'. Treat it like '\n', but ignore any
69 '\n' that immediately follows. */
74 if (ch != '\n' && ungetc (ch, fp) != ch)
80 /* Copy until the next '\r'. */
86 while ((*w++ = *r++) != '\r');
97 /* Extend *LOC to account for token TOKEN of size SIZE. */
100 extend_location (location_t *loc, char const *token, int size)
102 int line = loc->last_line;
103 int column = loc->last_column;
104 char const *p0 = token;
105 char const *p = token;
106 char const *lim = token + size;
108 for (p = token; p < lim; p++)
112 /* \r shouldn't survive no_cr_read. */
122 column += mbsnwidth (p0, p - p0, 0);
123 column += 8 - ((column - 1) & 7);
128 loc->last_line = line;
129 loc->last_column = column + mbsnwidth (p0, p - p0, 0);
134 /* STRING_OBSTACK -- Used to store all the characters that we need to
135 keep (to construct ID, STRINGS etc.). Use the following macros to
138 Use YY_OBS_GROW to append what has just been matched, and
139 YY_OBS_FINISH to end the string (it puts the ending 0).
140 YY_OBS_FINISH also stores this string in LAST_STRING, which can be
141 used, and which is used by YY_OBS_FREE to free the last string. */
143 static struct obstack string_obstack;
145 #define YY_OBS_GROW \
146 obstack_grow (&string_obstack, yytext, yyleng)
148 #define YY_OBS_FINISH \
150 obstack_1grow (&string_obstack, '\0'); \
151 last_string = obstack_finish (&string_obstack); \
154 #define YY_OBS_FREE \
155 obstack_free (&string_obstack, last_string)
158 /* Within well-formed rules, RULE_LENGTH is the number of values in
159 the current rule so far, which says where to find `$0' with respect
160 to the top of the stack. It is not the same as the rule->length in
161 the case of mid rule actions.
163 Outside of well-formed rules, RULE_LENGTH has an undefined value. */
164 static int rule_length;
166 static void handle_dollar (braced_code_t code_kind,
167 char *cp, location_t location);
168 static void handle_at (braced_code_t code_kind,
169 char *cp, location_t location);
170 static void handle_syncline (char *args, location_t *location);
171 static int convert_ucn_to_byte (char const *hex_text);
172 static void unexpected_end_of_file (location_t *, char const *);
175 %x SC_COMMENT SC_LINE_COMMENT SC_YACC_COMMENT
176 %x SC_STRING SC_CHARACTER
177 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
178 %x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
180 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
181 id {letter}({letter}|[0-9])*
182 directive %{letter}({letter}|[0-9]|-)*
185 /* POSIX says that a tag must be both an id and a C union member, but
186 historically almost any character is allowed in a tag. We disallow
187 NUL and newline, as this simplifies our implementation. */
190 /* Zero or more instances of backslash-newline. Following GCC, allow
191 white space between the backslash and the newline. */
192 splice (\\[ \f\t\v]*\n)*
196 /* Nesting level of the current code in braces. */
197 int braces_level IF_LINT (= 0);
199 /* Scanner context when scanning C code. */
200 int c_context IF_LINT (= 0);
202 /* A string representing the most recently saved token. */
205 /* At each yylex invocation, mark the current position as the
206 start of the next token. */
211 /*----------------------------.
212 | Scanning Bison directives. |
213 `----------------------------*/
216 "%binary" return PERCENT_NONASSOC;
217 "%debug" return PERCENT_DEBUG;
218 "%define" return PERCENT_DEFINE;
219 "%defines" return PERCENT_DEFINES;
220 "%destructor" return PERCENT_DESTRUCTOR;
221 "%dprec" return PERCENT_DPREC;
222 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
223 "%expect" return PERCENT_EXPECT;
224 "%file-prefix" return PERCENT_FILE_PREFIX;
225 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
226 "%glr-parser" return PERCENT_GLR_PARSER;
227 "%left" return PERCENT_LEFT;
228 "%locations" return PERCENT_LOCATIONS;
229 "%merge" return PERCENT_MERGE;
230 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
231 "%no"[-_]"lines" return PERCENT_NO_LINES;
232 "%nonassoc" return PERCENT_NONASSOC;
233 "%nterm" return PERCENT_NTERM;
234 "%output" return PERCENT_OUTPUT;
235 "%parse-param" return PERCENT_PARSE_PARAM;
236 "%prec" rule_length--; return PERCENT_PREC;
237 "%printer" return PERCENT_PRINTER;
238 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
239 "%right" return PERCENT_RIGHT;
240 "%lex-param" return PERCENT_LEX_PARAM;
241 "%skeleton" return PERCENT_SKELETON;
242 "%start" return PERCENT_START;
243 "%term" return PERCENT_TOKEN;
244 "%token" return PERCENT_TOKEN;
245 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
246 "%type" return PERCENT_TYPE;
247 "%union" return PERCENT_UNION;
248 "%verbose" return PERCENT_VERBOSE;
249 "%yacc" return PERCENT_YACC;
252 complain_at (*yylloc, _("invalid directive: %s"), quote (yytext));
256 ^"#line "{int}" \""[^\"]*"\"\n" handle_syncline (yytext + strlen ("#line "), yylloc); YY_STEP;
259 ":" rule_length = 0; return COLON;
260 "|" rule_length = 0; return PIPE;
262 ";" return SEMICOLON;
267 yylval->symbol = symbol_get (yytext, *yylloc);
275 num = strtoul (yytext, 0, 10);
276 if (INT_MAX < num || errno)
278 complain_at (*yylloc, _("integer out of range: %s"), quote (yytext));
281 yylval->integer = num;
285 /* Characters. We don't check there is only one. */
286 "'" YY_OBS_GROW; BEGIN SC_ESCAPED_CHARACTER;
289 "\"" YY_OBS_GROW; BEGIN SC_ESCAPED_STRING;
292 "/*" BEGIN SC_YACC_COMMENT;
296 "%{" BEGIN SC_PROLOGUE;
298 /* Code in between braces. */
299 "{" YY_OBS_GROW; braces_level = 0; BEGIN SC_BRACED_CODE;
303 obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
305 yylval->struniq = struniq_new (last_string);
311 static int percent_percent_count;
312 if (++percent_percent_count == 2)
314 return PERCENT_PERCENT;
318 complain_at (*yylloc, _("invalid character: %s"), quote (yytext));
324 /*---------------------------------------------------------------.
325 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
326 `---------------------------------------------------------------*/
336 <<EOF>> unexpected_end_of_file (yylloc, "*/");
340 /*------------------------------------------------------------.
341 | Scanning a C comment. The initial `/ *' is already eaten. |
342 `------------------------------------------------------------*/
346 "*"{splice}"/" YY_OBS_GROW; BEGIN c_context;
347 <<EOF>> unexpected_end_of_file (yylloc, "*/");
351 /*--------------------------------------------------------------.
352 | Scanning a line comment. The initial `//' is already eaten. |
353 `--------------------------------------------------------------*/
357 "\n" YY_OBS_GROW; BEGIN c_context;
358 {splice} YY_OBS_GROW;
359 <<EOF>> BEGIN c_context;
363 /*----------------------------------------------------------------.
364 | Scanning a C string, including its escapes. The initial `"' is |
366 `----------------------------------------------------------------*/
373 yylval->string = last_string;
380 <<EOF>> unexpected_end_of_file (yylloc, "\"");
383 /*---------------------------------------------------------------.
384 | Scanning a C character, decoding its escapes. The initial "'" |
385 | is already eaten. |
386 `---------------------------------------------------------------*/
388 <SC_ESCAPED_CHARACTER>
393 yylval->symbol = symbol_get (last_string, *yylloc);
394 symbol_class_set (yylval->symbol, token_sym, *yylloc);
395 symbol_user_token_number_set (yylval->symbol,
396 (unsigned char) last_string[1], *yylloc);
404 <<EOF>> unexpected_end_of_file (yylloc, "'");
408 /*----------------------------.
409 | Decode escaped characters. |
410 `----------------------------*/
412 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
415 unsigned long c = strtoul (yytext + 1, 0, 8);
418 complain_at (*yylloc, _("invalid escape sequence: %s"),
423 obstack_1grow (&string_obstack, c);
429 c = strtoul (yytext + 2, 0, 16);
430 if (UCHAR_MAX < c || errno)
432 complain_at (*yylloc, _("invalid escape sequence: %s"),
437 obstack_1grow (&string_obstack, c);
440 \\a obstack_1grow (&string_obstack, '\a');
441 \\b obstack_1grow (&string_obstack, '\b');
442 \\f obstack_1grow (&string_obstack, '\f');
443 \\n obstack_1grow (&string_obstack, '\n');
444 \\r obstack_1grow (&string_obstack, '\r');
445 \\t obstack_1grow (&string_obstack, '\t');
446 \\v obstack_1grow (&string_obstack, '\v');
447 \\[\"\'?\\] obstack_1grow (&string_obstack, yytext[1]);
448 \\(u|U[0-9a-fA-F]{4})[0-9a-fA-F]{4} {
449 int c = convert_ucn_to_byte (yytext);
452 complain_at (*yylloc, _("invalid escape sequence: %s"),
457 obstack_1grow (&string_obstack, c);
460 complain_at (*yylloc, _("unrecognized escape sequence: %s"),
467 /*----------------------------------------------------------.
468 | Scanning a C character without decoding its escapes. The |
469 | initial "'" is already eaten. |
470 `----------------------------------------------------------*/
474 "'" YY_OBS_GROW; BEGIN c_context;
475 \\{splice}[\'\\] YY_OBS_GROW;
476 <<EOF>> unexpected_end_of_file (yylloc, "'");
480 /*----------------------------------------------------------------.
481 | Scanning a C string, without decoding its escapes. The initial |
482 | `"' is already eaten. |
483 `----------------------------------------------------------------*/
487 "\"" YY_OBS_GROW; BEGIN c_context;
488 \\{splice}[\"\\] YY_OBS_GROW;
489 <<EOF>> unexpected_end_of_file (yylloc, "\"");
493 /*---------------------------------------------------.
494 | Strings, comments etc. can be found in user code. |
495 `---------------------------------------------------*/
497 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
499 "'" YY_OBS_GROW; c_context = YY_START; BEGIN SC_CHARACTER;
500 "\"" YY_OBS_GROW; c_context = YY_START; BEGIN SC_STRING;
501 "/"{splice}"*" YY_OBS_GROW; c_context = YY_START; BEGIN SC_COMMENT;
502 "/"{splice}"/" YY_OBS_GROW; c_context = YY_START; BEGIN SC_LINE_COMMENT;
506 /*---------------------------------------------------------------.
507 | Scanning some code in braces (%union and actions). The initial |
508 | "{" is already eaten. |
509 `---------------------------------------------------------------*/
513 "{"|"<"{splice}"%" YY_OBS_GROW; braces_level++;
514 "%"{splice}">" YY_OBS_GROW; braces_level--;
518 if (braces_level < 0)
521 yylval->string = last_string;
528 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
530 "<"{splice}"<" YY_OBS_GROW;
532 "$"("<"{tag}">")?(-?[0-9]+|"$") { handle_dollar (current_braced_code,
534 "@"(-?[0-9]+|"$") { handle_at (current_braced_code,
537 <<EOF>> unexpected_end_of_file (yylloc, "}");
541 /*--------------------------------------------------------------.
542 | Scanning some prologue: from "%{" (already scanned) to "%}". |
543 `--------------------------------------------------------------*/
549 yylval->string = last_string;
554 <<EOF>> unexpected_end_of_file (yylloc, "%}");
558 /*---------------------------------------------------------------.
559 | Scanning the epilogue (everything after the second "%%", which |
560 | has already been eaten). |
561 `---------------------------------------------------------------*/
567 yylval->string = last_string;
574 /*----------------------------------------------------------------.
575 | By default, grow the string obstack with the input, escaping M4 |
576 | quoting characters. |
577 `----------------------------------------------------------------*/
579 <SC_COMMENT,SC_LINE_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
581 \$ obstack_sgrow (&string_obstack, "$][");
582 \@ obstack_sgrow (&string_obstack, "@@");
583 \[ obstack_sgrow (&string_obstack, "@{");
584 \] obstack_sgrow (&string_obstack, "@}");
591 /*------------------------------------------------------------------.
592 | TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
594 | Possible inputs: $[<TYPENAME>]($|integer) |
596 | Output to the STRING_OBSTACK a reference to this semantic value. |
597 `------------------------------------------------------------------*/
600 handle_action_dollar (char *text, location_t location)
602 const char *type_name = NULL;
605 /* Get the type name if explicit. */
618 type_name = symbol_list_n_type_name_get (current_rule, location, 0);
619 if (!type_name && typed)
620 complain_at (location, _("$$ of `%s' has no declared type"),
621 current_rule->sym->tag);
624 obstack_fgrow1 (&string_obstack,
625 "]b4_lhs_value([%s])[", type_name);
631 num = strtol (cp, 0, 10);
633 if (INT_MIN <= num && num <= rule_length && ! errno)
636 if (!type_name && n > 0)
637 type_name = symbol_list_n_type_name_get (current_rule, location,
639 if (!type_name && typed)
640 complain_at (location, _("$%d of `%s' has no declared type"),
641 n, current_rule->sym->tag);
644 obstack_fgrow3 (&string_obstack,
645 "]b4_rhs_value([%d], [%d], [%s])[",
646 rule_length, n, type_name);
649 complain_at (location, _("integer out of range: %s"), quote (text));
654 /*---------------------------------------------------------------.
655 | TEXT is expected to be $$ in some code associated to a symbol: |
656 | destructor or printer. |
657 `---------------------------------------------------------------*/
660 handle_symbol_code_dollar (char *text, location_t location)
664 obstack_sgrow (&string_obstack, "]b4_dollar_dollar[");
666 complain_at (location, _("invalid value: %s"), quote (text));
670 /*-----------------------------------------------------------------.
671 | Dispatch onto handle_action_dollar, or handle_destructor_dollar, |
672 | depending upon CODE_KIND. |
673 `-----------------------------------------------------------------*/
676 handle_dollar (braced_code_t braced_code_kind,
677 char *text, location_t location)
679 switch (braced_code_kind)
681 case action_braced_code:
682 handle_action_dollar (text, location);
685 case destructor_braced_code:
686 case printer_braced_code:
687 handle_symbol_code_dollar (text, location);
693 /*------------------------------------------------------.
694 | TEXT is a location token (i.e., a `@...'). Output to |
695 | STRING_OBSTACK a reference to this location. |
696 `------------------------------------------------------*/
699 handle_action_at (char *text, location_t location)
706 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
712 num = strtol (cp, 0, 10);
714 if (INT_MIN <= num && num <= rule_length && ! errno)
717 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
721 complain_at (location, _("integer out of range: %s"), quote (text));
726 /*---------------------------------------------------------------.
727 | TEXT is expected to be @$ in some code associated to a symbol: |
728 | destructor or printer. |
729 `---------------------------------------------------------------*/
732 handle_symbol_code_at (char *text, location_t location)
736 obstack_sgrow (&string_obstack, "]b4_at_dollar[");
738 complain_at (location, _("invalid value: %s"), quote (text));
742 /*-------------------------------------------------------------------.
743 | Dispatch onto handle_action_at, or handle_destructor_at, depending |
745 `-------------------------------------------------------------------*/
748 handle_at (braced_code_t braced_code_kind,
749 char *text, location_t location)
751 switch (braced_code_kind)
753 case action_braced_code:
754 handle_action_at (text, location);
757 case destructor_braced_code:
758 case printer_braced_code:
759 handle_symbol_code_at (text, location);
765 /*------------------------------------------------------------------.
766 | Convert universal character name UCN to a single-byte character, |
767 | and return that character. Return -1 if UCN does not correspond |
768 | to a single-byte character. |
769 `------------------------------------------------------------------*/
772 convert_ucn_to_byte (char const *ucn)
774 unsigned long code = strtoul (ucn + 2, 0, 16);
776 /* FIXME: Currently we assume Unicode-compatible unibyte characters
777 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
778 non-ASCII hosts we support only the portable C character set.
779 These limitations should be removed once we add support for
780 multibyte characters. */
782 if (UCHAR_MAX < code)
785 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
787 /* A non-ASCII host. Use CODE to index into a table of the C
788 basic execution character set, which is guaranteed to exist on
789 all Standard C platforms. This table also includes '$', '@',
790 and '`', which are not in the basic execution character set but
791 which are unibyte characters on all the platforms that we know
793 static signed char const table[] =
795 '\0', -1, -1, -1, -1, -1, -1, '\a',
796 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
797 -1, -1, -1, -1, -1, -1, -1, -1,
798 -1, -1, -1, -1, -1, -1, -1, -1,
799 ' ', '!', '"', '#', '$', '%', '&', '\'',
800 '(', ')', '*', '+', ',', '-', '.', '/',
801 '0', '1', '2', '3', '4', '5', '6', '7',
802 '8', '9', ':', ';', '<', '=', '>', '?',
803 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
804 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
805 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
806 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
807 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
808 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
809 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
810 'x', 'y', 'z', '{', '|', '}', '~'
813 code = code < sizeof table ? table[code] : -1;
821 /*----------------------------------------------------------------.
822 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
823 `----------------------------------------------------------------*/
826 handle_syncline (char *args, location_t *location)
828 int lineno = strtol (args, &args, 10);
829 const char *file = NULL;
830 file = strchr (args, '"') + 1;
831 *strchr (file, '"') = 0;
832 /* FIXME: Leaking... Can't free, as some locations are still
833 pointing to the old file name. */
834 infile = xstrdup (file);
835 location->file = infile;
836 location->last_line = lineno;
840 /*-------------------------------------------------------------.
841 | Report an unexpected end of file at LOC. An end of file was |
842 | encountered and the expected TOKEN_END was missing. After |
843 | reporting the problem, pretend that TOKEN_END was found. |
844 `-------------------------------------------------------------*/
847 unexpected_end_of_file (location_t *loc, char const *token_end)
849 size_t i = strlen (token_end);
851 complain_at (*loc, _("missing `%s' at end of file"), token_end);
853 /* Adjust location's last column so that any later message does not
854 mention the characters just inserted. */
855 loc->last_column -= i;
858 unput (token_end[--i]);
862 /*-------------------------.
863 | Initialize the scanner. |
864 `-------------------------*/
867 scanner_initialize (void)
869 obstack_init (&string_obstack);
873 /*-----------------------------------------------.
874 | Free all the memory allocated to the scanner. |
875 `-----------------------------------------------*/
880 obstack_free (&string_obstack, 0);
881 /* Reclaim Flex's buffers. */
882 yy_delete_buffer (YY_CURRENT_BUFFER);