]> git.saurik.com Git - bison.git/blob - src/scan-gram.l
e7ceecac338d338026dd882f7a5f3ad5d8305c5d
[bison.git] / src / scan-gram.l
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
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
49 static struct obstack string_obstack;
50 char *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); \
62 yylval->string = last_string; \
63 } while (0)
64
65 #define YY_OBS_FREE \
66 do { \
67 obstack_free (&string_obstack, last_string); \
68 } while (0)
69
70 /* This is only to avoid GCC warnings. */
71 #define YY_USER_INIT if (yycontrol) {;};
72
73
74 static int braces_level = 0;
75 static int percent_percent_count = 0;
76
77 static void handle_dollar PARAMS ((char *cp));
78 static void handle_at PARAMS ((char *cp));
79
80 %}
81 %x SC_COMMENT
82 %x SC_STRING SC_CHARACTER
83 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
84 %x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
85
86 id [.a-zA-Z][.a-zA-Z_0-9]*
87 int [0-9]+
88 eols (\n|\r|\n\r|\r\n)+
89 blanks [ \t\f]+
90
91 %%
92 %{
93 /* At each yylex invocation, mark the current position as the
94 start of the next token. */
95 #define TR_POS 0
96 #if TR_POS
97 fprintf (stderr, "FOO1: ");
98 LOCATION_PRINT (stderr, *yylloc);
99 fprintf (stderr, "\n");
100 #endif
101 YY_STEP;
102 #if TR_POS
103 fprintf (stderr, "BAR1: ");
104 LOCATION_PRINT (stderr, *yylloc);
105 fprintf (stderr, "\n");
106 #endif
107 %}
108
109
110 /*----------------------------.
111 | Scanning Bison directives. |
112 `----------------------------*/
113 <INITIAL>
114 {
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;
142
143 "=" return EQUAL;
144 ":" return COLON;
145 "|" return PIPE;
146 ";" return SEMICOLON;
147
148 {eols} YY_LINES; YY_STEP;
149 {blanks} YY_STEP;
150 {id} {
151 YY_OBS_INIT; YY_OBS_GROW; YY_OBS_FINISH;
152 yylval->symbol = getsym (last_string);
153 YY_OBS_FREE;
154 return ID;
155 }
156
157 {int} yylval->integer = strtol (yytext, 0, 10); return INT;
158
159 /* Characters. We don't check there is only one. */
160 \' YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
161
162 /* Strings. */
163 \" YY_OBS_INIT; YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
164
165 /* Comments. */
166 "/*" yy_push_state (SC_COMMENT);
167 "//".* YY_STEP;
168
169 /* Prologue. */
170 "%{" YY_OBS_INIT; yy_push_state (SC_PROLOGUE);
171
172 /* Code in between braces. */
173 "{" YY_OBS_INIT; YY_OBS_GROW; ++braces_level; yy_push_state (SC_BRACED_CODE);
174
175 /* A type. */
176 "<"[^>]+">" YY_OBS_INIT; obstack_grow (&string_obstack, yytext + 1, yyleng - 2); YY_OBS_FINISH; return TYPE;
177
178 "%%" {
179 if (++percent_percent_count == 2)
180 yy_push_state (SC_EPILOGUE);
181 return PERCENT_PERCENT;
182 }
183
184 . {
185 LOCATION_PRINT (stderr, *yylloc);
186 fprintf (stderr, ": invalid character: `%c'\n", *yytext);
187 YY_STEP;
188 }
189 }
190
191
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 `------------------------------------------------------------*/
197
198 <SC_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
199 {
200 \[ obstack_sgrow (&string_obstack, "@<:@");
201 \] obstack_sgrow (&string_obstack, "@:>@");
202 }
203
204
205
206 /*-----------------------------------------------------------.
207 | Scanning a C comment. The initial `/ *' is already eaten. |
208 `-----------------------------------------------------------*/
209
210 <SC_COMMENT>
211 {
212 "*/" { /* End of the comment. */
213 if (yy_top_state () == INITIAL)
214 {
215 YY_STEP;
216 }
217 else
218 {
219 YY_OBS_GROW;
220 }
221 yy_pop_state ();
222 }
223
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;
227
228 <<EOF>> {
229 LOCATION_PRINT (stderr, *yylloc);
230 fprintf (stderr, ": unexpected end of file in a comment\n");
231 yy_pop_state ();
232 }
233 }
234
235
236 /*----------------------------------------------------------------.
237 | Scanning a C string, including its escapes. The initial `"' is |
238 | already eaten. |
239 `----------------------------------------------------------------*/
240
241 <SC_ESCAPED_STRING>
242 {
243 \" {
244 assert (yy_top_state () == INITIAL);
245 YY_OBS_GROW;
246 YY_OBS_FINISH;
247 yy_pop_state ();
248 return STRING;
249 }
250
251 [^\"\n\r\\]+ YY_OBS_GROW;
252
253 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
254
255 <<EOF>> {
256 LOCATION_PRINT (stderr, *yylloc);
257 fprintf (stderr, ": unexpected end of file in a string\n");
258 assert (yy_top_state () == INITIAL);
259 YY_OBS_FINISH;
260 yy_pop_state ();
261 return STRING;
262 }
263 }
264
265 /*---------------------------------------------------------------.
266 | Scanning a C character, decoding its escapes. The initial "'" |
267 | is already eaten. |
268 `---------------------------------------------------------------*/
269
270 <SC_ESCAPED_CHARACTER>
271 {
272 \' {
273 YY_OBS_GROW;
274 assert (yy_top_state () == INITIAL);
275 {
276 YY_OBS_FINISH;
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]);
280 YY_OBS_FREE;
281 yy_pop_state ();
282 return ID;
283 }
284 }
285
286 [^\'\n\r\\] YY_OBS_GROW;
287
288 {eols} obstack_1grow (&string_obstack, '\n'); YY_LINES;
289
290 <<EOF>> {
291 LOCATION_PRINT (stderr, *yylloc);
292 fprintf (stderr, ": unexpected end of file in a character\n");
293 assert (yy_top_state () == INITIAL);
294 YY_OBS_FINISH;
295 yy_pop_state ();
296 return CHARACTER;
297 }
298 }
299
300
301 /*----------------------------.
302 | Decode escaped characters. |
303 `----------------------------*/
304
305 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
306 {
307 \\[0-7]{3} {
308 long c = strtol (yytext + 1, 0, 8);
309 if (c > 255)
310 {
311 LOCATION_PRINT (stderr, *yylloc);
312 fprintf (stderr, ": invalid escape: %s\n", yytext);
313 YY_STEP;
314 }
315 else
316 obstack_1grow (&string_obstack, c);
317 }
318
319 \\x[0-9a-fA-F]{2} {
320 obstack_1grow (&string_obstack, strtol (yytext + 2, 0, 16));
321 }
322
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]);
331 \\. {
332 LOCATION_PRINT (stderr, *yylloc);
333 fprintf (stderr, ": unrecognized escape: %s\n", yytext);
334 YY_OBS_GROW;
335 }
336 }
337
338
339 /*----------------------------------------------------------.
340 | Scanning a C character without decoding its escapes. The |
341 | initial "'" is already eaten. |
342 `----------------------------------------------------------*/
343
344 <SC_CHARACTER>
345 {
346 \' {
347 YY_OBS_GROW;
348 assert (yy_top_state () != INITIAL);
349 yy_pop_state ();
350 }
351
352 [^\[\]\'\n\r\\] YY_OBS_GROW;
353 \\. YY_OBS_GROW;
354
355 {eols} YY_OBS_GROW; YY_LINES;
356
357 <<EOF>> {
358 LOCATION_PRINT (stderr, *yylloc);
359 fprintf (stderr, ": unexpected end of file in a character\n");
360 assert (yy_top_state () != INITIAL);
361 yy_pop_state ();
362 }
363 }
364
365
366 /*----------------------------------------------------------------.
367 | Scanning a C string, without decoding its escapes. The initial |
368 | `"' is already eaten. |
369 `----------------------------------------------------------------*/
370
371 <SC_STRING>
372 {
373 \" {
374 assert (yy_top_state () != INITIAL);
375 YY_OBS_GROW;
376 yy_pop_state ();
377 }
378
379 [^\[\]\"\n\r\\]+ YY_OBS_GROW;
380 \\. YY_OBS_GROW;
381
382 {eols} YY_OBS_GROW; YY_LINES;
383
384 <<EOF>> {
385 LOCATION_PRINT (stderr, *yylloc);
386 fprintf (stderr, ": unexpected end of file in a string\n");
387 assert (yy_top_state () != INITIAL);
388 yy_pop_state ();
389 }
390 }
391
392
393 /*---------------------------------------------------.
394 | Strings, comments etc. can be found in user code. |
395 `---------------------------------------------------*/
396
397 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
398 {
399 /* Characters. We don't check there is only one. */
400 \' YY_OBS_GROW; yy_push_state (SC_CHARACTER);
401
402 /* Strings. */
403 \" YY_OBS_GROW; yy_push_state (SC_STRING);
404
405 /* Comments. */
406 "/*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
407 "//".* YY_OBS_GROW;
408 }
409
410
411 /*---------------------------------------------------------------.
412 | Scanning some code in braces (%union and actions). The initial |
413 | "{" is already eaten. |
414 `---------------------------------------------------------------*/
415
416 <SC_BRACED_CODE>
417 {
418 "}" {
419 YY_OBS_GROW;
420 if (--braces_level == 0)
421 {
422 yy_pop_state ();
423 YY_OBS_FINISH;
424 return BRACED_CODE;
425 }
426 }
427
428 "{" YY_OBS_GROW; braces_level++;
429
430 "$"("<".*">")?(-?[0-9]+|"$") { handle_dollar (yytext); }
431 "@"(-?[0-9]+|"$") { handle_at (yytext); }
432
433 [^\[\]$/\'\"@\{\}\n\r]+ YY_OBS_GROW;
434 {eols} YY_OBS_GROW; YY_LINES;
435
436 /* A lose $, or /, or etc. */
437 . YY_OBS_GROW;
438
439 <<EOF>> {
440 LOCATION_PRINT (stderr, *yylloc);
441 fprintf (stderr, ": unexpected end of file in a braced code\n");
442 yy_pop_state ();
443 YY_OBS_FINISH;
444 return PROLOGUE;
445 }
446
447 }
448
449
450 /*--------------------------------------------------------------.
451 | Scanning some prologue: from "%{" (already scanned) to "%}". |
452 `--------------------------------------------------------------*/
453
454 <SC_PROLOGUE>
455 {
456 "%}" {
457 yy_pop_state ();
458 YY_OBS_FINISH;
459 return PROLOGUE;
460 }
461
462 [^\[\]%\n\r]+ YY_OBS_GROW;
463 "%"+[^%\}\n\r]+ YY_OBS_GROW;
464 {eols} YY_OBS_GROW; YY_LINES;
465
466 <<EOF>> {
467 LOCATION_PRINT (stderr, *yylloc);
468 fprintf (stderr, ": unexpected end of file in a prologue\n");
469 yy_pop_state ();
470 YY_OBS_FINISH;
471 return PROLOGUE;
472 }
473
474 }
475
476
477 /*---------------------------------------------------------------.
478 | Scanning the epilogue (everything after the second "%%", which |
479 | has already been eaten. |
480 `---------------------------------------------------------------*/
481
482 <SC_EPILOGUE>
483 {
484 ([^\[\]]|{eols})+ YY_OBS_GROW;
485
486 <<EOF>> {
487 yy_pop_state ();
488 YY_OBS_FINISH;
489 return EPILOGUE;
490 }
491 }
492
493
494 %%
495
496 /*------------------------------------------------------------------.
497 | CP is pointing to a wannabee semantic value (i.e., a `$'). |
498 | |
499 | Possible inputs: $[<TYPENAME>]($|integer) |
500 | |
501 | Output to the STRING_OBSTACK a reference to this semantic value. |
502 `------------------------------------------------------------------*/
503
504 static void
505 handle_dollar (char *cp)
506 {
507 const char *type_name = NULL;
508
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
512 rule actions. */
513 int rule_length = 0;
514 symbol_list *rhs;
515 for (rhs = current_rule->next; rhs; rhs = rhs->next)
516 ++rule_length;
517
518 ++cp;
519
520 /* Get the type name if explicit. */
521 if (*cp == '<')
522 {
523 type_name = ++cp;
524 while (*cp != '>')
525 ++cp;
526 *cp = '\0';
527 ++cp;
528 }
529
530 if (*cp == '$')
531 {
532 if (!type_name)
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);
537 if (!type_name)
538 type_name = "";
539 obstack_fgrow1 (&string_obstack,
540 "]b4_lhs_value([%s])[", type_name);
541 }
542 else if (isdigit (*cp) || *cp == '-')
543 {
544 int n = strtol (cp, &cp, 10);
545
546 if (n > rule_length)
547 complain (_("invalid value: %s%d"), "$", n);
548 else
549 {
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);
555 if (!type_name)
556 type_name = "";
557 obstack_fgrow3 (&string_obstack,
558 "]b4_rhs_value([%d], [%d], [%s])[",
559 rule_length, n, type_name);
560 }
561 }
562 else
563 {
564 char buf[] = "$c";
565 buf[1] = *cp;
566 complain (_("%s is invalid"), quote (buf));
567 }
568 }
569
570 /*-------------------------------------------------------.
571 | CP is pointing to a location (i.e., a `@'). Output to |
572 | STRING_OBSTACK a reference to this location. |
573 `-------------------------------------------------------*/
574
575 static void
576 handle_at (char *cp)
577 {
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
581 rule actions. */
582 int rule_length = 0;
583 symbol_list *rhs;
584 for (rhs = current_rule->next; rhs; rhs = rhs->next)
585 ++rule_length;
586
587 locations_flag = 1;
588 ++cp;
589
590 if (*cp == '$')
591 {
592 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
593 }
594 else if (isdigit (*cp) || *cp == '-')
595 {
596 int n = strtol (cp, &cp, 10);
597 if (n > rule_length)
598 complain (_("invalid value: %s%d"), "@", n);
599 else
600 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
601 rule_length, n);
602 }
603 else
604 {
605 char buf[] = "@c";
606 buf[1] = *cp;
607 complain (_("%s is invalid"), quote (buf));
608 }
609 }