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 */
425 obstack_1grow (&token_obstack
, '\'');
426 literalchar (&token_obstack
, &code
, '\'');
431 complain (_("use \"...\" for multi-character literal tokens"));
433 if (!literalchar (0, &discode
, '\''))
436 obstack_1grow (&token_obstack
, '\'');
437 obstack_1grow (&token_obstack
, '\0');
438 token_buffer
= obstack_finish (&token_obstack
);
439 symval
= getsym (token_buffer
);
440 symval
->class = token_sym
;
441 if (!symval
->user_token_number
)
442 symval
->user_token_number
= code
;
443 return tok_identifier
;
447 /* parse the literal string token and treat as an identifier */
450 int code
; /* ignored here */
452 obstack_1grow (&token_obstack
, '\"');
453 /* Read up to and including ". */
454 while (literalchar (&token_obstack
, &code
, '\"'))
456 obstack_1grow (&token_obstack
, '\0');
457 token_buffer
= obstack_finish (&token_obstack
);
459 symval
= getsym (token_buffer
);
460 symval
->class = token_sym
;
462 return tok_identifier
;
472 return tok_semicolon
;
478 return tok_left_curly
;
487 while (c
== ' ' || c
== '\n' || c
== '\t');
492 return tok_left_curly
;
501 read_type_name (finput
);
505 return parse_percent_token ();
512 /* the following table dictates the action taken for the various %
513 directives. A set_flag value causes the named flag to be set. A
514 retval action returns the code. */
515 struct percent_table_struct
522 struct percent_table_struct percent_table
[] =
524 { "token", NULL
, tok_token
},
525 { "term", NULL
, tok_token
},
526 { "nterm", NULL
, tok_nterm
},
527 { "type", NULL
, tok_type
},
528 { "guard", NULL
, tok_guard
},
529 { "union", NULL
, tok_union
},
530 { "expect", NULL
, tok_expect
},
531 { "thong", NULL
, tok_thong
},
532 { "start", NULL
, tok_start
},
533 { "left", NULL
, tok_left
},
534 { "right", NULL
, tok_right
},
535 { "nonassoc", NULL
, tok_nonassoc
},
536 { "binary", NULL
, tok_nonassoc
},
537 { "prec", NULL
, tok_prec
},
538 { "locations", &locations_flag
, tok_noop
}, /* -l */
539 { "no_lines", &no_lines_flag
, tok_noop
}, /* -l */
540 { "raw", NULL
, tok_obsolete
}, /* -r */
541 { "token_table", &token_table_flag
, tok_noop
}, /* -k */
542 { "yacc", &yacc_flag
, tok_noop
}, /* -y */
543 { "fixed_output_files",&yacc_flag
, tok_noop
}, /* -y */
544 { "defines", &defines_flag
, tok_noop
}, /* -d */
545 { "no_parser", &no_parser_flag
, tok_noop
}, /* -n */
546 { "graph", &graph_flag
, tok_noop
}, /* -g */
548 /* For the time being, this is not enabled yet, while it's possible
549 though, since we use obstacks. The only risk is with semantic
550 parsers which will output an `include' of an output file: be sure
551 that the name included is indeed the name of the output file. */
552 { "output_file", &spec_outfile
, tok_setopt
}, /* -o */
553 { "file_prefix", &spec_file_prefix
, tok_setopt
}, /* -b */
554 { "name_prefix", &spec_name_prefix
, tok_setopt
}, /* -p */
556 { "verbose", &verbose_flag
, tok_noop
}, /* -v */
557 { "debug", &debug_flag
, tok_noop
}, /* -t */
558 { "semantic_parser", &semantic_parser
, tok_noop
},
559 { "pure_parser", &pure_parser
, tok_noop
},
561 { NULL
, NULL
, tok_illegal
}
564 /* Parse a token which starts with %.
565 Assumes the % has already been read and discarded. */
568 parse_percent_token (void)
570 struct percent_table_struct
*tx
;
572 int c
= getc (finput
);
577 return tok_two_percents
;
580 return tok_percent_left_curly
;
601 obstack_1grow (&token_obstack
, '%');
602 while (isalpha (c
) || c
== '_' || c
== '-')
606 obstack_1grow (&token_obstack
, c
);
611 obstack_1grow (&token_obstack
, '\0');
612 token_buffer
= obstack_finish (&token_obstack
);
614 /* table lookup % directive */
615 for (tx
= percent_table
; tx
->name
; tx
++)
616 if (strcmp (token_buffer
+ 1, tx
->name
) == 0)
621 *((int *) (tx
->set_flag
)) = 1;
628 *((char **) (tx
->set_flag
)) = optarg
;
633 fatal (_("`%s' is no longer supported"), token_buffer
);
637 /* Other cases do not apply here. */