]> git.saurik.com Git - bison.git/blame - src/scan-gram.l
* src/scan-gram.l (YY_OBS_FINISH): Don't set yylval.
[bison.git] / src / scan-gram.l
CommitLineData
e9955c83
AD
1/* Bison Grammar Scanner -*- C -*-
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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
19 02111-1307 USA
20*/
21
22%option debug nodefault noyywrap nounput never-interactive stack
23%option prefix="gram_" outfile="lex.yy.c"
24
25%{
26#include "system.h"
27#include "complain.h"
28#include "quote.h"
29#include "getargs.h"
30#include "gram.h"
31#include "reader.h"
32
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)
37
44995b2e
AD
38
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
41 use it.
42
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. */
48
49static struct obstack string_obstack;
50char *last_string;
51
52#define YY_OBS_INIT \
53 obstack_init (&string_obstack)
54
55#define YY_OBS_GROW \
56 obstack_grow (&string_obstack, yytext, yyleng)
57
58#define YY_OBS_FINISH \
59 do { \
60 obstack_1grow (&string_obstack, '\0'); \
61 last_string = obstack_finish (&string_obstack); \
44995b2e
AD
62 } while (0)
63
64#define YY_OBS_FREE \
65 do { \
66 obstack_free (&string_obstack, last_string); \
67 } while (0)
e9955c83 68
4cdb01db
AD
69void
70scanner_last_string_free (void)
71{
72 YY_OBS_FREE;
73}
74
75
e9955c83
AD
76/* This is only to avoid GCC warnings. */
77#define YY_USER_INIT if (yycontrol) {;};
78
44995b2e 79
e9955c83
AD
80static int braces_level = 0;
81static int percent_percent_count = 0;
82
83static void handle_dollar PARAMS ((char *cp));
84static void handle_at PARAMS ((char *cp));
85
86%}
87%x SC_COMMENT
88%x SC_STRING SC_CHARACTER
89%x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
90%x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
91
92id [.a-zA-Z][.a-zA-Z_0-9]*
93int [0-9]+
94eols (\n|\r|\n\r|\r\n)+
95blanks [ \t\f]+
96
97%%
98%{
99 /* At each yylex invocation, mark the current position as the
100 start of the next token. */
101#define TR_POS 0
102#if TR_POS
103 fprintf (stderr, "FOO1: ");
104 LOCATION_PRINT (stderr, *yylloc);
105 fprintf (stderr, "\n");
106#endif
107 YY_STEP;
108#if TR_POS
109 fprintf (stderr, "BAR1: ");
110 LOCATION_PRINT (stderr, *yylloc);
111 fprintf (stderr, "\n");
112#endif
113%}
114
115
116 /*----------------------------.
117 | Scanning Bison directives. |
118 `----------------------------*/
119<INITIAL>
120{
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;
148
149 "=" return EQUAL;
150 ":" return COLON;
151 "|" return PIPE;
152 ";" return SEMICOLON;
153
154 {eols} YY_LINES; YY_STEP;
155 {blanks} YY_STEP;
156 {id} {
4cdb01db 157 yylval->symbol = getsym (yytext);
e9955c83
AD
158 return ID;
159 }
160
161 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
162
163 /* Characters. We don't check there is only one. */
44995b2e 164 \' YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
e9955c83
AD
165
166 /* Strings. */
44995b2e 167 \" YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
e9955c83
AD
168
169 /* Comments. */
170 "/*" yy_push_state (SC_COMMENT);
171 "//".* YY_STEP;
172
173 /* Prologue. */
44995b2e 174 "%{" YY_OBS_INIT; yy_push_state (SC_PROLOGUE);
e9955c83
AD
175
176 /* Code in between braces. */
44995b2e 177 "{" YY_OBS_INIT; YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
e9955c83
AD
178
179 /* A type. */
4cdb01db
AD
180 "<"[^>]+">" {
181 YY_OBS_INIT;
182 obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
183 YY_OBS_FINISH;
184 yylval->string = last_string;
185 return TYPE;
186 }
187
e9955c83
AD
188
189 "%%" {
190 if (++percent_percent_count == 2)
191 yy_push_state (SC_EPILOGUE);
192 return PERCENT_PERCENT;
193 }
194
195 . {
196 LOCATION_PRINT (stderr, *yylloc);
197 fprintf (stderr, ": invalid character: `%c'\n", *yytext);
198 YY_STEP;
199 }
200}
201
202
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 `------------------------------------------------------------*/
208
209<SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
210{
211 \[ obstack_sgrow (&string_obstack, "@<:@");
212 \] obstack_sgrow (&string_obstack, "@:>@");
213}
214
215
216
217 /*-----------------------------------------------------------.
218 | Scanning a C comment. The initial `/ *' is already eaten. |
219 `-----------------------------------------------------------*/
220
221<SC_COMMENT>
222{
223 "*/" { /* End of the comment. */
224 if (yy_top_state () == INITIAL)
225 {
226 YY_STEP;
227 }
228 else
229 {
44995b2e 230 YY_OBS_GROW;
e9955c83
AD
231 }
232 yy_pop_state ();
233 }
234
44995b2e
AD
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;
e9955c83
AD
238
239 <<EOF>> {
240 LOCATION_PRINT (stderr, *yylloc);
241 fprintf (stderr, ": unexpected end of file in a comment\n");
242 yy_pop_state ();
243 }
244}
245
246
247 /*----------------------------------------------------------------.
248 | Scanning a C string, including its escapes. The initial `"' is |
249 | already eaten. |
250 `----------------------------------------------------------------*/
251
252<SC_ESCAPED_STRING>
253{
254 \" {
255 assert (yy_top_state () == INITIAL);
44995b2e
AD
256 YY_OBS_GROW;
257 YY_OBS_FINISH;
4cdb01db 258 yylval->string = last_string;
e9955c83
AD
259 yy_pop_state ();
260 return STRING;
261 }
262
44995b2e 263 [^\"\n\r\\]+ YY_OBS_GROW;
e9955c83
AD
264
265 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
266
267 <<EOF>> {
268 LOCATION_PRINT (stderr, *yylloc);
269 fprintf (stderr, ": unexpected end of file in a string\n");
270 assert (yy_top_state () == INITIAL);
44995b2e 271 YY_OBS_FINISH;
4cdb01db 272 yylval->string = last_string;
e9955c83
AD
273 yy_pop_state ();
274 return STRING;
275 }
276}
277
278 /*---------------------------------------------------------------.
279 | Scanning a C character, decoding its escapes. The initial "'" |
280 | is already eaten. |
281 `---------------------------------------------------------------*/
282
283<SC_ESCAPED_CHARACTER>
284{
285 \' {
44995b2e 286 YY_OBS_GROW;
e9955c83
AD
287 assert (yy_top_state () == INITIAL);
288 {
44995b2e
AD
289 YY_OBS_FINISH;
290 yylval->symbol = getsym (last_string);
e9955c83 291 symbol_class_set (yylval->symbol, token_sym);
44995b2e
AD
292 symbol_user_token_number_set (yylval->symbol, last_string[1]);
293 YY_OBS_FREE;
e9955c83
AD
294 yy_pop_state ();
295 return ID;
296 }
297 }
298
44995b2e 299 [^\'\n\r\\] YY_OBS_GROW;
e9955c83
AD
300
301 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
302
303 <<EOF>> {
304 LOCATION_PRINT (stderr, *yylloc);
305 fprintf (stderr, ": unexpected end of file in a character\n");
306 assert (yy_top_state () == INITIAL);
44995b2e 307 YY_OBS_FINISH;
4cdb01db 308 yylval->string = last_string;
e9955c83
AD
309 yy_pop_state ();
310 return CHARACTER;
311 }
312}
313
314
315 /*----------------------------.
316 | Decode escaped characters. |
317 `----------------------------*/
318
319<SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
320{
321 \\[0-7]{3} {
322 long c = strtol (yytext + 1, 0, 8);
323 if (c > 255)
324 {
325 LOCATION_PRINT (stderr, *yylloc);
326 fprintf (stderr, ": invalid escape: %s\n", yytext);
327 YY_STEP;
328 }
329 else
330 obstack_1grow (&string_obstack, c);
331 }
332
333 \\x[0-9a-fA-F]{2} {
334 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
335 }
336
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]);
345 \\. {
346 LOCATION_PRINT (stderr, *yylloc);
347 fprintf (stderr, ": unrecognized escape: %s\n", yytext);
44995b2e 348 YY_OBS_GROW;
e9955c83
AD
349 }
350}
351
352
353 /*----------------------------------------------------------.
354 | Scanning a C character without decoding its escapes. The |
355 | initial "'" is already eaten. |
356 `----------------------------------------------------------*/
357
358<SC_CHARACTER>
359{
360 \' {
44995b2e 361 YY_OBS_GROW;
e9955c83
AD
362 assert (yy_top_state () != INITIAL);
363 yy_pop_state ();
364 }
365
44995b2e
AD
366 [^\[\]\'\n\r\\] YY_OBS_GROW;
367 \\. YY_OBS_GROW;
e9955c83 368
44995b2e 369 {eols} YY_OBS_GROW; YY_LINES;
e9955c83
AD
370
371 <<EOF>> {
372 LOCATION_PRINT (stderr, *yylloc);
373 fprintf (stderr, ": unexpected end of file in a character\n");
374 assert (yy_top_state () != INITIAL);
375 yy_pop_state ();
376 }
377}
378
379
380 /*----------------------------------------------------------------.
381 | Scanning a C string, without decoding its escapes. The initial |
382 | `"' is already eaten. |
383 `----------------------------------------------------------------*/
384
385<SC_STRING>
386{
387 \" {
388 assert (yy_top_state () != INITIAL);
44995b2e 389 YY_OBS_GROW;
e9955c83
AD
390 yy_pop_state ();
391 }
392
44995b2e
AD
393 [^\[\]\"\n\r\\]+ YY_OBS_GROW;
394 \\. YY_OBS_GROW;
e9955c83 395
44995b2e 396 {eols} YY_OBS_GROW; YY_LINES;
e9955c83
AD
397
398 <<EOF>> {
399 LOCATION_PRINT (stderr, *yylloc);
400 fprintf (stderr, ": unexpected end of file in a string\n");
401 assert (yy_top_state () != INITIAL);
402 yy_pop_state ();
403 }
404}
405
406
407 /*---------------------------------------------------.
408 | Strings, comments etc. can be found in user code. |
409 `---------------------------------------------------*/
410
411<SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
412{
413 /* Characters. We don't check there is only one. */
44995b2e 414 \' YY_OBS_GROW; yy_push_state (SC_CHARACTER);
e9955c83
AD
415
416 /* Strings. */
44995b2e 417 \" YY_OBS_GROW; yy_push_state (SC_STRING);
e9955c83
AD
418
419 /* Comments. */
44995b2e
AD
420 "/*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
421 "//".* YY_OBS_GROW;
e9955c83
AD
422}
423
424
425 /*---------------------------------------------------------------.
426 | Scanning some code in braces (%union and actions). The initial |
427 | "{" is already eaten. |
428 `---------------------------------------------------------------*/
429
430<SC_BRACED_CODE>
431{
432 "}" {
44995b2e 433 YY_OBS_GROW;
e9955c83
AD
434 if (--braces_level == 0)
435 {
436 yy_pop_state ();
44995b2e 437 YY_OBS_FINISH;
4cdb01db 438 yylval->string = last_string;
e9955c83
AD
439 return BRACED_CODE;
440 }
441 }
442
44995b2e 443 "{" YY_OBS_GROW; braces_level++;
e9955c83
AD
444
445 "$"("<".*">")?(-?[0-9]+|"$") { handle_dollar (yytext); }
446 "@"(-?[0-9]+|"$") { handle_at (yytext); }
447
44995b2e
AD
448 [^\[\]$/\'\"@\{\}\n\r]+ YY_OBS_GROW;
449 {eols} YY_OBS_GROW; YY_LINES;
e9955c83
AD
450
451 /* A lose $, or /, or etc. */
44995b2e 452 . YY_OBS_GROW;
e9955c83
AD
453
454 <<EOF>> {
455 LOCATION_PRINT (stderr, *yylloc);
456 fprintf (stderr, ": unexpected end of file in a braced code\n");
457 yy_pop_state ();
44995b2e 458 YY_OBS_FINISH;
4cdb01db
AD
459 yylval->string = last_string;
460 return BRACED_CODE;
e9955c83
AD
461 }
462
463}
464
465
466 /*--------------------------------------------------------------.
467 | Scanning some prologue: from "%{" (already scanned) to "%}". |
468 `--------------------------------------------------------------*/
469
470<SC_PROLOGUE>
471{
472 "%}" {
473 yy_pop_state ();
44995b2e 474 YY_OBS_FINISH;
4cdb01db 475 yylval->string = last_string;
e9955c83
AD
476 return PROLOGUE;
477 }
478
44995b2e
AD
479 [^\[\]%\n\r]+ YY_OBS_GROW;
480 "%"+[^%\}\n\r]+ YY_OBS_GROW;
481 {eols} YY_OBS_GROW; YY_LINES;
e9955c83
AD
482
483 <<EOF>> {
484 LOCATION_PRINT (stderr, *yylloc);
485 fprintf (stderr, ": unexpected end of file in a prologue\n");
486 yy_pop_state ();
44995b2e 487 YY_OBS_FINISH;
4cdb01db 488 yylval->string = last_string;
e9955c83
AD
489 return PROLOGUE;
490 }
491
492}
493
494
495 /*---------------------------------------------------------------.
496 | Scanning the epilogue (everything after the second "%%", which |
497 | has already been eaten. |
498 `---------------------------------------------------------------*/
499
500<SC_EPILOGUE>
501{
44995b2e 502 ([^\[\]]|{eols})+ YY_OBS_GROW;
e9955c83
AD
503
504 <<EOF>> {
505 yy_pop_state ();
44995b2e 506 YY_OBS_FINISH;
4cdb01db 507 yylval->string = last_string;
e9955c83
AD
508 return EPILOGUE;
509 }
510}
511
512
513%%
514
515/*------------------------------------------------------------------.
516| CP is pointing to a wannabee semantic value (i.e., a `$'). |
517| |
518| Possible inputs: $[<TYPENAME>]($|integer) |
519| |
520| Output to the STRING_OBSTACK a reference to this semantic value. |
521`------------------------------------------------------------------*/
522
523static void
524handle_dollar (char *cp)
525{
526 const char *type_name = NULL;
527
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
531 rule actions. */
532 int rule_length = 0;
533 symbol_list *rhs;
534 for (rhs = current_rule->next; rhs; rhs = rhs->next)
535 ++rule_length;
536
537 ++cp;
538
539 /* Get the type name if explicit. */
540 if (*cp == '<')
541 {
542 type_name = ++cp;
543 while (*cp != '>')
544 ++cp;
545 *cp = '\0';
546 ++cp;
547 }
548
549 if (*cp == '$')
550 {
551 if (!type_name)
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);
556 if (!type_name)
557 type_name = "";
558 obstack_fgrow1 (&string_obstack,
559 "]b4_lhs_value([%s])[", type_name);
560 }
561 else if (isdigit (*cp) || *cp == '-')
562 {
563 int n = strtol (cp, &cp, 10);
564
565 if (n > rule_length)
566 complain (_("invalid value: %s%d"), "$", n);
567 else
568 {
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);
574 if (!type_name)
575 type_name = "";
576 obstack_fgrow3 (&string_obstack,
577 "]b4_rhs_value([%d], [%d], [%s])[",
578 rule_length, n, type_name);
579 }
580 }
581 else
582 {
583 char buf[] = "$c";
584 buf[1] = *cp;
585 complain (_("%s is invalid"), quote (buf));
586 }
587}
588
589/*-------------------------------------------------------.
590| CP is pointing to a location (i.e., a `@'). Output to |
591| STRING_OBSTACK a reference to this location. |
592`-------------------------------------------------------*/
593
594static void
595handle_at (char *cp)
596{
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
600 rule actions. */
601 int rule_length = 0;
602 symbol_list *rhs;
603 for (rhs = current_rule->next; rhs; rhs = rhs->next)
604 ++rule_length;
605
606 locations_flag = 1;
607 ++cp;
608
609 if (*cp == '$')
610 {
611 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
612 }
613 else if (isdigit (*cp) || *cp == '-')
614 {
615 int n = strtol (cp, &cp, 10);
616 if (n > rule_length)
617 complain (_("invalid value: %s%d"), "@", n);
618 else
619 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
620 rule_length, n);
621 }
622 else
623 {
624 char buf[] = "@c";
625 buf[1] = *cp;
626 complain (_("%s is invalid"), quote (buf));
627 }
628}
4cdb01db
AD
629
630void
631scanner_free (void)
632{
633 obstack_free (&string_obstack, 0);
634}