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)
38 /* Appending to the STRING_OBSTACK. */
39 #define YY_INIT obstack_init (&string_obstack)
40 #define YY_GROW obstack_grow (&string_obstack, yytext, yyleng)
41 #define YY_FINISH obstack_1grow (&string_obstack, '\0'); yylval->string = obstack_finish (&string_obstack);
43 /* This is only to avoid GCC warnings. */
44 #define YY_USER_INIT if (yycontrol) {;};
46 static struct obstack string_obstack;
47 static int braces_level = 0;
48 static int percent_percent_count = 0;
50 static void handle_dollar PARAMS ((char *cp));
51 static void handle_at PARAMS ((char *cp));
55 %x SC_STRING SC_CHARACTER
56 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
57 %x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
59 id [.a-zA-Z][.a-zA-Z_0-9]*
61 eols (\n|\r|\n\r|\r\n)+
66 /* At each yylex invocation, mark the current position as the
67 start of the next token. */
70 fprintf (stderr, "FOO1: ");
71 LOCATION_PRINT (stderr, *yylloc);
72 fprintf (stderr, "\n");
76 fprintf (stderr, "BAR1: ");
77 LOCATION_PRINT (stderr, *yylloc);
78 fprintf (stderr, "\n");
83 /*----------------------------.
84 | Scanning Bison directives. |
85 `----------------------------*/
88 "%binary" return PERCENT_NONASSOC;
89 "%debug" return PERCENT_DEBUG;
90 "%define" return PERCENT_DEFINE;
91 "%defines" return PERCENT_DEFINES;
92 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
93 "%expect" return PERCENT_EXPECT;
94 "%file-prefix" return PERCENT_FILE_PREFIX;
95 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
96 "%left" return PERCENT_LEFT;
97 "%locations" return PERCENT_LOCATIONS;
98 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
99 "%no"[-_]"lines" return PERCENT_NO_LINES;
100 "%nonassoc" return PERCENT_NONASSOC;
101 "%nterm" return PERCENT_NTERM;
102 "%output" return PERCENT_OUTPUT;
103 "%prec" return PERCENT_PREC;
104 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
105 "%right" return PERCENT_RIGHT;
106 "%skeleton" return PERCENT_SKELETON;
107 "%start" return PERCENT_START;
108 "%term" return PERCENT_TOKEN;
109 "%token" return PERCENT_TOKEN;
110 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
111 "%type" return PERCENT_TYPE;
112 "%union" return PERCENT_UNION;
113 "%verbose" return PERCENT_VERBOSE;
114 "%yacc" return PERCENT_YACC;
119 ";" return SEMICOLON;
121 {eols} YY_LINES; YY_STEP;
124 YY_INIT; YY_GROW; YY_FINISH;
125 yylval->symbol = getsym (yylval->string);
129 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
131 /* Characters. We don't check there is only one. */
132 \' YY_INIT; YY_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
135 \" YY_INIT; YY_GROW; yy_push_state (SC_ESCAPED_STRING);
138 "/*" yy_push_state (SC_COMMENT);
142 "%{" YY_INIT; yy_push_state (SC_PROLOGUE);
144 /* Code in between braces. */
145 "{" YY_INIT; YY_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
148 "<"[^>]+">" YY_INIT; obstack_grow (&string_obstack, yytext + 1, yyleng - 2); YY_FINISH; return TYPE;
151 if (++percent_percent_count == 2)
152 yy_push_state (SC_EPILOGUE);
153 return PERCENT_PERCENT;
157 LOCATION_PRINT (stderr, *yylloc);
158 fprintf (stderr, ": invalid character: `%c'\n", *yytext);
164 /*------------------------------------------------------------.
165 | Whatever the start condition (but those which correspond to |
166 | entity `swallowed' by Bison: SC_ESCAPED_STRING and |
167 | SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
168 `------------------------------------------------------------*/
170 <SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
172 \[ obstack_sgrow (&string_obstack, "@<:@");
173 \] obstack_sgrow (&string_obstack, "@:>@");
178 /*-----------------------------------------------------------.
179 | Scanning a C comment. The initial `/ *' is already eaten. |
180 `-----------------------------------------------------------*/
184 "*/" { /* End of the comment. */
185 if (yy_top_state () == INITIAL)
196 [^\[\]*\n\r]+ if (yy_top_state () != INITIAL) YY_GROW;
197 {eols} if (yy_top_state () != INITIAL) YY_GROW; YY_LINES;
198 . /* Stray `*'. */if (yy_top_state () != INITIAL) YY_GROW;
201 LOCATION_PRINT (stderr, *yylloc);
202 fprintf (stderr, ": unexpected end of file in a comment\n");
208 /*----------------------------------------------------------------.
209 | Scanning a C string, including its escapes. The initial `"' is |
211 `----------------------------------------------------------------*/
216 assert (yy_top_state () == INITIAL);
223 [^\"\n\r\\]+ YY_GROW;
225 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
228 LOCATION_PRINT (stderr, *yylloc);
229 fprintf (stderr, ": unexpected end of file in a string\n");
230 assert (yy_top_state () == INITIAL);
237 /*---------------------------------------------------------------.
238 | Scanning a C character, decoding its escapes. The initial "'" |
239 | is already eaten. |
240 `---------------------------------------------------------------*/
242 <SC_ESCAPED_CHARACTER>
246 assert (yy_top_state () == INITIAL);
250 c = yylval->string[1];
251 yylval->symbol = getsym (yylval->string);
252 symbol_class_set (yylval->symbol, token_sym);
253 symbol_user_token_number_set (yylval->symbol, (unsigned int) c);
261 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
264 LOCATION_PRINT (stderr, *yylloc);
265 fprintf (stderr, ": unexpected end of file in a character\n");
266 assert (yy_top_state () == INITIAL);
274 /*----------------------------.
275 | Decode escaped characters. |
276 `----------------------------*/
278 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
281 long c = strtol (yytext + 1, 0, 8);
284 LOCATION_PRINT (stderr, *yylloc);
285 fprintf (stderr, ": invalid escape: %s\n", yytext);
289 obstack_1grow (&string_obstack, c);
293 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
296 \\a obstack_1grow (&string_obstack, '\a');
297 \\b obstack_1grow (&string_obstack, '\b');
298 \\f obstack_1grow (&string_obstack, '\f');
299 \\n obstack_1grow (&string_obstack, '\n');
300 \\r obstack_1grow (&string_obstack, '\r');
301 \\t obstack_1grow (&string_obstack, '\t');
302 \\v obstack_1grow (&string_obstack, '\v');
303 \\[\\""] obstack_1grow (&string_obstack, yytext[1]);
305 LOCATION_PRINT (stderr, *yylloc);
306 fprintf (stderr, ": unrecognized escape: %s\n", yytext);
312 /*----------------------------------------------------------.
313 | Scanning a C character without decoding its escapes. The |
314 | initial "'" is already eaten. |
315 `----------------------------------------------------------*/
321 assert (yy_top_state () != INITIAL);
325 [^\[\]\'\n\r\\] YY_GROW;
328 {eols} YY_GROW; YY_LINES;
331 LOCATION_PRINT (stderr, *yylloc);
332 fprintf (stderr, ": unexpected end of file in a character\n");
333 assert (yy_top_state () != INITIAL);
339 /*----------------------------------------------------------------.
340 | Scanning a C string, without decoding its escapes. The initial |
341 | `"' is already eaten. |
342 `----------------------------------------------------------------*/
347 assert (yy_top_state () != INITIAL);
352 [^\[\]\"\n\r\\]+ YY_GROW;
355 {eols} YY_GROW; YY_LINES;
358 LOCATION_PRINT (stderr, *yylloc);
359 fprintf (stderr, ": unexpected end of file in a string\n");
360 assert (yy_top_state () != INITIAL);
366 /*---------------------------------------------------.
367 | Strings, comments etc. can be found in user code. |
368 `---------------------------------------------------*/
370 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
372 /* Characters. We don't check there is only one. */
373 \' YY_GROW; yy_push_state (SC_CHARACTER);
376 \" YY_GROW; yy_push_state (SC_STRING);
379 "/*" YY_GROW; yy_push_state (SC_COMMENT);
384 /*---------------------------------------------------------------.
385 | Scanning some code in braces (%union and actions). The initial |
386 | "{" is already eaten. |
387 `---------------------------------------------------------------*/
393 if (--braces_level == 0)
401 "{" YY_GROW; braces_level++;
403 "$"("<".*">")?(-?[0-9]+|"$") { handle_dollar (yytext); }
404 "@"(-?[0-9]+|"$") { handle_at (yytext); }
406 [^\[\]$/\'\"@\{\}\n\r]+ YY_GROW;
407 {eols} YY_GROW; YY_LINES;
409 /* A lose $, or /, or etc. */
413 LOCATION_PRINT (stderr, *yylloc);
414 fprintf (stderr, ": unexpected end of file in a braced code\n");
423 /*--------------------------------------------------------------.
424 | Scanning some prologue: from "%{" (already scanned) to "%}". |
425 `--------------------------------------------------------------*/
435 [^\[\]%\n\r]+ YY_GROW;
436 "%"+[^%\}\n\r]+ YY_GROW;
437 {eols} YY_GROW; YY_LINES;
440 LOCATION_PRINT (stderr, *yylloc);
441 fprintf (stderr, ": unexpected end of file in a prologue\n");
450 /*---------------------------------------------------------------.
451 | Scanning the epilogue (everything after the second "%%", which |
452 | has already been eaten. |
453 `---------------------------------------------------------------*/
457 ([^\[\]]|{eols})+ YY_GROW;
469 /*------------------------------------------------------------------.
470 | CP is pointing to a wannabee semantic value (i.e., a `$'). |
472 | Possible inputs: $[<TYPENAME>]($|integer) |
474 | Output to the STRING_OBSTACK a reference to this semantic value. |
475 `------------------------------------------------------------------*/
478 handle_dollar (char *cp)
480 const char *type_name = NULL;
482 /* RULE_LENGTH is the number of values in the current rule so far,
483 which says where to find `$0' with respect to the top of the
484 stack. It is not the same as the rule->length in the case of mid
488 for (rhs = current_rule->next; rhs; rhs = rhs->next)
493 /* Get the type name if explicit. */
506 type_name = get_type_name (0, current_rule);
507 if (!type_name && typed)
508 complain (_("$$ of `%s' has no declared type"),
509 current_rule->sym->tag);
512 obstack_fgrow1 (&string_obstack,
513 "]b4_lhs_value([%s])[", type_name);
515 else if (isdigit (*cp) || *cp == '-')
517 int n = strtol (cp, &cp, 10);
520 complain (_("invalid value: %s%d"), "$", n);
523 if (!type_name && n > 0)
524 type_name = get_type_name (n, current_rule);
525 if (!type_name && typed)
526 complain (_("$%d of `%s' has no declared type"),
527 n, current_rule->sym->tag);
530 obstack_fgrow3 (&string_obstack,
531 "]b4_rhs_value([%d], [%d], [%s])[",
532 rule_length, n, type_name);
539 complain (_("%s is invalid"), quote (buf));
543 /*-------------------------------------------------------.
544 | CP is pointing to a location (i.e., a `@'). Output to |
545 | STRING_OBSTACK a reference to this location. |
546 `-------------------------------------------------------*/
551 /* RULE_LENGTH is the number of values in the current rule so far,
552 which says where to find `$0' with respect to the top of the
553 stack. It is not the same as the rule->length in the case of mid
557 for (rhs = current_rule->next; rhs; rhs = rhs->next)
565 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
567 else if (isdigit (*cp) || *cp == '-')
569 int n = strtol (cp, &cp, 10);
571 complain (_("invalid value: %s%d"), "@", n);
573 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
580 complain (_("%s is invalid"), quote (buf));