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_ACTION LOCATION_COLUMNS (*yylloc, yyleng)
35 #define YY_LINES LOCATION_LINES (*yylloc, yyleng); lineno += yyleng;
36 #define YY_STEP LOCATION_STEP (*yylloc)
39 /* STRING_OBSTACK -- Used to store all the characters that we need to
40 keep (to construct ID, STRINGS etc.). Use the following macros to
43 Use YY_OBS_INIT to initialize a new growing string, YY_OBS_GROW to
44 append what has just been matched, and YY_OBS_FINISH to end the
45 string (it puts the ending 0). YY_OBS_FINISH also stores this
46 string in LAST_STRING, which can be used, and which is used by
47 YY_OBS_FREE to free the last string. */
49 static struct obstack string_obstack;
53 obstack_init (&string_obstack)
56 obstack_grow (&string_obstack, yytext, yyleng)
58 #define YY_OBS_FINISH \
60 obstack_1grow (&string_obstack, '\0'); \
61 last_string = obstack_finish (&string_obstack); \
66 obstack_free (&string_obstack, last_string); \
70 scanner_last_string_free (void)
76 /* This is only to avoid GCC warnings. */
77 #define YY_USER_INIT if (yycontrol) {;};
80 static int braces_level = 0;
81 static int percent_percent_count = 0;
83 static void handle_dollar PARAMS ((char *cp));
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: ");
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);
161 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
163 /* Characters. We don't check there is only one. */
164 \' YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
167 \" YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
170 "/*" yy_push_state (SC_COMMENT);
174 "%{" YY_OBS_INIT; yy_push_state (SC_PROLOGUE);
176 /* Code in between braces. */
177 "{" YY_OBS_INIT; YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
182 obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
184 yylval->string = last_string;
190 if (++percent_percent_count == 2)
191 yy_push_state (SC_EPILOGUE);
192 return PERCENT_PERCENT;
196 LOCATION_PRINT (stderr, *yylloc);
197 fprintf (stderr, ": invalid character: `%c'\n", *yytext);
203 /*------------------------------------------------------------.
204 | Whatever the start condition (but those which correspond to |
205 | entity `swallowed' by Bison: SC_ESCAPED_STRING and |
206 | SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
207 `------------------------------------------------------------*/
209 <SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
211 \[ obstack_sgrow (&string_obstack, "@<:@");
212 \] obstack_sgrow (&string_obstack, "@:>@");
217 /*-----------------------------------------------------------.
218 | Scanning a C comment. The initial `/ *' is already eaten. |
219 `-----------------------------------------------------------*/
223 "*/" { /* End of the comment. */
224 if (yy_top_state () == INITIAL)
235 [^\[\]*\n\r]+ if (yy_top_state () != INITIAL) YY_OBS_GROW;
236 {eols} if (yy_top_state () != INITIAL) YY_OBS_GROW; YY_LINES;
237 . /* Stray `*'. */if (yy_top_state () != INITIAL) YY_OBS_GROW;
240 LOCATION_PRINT (stderr, *yylloc);
241 fprintf (stderr, ": unexpected end of file in a comment\n");
247 /*----------------------------------------------------------------.
248 | Scanning a C string, including its escapes. The initial `"' is |
250 `----------------------------------------------------------------*/
255 assert (yy_top_state () == INITIAL);
258 yylval->string = last_string;
263 [^\"\n\r\\]+ YY_OBS_GROW;
265 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
268 LOCATION_PRINT (stderr, *yylloc);
269 fprintf (stderr, ": unexpected end of file in a string\n");
270 assert (yy_top_state () == INITIAL);
272 yylval->string = last_string;
278 /*---------------------------------------------------------------.
279 | Scanning a C character, decoding its escapes. The initial "'" |
280 | is already eaten. |
281 `---------------------------------------------------------------*/
283 <SC_ESCAPED_CHARACTER>
287 assert (yy_top_state () == INITIAL);
290 yylval->symbol = getsym (last_string);
291 symbol_class_set (yylval->symbol, token_sym);
292 symbol_user_token_number_set (yylval->symbol, last_string[1]);
299 [^\'\n\r\\] YY_OBS_GROW;
301 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
304 LOCATION_PRINT (stderr, *yylloc);
305 fprintf (stderr, ": unexpected end of file in a character\n");
306 assert (yy_top_state () == INITIAL);
308 yylval->string = last_string;
315 /*----------------------------.
316 | Decode escaped characters. |
317 `----------------------------*/
319 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
322 long c = strtol (yytext + 1, 0, 8);
325 LOCATION_PRINT (stderr, *yylloc);
326 fprintf (stderr, ": invalid escape: %s\n", yytext);
330 obstack_1grow (&string_obstack, c);
334 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
337 \\a obstack_1grow (&string_obstack, '\a');
338 \\b obstack_1grow (&string_obstack, '\b');
339 \\f obstack_1grow (&string_obstack, '\f');
340 \\n obstack_1grow (&string_obstack, '\n');
341 \\r obstack_1grow (&string_obstack, '\r');
342 \\t obstack_1grow (&string_obstack, '\t');
343 \\v obstack_1grow (&string_obstack, '\v');
344 \\[\\""] obstack_1grow (&string_obstack, yytext[1]);
346 LOCATION_PRINT (stderr, *yylloc);
347 fprintf (stderr, ": unrecognized escape: %s\n", yytext);
353 /*----------------------------------------------------------.
354 | Scanning a C character without decoding its escapes. The |
355 | initial "'" is already eaten. |
356 `----------------------------------------------------------*/
362 assert (yy_top_state () != INITIAL);
366 [^\[\]\'\n\r\\] YY_OBS_GROW;
369 {eols} YY_OBS_GROW; YY_LINES;
372 LOCATION_PRINT (stderr, *yylloc);
373 fprintf (stderr, ": unexpected end of file in a character\n");
374 assert (yy_top_state () != INITIAL);
380 /*----------------------------------------------------------------.
381 | Scanning a C string, without decoding its escapes. The initial |
382 | `"' is already eaten. |
383 `----------------------------------------------------------------*/
388 assert (yy_top_state () != INITIAL);
393 [^\[\]\"\n\r\\]+ YY_OBS_GROW;
396 {eols} YY_OBS_GROW; YY_LINES;
399 LOCATION_PRINT (stderr, *yylloc);
400 fprintf (stderr, ": unexpected end of file in a string\n");
401 assert (yy_top_state () != INITIAL);
407 /*---------------------------------------------------.
408 | Strings, comments etc. can be found in user code. |
409 `---------------------------------------------------*/
411 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
413 /* Characters. We don't check there is only one. */
414 \' YY_OBS_GROW; yy_push_state (SC_CHARACTER);
417 \" YY_OBS_GROW; yy_push_state (SC_STRING);
420 "/*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
425 /*---------------------------------------------------------------.
426 | Scanning some code in braces (%union and actions). The initial |
427 | "{" is already eaten. |
428 `---------------------------------------------------------------*/
434 if (--braces_level == 0)
438 yylval->string = last_string;
443 "{" YY_OBS_GROW; braces_level++;
445 "$"("<".*">")?(-?[0-9]+|"$") { handle_dollar (yytext); }
446 "@"(-?[0-9]+|"$") { handle_at (yytext); }
448 [^\[\]$/\'\"@\{\}\n\r]+ YY_OBS_GROW;
449 {eols} YY_OBS_GROW; YY_LINES;
451 /* A lose $, or /, or etc. */
455 LOCATION_PRINT (stderr, *yylloc);
456 fprintf (stderr, ": unexpected end of file in a braced code\n");
459 yylval->string = last_string;
466 /*--------------------------------------------------------------.
467 | Scanning some prologue: from "%{" (already scanned) to "%}". |
468 `--------------------------------------------------------------*/
475 yylval->string = last_string;
479 [^\[\]%\n\r]+ YY_OBS_GROW;
480 "%"+[^%\}\n\r]+ YY_OBS_GROW;
481 {eols} YY_OBS_GROW; YY_LINES;
484 LOCATION_PRINT (stderr, *yylloc);
485 fprintf (stderr, ": unexpected end of file in a prologue\n");
488 yylval->string = last_string;
495 /*---------------------------------------------------------------.
496 | Scanning the epilogue (everything after the second "%%", which |
497 | has already been eaten. |
498 `---------------------------------------------------------------*/
502 ([^\[\]]|{eols})+ YY_OBS_GROW;
507 yylval->string = last_string;
515 /*------------------------------------------------------------------.
516 | CP is pointing to a wannabee semantic value (i.e., a `$'). |
518 | Possible inputs: $[<TYPENAME>]($|integer) |
520 | Output to the STRING_OBSTACK a reference to this semantic value. |
521 `------------------------------------------------------------------*/
524 handle_dollar (char *cp)
526 const char *type_name = NULL;
528 /* RULE_LENGTH is the number of values in the current rule so far,
529 which says where to find `$0' with respect to the top of the
530 stack. It is not the same as the rule->length in the case of mid
534 for (rhs = current_rule->next; rhs; rhs = rhs->next)
539 /* Get the type name if explicit. */
552 type_name = get_type_name (0, current_rule);
553 if (!type_name && typed)
554 complain (_("$$ of `%s' has no declared type"),
555 current_rule->sym->tag);
558 obstack_fgrow1 (&string_obstack,
559 "]b4_lhs_value([%s])[", type_name);
561 else if (isdigit (*cp) || *cp == '-')
563 int n = strtol (cp, &cp, 10);
566 complain (_("invalid value: %s%d"), "$", n);
569 if (!type_name && n > 0)
570 type_name = get_type_name (n, current_rule);
571 if (!type_name && typed)
572 complain (_("$%d of `%s' has no declared type"),
573 n, current_rule->sym->tag);
576 obstack_fgrow3 (&string_obstack,
577 "]b4_rhs_value([%d], [%d], [%s])[",
578 rule_length, n, type_name);
585 complain (_("%s is invalid"), quote (buf));
589 /*-------------------------------------------------------.
590 | CP is pointing to a location (i.e., a `@'). Output to |
591 | STRING_OBSTACK a reference to this location. |
592 `-------------------------------------------------------*/
597 /* RULE_LENGTH is the number of values in the current rule so far,
598 which says where to find `$0' with respect to the top of the
599 stack. It is not the same as the rule->length in the case of mid
603 for (rhs = current_rule->next; rhs; rhs = rhs->next)
611 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
613 else if (isdigit (*cp) || *cp == '-')
615 int n = strtol (cp, &cp, 10);
617 complain (_("invalid value: %s%d"), "@", n);
619 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
626 complain (_("%s is invalid"), quote (buf));
633 obstack_free (&string_obstack, 0);