]>
git.saurik.com Git - bison.git/blob - src/lex.c
1 /* Token-reader for Bison's input parser,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000 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. */
35 /* Allocated size of token_buffer, not including space for terminator. */
41 static int unlexed
; /* these two describe a token to be reread */
42 static bucket
*unlexed_symval
; /* by the next call to lex */
49 token_buffer
= XCALLOC (char, maxtoken
+ 1);
55 grow_token_buffer (char *p
)
57 int offset
= p
- token_buffer
;
59 token_buffer
= XREALLOC (token_buffer
, char, maxtoken
+ 1);
60 return token_buffer
+ offset
;
65 skip_white_space (void)
80 if (c
!= '*' && c
!= '/')
82 complain (_("unexpected `/' found and ignored"));
85 cplus_comment
= (c
== '/');
92 if (!cplus_comment
&& c
== '*')
111 fatal (_("unterminated comment"));
133 /* do a getc, but give error message if EOF encountered */
139 fatal (_("unexpected end of file"));
144 /*------------------------------------------------------------------.
145 | Read one literal character from finput. Process \ escapes. |
146 | Append the normalized string version of the char to *PP. Assign |
147 | the character code to *PCODE. Return 1 unless the character is an |
148 | unescaped `term' or \n report error for \n |
149 `------------------------------------------------------------------*/
152 literalchar (char **pp
, 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
);
241 complain (_("unknown escape sequence: `\\' followed by `%s'"),
247 /* now fill token_buffer with the canonical name for this character
248 as a literal token. Do not use what the user typed,
249 so that `\012' 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 *p
++ = code
/ 0100 + '0';
305 *p
++ = ((code
/ 010) & 07) + '0';
306 *p
++ = (code
& 07) + '0';
318 unlexed_symval
= symval
;
330 symval
= unlexed_symval
;
336 c
= skip_white_space ();
337 *token_buffer
= c
; /* for error messages (token buffer always valid) */
343 strcpy (token_buffer
, "EOF");
346 case 'A': case 'B': case 'C': case 'D': case 'E':
347 case 'F': case 'G': case 'H': case 'I': case 'J':
348 case 'K': case 'L': case 'M': case 'N': case 'O':
349 case 'P': case 'Q': case 'R': case 'S': case 'T':
350 case 'U': case 'V': case 'W': case 'X': case 'Y':
352 case 'a': case 'b': case 'c': case 'd': case 'e':
353 case 'f': case 'g': case 'h': case 'i': case 'j':
354 case 'k': case 'l': case 'm': case 'n': case 'o':
355 case 'p': case 'q': case 'r': case 's': case 't':
356 case 'u': case 'v': case 'w': case 'x': case 'y':
361 while (isalnum (c
) || c
== '_' || c
== '.')
363 if (p
== token_buffer
+ maxtoken
)
364 p
= grow_token_buffer (p
);
372 symval
= getsym (token_buffer
);
375 case '0': case '1': case '2': case '3': case '4':
376 case '5': case '6': case '7': case '8': case '9':
383 if (p
== token_buffer
+ maxtoken
)
384 p
= grow_token_buffer (p
);
387 numval
= numval
* 10 + c
- '0';
396 /* parse the literal token and compute character code in code */
401 char discard
[10], *dp
;
405 literalchar (&p
, &code
, '\'');
410 complain (_("use \"...\" for multi-character literal tokens"));
414 if (!literalchar (&dp
, &discode
, '\''))
420 symval
= getsym (token_buffer
);
421 symval
->class = token_sym
;
422 if (!symval
->user_token_number
)
423 symval
->user_token_number
= code
;
428 /* parse the literal string token and treat as an identifier */
432 int code
; /* ignored here */
435 while (literalchar (&p
, &code
, '\"')) /* read up to and including " */
437 if (p
>= token_buffer
+ maxtoken
- 4)
438 p
= grow_token_buffer (p
);
442 symval
= getsym (token_buffer
);
443 symval
->class = token_sym
;
470 while (c
== ' ' || c
== '\n' || c
== '\t');
474 strcpy (token_buffer
, "={");
489 fatal (_("unterminated type name at end of file"));
492 complain (_("unterminated type name"));
497 if (p
== token_buffer
+ maxtoken
)
498 p
= grow_token_buffer (p
);
508 return parse_percent_token ();
515 /* the following table dictates the action taken for the various %
516 directives. A set_flag value causes the named flag to be set. A
517 retval action returns the code. */
518 struct percent_table_struct
526 { "token", NULL
, TOKEN
},
527 { "term", NULL
, TOKEN
},
528 { "nterm", NULL
, NTERM
},
529 { "type", NULL
, TYPE
},
530 { "guard", NULL
, GUARD
},
531 { "union", NULL
, UNION
},
532 { "expect", NULL
, EXPECT
},
533 { "thong", NULL
, THONG
},
534 { "start", NULL
, START
},
535 { "left", NULL
, LEFT
},
536 { "right", NULL
, RIGHT
},
537 { "nonassoc", NULL
, NONASSOC
},
538 { "binary", NULL
, NONASSOC
},
539 { "semantic_parser", NULL
, SEMANTIC_PARSER
},
540 { "pure_parser", NULL
, PURE_PARSER
},
541 { "prec", NULL
, PREC
},
542 { "locations", &locations_flag
, NOOP
}, /* -l */
543 { "no_lines", &no_lines_flag
, NOOP
}, /* -l */
544 { "raw", &raw_flag
, NOOP
}, /* -r */
545 { "token_table", &token_table_flag
, NOOP
}, /* -k */
547 /* These can be utilized after main is reoganized so
548 open_files() is deferred 'til after read_declarations().
549 But %{ and %union both put information into files
550 that have to be opened before read_declarations().
552 { "yacc", &yacc_flag
, NOOP
}, /* -y */
553 { "fixed_output_files", &yacc_flag
, NOOP
}, /* -y */
554 { "defines", &defines_flag
, NOOP
}, /* -d */
555 { "no_parser", &no_parser_flag
, NOOP
}, /* -n */
556 { "output_file", &spec_outfile
, SETOPT
}, /* -o */
557 { "file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
558 { "name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
559 /* These would be acceptable, but they do not affect processing */
560 { "verbose", &verbose_flag
, NOOP
}, /* -v */
561 { "debug", &debug_flag
, NOOP
}, /* -t */
562 /* {"help", <print usage stmt>, NOOP}, *//* -h */
563 /* {"version", <print version number> , NOOP}, *//* -V */
565 { NULL
, NULL
, ILLEGAL
}
568 /* Parse a token which starts with %.
569 Assumes the % has already been read and discarded. */
572 parse_percent_token (void)
576 struct percent_table_struct
*tx
;
581 *p
++ = c
; /* for error msg */
590 return PERCENT_LEFT_CURLY
;
612 while (isalpha (c
) || c
== '_' || c
== '-')
614 if (p
== token_buffer
+ maxtoken
)
615 p
= grow_token_buffer (p
);
627 /* table lookup % directive */
628 for (tx
= percent_table
; tx
->name
; tx
++)
629 if (strcmp (token_buffer
+ 1, tx
->name
) == 0)
631 if (tx
->retval
== SETOPT
)
633 *((char **) (tx
->set_flag
)) = optarg
;
638 *((int *) (tx
->set_flag
)) = 1;