]>
git.saurik.com Git - bison.git/blob - src/lex.c
cc27ccbb94f5e8c7d3da2f8ba39ad4af04c6edc0
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 */
31 /* functions from main.c */
32 extern char *printable_version
PARAMS ((int));
34 /* Buffer for storing the current token. */
37 /* Allocated size of token_buffer, not including space for terminator. */
43 static int unlexed
; /* these two describe a token to be reread */
44 static bucket
*unlexed_symval
; /* by the next call to lex */
51 token_buffer
= XCALLOC (char, maxtoken
+ 1);
57 grow_token_buffer (char *p
)
59 int offset
= p
- token_buffer
;
61 token_buffer
= XREALLOC (token_buffer
, char, maxtoken
+ 1);
62 return token_buffer
+ offset
;
67 skip_white_space (void)
82 if (c
!= '*' && c
!= '/')
84 complain (_("unexpected `/' found and ignored"));
87 cplus_comment
= (c
== '/');
94 if (!cplus_comment
&& c
== '*')
113 fatal (_("unterminated comment"));
135 /* do a getc, but give error message if EOF encountered */
141 fatal (_("unexpected end of file"));
146 /*------------------------------------------------------------------.
147 | Read one literal character from finput. Process \ escapes. |
148 | Append the normalized string version of the char to *PP. Assign |
149 | the character code to *PCODE. Return 1 unless the character is an |
150 | unescaped `term' or \n report error for \n |
151 `------------------------------------------------------------------*/
154 literalchar (char **pp
, int *pcode
, char term
)
164 complain (_("unescaped newline in constant"));
198 else if (c
<= '7' && c
>= '0')
201 while (c
<= '7' && c
>= '0')
203 code
= (code
* 8) + (c
- '0');
204 if (code
>= 256 || code
< 0)
206 complain (_("octal value outside range 0...255: `\\%o'"),
221 if (c
>= '0' && c
<= '9')
222 code
*= 16, code
+= c
- '0';
223 else if (c
>= 'a' && c
<= 'f')
224 code
*= 16, code
+= c
- 'a' + 10;
225 else if (c
>= 'A' && c
<= 'F')
226 code
*= 16, code
+= c
- 'A' + 10;
229 if (code
>= 256 || code
< 0)
231 complain (_("hexadecimal value above 255: `\\x%x'"), code
);
241 complain (_("unknown escape sequence: `\\' followed by `%s'"),
242 printable_version (c
));
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 = STOKEN
;
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 = STOKEN
;
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 setflag 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 { "no_lines", &nolinesflag
, NOOP
}, /* -l */
543 { "raw", &rawtoknumflag
, NOOP
}, /* -r */
544 { "token_table", &toknumflag
, NOOP
}, /* -k */
546 /* These can be utilized after main is reoganized so
547 open_files() is deferred 'til after read_declarations().
548 But %{ and %union both put information into files
549 that have to be opened before read_declarations().
551 { "yacc", &yaccflag
, NOOP
}, /* -y */
552 { "fixed_output_files", &yaccflag
, NOOP
}, /* -y */
553 { "defines", &definesflag
, NOOP
}, /* -d */
554 { "no_parser", &noparserflag
, NOOP
}, /* -n */
555 { "output_file", &spec_outfile
, SETOPT
}, /* -o */
556 { "file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
557 { "name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
558 /* These would be acceptable, but they do not affect processing */
559 { "verbose", &verboseflag
, NOOP
}, /* -v */
560 { "debug", &debugflag
, NOOP
}, /* -t */
561 /* {"help", <print usage stmt>, NOOP}, *//* -h */
562 /* {"version", <print version number> , NOOP}, *//* -V */
564 { NULL
, NULL
, ILLEGAL
}
567 /* Parse a token which starts with %.
568 Assumes the % has already been read and discarded. */
571 parse_percent_token (void)
575 struct percent_table_struct
*tx
;
580 *p
++ = c
; /* for error msg */
589 return PERCENT_LEFT_CURLY
;
611 while (isalpha (c
) || c
== '_' || c
== '-')
613 if (p
== token_buffer
+ maxtoken
)
614 p
= grow_token_buffer (p
);
626 /* table lookup % directive */
627 for (tx
= percent_table
; tx
->name
; tx
++)
628 if (strcmp (token_buffer
+ 1, tx
->name
) == 0)
630 if (tx
->retval
== SETOPT
)
632 *((char **) (tx
->setflag
)) = optarg
;
637 *((int *) (tx
->setflag
)) = 1;