]> git.saurik.com Git - bison.git/blame - src/lex.c
The computation of nullable is broken: it doesn't handle empty
[bison.git] / src / lex.c
CommitLineData
40675e7c 1/* Token-reader for Bison's input parser,
62ab6972 2 Copyright 1984, 1986, 1989, 1992, 2000, 2001 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
DM
23#include "files.h"
24#include "symtab.h"
82b6d266 25#include "options.h"
40675e7c 26#include "lex.h"
a0f6b076 27#include "complain.h"
b2ca4022 28#include "gram.h"
ff4a34be 29#include "quote.h"
40675e7c
DM
30
31/* Buffer for storing the current token. */
0846f581 32static struct obstack token_obstack;
09b503c8 33const char *token_buffer = NULL;
40675e7c 34
09b503c8 35bucket *symval = NULL;
40675e7c
DM
36int numval;
37
561f9a30 38/* A token to be reread, see unlex and lex. */
342b8b6e 39static token_t unlexed = tok_undef;
342b8b6e 40static bucket *unlexed_symval = NULL;
09b503c8 41static const char *unlexed_token_buffer = NULL;
40675e7c
DM
42
43void
342b8b6e 44lex_init (void)
40675e7c 45{
f17bcd1f 46 obstack_init (&token_obstack);
342b8b6e
AD
47 unlexed = tok_undef;
48}
49
50
51void
52lex_free (void)
53{
54 obstack_free (&token_obstack, NULL);
40675e7c
DM
55}
56
57
40675e7c 58int
d2729d44 59skip_white_space (void)
40675e7c 60{
abadc117
AD
61 int c;
62 int inside;
40675e7c 63
abadc117 64 c = getc (finput);
40675e7c
DM
65
66 for (;;)
67 {
68 int cplus_comment;
69
70 switch (c)
71 {
72 case '/':
79282c5a 73 /* FIXME: Should probably be merged with copy_comment. */
abadc117 74 c = getc (finput);
a083fbbf 75 if (c != '*' && c != '/')
a44c2277 76 {
a0f6b076 77 complain (_("unexpected `/' found and ignored"));
a44c2277
RS
78 break;
79 }
40675e7c
DM
80 cplus_comment = (c == '/');
81
abadc117 82 c = getc (finput);
40675e7c
DM
83
84 inside = 1;
85 while (inside)
86 {
87 if (!cplus_comment && c == '*')
88 {
89 while (c == '*')
abadc117 90 c = getc (finput);
40675e7c
DM
91
92 if (c == '/')
93 {
94 inside = 0;
abadc117 95 c = getc (finput);
40675e7c
DM
96 }
97 }
98 else if (c == '\n')
99 {
100 lineno++;
101 if (cplus_comment)
102 inside = 0;
abadc117 103 c = getc (finput);
40675e7c
DM
104 }
105 else if (c == EOF)
a0f6b076 106 fatal (_("unterminated comment"));
40675e7c 107 else
abadc117 108 c = getc (finput);
40675e7c
DM
109 }
110
111 break;
112
113 case '\n':
114 lineno++;
115
116 case ' ':
117 case '\t':
118 case '\f':
abadc117 119 c = getc (finput);
40675e7c
DM
120 break;
121
122 default:
36281465 123 return c;
40675e7c
DM
124 }
125 }
126}
127
79282c5a
AD
128
129/*-----------------------------------------------------.
130| Do a getc, but give error message if EOF encountered |
131`-----------------------------------------------------*/
132
4a120d45 133static int
abadc117 134xgetc (FILE *f)
a44c2277 135{
abadc117 136 int c = getc (f);
a44c2277 137 if (c == EOF)
a0f6b076 138 fatal (_("unexpected end of file"));
a44c2277
RS
139 return c;
140}
141
abadc117
AD
142
143/*------------------------------------------------------------------.
144| Read one literal character from finput. Process \ escapes. |
f17bcd1f 145| Append the normalized string version of the char to OUT. Assign |
abadc117 146| the character code to *PCODE. Return 1 unless the character is an |
f17bcd1f 147| unescaped `term' or \n report error for \n. |
abadc117
AD
148`------------------------------------------------------------------*/
149
f17bcd1f
AD
150/* FIXME: We could directly work in the obstack, but that would make
151 it more difficult to move to quotearg some day. So for the time
152 being, I prefer have literalchar behave like quotearg, and change
153 my mind later if I was wrong. */
154
426cf563 155int
f17bcd1f 156literalchar (struct obstack *out, int *pcode, char term)
a44c2277 157{
abadc117 158 int c;
f17bcd1f
AD
159 char buf[4096];
160 char *cp;
abadc117 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 {
b0ce6046
AD
244 char badchar [] = "c";
245 badchar[0] = c;
a0f6b076 246 complain (_("unknown escape sequence: `\\' followed by `%s'"),
b0ce6046 247 quote (badchar));
a44c2277
RS
248 code = '?';
249 }
abadc117 250 } /* has \ */
a44c2277 251
f17bcd1f
AD
252 /* now fill BUF with the canonical name for this character as a
253 literal token. Do not use what the user typed, so that `\012'
254 and `\n' can be interchangeable. */
a44c2277 255
f17bcd1f 256 cp = buf;
e5335b74 257 if (code == term && wasquote)
f17bcd1f 258 *cp++ = code;
abadc117
AD
259 else if (code == '\\')
260 {
f17bcd1f
AD
261 *cp++ = '\\';
262 *cp++ = '\\';
abadc117
AD
263 }
264 else if (code == '\'')
265 {
f17bcd1f
AD
266 *cp++ = '\\';
267 *cp++ = '\'';
abadc117
AD
268 }
269 else if (code == '\"')
270 {
f17bcd1f
AD
271 *cp++ = '\\';
272 *cp++ = '\"';
abadc117 273 }
5ce94c29 274 else if (code >= 040 && code < 0177)
f17bcd1f 275 *cp++ = code;
abadc117
AD
276 else if (code == '\t')
277 {
f17bcd1f
AD
278 *cp++ = '\\';
279 *cp++ = 't';
abadc117
AD
280 }
281 else if (code == '\n')
282 {
f17bcd1f
AD
283 *cp++ = '\\';
284 *cp++ = 'n';
abadc117
AD
285 }
286 else if (code == '\r')
287 {
f17bcd1f
AD
288 *cp++ = '\\';
289 *cp++ = 'r';
abadc117
AD
290 }
291 else if (code == '\v')
292 {
f17bcd1f
AD
293 *cp++ = '\\';
294 *cp++ = 'v';
abadc117
AD
295 }
296 else if (code == '\b')
297 {
f17bcd1f
AD
298 *cp++ = '\\';
299 *cp++ = 'b';
abadc117
AD
300 }
301 else if (code == '\f')
302 {
f17bcd1f
AD
303 *cp++ = '\\';
304 *cp++ = 'f';
abadc117 305 }
a44c2277
RS
306 else
307 {
f17bcd1f
AD
308 *cp++ = '\\';
309 *cp++ = code / 0100 + '0';
310 *cp++ = ((code / 010) & 07) + '0';
311 *cp++ = (code & 07) + '0';
a44c2277 312 }
f17bcd1f
AD
313 *cp = '\0';
314
315 if (out)
316 obstack_sgrow (out, buf);
a44c2277 317 *pcode = code;
abadc117 318 return !wasquote;
a44c2277
RS
319}
320
40675e7c
DM
321
322void
342b8b6e 323unlex (token_t token)
40675e7c
DM
324{
325 unlexed = token;
561f9a30 326 unlexed_token_buffer = token_buffer;
40675e7c
DM
327 unlexed_symval = symval;
328}
329
f282676b
AD
330/*-----------------------------------------------------------------.
331| We just read `<' from FIN. Store in TOKEN_BUFFER, the type name |
332| specified between the `<...>'. |
333`-----------------------------------------------------------------*/
334
335void
336read_type_name (FILE *fin)
337{
f282676b
AD
338 int c = getc (fin);
339
340 while (c != '>')
341 {
342 if (c == EOF)
343 fatal (_("unterminated type name at end of file"));
344 if (c == '\n')
345 {
346 complain (_("unterminated type name"));
347 ungetc (c, fin);
348 break;
349 }
350
f17bcd1f 351 obstack_1grow (&token_obstack, c);
f282676b
AD
352 c = getc (fin);
353 }
f17bcd1f
AD
354 obstack_1grow (&token_obstack, '\0');
355 token_buffer = obstack_finish (&token_obstack);
f282676b
AD
356}
357
40675e7c 358
511e79b3 359token_t
d2729d44 360lex (void)
40675e7c 361{
abadc117 362 int c;
f17bcd1f
AD
363
364 /* Just to make sure. */
365 token_buffer = NULL;
40675e7c 366
342b8b6e 367 if (unlexed != tok_undef)
40675e7c 368 {
342b8b6e 369 token_t res = unlexed;
40675e7c 370 symval = unlexed_symval;
561f9a30 371 token_buffer = unlexed_token_buffer;
342b8b6e
AD
372 unlexed = tok_undef;
373 return res;
40675e7c
DM
374 }
375
abadc117 376 c = skip_white_space ();
40675e7c
DM
377
378 switch (c)
379 {
380 case EOF:
f17bcd1f 381 token_buffer = "EOF";
511e79b3 382 return tok_eof;
40675e7c 383
abadc117
AD
384 case 'A': case 'B': case 'C': case 'D': case 'E':
385 case 'F': case 'G': case 'H': case 'I': case 'J':
386 case 'K': case 'L': case 'M': case 'N': case 'O':
387 case 'P': case 'Q': case 'R': case 'S': case 'T':
388 case 'U': case 'V': case 'W': case 'X': case 'Y':
40675e7c 389 case 'Z':
abadc117
AD
390 case 'a': case 'b': case 'c': case 'd': case 'e':
391 case 'f': case 'g': case 'h': case 'i': case 'j':
392 case 'k': case 'l': case 'm': case 'n': case 'o':
393 case 'p': case 'q': case 'r': case 's': case 't':
394 case 'u': case 'v': case 'w': case 'x': case 'y':
40675e7c 395 case 'z':
abadc117
AD
396 case '.': case '_':
397
abadc117 398 while (isalnum (c) || c == '_' || c == '.')
40675e7c 399 {
f17bcd1f 400 obstack_1grow (&token_obstack, c);
abadc117 401 c = getc (finput);
40675e7c 402 }
f17bcd1f
AD
403 obstack_1grow (&token_obstack, '\0');
404 token_buffer = obstack_finish (&token_obstack);
abadc117
AD
405 ungetc (c, finput);
406 symval = getsym (token_buffer);
511e79b3 407 return tok_identifier;
40675e7c 408
abadc117
AD
409 case '0': case '1': case '2': case '3': case '4':
410 case '5': case '6': case '7': case '8': case '9':
40675e7c
DM
411 {
412 numval = 0;
413
abadc117 414 while (isdigit (c))
40675e7c 415 {
f17bcd1f 416 obstack_1grow (&token_obstack, c);
abadc117
AD
417 numval = numval * 10 + c - '0';
418 c = getc (finput);
40675e7c 419 }
f17bcd1f
AD
420 obstack_1grow (&token_obstack, '\0');
421 token_buffer = obstack_finish (&token_obstack);
abadc117 422 ungetc (c, finput);
511e79b3 423 return tok_number;
40675e7c
DM
424 }
425
426 case '\'':
40675e7c
DM
427 /* parse the literal token and compute character code in code */
428
40675e7c 429 {
951366c1 430 int code;
5ce94c29 431
f17bcd1f
AD
432 obstack_1grow (&token_obstack, '\'');
433 literalchar (&token_obstack, &code, '\'');
40675e7c 434
abadc117 435 c = getc (finput);
a44c2277 436 if (c != '\'')
40675e7c 437 {
951366c1 438 int discode;
a0f6b076 439 complain (_("use \"...\" for multi-character literal tokens"));
5ce94c29 440 while (1)
f17bcd1f
AD
441 if (!literalchar (0, &discode, '\''))
442 break;
40675e7c 443 }
f17bcd1f
AD
444 obstack_1grow (&token_obstack, '\'');
445 obstack_1grow (&token_obstack, '\0');
446 token_buffer = obstack_finish (&token_obstack);
abadc117 447 symval = getsym (token_buffer);
d7020c20 448 symval->class = token_sym;
6b7e85b9 449 if (symval->user_token_number == SUNDEF)
a44c2277 450 symval->user_token_number = code;
511e79b3 451 return tok_identifier;
a44c2277 452 }
40675e7c 453
a44c2277 454 case '\"':
a44c2277
RS
455 /* parse the literal string token and treat as an identifier */
456
a44c2277 457 {
abadc117 458 int code; /* ignored here */
f17bcd1f
AD
459
460 obstack_1grow (&token_obstack, '\"');
79282c5a 461 /* Read up to and including ". */
f17bcd1f
AD
462 while (literalchar (&token_obstack, &code, '\"'))
463 /* nothing */;
464 obstack_1grow (&token_obstack, '\0');
465 token_buffer = obstack_finish (&token_obstack);
a44c2277 466
abadc117 467 symval = getsym (token_buffer);
d7020c20 468 symval->class = token_sym;
a44c2277 469
511e79b3 470 return tok_identifier;
40675e7c
DM
471 }
472
473 case ',':
342b8b6e 474 token_buffer = ",";
511e79b3 475 return tok_comma;
40675e7c
DM
476
477 case ':':
342b8b6e 478 token_buffer = ":";
511e79b3 479 return tok_colon;
40675e7c
DM
480
481 case ';':
342b8b6e 482 token_buffer = ";";
511e79b3 483 return tok_semicolon;
40675e7c
DM
484
485 case '|':
342b8b6e 486 token_buffer = "|";
511e79b3 487 return tok_bar;
40675e7c
DM
488
489 case '{':
342b8b6e 490 token_buffer = "{";
511e79b3 491 return tok_left_curly;
40675e7c
DM
492
493 case '=':
342b8b6e 494 obstack_1grow (&token_obstack, c);
40675e7c
DM
495 do
496 {
abadc117 497 c = getc (finput);
342b8b6e 498 obstack_1grow (&token_obstack, c);
abadc117
AD
499 if (c == '\n')
500 lineno++;
40675e7c 501 }
abadc117 502 while (c == ' ' || c == '\n' || c == '\t');
342b8b6e
AD
503 obstack_1grow (&token_obstack, '\0');
504 token_buffer = obstack_finish (&token_obstack);
40675e7c
DM
505
506 if (c == '{')
a44c2277 507 {
511e79b3 508 return tok_left_curly;
a44c2277 509 }
40675e7c
DM
510 else
511 {
abadc117 512 ungetc (c, finput);
511e79b3 513 return tok_illegal;
40675e7c
DM
514 }
515
516 case '<':
f282676b 517 read_type_name (finput);
511e79b3 518 return tok_typename;
a083fbbf 519
40675e7c 520 case '%':
abadc117 521 return parse_percent_token ();
40675e7c
DM
522
523 default:
342b8b6e
AD
524 obstack_1grow (&token_obstack, c);
525 obstack_1grow (&token_obstack, '\0');
526 token_buffer = obstack_finish (&token_obstack);
511e79b3 527 return tok_illegal;
40675e7c
DM
528 }
529}
530
82b6d266
PB
531/* This function is a strcmp, which doesn't differentiate `-' and `_'
532 chars. */
6deb4447 533
82b6d266
PB
534static int
535option_strcmp (const char *left, const char *right)
abadc117 536{
342b8b6e
AD
537 const unsigned char *l, *r;
538 int c;
539
540 assert (left);
541 assert (right);
542 l = (const unsigned char *)left;
543 r = (const unsigned char *)right;
544 while (((c = *l - *r++) == 0 && *l != '\0')
545 || ((*l == '-' || *l == '_') && (*r == '_' || *r == '-')))
546 l++;
547 return c;
82b6d266 548}
a44c2277
RS
549
550/* Parse a token which starts with %.
551 Assumes the % has already been read and discarded. */
40675e7c 552
342b8b6e 553token_t
d2729d44 554parse_percent_token (void)
40675e7c 555{
951366c1 556 const struct option_table_struct *tx = NULL;
09b503c8
AD
557 const char *arg = NULL;
558 /* Where the ARG was found in token_buffer. */
559 size_t arg_offset = 0;
40675e7c 560
342b8b6e 561 int c = getc (finput);
40675e7c
DM
562
563 switch (c)
564 {
565 case '%':
511e79b3 566 return tok_two_percents;
40675e7c
DM
567
568 case '{':
511e79b3 569 return tok_percent_left_curly;
40675e7c 570
951366c1
AD
571 /* FIXME: Who the heck are those 5 guys!?! `%<' = `%left'!!!
572 Let's ask for there removal. */
40675e7c 573 case '<':
511e79b3 574 return tok_left;
40675e7c
DM
575
576 case '>':
511e79b3 577 return tok_right;
40675e7c
DM
578
579 case '2':
511e79b3 580 return tok_nonassoc;
40675e7c
DM
581
582 case '0':
511e79b3 583 return tok_token;
40675e7c
DM
584
585 case '=':
511e79b3 586 return tok_prec;
40675e7c 587 }
f17bcd1f 588
abadc117 589 if (!isalpha (c))
511e79b3 590 return tok_illegal;
40675e7c 591
f17bcd1f 592 obstack_1grow (&token_obstack, '%');
abadc117 593 while (isalpha (c) || c == '_' || c == '-')
40675e7c 594 {
6bc35ae5
MA
595 if (c == '_')
596 c = '-';
f17bcd1f 597 obstack_1grow (&token_obstack, c);
abadc117 598 c = getc (finput);
40675e7c
DM
599 }
600
09b503c8
AD
601 /* %DIRECTIVE="ARG". Separate into
602 TOKEN_BUFFER = `%DIRECTIVE\0ARG\0'.
603 This is a bit hackish, but once we move to a Bison parser,
604 things will be cleaned up. */
951366c1
AD
605 if (c == '=')
606 {
09b503c8
AD
607 /* End of the directive. We skip the `='. */
608 obstack_1grow (&token_obstack, '\0');
609 /* Fetch the ARG if present. */
951366c1 610 c = getc (finput);
09b503c8 611 if (c == '"')
951366c1 612 {
09b503c8
AD
613 int code;
614 arg_offset = obstack_object_size (&token_obstack);
615 /* Read up to and including `"'. Do not append the closing
616 `"' in the output: it's not part of the ARG. */
617 while (literalchar (NULL, &code, '"'))
618 obstack_1grow (&token_obstack, code);
951366c1 619 }
09b503c8 620 /* else: should be an error. */
951366c1
AD
621 }
622 else
623 ungetc (c, finput);
624
f17bcd1f
AD
625 obstack_1grow (&token_obstack, '\0');
626 token_buffer = obstack_finish (&token_obstack);
09b503c8
AD
627 if (arg_offset)
628 arg = token_buffer + arg_offset;
40675e7c 629
a44c2277 630 /* table lookup % directive */
82b6d266
PB
631 for (tx = option_table; tx->name; tx++)
632 if ((tx->access == opt_percent || tx->access == opt_both)
633 && option_strcmp (token_buffer + 1, tx->name) == 0)
a44c2277 634 break;
6deb4447 635
65be0866
AD
636 if (arg && tx->ret_val != tok_stropt)
637 fatal (_("`%s' supports no argument: %s"), token_buffer, quote (arg));
951366c1 638
62ab6972 639
82b6d266 640 switch (tx->ret_val)
a44c2277 641 {
951366c1
AD
642 case tok_stropt:
643 assert (tx->set_flag);
644 if (arg)
645 {
646 /* Keep only the first assignment: command line options have
647 already been processed, and we want them to have
648 precedence. Side effect: if this %-option is used
649 several times, only the first is honored. Bah. */
650 if (!*((char **) (tx->set_flag)))
09b503c8 651 *((char **) (tx->set_flag)) = xstrdup (arg);
951366c1
AD
652 }
653 else
654 fatal (_("`%s' requires an argument"), token_buffer);
655 return tok_noop;
656 break;
657
658 case tok_intopt:
659 assert (tx->set_flag);
660 *((int *) (tx->set_flag)) = 1;
511e79b3 661 return tok_noop;
62ab6972
AD
662 break;
663
664 case tok_obsolete:
665 fatal (_("`%s' is no longer supported"), token_buffer);
951366c1 666 return tok_noop;
62ab6972 667 break;
342b8b6e
AD
668
669 default:
951366c1 670 return tx->ret_val;
342b8b6e 671 break;
a44c2277 672 }
951366c1 673 abort ();
40675e7c 674}