]>
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)
79 /* FIXME: Should probably be merged with copy_comment. */
81 if (c
!= '*' && c
!= '/')
83 complain (_("unexpected `/' found and ignored"));
86 cplus_comment
= (c
== '/');
93 if (!cplus_comment
&& c
== '*')
112 fatal (_("unterminated comment"));
135 /*-----------------------------------------------------.
136 | Do a getc, but give error message if EOF encountered |
137 `-----------------------------------------------------*/
144 fatal (_("unexpected end of file"));
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 `------------------------------------------------------------------*/
157 literalchar (char **pp
, int *pcode
, char term
)
167 complain (_("unescaped newline in constant"));
201 else if (c
<= '7' && c
>= '0')
204 while (c
<= '7' && c
>= '0')
206 code
= (code
* 8) + (c
- '0');
207 if (code
>= 256 || code
< 0)
209 complain (_("octal value outside range 0...255: `\\%o'"),
224 if (c
>= '0' && c
<= '9')
225 code
*= 16, code
+= c
- '0';
226 else if (c
>= 'a' && c
<= 'f')
227 code
*= 16, code
+= c
- 'a' + 10;
228 else if (c
>= 'A' && c
<= 'F')
229 code
*= 16, code
+= c
- 'A' + 10;
232 if (code
>= 256 || code
< 0)
234 complain (_("hexadecimal value above 255: `\\x%x'"), code
);
246 complain (_("unknown escape sequence: `\\' followed by `%s'"),
252 /* now fill token_buffer with the canonical name for this character
253 as a literal token. Do not use what the user typed,
254 so that `\012' and `\n' can be interchangeable. */
257 if (code
== term
&& wasquote
)
259 else if (code
== '\\')
264 else if (code
== '\'')
269 else if (code
== '\"')
274 else if (code
>= 040 && code
< 0177)
276 else if (code
== '\t')
281 else if (code
== '\n')
286 else if (code
== '\r')
291 else if (code
== '\v')
296 else if (code
== '\b')
301 else if (code
== '\f')
309 *p
++ = code
/ 0100 + '0';
310 *p
++ = ((code
/ 010) & 07) + '0';
311 *p
++ = (code
& 07) + '0';
323 unlexed_symval
= symval
;
335 symval
= unlexed_symval
;
341 c
= skip_white_space ();
342 /* for error messages (token buffer always valid) */
349 strcpy (token_buffer
, "EOF");
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':
358 case 'a': case 'b': case 'c': case 'd': case 'e':
359 case 'f': case 'g': case 'h': case 'i': case 'j':
360 case 'k': case 'l': case 'm': case 'n': case 'o':
361 case 'p': case 'q': case 'r': case 's': case 't':
362 case 'u': case 'v': case 'w': case 'x': case 'y':
367 while (isalnum (c
) || c
== '_' || c
== '.')
369 if (p
== token_buffer
+ maxtoken
)
370 p
= grow_token_buffer (p
);
378 symval
= getsym (token_buffer
);
381 case '0': case '1': case '2': case '3': case '4':
382 case '5': case '6': case '7': case '8': case '9':
389 if (p
== token_buffer
+ maxtoken
)
390 p
= grow_token_buffer (p
);
393 numval
= numval
* 10 + c
- '0';
402 /* parse the literal token and compute character code in code */
407 char discard
[10], *dp
;
411 literalchar (&p
, &code
, '\'');
416 complain (_("use \"...\" for multi-character literal tokens"));
420 if (!literalchar (&dp
, &discode
, '\''))
426 symval
= getsym (token_buffer
);
427 symval
->class = token_sym
;
428 if (!symval
->user_token_number
)
429 symval
->user_token_number
= code
;
434 /* parse the literal string token and treat as an identifier */
438 int code
; /* ignored here */
441 /* Read up to and including ". */
442 while (literalchar (&p
, &code
, '\"'))
444 if (p
>= token_buffer
+ maxtoken
- 4)
445 p
= grow_token_buffer (p
);
449 symval
= getsym (token_buffer
);
450 symval
->class = token_sym
;
477 while (c
== ' ' || c
== '\n' || c
== '\t');
481 strcpy (token_buffer
, "={");
496 fatal (_("unterminated type name at end of file"));
499 complain (_("unterminated type name"));
504 if (p
== token_buffer
+ maxtoken
)
505 p
= grow_token_buffer (p
);
515 return parse_percent_token ();
522 /* the following table dictates the action taken for the various %
523 directives. A set_flag value causes the named flag to be set. A
524 retval action returns the code. */
525 struct percent_table_struct
533 { "token", NULL
, TOKEN
},
534 { "term", NULL
, TOKEN
},
535 { "nterm", NULL
, NTERM
},
536 { "type", NULL
, TYPE
},
537 { "guard", NULL
, GUARD
},
538 { "union", NULL
, UNION
},
539 { "expect", NULL
, EXPECT
},
540 { "thong", NULL
, THONG
},
541 { "start", NULL
, START
},
542 { "left", NULL
, LEFT
},
543 { "right", NULL
, RIGHT
},
544 { "nonassoc", NULL
, NONASSOC
},
545 { "binary", NULL
, NONASSOC
},
546 { "semantic_parser", NULL
, SEMANTIC_PARSER
},
547 { "pure_parser", NULL
, PURE_PARSER
},
548 { "prec", NULL
, PREC
},
549 { "locations", &locations_flag
, NOOP
}, /* -l */
550 { "no_lines", &no_lines_flag
, NOOP
}, /* -l */
551 { "raw", &raw_flag
, NOOP
}, /* -r */
552 { "token_table", &token_table_flag
, NOOP
}, /* -k */
554 /* These can be utilized after main is reoganized so
555 open_files() is deferred 'til after read_declarations().
556 But %{ and %union both put information into files
557 that have to be opened before read_declarations().
559 { "yacc", &yacc_flag
, NOOP
}, /* -y */
560 { "fixed_output_files", &yacc_flag
, NOOP
}, /* -y */
561 { "defines", &defines_flag
, NOOP
}, /* -d */
562 { "no_parser", &no_parser_flag
, NOOP
}, /* -n */
563 { "output_file", &spec_outfile
, SETOPT
}, /* -o */
564 { "file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
565 { "name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
566 /* These would be acceptable, but they do not affect processing */
567 { "verbose", &verbose_flag
, NOOP
}, /* -v */
568 { "debug", &debug_flag
, NOOP
}, /* -t */
569 /* {"help", <print usage stmt>, NOOP}, *//* -h */
570 /* {"version", <print version number> , NOOP}, *//* -V */
572 { NULL
, NULL
, ILLEGAL
}
575 /* Parse a token which starts with %.
576 Assumes the % has already been read and discarded. */
579 parse_percent_token (void)
583 struct percent_table_struct
*tx
;
588 *p
++ = c
; /* for error msg */
597 return PERCENT_LEFT_CURLY
;
619 while (isalpha (c
) || c
== '_' || c
== '-')
621 if (p
== token_buffer
+ maxtoken
)
622 p
= grow_token_buffer (p
);
634 /* table lookup % directive */
635 for (tx
= percent_table
; tx
->name
; tx
++)
636 if (strcmp (token_buffer
+ 1, tx
->name
) == 0)
638 if (tx
->retval
== SETOPT
)
640 *((char **) (tx
->set_flag
)) = optarg
;
645 *((int *) (tx
->set_flag
)) = 1;