]> git.saurik.com Git - bison.git/blame - src/lex.c
* src/nullable.h: New file.
[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"
7612000c 27#include "alloc.h"
a0f6b076 28#include "complain.h"
40675e7c 29
a44c2277 30/*spec_outfile is declared in files.h, for -o */
40675e7c 31
40675e7c
DM
32extern int translations;
33
a44c2277 34/* functions from main.c */
abadc117 35extern char *printable_version PARAMS ((int));
40675e7c
DM
36
37/* Buffer for storing the current token. */
38char *token_buffer;
39
40/* Allocated size of token_buffer, not including space for terminator. */
d2729d44 41int maxtoken;
40675e7c
DM
42
43bucket *symval;
44int numval;
45
46static int unlexed; /* these two describe a token to be reread */
47static bucket *unlexed_symval; /* by the next call to lex */
48
49
50void
d2729d44 51init_lex (void)
40675e7c
DM
52{
53 maxtoken = 100;
54 token_buffer = NEW2 (maxtoken + 1, char);
55 unlexed = -1;
56}
57
58
d2729d44
JT
59char *
60grow_token_buffer (char *p)
40675e7c
DM
61{
62 int offset = p - token_buffer;
63 maxtoken *= 2;
abadc117 64 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 1);
40675e7c
DM
65 return token_buffer + offset;
66}
67
68
69int
d2729d44 70skip_white_space (void)
40675e7c 71{
abadc117
AD
72 int c;
73 int inside;
40675e7c 74
abadc117 75 c = getc (finput);
40675e7c
DM
76
77 for (;;)
78 {
79 int cplus_comment;
80
81 switch (c)
82 {
83 case '/':
abadc117 84 c = getc (finput);
a083fbbf 85 if (c != '*' && c != '/')
a44c2277 86 {
a0f6b076 87 complain (_("unexpected `/' found and ignored"));
a44c2277
RS
88 break;
89 }
40675e7c
DM
90 cplus_comment = (c == '/');
91
abadc117 92 c = getc (finput);
40675e7c
DM
93
94 inside = 1;
95 while (inside)
96 {
97 if (!cplus_comment && c == '*')
98 {
99 while (c == '*')
abadc117 100 c = getc (finput);
40675e7c
DM
101
102 if (c == '/')
103 {
104 inside = 0;
abadc117 105 c = getc (finput);
40675e7c
DM
106 }
107 }
108 else if (c == '\n')
109 {
110 lineno++;
111 if (cplus_comment)
112 inside = 0;
abadc117 113 c = getc (finput);
40675e7c
DM
114 }
115 else if (c == EOF)
a0f6b076 116 fatal (_("unterminated comment"));
40675e7c 117 else
abadc117 118 c = getc (finput);
40675e7c
DM
119 }
120
121 break;
122
123 case '\n':
124 lineno++;
125
126 case ' ':
127 case '\t':
128 case '\f':
abadc117 129 c = getc (finput);
40675e7c
DM
130 break;
131
132 default:
36281465 133 return c;
40675e7c
DM
134 }
135 }
136}
137
a44c2277 138/* do a getc, but give error message if EOF encountered */
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 {
a0f6b076 244 complain (_("unknown escape sequence: `\\' followed by `%s'"),
abadc117 245 printable_version (c));
a44c2277
RS
246 code = '?';
247 }
abadc117 248 } /* has \ */
a44c2277
RS
249
250 /* now fill token_buffer with the canonical name for this character
251 as a literal token. Do not use what the user typed,
252 so that `\012' and `\n' can be interchangeable. */
253
254 p = *pp;
e5335b74
JT
255 if (code == term && wasquote)
256 *p++ = code;
abadc117
AD
257 else if (code == '\\')
258 {
259 *p++ = '\\';
260 *p++ = '\\';
261 }
262 else if (code == '\'')
263 {
264 *p++ = '\\';
265 *p++ = '\'';
266 }
267 else if (code == '\"')
268 {
269 *p++ = '\\';
270 *p++ = '\"';
271 }
5ce94c29
RS
272 else if (code >= 040 && code < 0177)
273 *p++ = code;
abadc117
AD
274 else if (code == '\t')
275 {
276 *p++ = '\\';
277 *p++ = 't';
278 }
279 else if (code == '\n')
280 {
281 *p++ = '\\';
282 *p++ = 'n';
283 }
284 else if (code == '\r')
285 {
286 *p++ = '\\';
287 *p++ = 'r';
288 }
289 else if (code == '\v')
290 {
291 *p++ = '\\';
292 *p++ = 'v';
293 }
294 else if (code == '\b')
295 {
296 *p++ = '\\';
297 *p++ = 'b';
298 }
299 else if (code == '\f')
300 {
301 *p++ = '\\';
302 *p++ = 'f';
303 }
a44c2277
RS
304 else
305 {
306 *p++ = '\\';
307 *p++ = code / 0100 + '0';
308 *p++ = ((code / 010) & 07) + '0';
309 *p++ = (code & 07) + '0';
310 }
311 *pp = p;
312 *pcode = code;
abadc117 313 return !wasquote;
a44c2277
RS
314}
315
40675e7c
DM
316
317void
d2729d44 318unlex (int token)
40675e7c
DM
319{
320 unlexed = token;
321 unlexed_symval = symval;
322}
323
324
40675e7c 325int
d2729d44 326lex (void)
40675e7c 327{
abadc117 328 int c;
a44c2277 329 char *p;
40675e7c
DM
330
331 if (unlexed >= 0)
332 {
333 symval = unlexed_symval;
334 c = unlexed;
335 unlexed = -1;
36281465 336 return c;
40675e7c
DM
337 }
338
abadc117
AD
339 c = skip_white_space ();
340 *token_buffer = c; /* for error messages (token buffer always valid) */
a44c2277 341 token_buffer[1] = 0;
40675e7c
DM
342
343 switch (c)
344 {
345 case EOF:
abadc117 346 strcpy (token_buffer, "EOF");
36281465 347 return ENDFILE;
40675e7c 348
abadc117
AD
349 case 'A': case 'B': case 'C': case 'D': case 'E':
350 case 'F': case 'G': case 'H': case 'I': case 'J':
351 case 'K': case 'L': case 'M': case 'N': case 'O':
352 case 'P': case 'Q': case 'R': case 'S': case 'T':
353 case 'U': case 'V': case 'W': case 'X': case 'Y':
40675e7c 354 case 'Z':
abadc117
AD
355 case 'a': case 'b': case 'c': case 'd': case 'e':
356 case 'f': case 'g': case 'h': case 'i': case 'j':
357 case 'k': case 'l': case 'm': case 'n': case 'o':
358 case 'p': case 'q': case 'r': case 's': case 't':
359 case 'u': case 'v': case 'w': case 'x': case 'y':
40675e7c 360 case 'z':
abadc117
AD
361 case '.': case '_':
362
40675e7c 363 p = token_buffer;
abadc117 364 while (isalnum (c) || c == '_' || c == '.')
40675e7c
DM
365 {
366 if (p == token_buffer + maxtoken)
abadc117 367 p = grow_token_buffer (p);
40675e7c
DM
368
369 *p++ = c;
abadc117 370 c = getc (finput);
40675e7c
DM
371 }
372
373 *p = 0;
abadc117
AD
374 ungetc (c, finput);
375 symval = getsym (token_buffer);
36281465 376 return IDENTIFIER;
40675e7c 377
abadc117
AD
378 case '0': case '1': case '2': case '3': case '4':
379 case '5': case '6': case '7': case '8': case '9':
40675e7c
DM
380 {
381 numval = 0;
382
a44c2277 383 p = token_buffer;
abadc117 384 while (isdigit (c))
40675e7c 385 {
a44c2277 386 if (p == token_buffer + maxtoken)
abadc117 387 p = grow_token_buffer (p);
a44c2277
RS
388
389 *p++ = c;
abadc117
AD
390 numval = numval * 10 + c - '0';
391 c = getc (finput);
40675e7c 392 }
a44c2277 393 *p = 0;
abadc117 394 ungetc (c, finput);
36281465 395 return NUMBER;
40675e7c
DM
396 }
397
398 case '\'':
40675e7c
DM
399 /* parse the literal token and compute character code in code */
400
a44c2277 401 translations = -1;
40675e7c 402 {
a44c2277
RS
403 int code, discode;
404 char discard[10], *dp;
5ce94c29 405
a44c2277
RS
406 p = token_buffer;
407 *p++ = '\'';
abadc117 408 literalchar (&p, &code, '\'');
40675e7c 409
abadc117 410 c = getc (finput);
a44c2277 411 if (c != '\'')
40675e7c 412 {
a0f6b076 413 complain (_("use \"...\" for multi-character literal tokens"));
5ce94c29
RS
414 while (1)
415 {
416 dp = discard;
abadc117 417 if (!literalchar (&dp, &discode, '\''))
5ce94c29
RS
418 break;
419 }
40675e7c 420 }
a44c2277
RS
421 *p++ = '\'';
422 *p = 0;
abadc117 423 symval = getsym (token_buffer);
a44c2277 424 symval->class = STOKEN;
abadc117 425 if (!symval->user_token_number)
a44c2277 426 symval->user_token_number = code;
36281465 427 return IDENTIFIER;
a44c2277 428 }
40675e7c 429
a44c2277 430 case '\"':
a44c2277
RS
431 /* parse the literal string token and treat as an identifier */
432
433 translations = -1;
434 {
abadc117 435 int code; /* ignored here */
40675e7c 436 p = token_buffer;
a44c2277 437 *p++ = '\"';
abadc117 438 while (literalchar (&p, &code, '\"')) /* read up to and including " */
40675e7c 439 {
a44c2277 440 if (p >= token_buffer + maxtoken - 4)
abadc117 441 p = grow_token_buffer (p);
40675e7c 442 }
40675e7c 443 *p = 0;
a44c2277 444
abadc117 445 symval = getsym (token_buffer);
40675e7c 446 symval->class = STOKEN;
a44c2277 447
36281465 448 return IDENTIFIER;
40675e7c
DM
449 }
450
451 case ',':
36281465 452 return COMMA;
40675e7c
DM
453
454 case ':':
36281465 455 return COLON;
40675e7c
DM
456
457 case ';':
36281465 458 return SEMICOLON;
40675e7c
DM
459
460 case '|':
36281465 461 return BAR;
40675e7c
DM
462
463 case '{':
36281465 464 return LEFT_CURLY;
40675e7c
DM
465
466 case '=':
467 do
468 {
abadc117
AD
469 c = getc (finput);
470 if (c == '\n')
471 lineno++;
40675e7c 472 }
abadc117 473 while (c == ' ' || c == '\n' || c == '\t');
40675e7c
DM
474
475 if (c == '{')
a44c2277 476 {
abadc117 477 strcpy (token_buffer, "={");
36281465 478 return LEFT_CURLY;
a44c2277 479 }
40675e7c
DM
480 else
481 {
abadc117 482 ungetc (c, finput);
36281465 483 return ILLEGAL;
40675e7c
DM
484 }
485
486 case '<':
487 p = token_buffer;
abadc117 488 c = getc (finput);
40675e7c
DM
489 while (c != '>')
490 {
a44c2277 491 if (c == EOF)
a0f6b076 492 fatal (_("unterminated type name at end of file"));
a083fbbf 493 if (c == '\n')
a44c2277 494 {
a0f6b076 495 complain (_("unterminated type name"));
abadc117 496 ungetc (c, finput);
a44c2277
RS
497 break;
498 }
40675e7c
DM
499
500 if (p == token_buffer + maxtoken)
abadc117 501 p = grow_token_buffer (p);
40675e7c
DM
502
503 *p++ = c;
abadc117 504 c = getc (finput);
40675e7c
DM
505 }
506 *p = 0;
36281465 507 return TYPENAME;
a083fbbf 508
40675e7c
DM
509
510 case '%':
abadc117 511 return parse_percent_token ();
40675e7c
DM
512
513 default:
36281465 514 return ILLEGAL;
40675e7c
DM
515 }
516}
517
abadc117
AD
518/* the following table dictates the action taken for the various %
519 directives. A setflag value causes the named flag to be set. A
520 retval action returns the code. */
521struct percent_table_struct
a44c2277 522{
abadc117
AD
523 const char *name;
524 void *setflag;
525 int retval;
526}
527percent_table[] =
528{
529 { "token", NULL, TOKEN },
530 { "term", NULL, TOKEN },
531 { "nterm", NULL, NTERM },
532 { "type", NULL, TYPE },
533 { "guard", NULL, GUARD },
534 { "union", NULL, UNION },
535 { "expect", NULL, EXPECT },
536 { "thong", NULL, THONG },
537 { "start", NULL, START },
538 { "left", NULL, LEFT },
539 { "right", NULL, RIGHT },
540 { "nonassoc", NULL, NONASSOC },
541 { "binary", NULL, NONASSOC },
542 { "semantic_parser", NULL, SEMANTIC_PARSER },
543 { "pure_parser", NULL, PURE_PARSER },
544 { "prec", NULL, PREC },
545 { "no_lines", &nolinesflag, NOOP}, /* -l */
546 { "raw", &rawtoknumflag, NOOP }, /* -r */
547 { "token_table", &toknumflag, NOOP}, /* -k */
a44c2277 548#if 0
abadc117
AD
549 /* These can be utilized after main is reoganized so
550 open_files() is deferred 'til after read_declarations().
551 But %{ and %union both put information into files
552 that have to be opened before read_declarations().
a44c2277 553 */
abadc117
AD
554 { "yacc", &fixed_outfiles, NOOP}, /* -y */
555 { "fixed_output_files", &fixed_outfiles, NOOP}, /* -y */
556 { "defines", &definesflag, NOOP}, /* -d */
557 { "no_parser", &noparserflag, NOOP}, /* -n */
558 { "output_file", &spec_outfile, SETOPT}, /* -o */
559 { "file_prefix", &spec_file_prefix, SETOPT}, /* -b */
560 { "name_prefix", &spec_name_prefix, SETOPT}, /* -p */
561 /* These would be acceptable, but they do not affect processing */
562 { "verbose", &verboseflag, NOOP}, /* -v */
563 { "debug", &debugflag, NOOP}, /* -t */
564/* {"help", <print usage stmt>, NOOP}, *//* -h */
565/* {"version", <print version number> , NOOP}, *//* -V */
a44c2277 566#endif
abadc117 567 { NULL, NULL, ILLEGAL}
a44c2277
RS
568};
569
570/* Parse a token which starts with %.
571 Assumes the % has already been read and discarded. */
40675e7c
DM
572
573int
d2729d44 574parse_percent_token (void)
40675e7c 575{
abadc117
AD
576 int c;
577 char *p;
578 struct percent_table_struct *tx;
40675e7c
DM
579
580 p = token_buffer;
abadc117 581 c = getc (finput);
a44c2277 582 *p++ = '%';
abadc117 583 *p++ = c; /* for error msg */
a44c2277 584 *p = 0;
40675e7c
DM
585
586 switch (c)
587 {
588 case '%':
36281465 589 return TWO_PERCENTS;
40675e7c
DM
590
591 case '{':
36281465 592 return PERCENT_LEFT_CURLY;
40675e7c
DM
593
594 case '<':
36281465 595 return LEFT;
40675e7c
DM
596
597 case '>':
36281465 598 return RIGHT;
40675e7c
DM
599
600 case '2':
36281465 601 return NONASSOC;
40675e7c
DM
602
603 case '0':
36281465 604 return TOKEN;
40675e7c
DM
605
606 case '=':
36281465 607 return PREC;
40675e7c 608 }
abadc117 609 if (!isalpha (c))
36281465 610 return ILLEGAL;
40675e7c 611
a44c2277
RS
612 p = token_buffer;
613 *p++ = '%';
abadc117 614 while (isalpha (c) || c == '_' || c == '-')
40675e7c
DM
615 {
616 if (p == token_buffer + maxtoken)
abadc117 617 p = grow_token_buffer (p);
40675e7c 618
abadc117
AD
619 if (c == '-')
620 c = '_';
40675e7c 621 *p++ = c;
abadc117 622 c = getc (finput);
40675e7c
DM
623 }
624
abadc117 625 ungetc (c, finput);
40675e7c
DM
626
627 *p = 0;
628
a44c2277
RS
629 /* table lookup % directive */
630 for (tx = percent_table; tx->name; tx++)
abadc117 631 if (strcmp (token_buffer + 1, tx->name) == 0)
a44c2277
RS
632 break;
633 if (tx->retval == SETOPT)
634 {
abadc117 635 *((char **) (tx->setflag)) = optarg;
a44c2277
RS
636 return NOOP;
637 }
638 if (tx->setflag)
639 {
abadc117 640 *((int *) (tx->setflag)) = 1;
a44c2277
RS
641 return NOOP;
642 }
643 return tx->retval;
40675e7c 644}