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 /* This is only to avoid GCC warnings. */ \
41 #define YY_USER_ACTION LOCATION_COLUMNS (*yylloc, yyleng)
42 #define YY_LINES LOCATION_LINES (*yylloc, yyleng); lineno += yyleng;
43 #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 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
128 "%expect" return PERCENT_EXPECT;
129 "%file-prefix" return PERCENT_FILE_PREFIX;
130 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
131 "%left" return PERCENT_LEFT;
132 "%locations" return PERCENT_LOCATIONS;
133 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
134 "%no"[-_]"lines" return PERCENT_NO_LINES;
135 "%nonassoc" return PERCENT_NONASSOC;
136 "%nterm" return PERCENT_NTERM;
137 "%output" return PERCENT_OUTPUT;
138 "%prec" return PERCENT_PREC;
139 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
140 "%right" return PERCENT_RIGHT;
141 "%skeleton" return PERCENT_SKELETON;
142 "%start" return PERCENT_START;
143 "%term" return PERCENT_TOKEN;
144 "%token" return PERCENT_TOKEN;
145 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
146 "%type" return PERCENT_TYPE;
147 "%union" return PERCENT_UNION;
148 "%verbose" return PERCENT_VERBOSE;
149 "%yacc" return PERCENT_YACC;
154 ";" return SEMICOLON;
156 {eols} YY_LINES; YY_STEP;
159 yylval->symbol = getsym (yytext, *yylloc);
163 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
165 /* Characters. We don't check there is only one. */
166 \' YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
169 \" YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
172 "/*" yy_push_state (SC_COMMENT);
176 "%{" yy_push_state (SC_PROLOGUE);
178 /* Code in between braces. */
179 "{" YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
183 obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
185 yylval->string = last_string;
191 if (++percent_percent_count == 2)
192 yy_push_state (SC_EPILOGUE);
193 return PERCENT_PERCENT;
197 LOCATION_PRINT (stderr, *yylloc);
198 fprintf (stderr, ": invalid character: `%c'\n", *yytext);
204 /*------------------------------------------------------------.
205 | Whatever the start condition (but those which correspond to |
206 | entity `swallowed' by Bison: SC_ESCAPED_STRING and |
207 | SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
208 `------------------------------------------------------------*/
210 <SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
212 \[ if (YY_START != SC_COMMENT) obstack_sgrow (&string_obstack, "@<:@");
213 \] if (YY_START != SC_COMMENT) obstack_sgrow (&string_obstack, "@:>@");
218 /*-----------------------------------------------------------.
219 | Scanning a C comment. The initial `/ *' is already eaten. |
220 `-----------------------------------------------------------*/
224 "*/" { /* End of the comment. */
225 if (yy_top_state () == INITIAL)
236 [^\[\]*\n\r]+ if (yy_top_state () != INITIAL) YY_OBS_GROW;
237 {eols} if (yy_top_state () != INITIAL) YY_OBS_GROW; YY_LINES;
238 . /* Stray `*'. */if (yy_top_state () != INITIAL) YY_OBS_GROW;
241 LOCATION_PRINT (stderr, *yylloc);
242 fprintf (stderr, ": unexpected end of file in a comment\n");
248 /*----------------------------------------------------------------.
249 | Scanning a C string, including its escapes. The initial `"' is |
251 `----------------------------------------------------------------*/
256 assert (yy_top_state () == INITIAL);
259 yylval->string = last_string;
264 [^\"\n\r\\]+ YY_OBS_GROW;
266 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
269 LOCATION_PRINT (stderr, *yylloc);
270 fprintf (stderr, ": unexpected end of file in a string\n");
271 assert (yy_top_state () == INITIAL);
273 yylval->string = last_string;
279 /*---------------------------------------------------------------.
280 | Scanning a C character, decoding its escapes. The initial "'" |
281 | is already eaten. |
282 `---------------------------------------------------------------*/
284 <SC_ESCAPED_CHARACTER>
288 assert (yy_top_state () == INITIAL);
291 yylval->symbol = getsym (last_string, *yylloc);
292 symbol_class_set (yylval->symbol, token_sym);
293 symbol_user_token_number_set (yylval->symbol, last_string[1]);
300 [^\'\n\r\\] YY_OBS_GROW;
302 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
305 LOCATION_PRINT (stderr, *yylloc);
306 fprintf (stderr, ": unexpected end of file in a character\n");
307 assert (yy_top_state () == INITIAL);
309 yylval->string = last_string;
316 /*----------------------------.
317 | Decode escaped characters. |
318 `----------------------------*/
320 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
323 long c = strtol (yytext + 1, 0, 8);
326 LOCATION_PRINT (stderr, *yylloc);
327 fprintf (stderr, ": invalid escape: %s\n", quote (yytext));
331 obstack_1grow (&string_obstack, c);
335 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
338 \\a obstack_1grow (&string_obstack, '\a');
339 \\b obstack_1grow (&string_obstack, '\b');
340 \\f obstack_1grow (&string_obstack, '\f');
341 \\n obstack_1grow (&string_obstack, '\n');
342 \\r obstack_1grow (&string_obstack, '\r');
343 \\t obstack_1grow (&string_obstack, '\t');
344 \\v obstack_1grow (&string_obstack, '\v');
345 \\[\\""] obstack_1grow (&string_obstack, yytext[1]);
347 LOCATION_PRINT (stderr, *yylloc);
348 fprintf (stderr, ": unrecognized escape: %s\n", quote (yytext));
351 /* FLex wants this rule, in case of a `\<<EOF>>'. */
356 /*----------------------------------------------------------.
357 | Scanning a C character without decoding its escapes. The |
358 | initial "'" is already eaten. |
359 `----------------------------------------------------------*/
365 assert (yy_top_state () != INITIAL);
369 [^\[\]\'\n\r\\]+ YY_OBS_GROW;
370 \\(.|\n) YY_OBS_GROW;
371 /* FLex wants this rule, in case of a `\<<EOF>>'. */
374 {eols} YY_OBS_GROW; YY_LINES;
377 LOCATION_PRINT (stderr, *yylloc);
378 fprintf (stderr, ": unexpected end of file in a character\n");
379 assert (yy_top_state () != INITIAL);
385 /*----------------------------------------------------------------.
386 | Scanning a C string, without decoding its escapes. The initial |
387 | `"' is already eaten. |
388 `----------------------------------------------------------------*/
393 assert (yy_top_state () != INITIAL);
398 [^\[\]\"\n\r\\]+ YY_OBS_GROW;
399 \\(.|\n) YY_OBS_GROW;
400 /* FLex wants this rule, in case of a `\<<EOF>>'. */
403 {eols} YY_OBS_GROW; YY_LINES;
406 LOCATION_PRINT (stderr, *yylloc);
407 fprintf (stderr, ": unexpected end of file in a string\n");
408 assert (yy_top_state () != INITIAL);
414 /*---------------------------------------------------.
415 | Strings, comments etc. can be found in user code. |
416 `---------------------------------------------------*/
418 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
420 /* Characters. We don't check there is only one. */
421 \' YY_OBS_GROW; yy_push_state (SC_CHARACTER);
424 \" YY_OBS_GROW; yy_push_state (SC_STRING);
427 "/*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
435 /*---------------------------------------------------------------.
436 | Scanning some code in braces (%union and actions). The initial |
437 | "{" is already eaten. |
438 `---------------------------------------------------------------*/
444 if (--braces_level == 0)
448 yylval->string = last_string;
453 "{" YY_OBS_GROW; braces_level++;
455 "$"("<"[^>]+">")?(-?[0-9]+|"$") { handle_dollar (current_braced_code,
457 "@"(-?[0-9]+|"$") { handle_at (current_braced_code,
460 [^$@\[\]/\'\"\{\}\n\r]+ YY_OBS_GROW;
461 {eols} YY_OBS_GROW; YY_LINES;
463 /* A lose $, or /, or etc. */
467 LOCATION_PRINT (stderr, *yylloc);
468 fprintf (stderr, ": unexpected end of file in a braced code\n");
471 yylval->string = last_string;
478 /*--------------------------------------------------------------.
479 | Scanning some prologue: from "%{" (already scanned) to "%}". |
480 `--------------------------------------------------------------*/
487 yylval->string = last_string;
491 [^%\[\]/\'\"\n\r]+ YY_OBS_GROW;
493 {eols} YY_OBS_GROW; YY_LINES;
496 LOCATION_PRINT (stderr, *yylloc);
497 fprintf (stderr, ": unexpected end of file in a prologue\n");
500 yylval->string = last_string;
506 /*---------------------------------------------------------------.
507 | Scanning the epilogue (everything after the second "%%", which |
508 | has already been eaten. |
509 `---------------------------------------------------------------*/
513 ([^\[\]]|{eols})+ YY_OBS_GROW;
518 yylval->string = last_string;
526 /*------------------------------------------------------------------.
527 | CP is pointing to a wannabee semantic value (i.e., a `$'). |
529 | Possible inputs: $[<TYPENAME>]($|integer) |
531 | Output to the STRING_OBSTACK a reference to this semantic value. |
532 `------------------------------------------------------------------*/
535 handle_action_dollar (char *cp, location_t location)
537 const char *type_name = NULL;
541 /* Get the type name if explicit. */
554 type_name = symbol_list_n_type_name_get (current_rule, location, 0);
555 if (!type_name && typed)
556 complain_at (location, _("$$ of `%s' has no declared type"),
557 symbol_tag_get (current_rule->sym));
560 obstack_fgrow1 (&string_obstack,
561 "]b4_lhs_value([%s])[", type_name);
563 else if (isdigit (*cp) || *cp == '-')
565 /* RULE_LENGTH is the number of values in the current rule so
566 far, which says where to find `$0' with respect to the top of
567 the stack. It is not the same as the rule->length in the
568 case of mid rule actions. */
569 int rule_length = symbol_list_length (current_rule->next);
570 int n = strtol (cp, &cp, 10);
573 complain_at (location, _("invalid value: %s%d"), "$", n);
576 if (!type_name && n > 0)
577 type_name = symbol_list_n_type_name_get (current_rule, location,
579 if (!type_name && typed)
580 complain_at (location, _("$%d of `%s' has no declared type"),
581 n, symbol_tag_get (current_rule->sym));
584 obstack_fgrow3 (&string_obstack,
585 "]b4_rhs_value([%d], [%d], [%s])[",
586 rule_length, n, type_name);
593 complain_at (location, _("%s is invalid"), quote (buf));
598 /*---------------------------------------.
599 | CP is pointing to $$ in a destructor. |
600 `---------------------------------------*/
603 handle_destructor_dollar (char *cp, location_t location)
608 obstack_sgrow (&string_obstack, "]b4_dollar_dollar[");
614 complain_at (location, _("%s is invalid"), quote (buf));
619 /*-----------------------------------------------------------------.
620 | Dispatch onto handle_action_dollar, or handle_destructor_dollar, |
621 | depending upon CODE_KIND. |
622 `-----------------------------------------------------------------*/
625 handle_dollar (braced_code_t braced_code_kind,
626 char *text, location_t location)
628 switch (braced_code_kind)
630 case action_braced_code:
631 handle_action_dollar (text, location);
634 case destructor_braced_code:
635 handle_destructor_dollar (text, location);
641 /*------------------------------------------------------.
642 | TEXT is a location token (i.e., a `@...'). Output to |
643 | STRING_OBSTACK a reference to this location. |
644 `------------------------------------------------------*/
647 handle_action_at (char *text, location_t location)
654 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
656 else if (isdigit (*text) || *text == '-')
658 /* RULE_LENGTH is the number of values in the current rule so
659 far, which says where to find `$0' with respect to the top of
660 the stack. It is not the same as the rule->length in the
661 case of mid rule actions. */
662 int rule_length = symbol_list_length (current_rule->next);
663 int n = strtol (text, &text, 10);
666 complain_at (location, _("invalid value: %s%d"), "@", n);
668 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
675 complain_at (location, _("%s is invalid"), quote (buf));
680 /*--------------------------------------------.
681 | TEXT is expexted tp be @$ in a destructor. |
682 `--------------------------------------------*/
685 handle_destructor_at (char *text, location_t location)
690 obstack_sgrow (&string_obstack, "]b4_at_dollar[");
696 complain_at (location, _("%s is invalid"), quote (buf));
701 /*-------------------------------------------------------------------.
702 | Dispatch onto handle_action_at, or handle_destructor_at, depending |
704 `-------------------------------------------------------------------*/
707 handle_at (braced_code_t braced_code_kind,
708 char *text, location_t location)
710 switch (braced_code_kind)
712 case action_braced_code:
713 handle_action_at (text, location);
716 case destructor_braced_code:
717 handle_destructor_at (text, location);
723 /*-------------------------.
724 | Initialize the scanner. |
725 `-------------------------*/
728 scanner_initialize (void)
730 obstack_init (&string_obstack);
734 /*-----------------------------------------------.
735 | Free all the memory allocated to the scanner. |
736 `-----------------------------------------------*/
741 obstack_free (&string_obstack, 0);