]> git.saurik.com Git - bison.git/blame - src/lex.c
* src/symtab.c (bucket_new): New function.
[bison.git] / src / lex.c
CommitLineData
40675e7c 1/* Token-reader for Bison's input parser,
a0f6b076 2 Copyright (C) 1984, 1986, 1989, 1992, 2000 Free Software Foundation, Inc.
40675e7c 3
a0f6b076 4 This file is part of Bison, the GNU Compiler Compiler.
40675e7c 5
a0f6b076
AD
6 Bison 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, or (at your option)
9 any later version.
40675e7c 10
a0f6b076
AD
11 Bison 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.
40675e7c 15
a0f6b076
AD
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
40675e7c 20
40675e7c 21#include "system.h"
ceed8467 22#include "getargs.h"
40675e7c 23#include "files.h"
a44c2277 24#include "getopt.h" /* for optarg */
40675e7c
DM
25#include "symtab.h"
26#include "lex.h"
d7913476 27#include "xalloc.h"
a0f6b076 28#include "complain.h"
b2ca4022 29#include "gram.h"
ff4a34be 30#include "quote.h"
40675e7c
DM
31
32/* Buffer for storing the current token. */
33char *token_buffer;
34
35/* Allocated size of token_buffer, not including space for terminator. */
d2729d44 36int maxtoken;
40675e7c
DM
37
38bucket *symval;
39int numval;
40
41static int unlexed; /* these two describe a token to be reread */
42static bucket *unlexed_symval; /* by the next call to lex */
43
44
45void
d2729d44 46init_lex (void)
40675e7c
DM
47{
48 maxtoken = 100;
d7913476 49 token_buffer = XCALLOC (char, maxtoken + 1);
40675e7c
DM
50 unlexed = -1;
51}
52
53
d2729d44
JT
54char *
55grow_token_buffer (char *p)
40675e7c
DM
56{
57 int offset = p - token_buffer;
58 maxtoken *= 2;
d7913476 59 token_buffer = XREALLOC (token_buffer, char, maxtoken + 1);
40675e7c
DM
60 return token_buffer + offset;
61}
62
63
64int
d2729d44 65skip_white_space (void)
40675e7c 66{
abadc117
AD
67 int c;
68 int inside;
40675e7c 69
abadc117 70 c = getc (finput);
40675e7c
DM
71
72 for (;;)
73 {
74 int cplus_comment;
75
76 switch (c)
77 {
78 case '/':
79282c5a 79 /* FIXME: Should probably be merged with copy_comment. */
abadc117 80 c = getc (finput);
a083fbbf 81 if (c != '*' && c != '/')
a44c2277 82 {
a0f6b076 83 complain (_("unexpected `/' found and ignored"));
a44c2277
RS
84 break;
85 }
40675e7c
DM
86 cplus_comment = (c == '/');
87
abadc117 88 c = getc (finput);
40675e7c
DM
89
90 inside = 1;
91 while (inside)
92 {
93 if (!cplus_comment && c == '*')
94 {
95 while (c == '*')
abadc117 96 c = getc (finput);
40675e7c
DM
97
98 if (c == '/')
99 {
100 inside = 0;
abadc117 101 c = getc (finput);
40675e7c
DM
102 }
103 }
104 else if (c == '\n')
105 {
106 lineno++;
107 if (cplus_comment)
108 inside = 0;
abadc117 109 c = getc (finput);
40675e7c
DM
110 }
111 else if (c == EOF)
a0f6b076 112 fatal (_("unterminated comment"));
40675e7c 113 else
abadc117 114 c = getc (finput);
40675e7c
DM
115 }
116
117 break;
118
119 case '\n':
120 lineno++;
121
122 case ' ':
123 case '\t':
124 case '\f':
abadc117 125 c = getc (finput);
40675e7c
DM
126 break;
127
128 default:
36281465 129 return c;
40675e7c
DM
130 }
131 }
132}
133
79282c5a
AD
134
135/*-----------------------------------------------------.
136| Do a getc, but give error message if EOF encountered |
137`-----------------------------------------------------*/
138
4a120d45 139static int
abadc117 140xgetc (FILE *f)
a44c2277 141{
abadc117 142 int c = getc (f);
a44c2277 143 if (c == EOF)
a0f6b076 144 fatal (_("unexpected end of file"));
a44c2277
RS
145 return c;
146}
147
abadc117
AD
148
149/*------------------------------------------------------------------.
150| Read one literal character from finput. Process \ escapes. |
151| Append the normalized string version of the char to *PP. Assign |
152| the character code to *PCODE. Return 1 unless the character is an |
153| unescaped `term' or \n report error for \n |
154`------------------------------------------------------------------*/
155
4a120d45 156static int
d2729d44 157literalchar (char **pp, int *pcode, char term)
a44c2277 158{
abadc117
AD
159 int c;
160 char *p;
161 int code;
a44c2277
RS
162 int wasquote = 0;
163
abadc117 164 c = xgetc (finput);
a083fbbf 165 if (c == '\n')
a44c2277 166 {
a0f6b076 167 complain (_("unescaped newline in constant"));
abadc117 168 ungetc (c, finput);
a44c2277
RS
169 code = '?';
170 wasquote = 1;
171 }
172 else if (c != '\\')
173 {
174 code = c;
a083fbbf 175 if (c == term)
a44c2277
RS
176 wasquote = 1;
177 }
178 else
179 {
abadc117
AD
180 c = xgetc (finput);
181 if (c == 't')
182 code = '\t';
183 else if (c == 'n')
184 code = '\n';
185 else if (c == 'a')
186 code = '\007';
187 else if (c == 'r')
188 code = '\r';
189 else if (c == 'f')
190 code = '\f';
191 else if (c == 'b')
192 code = '\b';
193 else if (c == 'v')
194 code = '\013';
195 else if (c == '\\')
196 code = '\\';
197 else if (c == '\'')
198 code = '\'';
199 else if (c == '\"')
200 code = '\"';
a44c2277
RS
201 else if (c <= '7' && c >= '0')
202 {
203 code = 0;
204 while (c <= '7' && c >= '0')
205 {
206 code = (code * 8) + (c - '0');
207 if (code >= 256 || code < 0)
208 {
a0f6b076
AD
209 complain (_("octal value outside range 0...255: `\\%o'"),
210 code);
a44c2277
RS
211 code &= 0xFF;
212 break;
213 }
abadc117 214 c = xgetc (finput);
a44c2277 215 }
abadc117 216 ungetc (c, finput);
a44c2277
RS
217 }
218 else if (c == 'x')
219 {
abadc117 220 c = xgetc (finput);
a44c2277
RS
221 code = 0;
222 while (1)
223 {
224 if (c >= '0' && c <= '9')
abadc117 225 code *= 16, code += c - '0';
a44c2277 226 else if (c >= 'a' && c <= 'f')
abadc117 227 code *= 16, code += c - 'a' + 10;
a44c2277 228 else if (c >= 'A' && c <= 'F')
abadc117 229 code *= 16, code += c - 'A' + 10;
a083fbbf 230 else
a44c2277 231 break;
abadc117 232 if (code >= 256 || code < 0)
a44c2277 233 {
abadc117 234 complain (_("hexadecimal value above 255: `\\x%x'"), code);
a44c2277
RS
235 code &= 0xFF;
236 break;
237 }
abadc117 238 c = xgetc (finput);
a44c2277 239 }
abadc117 240 ungetc (c, finput);
a44c2277
RS
241 }
242 else
243 {
ff4a34be
AD
244 char buf [] = "c";
245 buf[0] = c;
a0f6b076 246 complain (_("unknown escape sequence: `\\' followed by `%s'"),
ff4a34be 247 quote (buf));
a44c2277
RS
248 code = '?';
249 }
abadc117 250 } /* has \ */
a44c2277
RS
251
252 /* now fill token_buffer with the canonical name for this character
253 as a literal token. Do not use what the user typed,
254 so that `\012' and `\n' can be interchangeable. */
255
256 p = *pp;
e5335b74
JT
257 if (code == term && wasquote)
258 *p++ = code;
abadc117
AD
259 else if (code == '\\')
260 {
261 *p++ = '\\';
262 *p++ = '\\';
263 }
264 else if (code == '\'')
265 {
266 *p++ = '\\';
267 *p++ = '\'';
268 }
269 else if (code == '\"')
270 {
271 *p++ = '\\';
272 *p++ = '\"';
273 }
5ce94c29
RS
274 else if (code >= 040 && code < 0177)
275 *p++ = code;
abadc117
AD
276 else if (code == '\t')
277 {
278 *p++ = '\\';
279 *p++ = 't';
280 }
281 else if (code == '\n')
282 {
283 *p++ = '\\';
284 *p++ = 'n';
285 }
286 else if (code == '\r')
287 {
288 *p++ = '\\';
289 *p++ = 'r';
290 }
291 else if (code == '\v')
292 {
293 *p++ = '\\';
294 *p++ = 'v';
295 }
296 else if (code == '\b')
297 {
298 *p++ = '\\';
299 *p++ = 'b';
300 }
301 else if (code == '\f')
302 {
303 *p++ = '\\';
304 *p++ = 'f';
305 }
a44c2277
RS
306 else
307 {
308 *p++ = '\\';
309 *p++ = code / 0100 + '0';
310 *p++ = ((code / 010) & 07) + '0';
311 *p++ = (code & 07) + '0';
312 }
313 *pp = p;
314 *pcode = code;
abadc117 315 return !wasquote;
a44c2277
RS
316}
317
40675e7c
DM
318
319void
d2729d44 320unlex (int token)
40675e7c
DM
321{
322 unlexed = token;
323 unlexed_symval = symval;
324}
325
f282676b
AD
326/*-----------------------------------------------------------------.
327| We just read `<' from FIN. Store in TOKEN_BUFFER, the type name |
328| specified between the `<...>'. |
329`-----------------------------------------------------------------*/
330
331void
332read_type_name (FILE *fin)
333{
334 char *p = token_buffer;
335 int c = getc (fin);
336
337 while (c != '>')
338 {
339 if (c == EOF)
340 fatal (_("unterminated type name at end of file"));
341 if (c == '\n')
342 {
343 complain (_("unterminated type name"));
344 ungetc (c, fin);
345 break;
346 }
347
348 if (p == token_buffer + maxtoken)
349 p = grow_token_buffer (p);
350
351 *p++ = c;
352 c = getc (fin);
353 }
354 *p = 0;
355}
356
40675e7c 357
40675e7c 358int
d2729d44 359lex (void)
40675e7c 360{
abadc117 361 int c;
a44c2277 362 char *p;
40675e7c
DM
363
364 if (unlexed >= 0)
365 {
366 symval = unlexed_symval;
367 c = unlexed;
368 unlexed = -1;
36281465 369 return c;
40675e7c
DM
370 }
371
abadc117 372 c = skip_white_space ();
79282c5a
AD
373 /* for error messages (token buffer always valid) */
374 *token_buffer = c;
a44c2277 375 token_buffer[1] = 0;
40675e7c
DM
376
377 switch (c)
378 {
379 case EOF:
abadc117 380 strcpy (token_buffer, "EOF");
36281465 381 return ENDFILE;
40675e7c 382
abadc117
AD
383 case 'A': case 'B': case 'C': case 'D': case 'E':
384 case 'F': case 'G': case 'H': case 'I': case 'J':
385 case 'K': case 'L': case 'M': case 'N': case 'O':
386 case 'P': case 'Q': case 'R': case 'S': case 'T':
387 case 'U': case 'V': case 'W': case 'X': case 'Y':
40675e7c 388 case 'Z':
abadc117
AD
389 case 'a': case 'b': case 'c': case 'd': case 'e':
390 case 'f': case 'g': case 'h': case 'i': case 'j':
391 case 'k': case 'l': case 'm': case 'n': case 'o':
392 case 'p': case 'q': case 'r': case 's': case 't':
393 case 'u': case 'v': case 'w': case 'x': case 'y':
40675e7c 394 case 'z':
abadc117
AD
395 case '.': case '_':
396
40675e7c 397 p = token_buffer;
abadc117 398 while (isalnum (c) || c == '_' || c == '.')
40675e7c
DM
399 {
400 if (p == token_buffer + maxtoken)
abadc117 401 p = grow_token_buffer (p);
40675e7c
DM
402
403 *p++ = c;
abadc117 404 c = getc (finput);
40675e7c
DM
405 }
406
407 *p = 0;
abadc117
AD
408 ungetc (c, finput);
409 symval = getsym (token_buffer);
36281465 410 return IDENTIFIER;
40675e7c 411
abadc117
AD
412 case '0': case '1': case '2': case '3': case '4':
413 case '5': case '6': case '7': case '8': case '9':
40675e7c
DM
414 {
415 numval = 0;
416
a44c2277 417 p = token_buffer;
abadc117 418 while (isdigit (c))
40675e7c 419 {
a44c2277 420 if (p == token_buffer + maxtoken)
abadc117 421 p = grow_token_buffer (p);
a44c2277
RS
422
423 *p++ = c;
abadc117
AD
424 numval = numval * 10 + c - '0';
425 c = getc (finput);
40675e7c 426 }
a44c2277 427 *p = 0;
abadc117 428 ungetc (c, finput);
36281465 429 return NUMBER;
40675e7c
DM
430 }
431
432 case '\'':
40675e7c
DM
433 /* parse the literal token and compute character code in code */
434
a44c2277 435 translations = -1;
40675e7c 436 {
a44c2277
RS
437 int code, discode;
438 char discard[10], *dp;
5ce94c29 439
a44c2277
RS
440 p = token_buffer;
441 *p++ = '\'';
abadc117 442 literalchar (&p, &code, '\'');
40675e7c 443
abadc117 444 c = getc (finput);
a44c2277 445 if (c != '\'')
40675e7c 446 {
a0f6b076 447 complain (_("use \"...\" for multi-character literal tokens"));
5ce94c29
RS
448 while (1)
449 {
450 dp = discard;
abadc117 451 if (!literalchar (&dp, &discode, '\''))
5ce94c29
RS
452 break;
453 }
40675e7c 454 }
a44c2277
RS
455 *p++ = '\'';
456 *p = 0;
abadc117 457 symval = getsym (token_buffer);
d7020c20 458 symval->class = token_sym;
abadc117 459 if (!symval->user_token_number)
a44c2277 460 symval->user_token_number = code;
36281465 461 return IDENTIFIER;
a44c2277 462 }
40675e7c 463
a44c2277 464 case '\"':
a44c2277
RS
465 /* parse the literal string token and treat as an identifier */
466
467 translations = -1;
468 {
abadc117 469 int code; /* ignored here */
40675e7c 470 p = token_buffer;
a44c2277 471 *p++ = '\"';
79282c5a
AD
472 /* Read up to and including ". */
473 while (literalchar (&p, &code, '\"'))
40675e7c 474 {
a44c2277 475 if (p >= token_buffer + maxtoken - 4)
abadc117 476 p = grow_token_buffer (p);
40675e7c 477 }
40675e7c 478 *p = 0;
a44c2277 479
abadc117 480 symval = getsym (token_buffer);
d7020c20 481 symval->class = token_sym;
a44c2277 482
36281465 483 return IDENTIFIER;
40675e7c
DM
484 }
485
486 case ',':
36281465 487 return COMMA;
40675e7c
DM
488
489 case ':':
36281465 490 return COLON;
40675e7c
DM
491
492 case ';':
36281465 493 return SEMICOLON;
40675e7c
DM
494
495 case '|':
36281465 496 return BAR;
40675e7c
DM
497
498 case '{':
36281465 499 return LEFT_CURLY;
40675e7c
DM
500
501 case '=':
502 do
503 {
abadc117
AD
504 c = getc (finput);
505 if (c == '\n')
506 lineno++;
40675e7c 507 }
abadc117 508 while (c == ' ' || c == '\n' || c == '\t');
40675e7c
DM
509
510 if (c == '{')
a44c2277 511 {
abadc117 512 strcpy (token_buffer, "={");
36281465 513 return LEFT_CURLY;
a44c2277 514 }
40675e7c
DM
515 else
516 {
abadc117 517 ungetc (c, finput);
36281465 518 return ILLEGAL;
40675e7c
DM
519 }
520
521 case '<':
f282676b 522 read_type_name (finput);
36281465 523 return TYPENAME;
a083fbbf 524
40675e7c 525 case '%':
abadc117 526 return parse_percent_token ();
40675e7c
DM
527
528 default:
36281465 529 return ILLEGAL;
40675e7c
DM
530 }
531}
532
abadc117 533/* the following table dictates the action taken for the various %
89cab50d 534 directives. A set_flag value causes the named flag to be set. A
abadc117
AD
535 retval action returns the code. */
536struct percent_table_struct
a44c2277 537{
abadc117 538 const char *name;
89cab50d 539 void *set_flag;
abadc117
AD
540 int retval;
541}
542percent_table[] =
543{
544 { "token", NULL, TOKEN },
545 { "term", NULL, TOKEN },
546 { "nterm", NULL, NTERM },
547 { "type", NULL, TYPE },
548 { "guard", NULL, GUARD },
549 { "union", NULL, UNION },
550 { "expect", NULL, EXPECT },
551 { "thong", NULL, THONG },
552 { "start", NULL, START },
553 { "left", NULL, LEFT },
554 { "right", NULL, RIGHT },
555 { "nonassoc", NULL, NONASSOC },
556 { "binary", NULL, NONASSOC },
557 { "semantic_parser", NULL, SEMANTIC_PARSER },
558 { "pure_parser", NULL, PURE_PARSER },
559 { "prec", NULL, PREC },
89cab50d
AD
560 { "locations", &locations_flag, NOOP}, /* -l */
561 { "no_lines", &no_lines_flag, NOOP}, /* -l */
562 { "raw", &raw_flag, NOOP }, /* -r */
563 { "token_table", &token_table_flag, NOOP}, /* -k */
a44c2277 564#if 0
abadc117
AD
565 /* These can be utilized after main is reoganized so
566 open_files() is deferred 'til after read_declarations().
567 But %{ and %union both put information into files
568 that have to be opened before read_declarations().
a44c2277 569 */
89cab50d
AD
570 { "yacc", &yacc_flag, NOOP}, /* -y */
571 { "fixed_output_files", &yacc_flag, NOOP}, /* -y */
572 { "defines", &defines_flag, NOOP}, /* -d */
573 { "no_parser", &no_parser_flag, NOOP}, /* -n */
abadc117
AD
574 { "output_file", &spec_outfile, SETOPT}, /* -o */
575 { "file_prefix", &spec_file_prefix, SETOPT}, /* -b */
576 { "name_prefix", &spec_name_prefix, SETOPT}, /* -p */
577 /* These would be acceptable, but they do not affect processing */
89cab50d
AD
578 { "verbose", &verbose_flag, NOOP}, /* -v */
579 { "debug", &debug_flag, NOOP}, /* -t */
abadc117
AD
580/* {"help", <print usage stmt>, NOOP}, *//* -h */
581/* {"version", <print version number> , NOOP}, *//* -V */
a44c2277 582#endif
abadc117 583 { NULL, NULL, ILLEGAL}
a44c2277
RS
584};
585
586/* Parse a token which starts with %.
587 Assumes the % has already been read and discarded. */
40675e7c
DM
588
589int
d2729d44 590parse_percent_token (void)
40675e7c 591{
abadc117
AD
592 int c;
593 char *p;
594 struct percent_table_struct *tx;
40675e7c
DM
595
596 p = token_buffer;
abadc117 597 c = getc (finput);
a44c2277 598 *p++ = '%';
abadc117 599 *p++ = c; /* for error msg */
a44c2277 600 *p = 0;
40675e7c
DM
601
602 switch (c)
603 {
604 case '%':
36281465 605 return TWO_PERCENTS;
40675e7c
DM
606
607 case '{':
36281465 608 return PERCENT_LEFT_CURLY;
40675e7c
DM
609
610 case '<':
36281465 611 return LEFT;
40675e7c
DM
612
613 case '>':
36281465 614 return RIGHT;
40675e7c
DM
615
616 case '2':
36281465 617 return NONASSOC;
40675e7c
DM
618
619 case '0':
36281465 620 return TOKEN;
40675e7c
DM
621
622 case '=':
36281465 623 return PREC;
40675e7c 624 }
abadc117 625 if (!isalpha (c))
36281465 626 return ILLEGAL;
40675e7c 627
a44c2277
RS
628 p = token_buffer;
629 *p++ = '%';
abadc117 630 while (isalpha (c) || c == '_' || c == '-')
40675e7c
DM
631 {
632 if (p == token_buffer + maxtoken)
abadc117 633 p = grow_token_buffer (p);
40675e7c 634
abadc117
AD
635 if (c == '-')
636 c = '_';
40675e7c 637 *p++ = c;
abadc117 638 c = getc (finput);
40675e7c
DM
639 }
640
abadc117 641 ungetc (c, finput);
40675e7c
DM
642
643 *p = 0;
644
a44c2277
RS
645 /* table lookup % directive */
646 for (tx = percent_table; tx->name; tx++)
abadc117 647 if (strcmp (token_buffer + 1, tx->name) == 0)
a44c2277
RS
648 break;
649 if (tx->retval == SETOPT)
650 {
89cab50d 651 *((char **) (tx->set_flag)) = optarg;
a44c2277
RS
652 return NOOP;
653 }
89cab50d 654 if (tx->set_flag)
a44c2277 655 {
89cab50d 656 *((int *) (tx->set_flag)) = 1;
a44c2277
RS
657 return NOOP;
658 }
659 return tx->retval;
40675e7c 660}