1 /* Token-reader for Bison's input parser,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
24 #include "getopt.h" /* for optarg */
32 /* Buffer for storing the current token. */
33 struct obstack token_obstack
;
34 const char *token_buffer
= NULL
;
39 /* these two describe a token to be reread */
40 static token_t unlexed
= tok_undef
;
41 /* by the next call to lex */
42 static bucket
*unlexed_symval
= NULL
;
48 obstack_init (&token_obstack
);
54 skip_white_space (void)
68 /* FIXME: Should probably be merged with copy_comment. */
70 if (c
!= '*' && c
!= '/')
72 complain (_("unexpected `/' found and ignored"));
75 cplus_comment
= (c
== '/');
82 if (!cplus_comment
&& c
== '*')
101 fatal (_("unterminated comment"));
124 /*-----------------------------------------------------.
125 | Do a getc, but give error message if EOF encountered |
126 `-----------------------------------------------------*/
133 fatal (_("unexpected end of file"));
138 /*------------------------------------------------------------------.
139 | Read one literal character from finput. Process \ escapes. |
140 | Append the normalized string version of the char to OUT. Assign |
141 | the character code to *PCODE. Return 1 unless the character is an |
142 | unescaped `term' or \n report error for \n. |
143 `------------------------------------------------------------------*/
145 /* FIXME: We could directly work in the obstack, but that would make
146 it more difficult to move to quotearg some day. So for the time
147 being, I prefer have literalchar behave like quotearg, and change
148 my mind later if I was wrong. */
151 literalchar (struct obstack
*out
, int *pcode
, char term
)
162 complain (_("unescaped newline in constant"));
196 else if (c
<= '7' && c
>= '0')
199 while (c
<= '7' && c
>= '0')
201 code
= (code
* 8) + (c
- '0');
202 if (code
>= 256 || code
< 0)
204 complain (_("octal value outside range 0...255: `\\%o'"),
219 if (c
>= '0' && c
<= '9')
220 code
*= 16, code
+= c
- '0';
221 else if (c
>= 'a' && c
<= 'f')
222 code
*= 16, code
+= c
- 'a' + 10;
223 else if (c
>= 'A' && c
<= 'F')
224 code
*= 16, code
+= c
- 'A' + 10;
227 if (code
>= 256 || code
< 0)
229 complain (_("hexadecimal value above 255: `\\x%x'"), code
);
239 char badchar
[] = "c";
241 complain (_("unknown escape sequence: `\\' followed by `%s'"),
247 /* now fill BUF with the canonical name for this character as a
248 literal token. Do not use what the user typed, so that `\012'
249 and `\n' can be interchangeable. */
252 if (code
== term
&& wasquote
)
254 else if (code
== '\\')
259 else if (code
== '\'')
264 else if (code
== '\"')
269 else if (code
>= 040 && code
< 0177)
271 else if (code
== '\t')
276 else if (code
== '\n')
281 else if (code
== '\r')
286 else if (code
== '\v')
291 else if (code
== '\b')
296 else if (code
== '\f')
304 *cp
++ = code
/ 0100 + '0';
305 *cp
++ = ((code
/ 010) & 07) + '0';
306 *cp
++ = (code
& 07) + '0';
311 obstack_sgrow (out
, buf
);
321 unlexed_symval
= symval
;
324 /*-----------------------------------------------------------------.
325 | We just read `<' from FIN. Store in TOKEN_BUFFER, the type name |
326 | specified between the `<...>'. |
327 `-----------------------------------------------------------------*/
330 read_type_name (FILE *fin
)
337 fatal (_("unterminated type name at end of file"));
340 complain (_("unterminated type name"));
345 obstack_1grow (&token_obstack
, c
);
348 obstack_1grow (&token_obstack
, '\0');
349 token_buffer
= obstack_finish (&token_obstack
);
358 /* Just to make sure. */
361 if (unlexed
!= tok_undef
)
363 token_t res
= unlexed
;
364 symval
= unlexed_symval
;
369 c
= skip_white_space ();
374 token_buffer
= "EOF";
377 case 'A': case 'B': case 'C': case 'D': case 'E':
378 case 'F': case 'G': case 'H': case 'I': case 'J':
379 case 'K': case 'L': case 'M': case 'N': case 'O':
380 case 'P': case 'Q': case 'R': case 'S': case 'T':
381 case 'U': case 'V': case 'W': case 'X': case 'Y':
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':
391 while (isalnum (c
) || c
== '_' || c
== '.')
393 obstack_1grow (&token_obstack
, c
);
396 obstack_1grow (&token_obstack
, '\0');
397 token_buffer
= obstack_finish (&token_obstack
);
399 symval
= getsym (token_buffer
);
400 return tok_identifier
;
402 case '0': case '1': case '2': case '3': case '4':
403 case '5': case '6': case '7': case '8': case '9':
409 obstack_1grow (&token_obstack
, c
);
410 numval
= numval
* 10 + c
- '0';
413 obstack_1grow (&token_obstack
, '\0');
414 token_buffer
= obstack_finish (&token_obstack
);
420 /* parse the literal token and compute character code in code */
426 obstack_1grow (&token_obstack
, '\'');
427 literalchar (&token_obstack
, &code
, '\'');
432 complain (_("use \"...\" for multi-character literal tokens"));
434 if (!literalchar (0, &discode
, '\''))
437 obstack_1grow (&token_obstack
, '\'');
438 obstack_1grow (&token_obstack
, '\0');
439 token_buffer
= obstack_finish (&token_obstack
);
440 symval
= getsym (token_buffer
);
441 symval
->class = token_sym
;
442 if (!symval
->user_token_number
)
443 symval
->user_token_number
= code
;
444 return tok_identifier
;
448 /* parse the literal string token and treat as an identifier */
452 int code
; /* ignored here */
454 obstack_1grow (&token_obstack
, '\"');
455 /* Read up to and including ". */
456 while (literalchar (&token_obstack
, &code
, '\"'))
458 obstack_1grow (&token_obstack
, '\0');
459 token_buffer
= obstack_finish (&token_obstack
);
461 symval
= getsym (token_buffer
);
462 symval
->class = token_sym
;
464 return tok_identifier
;
474 return tok_semicolon
;
480 return tok_left_curly
;
489 while (c
== ' ' || c
== '\n' || c
== '\t');
494 return tok_left_curly
;
503 read_type_name (finput
);
507 return parse_percent_token ();
514 /* the following table dictates the action taken for the various %
515 directives. A set_flag value causes the named flag to be set. A
516 retval action returns the code. */
517 struct percent_table_struct
524 struct percent_table_struct percent_table
[] =
526 { "token", NULL
, tok_token
},
527 { "term", NULL
, tok_token
},
528 { "nterm", NULL
, tok_nterm
},
529 { "type", NULL
, tok_type
},
530 { "guard", NULL
, tok_guard
},
531 { "union", NULL
, tok_union
},
532 { "expect", NULL
, tok_expect
},
533 { "thong", NULL
, tok_thong
},
534 { "start", NULL
, tok_start
},
535 { "left", NULL
, tok_left
},
536 { "right", NULL
, tok_right
},
537 { "nonassoc", NULL
, tok_nonassoc
},
538 { "binary", NULL
, tok_nonassoc
},
539 { "prec", NULL
, tok_prec
},
540 { "locations", &locations_flag
, tok_noop
}, /* -l */
541 { "no_lines", &no_lines_flag
, tok_noop
}, /* -l */
542 { "raw", NULL
, tok_obsolete
}, /* -r */
543 { "token_table", &token_table_flag
, tok_noop
}, /* -k */
544 { "yacc", &yacc_flag
, tok_noop
}, /* -y */
545 { "fixed_output_files",&yacc_flag
, tok_noop
}, /* -y */
546 { "defines", &defines_flag
, tok_noop
}, /* -d */
547 { "no_parser", &no_parser_flag
, tok_noop
}, /* -n */
548 { "graph", &graph_flag
, tok_noop
}, /* -g */
550 /* For the time being, this is not enabled yet, while it's possible
551 though, since we use obstacks. The only risk is with semantic
552 parsers which will output an `include' of an output file: be sure
553 that the name included is indeed the name of the output file. */
554 { "output_file", &spec_outfile
, tok_setopt
}, /* -o */
555 { "file_prefix", &spec_file_prefix
, tok_setopt
}, /* -b */
556 { "name_prefix", &spec_name_prefix
, tok_setopt
}, /* -p */
558 { "header_extension", NULL
, tok_hdrext
},
559 { "source_extension", NULL
, tok_srcext
},
560 { "verbose", &verbose_flag
, tok_noop
}, /* -v */
561 { "debug", &debug_flag
, tok_noop
}, /* -t */
562 { "semantic_parser", &semantic_parser
, tok_noop
},
563 { "pure_parser", &pure_parser
, tok_noop
},
565 { NULL
, NULL
, tok_illegal
}
568 /* Parse a token which starts with %.
569 Assumes the % has already been read and discarded. */
572 parse_percent_token (void)
574 struct percent_table_struct
*tx
;
576 int c
= getc (finput
);
581 return tok_two_percents
;
584 return tok_percent_left_curly
;
605 obstack_1grow (&token_obstack
, '%');
606 while (isalpha (c
) || c
== '_' || c
== '-')
610 obstack_1grow (&token_obstack
, c
);
615 obstack_1grow (&token_obstack
, '\0');
616 token_buffer
= obstack_finish (&token_obstack
);
618 /* table lookup % directive */
619 for (tx
= percent_table
; tx
->name
; tx
++)
620 if (strcmp (token_buffer
+ 1, tx
->name
) == 0)
625 *((int *) (tx
->set_flag
)) = 1;
632 *((char **) (tx
->set_flag
)) = optarg
;
637 fatal (_("`%s' is no longer supported"), token_buffer
);
641 /* Other cases do not apply here. */