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 nounput never-interactive stack
23 %option prefix="gram_" outfile="lex.yy.c"
34 /* Each time we match a string, move the end cursor to its end. */
35 #define YY_USER_INIT \
37 LOCATION_RESET (*yylloc); \
38 yylloc->file = infile; \
39 /* This is only to avoid GCC warnings. */ \
43 #define YY_USER_ACTION extend_location (yylloc, yytext, yyleng);
44 #define YY_STEP LOCATION_STEP (*yylloc)
46 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
49 /* Read bytes from FP into buffer BUF of size SIZE. Return the
50 number of bytes read. Remove '\r' from input, treating \r\n
51 and isolated \r as \n. */
54 no_cr_read (FILE *fp, char *buf, size_t size)
56 size_t s = fread (buf, 1, size, fp);
59 char *w = memchr (buf, '\r', s);
63 char const *lim = buf + s;
67 /* Found an '\r'. Treat it like '\n', but ignore any
68 '\n' that immediately follows. */
73 if (ch != '\n' && ungetc (ch, fp) != ch)
79 /* Copy until the next '\r'. */
85 while ((*w++ = *r++) != '\r');
96 /* Extend *LOC to account for token TOKEN of size SIZE. */
99 extend_location (location_t *loc, char const *token, int size)
101 int line = loc->last_line;
102 int column = loc->last_column;
103 char const *p0 = token;
104 char const *p = token;
105 char const *lim = token + size;
107 for (p = token; p < lim; p++)
111 /* \r shouldn't survive no_cr_read. */
121 column += mbsnwidth (p0, p - p0, 0);
122 column += 8 - ((column - 1) & 7);
127 loc->last_line = line;
128 loc->last_column = column + mbsnwidth (p0, p - p0, 0);
132 /* Report an unexpected end of file at LOC. A token or comment began
133 with TOKEN_START, but an end of file was encountered and the
134 expected TOKEN_END was missing. */
137 unexpected_end_of_file (location_t loc,
138 char const *token_start, char const *token_end)
140 complain_at (loc, _("unexpected end of file in `%s ... %s'"),
141 token_start, token_end);
146 /* STRING_OBSTACK -- Used to store all the characters that we need to
147 keep (to construct ID, STRINGS etc.). Use the following macros to
150 Use YY_OBS_GROW to append what has just been matched, and
151 YY_OBS_FINISH to end the string (it puts the ending 0).
152 YY_OBS_FINISH also stores this string in LAST_STRING, which can be
153 used, and which is used by YY_OBS_FREE to free the last string. */
155 static struct obstack string_obstack;
158 #define YY_OBS_GROW \
159 obstack_grow (&string_obstack, yytext, yyleng)
161 #define YY_OBS_FINISH \
163 obstack_1grow (&string_obstack, '\0'); \
164 last_string = obstack_finish (&string_obstack); \
167 #define YY_OBS_FREE \
169 obstack_free (&string_obstack, last_string); \
173 scanner_last_string_free (void)
179 static int percent_percent_count = 0;
181 /* Within well-formed rules, RULE_LENGTH is the number of values in
182 the current rule so far, which says where to find `$0' with respect
183 to the top of the stack. It is not the same as the rule->length in
184 the case of mid rule actions.
186 Outside of well-formed rules, RULE_LENGTH has an undefined value. */
187 static int rule_length;
189 static void handle_dollar (braced_code_t code_kind,
190 char *cp, location_t location);
191 static void handle_at (braced_code_t code_kind,
192 char *cp, location_t location);
193 static void handle_syncline (char *args, location_t *location);
194 static int convert_ucn_to_byte (char const *hex_text);
197 %x SC_COMMENT SC_LINE_COMMENT SC_YACC_COMMENT
198 %x SC_STRING SC_CHARACTER
199 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
200 %x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
202 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
203 id {letter}({letter}|[0-9])*
204 directive %{letter}({letter}|[0-9]|-)*
207 /* POSIX says that a tag must be both an id and a C union member, but
208 historically almost any character is allowed in a tag. We disallow
209 NUL and newline, as this simplifies our implementation. */
212 /* Zero or more instances of backslash-newline. Following GCC, allow
213 white space between the backslash and the newline. */
214 splice (\\[ \f\t\v]*\n)*
218 int braces_level IF_LINT (= 0);
220 /* At each yylex invocation, mark the current position as the
221 start of the next token. */
226 /*----------------------------.
227 | Scanning Bison directives. |
228 `----------------------------*/
231 "%binary" return PERCENT_NONASSOC;
232 "%debug" return PERCENT_DEBUG;
233 "%define" return PERCENT_DEFINE;
234 "%defines" return PERCENT_DEFINES;
235 "%destructor" return PERCENT_DESTRUCTOR;
236 "%dprec" return PERCENT_DPREC;
237 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
238 "%expect" return PERCENT_EXPECT;
239 "%file-prefix" return PERCENT_FILE_PREFIX;
240 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
241 "%glr-parser" return PERCENT_GLR_PARSER;
242 "%left" return PERCENT_LEFT;
243 "%locations" return PERCENT_LOCATIONS;
244 "%merge" return PERCENT_MERGE;
245 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
246 "%no"[-_]"lines" return PERCENT_NO_LINES;
247 "%nonassoc" return PERCENT_NONASSOC;
248 "%nterm" return PERCENT_NTERM;
249 "%output" return PERCENT_OUTPUT;
250 "%parse-param" return PERCENT_PARSE_PARAM;
251 "%prec" rule_length--; return PERCENT_PREC;
252 "%printer" return PERCENT_PRINTER;
253 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
254 "%right" return PERCENT_RIGHT;
255 "%lex-param" return PERCENT_LEX_PARAM;
256 "%skeleton" return PERCENT_SKELETON;
257 "%start" return PERCENT_START;
258 "%term" return PERCENT_TOKEN;
259 "%token" return PERCENT_TOKEN;
260 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
261 "%type" return PERCENT_TYPE;
262 "%union" return PERCENT_UNION;
263 "%verbose" return PERCENT_VERBOSE;
264 "%yacc" return PERCENT_YACC;
267 complain_at (*yylloc, _("invalid directive: %s"), quote (yytext));
271 ^"#line "{int}" \""[^\"]*"\"\n" handle_syncline (yytext + strlen ("#line "), yylloc); YY_STEP;
274 ":" rule_length = 0; return COLON;
275 "|" rule_length = 0; return PIPE;
277 ";" return SEMICOLON;
279 [ \f\n\t\v]+ YY_STEP;
282 yylval->symbol = symbol_get (yytext, *yylloc);
290 num = strtoul (yytext, 0, 10);
291 if (INT_MAX < num || errno)
293 complain_at (*yylloc, _("integer out of range: %s"), quote (yytext));
296 yylval->integer = num;
300 /* Characters. We don't check there is only one. */
301 "'" YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
304 "\"" YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
307 "/*" BEGIN SC_YACC_COMMENT;
311 "%{" yy_push_state (SC_PROLOGUE);
313 /* Code in between braces. */
314 "{" YY_OBS_GROW; braces_level = 0; yy_push_state (SC_BRACED_CODE);
318 obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
320 yylval->string = last_string;
326 if (++percent_percent_count == 2)
327 yy_push_state (SC_EPILOGUE);
328 return PERCENT_PERCENT;
332 complain_at (*yylloc, _("invalid character: %s"), quote (yytext));
338 /*-------------------------------------------------------------------.
339 | Whatever the start condition (but those which correspond to |
340 | entities `swallowed' by Bison: SC_YACC_COMMENT, SC_ESCAPED_STRING, |
341 | and SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
342 `-------------------------------------------------------------------*/
344 <SC_COMMENT,SC_LINE_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
346 \[ obstack_sgrow (&string_obstack, "@<:@");
347 \] obstack_sgrow (&string_obstack, "@:>@");
351 /*---------------------------------------------------------------.
352 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
353 `---------------------------------------------------------------*/
365 unexpected_end_of_file (*yylloc, "/*", "*/");
371 /*------------------------------------------------------------.
372 | Scanning a C comment. The initial `/ *' is already eaten. |
373 `------------------------------------------------------------*/
377 "*"{splice}"/" YY_OBS_GROW; yy_pop_state ();
378 [^*\[\]]+|"*" YY_OBS_GROW;
381 unexpected_end_of_file (*yylloc, "/*", "*/");
387 /*--------------------------------------------------------------.
388 | Scanning a line comment. The initial `//' is already eaten. |
389 `--------------------------------------------------------------*/
393 "\n" YY_OBS_GROW; yy_pop_state ();
394 ([^\n\[\]]|{splice})+ YY_OBS_GROW;
395 <<EOF>> yy_pop_state ();
399 /*----------------------------------------------------------------.
400 | Scanning a C string, including its escapes. The initial `"' is |
402 `----------------------------------------------------------------*/
407 assert (yy_top_state () == INITIAL);
410 yylval->string = last_string;
416 [^\"\\]+ YY_OBS_GROW;
419 unexpected_end_of_file (*yylloc, "\"", "\"");
420 assert (yy_top_state () == INITIAL);
422 yylval->string = last_string;
428 /*---------------------------------------------------------------.
429 | Scanning a C character, decoding its escapes. The initial "'" |
430 | is already eaten. |
431 `---------------------------------------------------------------*/
433 <SC_ESCAPED_CHARACTER>
437 assert (yy_top_state () == INITIAL);
440 yylval->symbol = symbol_get (last_string, *yylloc);
441 symbol_class_set (yylval->symbol, token_sym, *yylloc);
442 symbol_user_token_number_set (yylval->symbol,
443 (unsigned char) last_string[1], *yylloc);
451 [^\'\\]+ YY_OBS_GROW;
454 unexpected_end_of_file (*yylloc, "'", "'");
455 assert (yy_top_state () == INITIAL);
457 yylval->string = last_string;
464 /*----------------------------.
465 | Decode escaped characters. |
466 `----------------------------*/
468 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
471 unsigned long c = strtoul (yytext + 1, 0, 8);
474 complain_at (*yylloc, _("invalid escape sequence: %s"),
479 obstack_1grow (&string_obstack, c);
485 c = strtoul (yytext + 2, 0, 16);
486 if (UCHAR_MAX < c || errno)
488 complain_at (*yylloc, _("invalid escape sequence: %s"),
493 obstack_1grow (&string_obstack, c);
496 \\a obstack_1grow (&string_obstack, '\a');
497 \\b obstack_1grow (&string_obstack, '\b');
498 \\f obstack_1grow (&string_obstack, '\f');
499 \\n obstack_1grow (&string_obstack, '\n');
500 \\r obstack_1grow (&string_obstack, '\r');
501 \\t obstack_1grow (&string_obstack, '\t');
502 \\v obstack_1grow (&string_obstack, '\v');
503 \\[\"\'?\\] obstack_1grow (&string_obstack, yytext[1]);
504 \\(u|U[0-9a-fA-F]{4})[0-9a-fA-F]{4} {
505 int c = convert_ucn_to_byte (yytext);
508 complain_at (*yylloc, _("invalid escape sequence: %s"),
513 obstack_1grow (&string_obstack, c);
516 complain_at (*yylloc, _("unrecognized escape sequence: %s"),
520 /* FLex wants this rule, in case of a `\<<EOF>>'. */
525 /*----------------------------------------------------------.
526 | Scanning a C character without decoding its escapes. The |
527 | initial "'" is already eaten. |
528 `----------------------------------------------------------*/
534 assert (yy_top_state () != INITIAL);
538 [^'\[\]\\]+ YY_OBS_GROW;
539 \\{splice}[^\[\]] YY_OBS_GROW;
540 {splice} YY_OBS_GROW;
541 /* Needed for `\<<EOF>>', `\\<<newline>>[', and `\\<<newline>>]'. */
545 unexpected_end_of_file (*yylloc, "'", "'");
546 assert (yy_top_state () != INITIAL);
552 /*----------------------------------------------------------------.
553 | Scanning a C string, without decoding its escapes. The initial |
554 | `"' is already eaten. |
555 `----------------------------------------------------------------*/
560 assert (yy_top_state () != INITIAL);
565 [^\"\[\]\\]+ YY_OBS_GROW;
566 \\{splice}[^\[\]] YY_OBS_GROW;
567 {splice} YY_OBS_GROW;
568 /* Needed for `\<<EOF>>', `\\<<newline>>[', and `\\<<newline>>]'. */
572 unexpected_end_of_file (*yylloc, "\"", "\"");
573 assert (yy_top_state () != INITIAL);
579 /*---------------------------------------------------.
580 | Strings, comments etc. can be found in user code. |
581 `---------------------------------------------------*/
583 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
585 /* Characters. We don't check there is only one. */
586 "'" YY_OBS_GROW; yy_push_state (SC_CHARACTER);
589 "\"" YY_OBS_GROW; yy_push_state (SC_STRING);
592 "/"{splice}"*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
593 "/"{splice}"/" YY_OBS_GROW; yy_push_state (SC_LINE_COMMENT);
600 /*---------------------------------------------------------------.
601 | Scanning some code in braces (%union and actions). The initial |
602 | "{" is already eaten. |
603 `---------------------------------------------------------------*/
607 "{"|"<"{splice}"%" YY_OBS_GROW; braces_level++;
608 "%"{splice}">" YY_OBS_GROW; braces_level--;
612 if (braces_level < 0)
616 yylval->string = last_string;
622 "$"("<"{tag}">")?(-?[0-9]+|"$") { handle_dollar (current_braced_code,
624 "@"(-?[0-9]+|"$") { handle_at (current_braced_code,
627 /* `"<"{splice}"<"' tokenizes `<<%' correctly (as `<<' `%') rather
628 than incorrrectly (as `<' `<%'). */
629 [^\"$%\'/<@\[\]\{\}]+|[$%/<@]|"<"{splice}"<" YY_OBS_GROW;
632 unexpected_end_of_file (*yylloc, "{", "}");
635 yylval->string = last_string;
642 /*--------------------------------------------------------------.
643 | Scanning some prologue: from "%{" (already scanned) to "%}". |
644 `--------------------------------------------------------------*/
651 yylval->string = last_string;
655 [^%\[\]/\'\"]+ YY_OBS_GROW;
659 unexpected_end_of_file (*yylloc, "%{", "%}");
662 yylval->string = last_string;
668 /*---------------------------------------------------------------.
669 | Scanning the epilogue (everything after the second "%%", which |
670 | has already been eaten). |
671 `---------------------------------------------------------------*/
675 [^\[\]]+ YY_OBS_GROW;
680 yylval->string = last_string;
688 /*------------------------------------------------------------------.
689 | TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
691 | Possible inputs: $[<TYPENAME>]($|integer) |
693 | Output to the STRING_OBSTACK a reference to this semantic value. |
694 `------------------------------------------------------------------*/
697 handle_action_dollar (char *text, location_t location)
699 const char *type_name = NULL;
702 /* Get the type name if explicit. */
715 type_name = symbol_list_n_type_name_get (current_rule, location, 0);
716 if (!type_name && typed)
717 complain_at (location, _("$$ of `%s' has no declared type"),
718 current_rule->sym->tag);
721 obstack_fgrow1 (&string_obstack,
722 "]b4_lhs_value([%s])[", type_name);
728 num = strtol (cp, 0, 10);
730 if (INT_MIN <= num && num <= rule_length && ! errno)
733 if (!type_name && n > 0)
734 type_name = symbol_list_n_type_name_get (current_rule, location,
736 if (!type_name && typed)
737 complain_at (location, _("$%d of `%s' has no declared type"),
738 n, current_rule->sym->tag);
741 obstack_fgrow3 (&string_obstack,
742 "]b4_rhs_value([%d], [%d], [%s])[",
743 rule_length, n, type_name);
746 complain_at (location, _("integer out of range: %s"), quote (text));
751 /*---------------------------------------------------------------.
752 | TEXT is expected to be $$ in some code associated to a symbol: |
753 | destructor or printer. |
754 `---------------------------------------------------------------*/
757 handle_symbol_code_dollar (char *text, location_t location)
761 obstack_sgrow (&string_obstack, "]b4_dollar_dollar[");
763 complain_at (location, _("invalid value: %s"), quote (text));
767 /*-----------------------------------------------------------------.
768 | Dispatch onto handle_action_dollar, or handle_destructor_dollar, |
769 | depending upon CODE_KIND. |
770 `-----------------------------------------------------------------*/
773 handle_dollar (braced_code_t braced_code_kind,
774 char *text, location_t location)
776 switch (braced_code_kind)
778 case action_braced_code:
779 handle_action_dollar (text, location);
782 case destructor_braced_code:
783 case printer_braced_code:
784 handle_symbol_code_dollar (text, location);
790 /*------------------------------------------------------.
791 | TEXT is a location token (i.e., a `@...'). Output to |
792 | STRING_OBSTACK a reference to this location. |
793 `------------------------------------------------------*/
796 handle_action_at (char *text, location_t location)
803 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
809 num = strtol (cp, 0, 10);
811 if (INT_MIN <= num && num <= rule_length && ! errno)
814 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
818 complain_at (location, _("integer out of range: %s"), quote (text));
823 /*---------------------------------------------------------------.
824 | TEXT is expected to be @$ in some code associated to a symbol: |
825 | destructor or printer. |
826 `---------------------------------------------------------------*/
829 handle_symbol_code_at (char *text, location_t location)
833 obstack_sgrow (&string_obstack, "]b4_at_dollar[");
835 complain_at (location, _("invalid value: %s"), quote (text));
839 /*-------------------------------------------------------------------.
840 | Dispatch onto handle_action_at, or handle_destructor_at, depending |
842 `-------------------------------------------------------------------*/
845 handle_at (braced_code_t braced_code_kind,
846 char *text, location_t location)
848 switch (braced_code_kind)
850 case action_braced_code:
851 handle_action_at (text, location);
854 case destructor_braced_code:
855 case printer_braced_code:
856 handle_symbol_code_at (text, location);
862 /*------------------------------------------------------------------.
863 | Convert universal character name UCN to a single-byte character, |
864 | and return that character. Return -1 if UCN does not correspond |
865 | to a single-byte character. |
866 `------------------------------------------------------------------*/
869 convert_ucn_to_byte (char const *ucn)
871 unsigned long code = strtoul (ucn + 2, 0, 16);
873 /* FIXME: Currently we assume Unicode-compatible unibyte characters
874 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
875 non-ASCII hosts we support only the portable C character set.
876 These limitations should be removed once we add support for
877 multibyte characters. */
879 if (UCHAR_MAX < code)
882 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
884 /* A non-ASCII host. Use CODE to index into a table of the C
885 basic execution character set, which is guaranteed to exist on
886 all Standard C platforms. This table also includes '$', '@',
887 and '`', which are not in the basic execution character set but
888 which are unibyte characters on all the platforms that we know
890 static signed char const table[] =
892 '\0', -1, -1, -1, -1, -1, -1, '\a',
893 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
894 -1, -1, -1, -1, -1, -1, -1, -1,
895 -1, -1, -1, -1, -1, -1, -1, -1,
896 ' ', '!', '"', '#', '$', '%', '&', '\'',
897 '(', ')', '*', '+', ',', '-', '.', '/',
898 '0', '1', '2', '3', '4', '5', '6', '7',
899 '8', '9', ':', ';', '<', '=', '>', '?',
900 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
901 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
902 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
903 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
904 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
905 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
906 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
907 'x', 'y', 'z', '{', '|', '}', '~'
910 code = code < sizeof table ? table[code] : -1;
918 /*----------------------------------------------------------------.
919 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
920 `----------------------------------------------------------------*/
923 handle_syncline (char *args, location_t *location)
925 int lineno = strtol (args, &args, 10);
926 const char *file = NULL;
927 file = strchr (args, '"') + 1;
928 *strchr (file, '"') = 0;
929 /* FIXME: Leaking... Can't free, as some locations are still
930 pointing to the old file name. */
931 infile = xstrdup (file);
932 location->file = infile;
933 location->last_line = lineno;
936 /*-------------------------.
937 | Initialize the scanner. |
938 `-------------------------*/
941 scanner_initialize (void)
943 obstack_init (&string_obstack);
947 /*-----------------------------------------------.
948 | Free all the memory allocated to the scanner. |
949 `-----------------------------------------------*/
954 obstack_free (&string_obstack, 0);
955 /* Reclaim Flex's buffers. */
956 yy_delete_buffer (YY_CURRENT_BUFFER);