]>
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
;
326 /*-----------------------------------------------------------------.
327 | We just read `<' from FIN. Store in TOKEN_BUFFER, the type name |
328 | specified between the `<...>'. |
329 `-----------------------------------------------------------------*/
332 read_type_name (FILE *fin
)
334 char *p
= token_buffer
;
340 fatal (_("unterminated type name at end of file"));
343 complain (_("unterminated type name"));
348 if (p
== token_buffer
+ maxtoken
)
349 p
= grow_token_buffer (p
);
366 symval
= unlexed_symval
;
372 c
= skip_white_space ();
373 /* for error messages (token buffer always valid) */
380 strcpy (token_buffer
, "EOF");
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':
389 case 'a': case 'b': case 'c': case 'd': case 'e':
390 case 'f': case 'g': case 'h': case 'i': case 'j':
391 case 'k': case 'l': case 'm': case 'n': case 'o':
392 case 'p': case 'q': case 'r': case 's': case 't':
393 case 'u': case 'v': case 'w': case 'x': case 'y':
398 while (isalnum (c
) || c
== '_' || c
== '.')
400 if (p
== token_buffer
+ maxtoken
)
401 p
= grow_token_buffer (p
);
409 symval
= getsym (token_buffer
);
412 case '0': case '1': case '2': case '3': case '4':
413 case '5': case '6': case '7': case '8': case '9':
420 if (p
== token_buffer
+ maxtoken
)
421 p
= grow_token_buffer (p
);
424 numval
= numval
* 10 + c
- '0';
433 /* parse the literal token and compute character code in code */
438 char discard
[10], *dp
;
442 literalchar (&p
, &code
, '\'');
447 complain (_("use \"...\" for multi-character literal tokens"));
451 if (!literalchar (&dp
, &discode
, '\''))
457 symval
= getsym (token_buffer
);
458 symval
->class = token_sym
;
459 if (!symval
->user_token_number
)
460 symval
->user_token_number
= code
;
465 /* parse the literal string token and treat as an identifier */
469 int code
; /* ignored here */
472 /* Read up to and including ". */
473 while (literalchar (&p
, &code
, '\"'))
475 if (p
>= token_buffer
+ maxtoken
- 4)
476 p
= grow_token_buffer (p
);
480 symval
= getsym (token_buffer
);
481 symval
->class = token_sym
;
508 while (c
== ' ' || c
== '\n' || c
== '\t');
512 strcpy (token_buffer
, "={");
522 read_type_name (finput
);
526 return parse_percent_token ();
533 /* the following table dictates the action taken for the various %
534 directives. A set_flag value causes the named flag to be set. A
535 retval action returns the code. */
536 struct percent_table_struct
544 { "token", NULL
, TOKEN
},
545 { "term", NULL
, TOKEN
},
546 { "nterm", NULL
, NTERM
},
547 { "type", NULL
, TYPE
},
548 { "guard", NULL
, GUARD
},
549 { "union", NULL
, UNION
},
550 { "expect", NULL
, EXPECT
},
551 { "thong", NULL
, THONG
},
552 { "start", NULL
, START
},
553 { "left", NULL
, LEFT
},
554 { "right", NULL
, RIGHT
},
555 { "nonassoc", NULL
, NONASSOC
},
556 { "binary", NULL
, NONASSOC
},
557 { "semantic_parser", NULL
, SEMANTIC_PARSER
},
558 { "pure_parser", NULL
, PURE_PARSER
},
559 { "prec", NULL
, PREC
},
560 { "locations", &locations_flag
, NOOP
}, /* -l */
561 { "no_lines", &no_lines_flag
, NOOP
}, /* -l */
562 { "raw", &raw_flag
, NOOP
}, /* -r */
563 { "token_table", &token_table_flag
, NOOP
}, /* -k */
565 /* These can be utilized after main is reoganized so
566 open_files() is deferred 'til after read_declarations().
567 But %{ and %union both put information into files
568 that have to be opened before read_declarations().
570 { "yacc", &yacc_flag
, NOOP
}, /* -y */
571 { "fixed_output_files", &yacc_flag
, NOOP
}, /* -y */
572 { "defines", &defines_flag
, NOOP
}, /* -d */
573 { "no_parser", &no_parser_flag
, NOOP
}, /* -n */
574 { "output_file", &spec_outfile
, SETOPT
}, /* -o */
575 { "file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
576 { "name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
577 /* These would be acceptable, but they do not affect processing */
578 { "verbose", &verbose_flag
, NOOP
}, /* -v */
579 { "debug", &debug_flag
, NOOP
}, /* -t */
580 /* {"help", <print usage stmt>, NOOP}, *//* -h */
581 /* {"version", <print version number> , NOOP}, *//* -V */
583 { NULL
, NULL
, ILLEGAL
}
586 /* Parse a token which starts with %.
587 Assumes the % has already been read and discarded. */
590 parse_percent_token (void)
594 struct percent_table_struct
*tx
;
599 *p
++ = c
; /* for error msg */
608 return PERCENT_LEFT_CURLY
;
630 while (isalpha (c
) || c
== '_' || c
== '-')
632 if (p
== token_buffer
+ maxtoken
)
633 p
= grow_token_buffer (p
);
645 /* table lookup % directive */
646 for (tx
= percent_table
; tx
->name
; tx
++)
647 if (strcmp (token_buffer
+ 1, tx
->name
) == 0)
649 if (tx
->retval
== SETOPT
)
651 *((char **) (tx
->set_flag
)) = optarg
;
656 *((int *) (tx
->set_flag
)) = 1;