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"
33 /* Each time we match a string, move the end cursor to its end. */
34 #define YY_USER_INIT \
36 LOCATION_RESET (*yylloc); \
37 yylloc->file = infile; \
38 /* This is only to avoid GCC warnings. */ \
42 #define YY_USER_ACTION LOCATION_COLUMNS (*yylloc, yyleng);
43 #define YY_LINES LOCATION_LINES (*yylloc, yyleng);
44 #define YY_STEP LOCATION_STEP (*yylloc);
46 /* STRING_OBSTACK -- Used to store all the characters that we need to
47 keep (to construct ID, STRINGS etc.). Use the following macros to
50 Use YY_OBS_GROW to append what has just been matched, and
51 YY_OBS_FINISH to end the string (it puts the ending 0).
52 YY_OBS_FINISH also stores this string in LAST_STRING, which can be
53 used, and which is used by YY_OBS_FREE to free the last string. */
55 static struct obstack string_obstack;
59 obstack_grow (&string_obstack, yytext, yyleng)
61 #define YY_OBS_FINISH \
63 obstack_1grow (&string_obstack, '\0'); \
64 last_string = obstack_finish (&string_obstack); \
69 obstack_free (&string_obstack, last_string); \
73 scanner_last_string_free (void)
79 static int braces_level = 0;
80 static int percent_percent_count = 0;
82 static void handle_dollar PARAMS ((braced_code_t code_kind,
83 char *cp, location_t location));
84 static void handle_at PARAMS ((braced_code_t code_kind,
85 char *cp, location_t location));
89 %x SC_STRING SC_CHARACTER
90 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
91 %x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
93 id [.a-zA-Z_][.a-zA-Z_0-9]*
95 eols (\n|\r|\n\r|\r\n)+
100 /* At each yylex invocation, mark the current position as the
101 start of the next token. */
104 fprintf (stderr, "FOO1: %p: ", yylloc);
105 LOCATION_PRINT (stderr, *yylloc);
106 fprintf (stderr, "\n");
110 fprintf (stderr, "BAR1: ");
111 LOCATION_PRINT (stderr, *yylloc);
112 fprintf (stderr, "\n");
117 /*----------------------------.
118 | Scanning Bison directives. |
119 `----------------------------*/
122 "%binary" return PERCENT_NONASSOC;
123 "%debug" return PERCENT_DEBUG;
124 "%define" return PERCENT_DEFINE;
125 "%defines" return PERCENT_DEFINES;
126 "%destructor" return PERCENT_DESTRUCTOR;
127 "%dprec" return PERCENT_DPREC;
128 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
129 "%expect" return PERCENT_EXPECT;
130 "%file-prefix" return PERCENT_FILE_PREFIX;
131 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
132 "%glr"[-_]"parser" return PERCENT_GLR_PARSER;
133 "%left" return PERCENT_LEFT;
134 "%locations" return PERCENT_LOCATIONS;
135 "%merge" return PERCENT_MERGE;
136 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
137 "%no"[-_]"lines" return PERCENT_NO_LINES;
138 "%nonassoc" return PERCENT_NONASSOC;
139 "%nterm" return PERCENT_NTERM;
140 "%output" return PERCENT_OUTPUT;
141 "%prec" return PERCENT_PREC;
142 "%printer" return PERCENT_PRINTER;
143 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
144 "%right" return PERCENT_RIGHT;
145 "%skeleton" return PERCENT_SKELETON;
146 "%start" return PERCENT_START;
147 "%term" return PERCENT_TOKEN;
148 "%token" return PERCENT_TOKEN;
149 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
150 "%type" return PERCENT_TYPE;
151 "%union" return PERCENT_UNION;
152 "%verbose" return PERCENT_VERBOSE;
153 "%yacc" return PERCENT_YACC;
158 ";" return SEMICOLON;
160 {eols} YY_LINES; YY_STEP;
163 yylval->symbol = symbol_get (yytext, *yylloc);
167 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
169 /* Characters. We don't check there is only one. */
170 "'" YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
173 "\"" YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
176 "/*" yy_push_state (SC_COMMENT);
180 "%{" yy_push_state (SC_PROLOGUE);
182 /* Code in between braces. */
183 "{" YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
187 obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
189 yylval->string = last_string;
195 if (++percent_percent_count == 2)
196 yy_push_state (SC_EPILOGUE);
197 return PERCENT_PERCENT;
201 LOCATION_PRINT (stderr, *yylloc);
202 fprintf (stderr, _(": invalid character: `%c'\n"), *yytext);
208 /*------------------------------------------------------------.
209 | Whatever the start condition (but those which correspond to |
210 | entity `swallowed' by Bison: SC_ESCAPED_STRING and |
211 | SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
212 `------------------------------------------------------------*/
214 <SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
216 \[ if (YY_START != SC_COMMENT) obstack_sgrow (&string_obstack, "@<:@");
217 \] if (YY_START != SC_COMMENT) obstack_sgrow (&string_obstack, "@:>@");
222 /*-----------------------------------------------------------.
223 | Scanning a C comment. The initial `/ *' is already eaten. |
224 `-----------------------------------------------------------*/
228 "*/" { /* End of the comment. */
229 if (yy_top_state () == INITIAL)
240 [^\[\]*\n\r]+ if (yy_top_state () != INITIAL) YY_OBS_GROW;
241 {eols} if (yy_top_state () != INITIAL) YY_OBS_GROW; YY_LINES;
242 . /* Stray `*'. */if (yy_top_state () != INITIAL) YY_OBS_GROW;
245 LOCATION_PRINT (stderr, *yylloc);
246 fprintf (stderr, _(": unexpected end of file in a comment\n"));
252 /*----------------------------------------------------------------.
253 | Scanning a C string, including its escapes. The initial `"' is |
255 `----------------------------------------------------------------*/
260 assert (yy_top_state () == INITIAL);
263 yylval->string = last_string;
268 [^\"\n\r\\]+ YY_OBS_GROW;
270 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
273 LOCATION_PRINT (stderr, *yylloc);
274 fprintf (stderr, _(": unexpected end of file in a string\n"));
275 assert (yy_top_state () == INITIAL);
277 yylval->string = last_string;
283 /*---------------------------------------------------------------.
284 | Scanning a C character, decoding its escapes. The initial "'" |
285 | is already eaten. |
286 `---------------------------------------------------------------*/
288 <SC_ESCAPED_CHARACTER>
292 assert (yy_top_state () == INITIAL);
295 yylval->symbol = symbol_get (last_string, *yylloc);
296 symbol_class_set (yylval->symbol, token_sym, *yylloc);
297 symbol_user_token_number_set (yylval->symbol,
298 (unsigned char) last_string[1], *yylloc);
305 [^\n\r\\] YY_OBS_GROW;
307 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
310 LOCATION_PRINT (stderr, *yylloc);
311 fprintf (stderr, _(": unexpected end of file in a character\n"));
312 assert (yy_top_state () == INITIAL);
314 yylval->string = last_string;
321 /*----------------------------.
322 | Decode escaped characters. |
323 `----------------------------*/
325 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
328 long c = strtol (yytext + 1, 0, 8);
331 LOCATION_PRINT (stderr, *yylloc);
332 fprintf (stderr, _(": invalid escape: %s\n"), quote (yytext));
336 obstack_1grow (&string_obstack, c);
340 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
343 \\a obstack_1grow (&string_obstack, '\a');
344 \\b obstack_1grow (&string_obstack, '\b');
345 \\f obstack_1grow (&string_obstack, '\f');
346 \\n obstack_1grow (&string_obstack, '\n');
347 \\r obstack_1grow (&string_obstack, '\r');
348 \\t obstack_1grow (&string_obstack, '\t');
349 \\v obstack_1grow (&string_obstack, '\v');
350 \\[\\""] obstack_1grow (&string_obstack, yytext[1]);
352 LOCATION_PRINT (stderr, *yylloc);
353 fprintf (stderr, _(": unrecognized escape: %s\n"), quote (yytext));
356 /* FLex wants this rule, in case of a `\<<EOF>>'. */
361 /*----------------------------------------------------------.
362 | Scanning a C character without decoding its escapes. The |
363 | initial "'" is already eaten. |
364 `----------------------------------------------------------*/
370 assert (yy_top_state () != INITIAL);
374 [^\[\]\'\n\r\\]+ YY_OBS_GROW;
375 \\(.|\n) YY_OBS_GROW;
376 /* FLex wants this rule, in case of a `\<<EOF>>'. */
379 {eols} YY_OBS_GROW; YY_LINES;
382 LOCATION_PRINT (stderr, *yylloc);
383 fprintf (stderr, _(": unexpected end of file in a character\n"));
384 assert (yy_top_state () != INITIAL);
390 /*----------------------------------------------------------------.
391 | Scanning a C string, without decoding its escapes. The initial |
392 | `"' is already eaten. |
393 `----------------------------------------------------------------*/
398 assert (yy_top_state () != INITIAL);
403 [^\[\]\"\n\r\\]+ YY_OBS_GROW;
404 \\(.|\n) YY_OBS_GROW;
405 /* FLex wants this rule, in case of a `\<<EOF>>'. */
408 {eols} YY_OBS_GROW; YY_LINES;
411 LOCATION_PRINT (stderr, *yylloc);
412 fprintf (stderr, _(": unexpected end of file in a string\n"));
413 assert (yy_top_state () != INITIAL);
419 /*---------------------------------------------------.
420 | Strings, comments etc. can be found in user code. |
421 `---------------------------------------------------*/
423 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
425 /* Characters. We don't check there is only one. */
426 "'" YY_OBS_GROW; yy_push_state (SC_CHARACTER);
429 "\"" YY_OBS_GROW; yy_push_state (SC_STRING);
432 "/*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
440 /*---------------------------------------------------------------.
441 | Scanning some code in braces (%union and actions). The initial |
442 | "{" is already eaten. |
443 `---------------------------------------------------------------*/
449 if (--braces_level == 0)
453 yylval->string = last_string;
458 "{" YY_OBS_GROW; braces_level++;
460 "$"("<"[^>]+">")?(-?[0-9]+|"$") { handle_dollar (current_braced_code,
462 "@"(-?[0-9]+|"$") { handle_at (current_braced_code,
465 [^$@\[\]/\'\"\{\}\n\r]+ YY_OBS_GROW;
466 {eols} YY_OBS_GROW; YY_LINES;
468 /* A lose $, or /, or etc. */
472 LOCATION_PRINT (stderr, *yylloc);
473 fprintf (stderr, _(": unexpected end of file in a braced code\n"));
476 yylval->string = last_string;
483 /*--------------------------------------------------------------.
484 | Scanning some prologue: from "%{" (already scanned) to "%}". |
485 `--------------------------------------------------------------*/
492 yylval->string = last_string;
496 [^%\[\]/\'\"\n\r]+ YY_OBS_GROW;
498 {eols} YY_OBS_GROW; YY_LINES;
501 LOCATION_PRINT (stderr, *yylloc);
502 fprintf (stderr, _(": unexpected end of file in a prologue\n"));
505 yylval->string = last_string;
511 /*---------------------------------------------------------------.
512 | Scanning the epilogue (everything after the second "%%", which |
513 | has already been eaten. |
514 `---------------------------------------------------------------*/
518 ([^\[\]]|{eols})+ YY_OBS_GROW;
523 yylval->string = last_string;
531 /*------------------------------------------------------------------.
532 | TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
534 | Possible inputs: $[<TYPENAME>]($|integer) |
536 | Output to the STRING_OBSTACK a reference to this semantic value. |
537 `------------------------------------------------------------------*/
540 handle_action_dollar (char *text, location_t location)
542 const char *type_name = NULL;
545 /* Get the type name if explicit. */
558 type_name = symbol_list_n_type_name_get (current_rule, location, 0);
559 if (!type_name && typed)
560 complain_at (location, _("$$ of `%s' has no declared type"),
561 current_rule->sym->tag);
564 obstack_fgrow1 (&string_obstack,
565 "]b4_lhs_value([%s])[", type_name);
567 else if (('0' <= *cp && *cp <= '9') || *cp == '-')
569 /* RULE_LENGTH is the number of values in the current rule so
570 far, which says where to find `$0' with respect to the top of
571 the stack. It is not the same as the rule->length in the
572 case of mid rule actions. */
573 int rule_length = symbol_list_length (current_rule->next);
574 int n = strtol (cp, &cp, 10);
577 complain_at (location, _("invalid value: %s%d"), "$", n);
580 if (!type_name && n > 0)
581 type_name = symbol_list_n_type_name_get (current_rule, location,
583 if (!type_name && typed)
584 complain_at (location, _("$%d of `%s' has no declared type"),
585 n, current_rule->sym->tag);
588 obstack_fgrow3 (&string_obstack,
589 "]b4_rhs_value([%d], [%d], [%s])[",
590 rule_length, n, type_name);
595 complain_at (location, _("%s is invalid"), quote (text));
600 /*---------------------------------------------------------------.
601 | TEXT is expexted tp be $$ in some code associated to a symbol: |
602 | destructor or printer. |
603 `---------------------------------------------------------------*/
606 handle_symbol_code_dollar (char *text, location_t location)
610 obstack_sgrow (&string_obstack, "]b4_dollar_dollar[");
612 complain_at (location, _("%s is invalid"), quote (text));
616 /*-----------------------------------------------------------------.
617 | Dispatch onto handle_action_dollar, or handle_destructor_dollar, |
618 | depending upon CODE_KIND. |
619 `-----------------------------------------------------------------*/
622 handle_dollar (braced_code_t braced_code_kind,
623 char *text, location_t location)
625 switch (braced_code_kind)
627 case action_braced_code:
628 handle_action_dollar (text, location);
631 case destructor_braced_code:
632 case printer_braced_code:
633 handle_symbol_code_dollar (text, location);
639 /*------------------------------------------------------.
640 | TEXT is a location token (i.e., a `@...'). Output to |
641 | STRING_OBSTACK a reference to this location. |
642 `------------------------------------------------------*/
645 handle_action_at (char *text, location_t location)
652 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
654 else if (('0' <= *cp && *cp <= '9') || *cp == '-')
656 /* RULE_LENGTH is the number of values in the current rule so
657 far, which says where to find `$0' with respect to the top of
658 the stack. It is not the same as the rule->length in the
659 case of mid rule actions. */
660 int rule_length = symbol_list_length (current_rule->next);
661 int n = strtol (cp, &cp, 10);
664 complain_at (location, _("invalid value: %s%d"), "@", n);
666 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
671 complain_at (location, _("%s is invalid"), quote (text));
676 /*---------------------------------------------------------------.
677 | TEXT is expexted tp be @$ in some code associated to a symbol: |
678 | destructor or printer. |
679 `---------------------------------------------------------------*/
682 handle_symbol_code_at (char *text, location_t location)
686 obstack_sgrow (&string_obstack, "]b4_at_dollar[");
688 complain_at (location, _("%s is invalid"), quote (text));
692 /*-------------------------------------------------------------------.
693 | Dispatch onto handle_action_at, or handle_destructor_at, depending |
695 `-------------------------------------------------------------------*/
698 handle_at (braced_code_t braced_code_kind,
699 char *text, location_t location)
701 switch (braced_code_kind)
703 case action_braced_code:
704 handle_action_at (text, location);
707 case destructor_braced_code:
708 case printer_braced_code:
709 handle_symbol_code_at (text, location);
715 /*-------------------------.
716 | Initialize the scanner. |
717 `-------------------------*/
720 scanner_initialize (void)
722 obstack_init (&string_obstack);
726 /*-----------------------------------------------.
727 | Free all the memory allocated to the scanner. |
728 `-----------------------------------------------*/
733 obstack_free (&string_obstack, 0);
734 /* Reclaim Flex's buffers. */
735 yy_delete_buffer (YY_CURRENT_BUFFER);