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)
80 static int braces_level = 0;
81 static int percent_percent_count = 0;
83 static void handle_dollar PARAMS ((char *cp, location_t location));
84 static void handle_at PARAMS ((char *cp));
88 %x SC_STRING SC_CHARACTER
89 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
90 %x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
92 id [.a-zA-Z][.a-zA-Z_0-9]*
94 eols (\n|\r|\n\r|\r\n)+
99 /* At each yylex invocation, mark the current position as the
100 start of the next token. */
103 fprintf (stderr, "FOO1: %p: ", yylloc);
104 LOCATION_PRINT (stderr, *yylloc);
105 fprintf (stderr, "\n");
109 fprintf (stderr, "BAR1: ");
110 LOCATION_PRINT (stderr, *yylloc);
111 fprintf (stderr, "\n");
116 /*----------------------------.
117 | Scanning Bison directives. |
118 `----------------------------*/
121 "%binary" return PERCENT_NONASSOC;
122 "%debug" return PERCENT_DEBUG;
123 "%define" return PERCENT_DEFINE;
124 "%defines" return PERCENT_DEFINES;
125 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
126 "%expect" return PERCENT_EXPECT;
127 "%file-prefix" return PERCENT_FILE_PREFIX;
128 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
129 "%left" return PERCENT_LEFT;
130 "%locations" return PERCENT_LOCATIONS;
131 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
132 "%no"[-_]"lines" return PERCENT_NO_LINES;
133 "%nonassoc" return PERCENT_NONASSOC;
134 "%nterm" return PERCENT_NTERM;
135 "%output" return PERCENT_OUTPUT;
136 "%prec" return PERCENT_PREC;
137 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
138 "%right" return PERCENT_RIGHT;
139 "%skeleton" return PERCENT_SKELETON;
140 "%start" return PERCENT_START;
141 "%term" return PERCENT_TOKEN;
142 "%token" return PERCENT_TOKEN;
143 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
144 "%type" return PERCENT_TYPE;
145 "%union" return PERCENT_UNION;
146 "%verbose" return PERCENT_VERBOSE;
147 "%yacc" return PERCENT_YACC;
152 ";" return SEMICOLON;
154 {eols} YY_LINES; YY_STEP;
157 yylval->symbol = getsym (yytext, *yylloc);
161 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
163 /* Characters. We don't check there is only one. */
164 \' YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
167 \" YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
170 "/*" yy_push_state (SC_COMMENT);
174 "%{" yy_push_state (SC_PROLOGUE);
176 /* Code in between braces. */
177 "{" YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
181 obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
183 yylval->string = last_string;
189 if (++percent_percent_count == 2)
190 yy_push_state (SC_EPILOGUE);
191 return PERCENT_PERCENT;
195 LOCATION_PRINT (stderr, *yylloc);
196 fprintf (stderr, ": invalid character: `%c'\n", *yytext);
202 /*------------------------------------------------------------.
203 | Whatever the start condition (but those which correspond to |
204 | entity `swallowed' by Bison: SC_ESCAPED_STRING and |
205 | SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
206 `------------------------------------------------------------*/
208 <SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
210 \[ if (YY_START != SC_COMMENT) obstack_sgrow (&string_obstack, "@<:@");
211 \] if (YY_START != SC_COMMENT) obstack_sgrow (&string_obstack, "@:>@");
216 /*-----------------------------------------------------------.
217 | Scanning a C comment. The initial `/ *' is already eaten. |
218 `-----------------------------------------------------------*/
222 "*/" { /* End of the comment. */
223 if (yy_top_state () == INITIAL)
234 [^\[\]*\n\r]+ if (yy_top_state () != INITIAL) YY_OBS_GROW;
235 {eols} if (yy_top_state () != INITIAL) YY_OBS_GROW; YY_LINES;
236 . /* Stray `*'. */if (yy_top_state () != INITIAL) YY_OBS_GROW;
239 LOCATION_PRINT (stderr, *yylloc);
240 fprintf (stderr, ": unexpected end of file in a comment\n");
246 /*----------------------------------------------------------------.
247 | Scanning a C string, including its escapes. The initial `"' is |
249 `----------------------------------------------------------------*/
254 assert (yy_top_state () == INITIAL);
257 yylval->string = last_string;
262 [^\"\n\r\\]+ YY_OBS_GROW;
264 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
267 LOCATION_PRINT (stderr, *yylloc);
268 fprintf (stderr, ": unexpected end of file in a string\n");
269 assert (yy_top_state () == INITIAL);
271 yylval->string = last_string;
277 /*---------------------------------------------------------------.
278 | Scanning a C character, decoding its escapes. The initial "'" |
279 | is already eaten. |
280 `---------------------------------------------------------------*/
282 <SC_ESCAPED_CHARACTER>
286 assert (yy_top_state () == INITIAL);
289 yylval->symbol = getsym (last_string, *yylloc);
290 symbol_class_set (yylval->symbol, token_sym);
291 symbol_user_token_number_set (yylval->symbol, last_string[1]);
298 [^\'\n\r\\] YY_OBS_GROW;
300 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
303 LOCATION_PRINT (stderr, *yylloc);
304 fprintf (stderr, ": unexpected end of file in a character\n");
305 assert (yy_top_state () == INITIAL);
307 yylval->string = last_string;
314 /*----------------------------.
315 | Decode escaped characters. |
316 `----------------------------*/
318 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
321 long c = strtol (yytext + 1, 0, 8);
324 LOCATION_PRINT (stderr, *yylloc);
325 fprintf (stderr, ": invalid escape: %s\n", yytext);
329 obstack_1grow (&string_obstack, c);
333 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
336 \\a obstack_1grow (&string_obstack, '\a');
337 \\b obstack_1grow (&string_obstack, '\b');
338 \\f obstack_1grow (&string_obstack, '\f');
339 \\n obstack_1grow (&string_obstack, '\n');
340 \\r obstack_1grow (&string_obstack, '\r');
341 \\t obstack_1grow (&string_obstack, '\t');
342 \\v obstack_1grow (&string_obstack, '\v');
343 \\[\\""] obstack_1grow (&string_obstack, yytext[1]);
345 LOCATION_PRINT (stderr, *yylloc);
346 fprintf (stderr, ": unrecognized escape: %s\n", yytext);
352 /*----------------------------------------------------------.
353 | Scanning a C character without decoding its escapes. The |
354 | initial "'" is already eaten. |
355 `----------------------------------------------------------*/
361 assert (yy_top_state () != INITIAL);
365 [^\[\]\'\n\r\\] YY_OBS_GROW;
368 {eols} YY_OBS_GROW; YY_LINES;
371 LOCATION_PRINT (stderr, *yylloc);
372 fprintf (stderr, ": unexpected end of file in a character\n");
373 assert (yy_top_state () != INITIAL);
379 /*----------------------------------------------------------------.
380 | Scanning a C string, without decoding its escapes. The initial |
381 | `"' is already eaten. |
382 `----------------------------------------------------------------*/
387 assert (yy_top_state () != INITIAL);
392 [^\[\]\"\n\r\\]+ YY_OBS_GROW;
395 {eols} YY_OBS_GROW; YY_LINES;
398 LOCATION_PRINT (stderr, *yylloc);
399 fprintf (stderr, ": unexpected end of file in a string\n");
400 assert (yy_top_state () != INITIAL);
406 /*---------------------------------------------------.
407 | Strings, comments etc. can be found in user code. |
408 `---------------------------------------------------*/
410 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
412 /* Characters. We don't check there is only one. */
413 \' YY_OBS_GROW; yy_push_state (SC_CHARACTER);
416 \" YY_OBS_GROW; yy_push_state (SC_STRING);
419 "/*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
424 /*---------------------------------------------------------------.
425 | Scanning some code in braces (%union and actions). The initial |
426 | "{" is already eaten. |
427 `---------------------------------------------------------------*/
433 if (--braces_level == 0)
437 yylval->string = last_string;
442 "{" YY_OBS_GROW; braces_level++;
444 "$"("<"[^>]+">")?(-?[0-9]+|"$") { handle_dollar (yytext, *yylloc); }
445 "@"(-?[0-9]+|"$") { handle_at (yytext); }
447 [^$@\[\]/\'\"\{\}\n\r]+ YY_OBS_GROW;
448 {eols} YY_OBS_GROW; YY_LINES;
450 /* A lose $, or /, or etc. */
454 LOCATION_PRINT (stderr, *yylloc);
455 fprintf (stderr, ": unexpected end of file in a braced code\n");
458 yylval->string = last_string;
465 /*--------------------------------------------------------------.
466 | Scanning some prologue: from "%{" (already scanned) to "%}". |
467 `--------------------------------------------------------------*/
474 yylval->string = last_string;
478 [^%\[\]/\'\"\n\r]+ YY_OBS_GROW;
479 "%"+[^%\}\n\r]+ YY_OBS_GROW;
480 {eols} YY_OBS_GROW; YY_LINES;
483 LOCATION_PRINT (stderr, *yylloc);
484 fprintf (stderr, ": unexpected end of file in a prologue\n");
487 yylval->string = last_string;
494 /*---------------------------------------------------------------.
495 | Scanning the epilogue (everything after the second "%%", which |
496 | has already been eaten. |
497 `---------------------------------------------------------------*/
501 ([^\[\]]|{eols})+ YY_OBS_GROW;
506 yylval->string = last_string;
514 /*------------------------------------------------------------------.
515 | CP is pointing to a wannabee semantic value (i.e., a `$'). |
517 | Possible inputs: $[<TYPENAME>]($|integer) |
519 | Output to the STRING_OBSTACK a reference to this semantic value. |
520 `------------------------------------------------------------------*/
523 handle_dollar (char *cp, location_t location)
525 const char *type_name = NULL;
529 /* Get the type name if explicit. */
542 type_name = symbol_list_n_type_name_get (current_rule, location, 0);
543 if (!type_name && typed)
544 complain_at (location, _("$$ of `%s' has no declared type"),
545 current_rule->sym->tag);
548 obstack_fgrow1 (&string_obstack,
549 "]b4_lhs_value([%s])[", type_name);
551 else if (isdigit (*cp) || *cp == '-')
553 /* RULE_LENGTH is the number of values in the current rule so
554 far, which says where to find `$0' with respect to the top of
555 the stack. It is not the same as the rule->length in the
556 case of mid rule actions. */
557 int rule_length = symbol_list_length (current_rule->next);
558 int n = strtol (cp, &cp, 10);
561 complain_at (location, _("invalid value: %s%d"), "$", n);
564 if (!type_name && n > 0)
565 type_name = symbol_list_n_type_name_get (current_rule, location,
567 if (!type_name && typed)
568 complain_at (location, _("$%d of `%s' has no declared type"),
569 n, current_rule->sym->tag);
572 obstack_fgrow3 (&string_obstack,
573 "]b4_rhs_value([%d], [%d], [%s])[",
574 rule_length, n, type_name);
581 complain (_("%s is invalid"), quote (buf));
585 /*-------------------------------------------------------.
586 | CP is pointing to a location (i.e., a `@'). Output to |
587 | STRING_OBSTACK a reference to this location. |
588 `-------------------------------------------------------*/
598 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
600 else if (isdigit (*cp) || *cp == '-')
602 /* RULE_LENGTH is the number of values in the current rule so
603 far, which says where to find `$0' with respect to the top of
604 the stack. It is not the same as the rule->length in the
605 case of mid rule actions. */
606 int rule_length = symbol_list_length (current_rule->next);
607 int n = strtol (cp, &cp, 10);
610 complain (_("invalid value: %s%d"), "@", n);
612 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
619 complain (_("%s is invalid"), quote (buf));
624 scanner_initialize (void)
626 obstack_init (&string_obstack);
633 obstack_free (&string_obstack, 0);