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); \
62 yylval->string = last_string; \
67 obstack_free (&string_obstack, last_string); \
70 /* This is only to avoid GCC warnings. */
71 #define YY_USER_INIT if (yycontrol) {;};
74 static int braces_level = 0;
75 static int percent_percent_count = 0;
77 static void handle_dollar PARAMS ((char *cp));
78 static void handle_at PARAMS ((char *cp));
82 %x SC_STRING SC_CHARACTER
83 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
84 %x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
86 id [.a-zA-Z][.a-zA-Z_0-9]*
88 eols (\n|\r|\n\r|\r\n)+
93 /* At each yylex invocation, mark the current position as the
94 start of the next token. */
97 fprintf (stderr, "FOO1: ");
98 LOCATION_PRINT (stderr, *yylloc);
99 fprintf (stderr, "\n");
103 fprintf (stderr, "BAR1: ");
104 LOCATION_PRINT (stderr, *yylloc);
105 fprintf (stderr, "\n");
110 /*----------------------------.
111 | Scanning Bison directives. |
112 `----------------------------*/
115 "%binary" return PERCENT_NONASSOC;
116 "%debug" return PERCENT_DEBUG;
117 "%define" return PERCENT_DEFINE;
118 "%defines" return PERCENT_DEFINES;
119 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
120 "%expect" return PERCENT_EXPECT;
121 "%file-prefix" return PERCENT_FILE_PREFIX;
122 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
123 "%left" return PERCENT_LEFT;
124 "%locations" return PERCENT_LOCATIONS;
125 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
126 "%no"[-_]"lines" return PERCENT_NO_LINES;
127 "%nonassoc" return PERCENT_NONASSOC;
128 "%nterm" return PERCENT_NTERM;
129 "%output" return PERCENT_OUTPUT;
130 "%prec" return PERCENT_PREC;
131 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
132 "%right" return PERCENT_RIGHT;
133 "%skeleton" return PERCENT_SKELETON;
134 "%start" return PERCENT_START;
135 "%term" return PERCENT_TOKEN;
136 "%token" return PERCENT_TOKEN;
137 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
138 "%type" return PERCENT_TYPE;
139 "%union" return PERCENT_UNION;
140 "%verbose" return PERCENT_VERBOSE;
141 "%yacc" return PERCENT_YACC;
146 ";" return SEMICOLON;
148 {eols} YY_LINES; YY_STEP;
151 YY_OBS_INIT; YY_OBS_GROW; YY_OBS_FINISH;
152 yylval->symbol = getsym (last_string);
157 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
159 /* Characters. We don't check there is only one. */
160 \' YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
163 \" YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
166 "/*" yy_push_state (SC_COMMENT);
170 "%{" YY_OBS_INIT; yy_push_state (SC_PROLOGUE);
172 /* Code in between braces. */
173 "{" YY_OBS_INIT; YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
176 "<"[^>]+">" YY_OBS_INIT; obstack_grow (&string_obstack, yytext + 1, yyleng - 2); YY_OBS_FINISH; return TYPE;
179 if (++percent_percent_count == 2)
180 yy_push_state (SC_EPILOGUE);
181 return PERCENT_PERCENT;
185 LOCATION_PRINT (stderr, *yylloc);
186 fprintf (stderr, ": invalid character: `%c'\n", *yytext);
192 /*------------------------------------------------------------.
193 | Whatever the start condition (but those which correspond to |
194 | entity `swallowed' by Bison: SC_ESCAPED_STRING and |
195 | SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
196 `------------------------------------------------------------*/
198 <SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
200 \[ obstack_sgrow (&string_obstack, "@<:@");
201 \] obstack_sgrow (&string_obstack, "@:>@");
206 /*-----------------------------------------------------------.
207 | Scanning a C comment. The initial `/ *' is already eaten. |
208 `-----------------------------------------------------------*/
212 "*/" { /* End of the comment. */
213 if (yy_top_state () == INITIAL)
224 [^\[\]*\n\r]+ if (yy_top_state () != INITIAL) YY_OBS_GROW;
225 {eols} if (yy_top_state () != INITIAL) YY_OBS_GROW; YY_LINES;
226 . /* Stray `*'. */if (yy_top_state () != INITIAL) YY_OBS_GROW;
229 LOCATION_PRINT (stderr, *yylloc);
230 fprintf (stderr, ": unexpected end of file in a comment\n");
236 /*----------------------------------------------------------------.
237 | Scanning a C string, including its escapes. The initial `"' is |
239 `----------------------------------------------------------------*/
244 assert (yy_top_state () == INITIAL);
251 [^\"\n\r\\]+ YY_OBS_GROW;
253 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
256 LOCATION_PRINT (stderr, *yylloc);
257 fprintf (stderr, ": unexpected end of file in a string\n");
258 assert (yy_top_state () == INITIAL);
265 /*---------------------------------------------------------------.
266 | Scanning a C character, decoding its escapes. The initial "'" |
267 | is already eaten. |
268 `---------------------------------------------------------------*/
270 <SC_ESCAPED_CHARACTER>
274 assert (yy_top_state () == INITIAL);
277 yylval->symbol = getsym (last_string);
278 symbol_class_set (yylval->symbol, token_sym);
279 symbol_user_token_number_set (yylval->symbol, last_string[1]);
286 [^\'\n\r\\] YY_OBS_GROW;
288 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
291 LOCATION_PRINT (stderr, *yylloc);
292 fprintf (stderr, ": unexpected end of file in a character\n");
293 assert (yy_top_state () == INITIAL);
301 /*----------------------------.
302 | Decode escaped characters. |
303 `----------------------------*/
305 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
308 long c = strtol (yytext + 1, 0, 8);
311 LOCATION_PRINT (stderr, *yylloc);
312 fprintf (stderr, ": invalid escape: %s\n", yytext);
316 obstack_1grow (&string_obstack, c);
320 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
323 \\a obstack_1grow (&string_obstack, '\a');
324 \\b obstack_1grow (&string_obstack, '\b');
325 \\f obstack_1grow (&string_obstack, '\f');
326 \\n obstack_1grow (&string_obstack, '\n');
327 \\r obstack_1grow (&string_obstack, '\r');
328 \\t obstack_1grow (&string_obstack, '\t');
329 \\v obstack_1grow (&string_obstack, '\v');
330 \\[\\""] obstack_1grow (&string_obstack, yytext[1]);
332 LOCATION_PRINT (stderr, *yylloc);
333 fprintf (stderr, ": unrecognized escape: %s\n", yytext);
339 /*----------------------------------------------------------.
340 | Scanning a C character without decoding its escapes. The |
341 | initial "'" is already eaten. |
342 `----------------------------------------------------------*/
348 assert (yy_top_state () != INITIAL);
352 [^\[\]\'\n\r\\] YY_OBS_GROW;
355 {eols} YY_OBS_GROW; YY_LINES;
358 LOCATION_PRINT (stderr, *yylloc);
359 fprintf (stderr, ": unexpected end of file in a character\n");
360 assert (yy_top_state () != INITIAL);
366 /*----------------------------------------------------------------.
367 | Scanning a C string, without decoding its escapes. The initial |
368 | `"' is already eaten. |
369 `----------------------------------------------------------------*/
374 assert (yy_top_state () != INITIAL);
379 [^\[\]\"\n\r\\]+ YY_OBS_GROW;
382 {eols} YY_OBS_GROW; YY_LINES;
385 LOCATION_PRINT (stderr, *yylloc);
386 fprintf (stderr, ": unexpected end of file in a string\n");
387 assert (yy_top_state () != INITIAL);
393 /*---------------------------------------------------.
394 | Strings, comments etc. can be found in user code. |
395 `---------------------------------------------------*/
397 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
399 /* Characters. We don't check there is only one. */
400 \' YY_OBS_GROW; yy_push_state (SC_CHARACTER);
403 \" YY_OBS_GROW; yy_push_state (SC_STRING);
406 "/*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
411 /*---------------------------------------------------------------.
412 | Scanning some code in braces (%union and actions). The initial |
413 | "{" is already eaten. |
414 `---------------------------------------------------------------*/
420 if (--braces_level == 0)
428 "{" YY_OBS_GROW; braces_level++;
430 "$"("<".*">")?(-?[0-9]+|"$") { handle_dollar (yytext); }
431 "@"(-?[0-9]+|"$") { handle_at (yytext); }
433 [^\[\]$/\'\"@\{\}\n\r]+ YY_OBS_GROW;
434 {eols} YY_OBS_GROW; YY_LINES;
436 /* A lose $, or /, or etc. */
440 LOCATION_PRINT (stderr, *yylloc);
441 fprintf (stderr, ": unexpected end of file in a braced code\n");
450 /*--------------------------------------------------------------.
451 | Scanning some prologue: from "%{" (already scanned) to "%}". |
452 `--------------------------------------------------------------*/
462 [^\[\]%\n\r]+ YY_OBS_GROW;
463 "%"+[^%\}\n\r]+ YY_OBS_GROW;
464 {eols} YY_OBS_GROW; YY_LINES;
467 LOCATION_PRINT (stderr, *yylloc);
468 fprintf (stderr, ": unexpected end of file in a prologue\n");
477 /*---------------------------------------------------------------.
478 | Scanning the epilogue (everything after the second "%%", which |
479 | has already been eaten. |
480 `---------------------------------------------------------------*/
484 ([^\[\]]|{eols})+ YY_OBS_GROW;
496 /*------------------------------------------------------------------.
497 | CP is pointing to a wannabee semantic value (i.e., a `$'). |
499 | Possible inputs: $[<TYPENAME>]($|integer) |
501 | Output to the STRING_OBSTACK a reference to this semantic value. |
502 `------------------------------------------------------------------*/
505 handle_dollar (char *cp)
507 const char *type_name = NULL;
509 /* RULE_LENGTH is the number of values in the current rule so far,
510 which says where to find `$0' with respect to the top of the
511 stack. It is not the same as the rule->length in the case of mid
515 for (rhs = current_rule->next; rhs; rhs = rhs->next)
520 /* Get the type name if explicit. */
533 type_name = get_type_name (0, current_rule);
534 if (!type_name && typed)
535 complain (_("$$ of `%s' has no declared type"),
536 current_rule->sym->tag);
539 obstack_fgrow1 (&string_obstack,
540 "]b4_lhs_value([%s])[", type_name);
542 else if (isdigit (*cp) || *cp == '-')
544 int n = strtol (cp, &cp, 10);
547 complain (_("invalid value: %s%d"), "$", n);
550 if (!type_name && n > 0)
551 type_name = get_type_name (n, current_rule);
552 if (!type_name && typed)
553 complain (_("$%d of `%s' has no declared type"),
554 n, current_rule->sym->tag);
557 obstack_fgrow3 (&string_obstack,
558 "]b4_rhs_value([%d], [%d], [%s])[",
559 rule_length, n, type_name);
566 complain (_("%s is invalid"), quote (buf));
570 /*-------------------------------------------------------.
571 | CP is pointing to a location (i.e., a `@'). Output to |
572 | STRING_OBSTACK a reference to this location. |
573 `-------------------------------------------------------*/
578 /* RULE_LENGTH is the number of values in the current rule so far,
579 which says where to find `$0' with respect to the top of the
580 stack. It is not the same as the rule->length in the case of mid
584 for (rhs = current_rule->next; rhs; rhs = rhs->next)
592 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
594 else if (isdigit (*cp) || *cp == '-')
596 int n = strtol (cp, &cp, 10);
598 complain (_("invalid value: %s%d"), "@", n);
600 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
607 complain (_("%s is invalid"), quote (buf));