]>
git.saurik.com Git - bison.git/blob - src/lex.c
f0fbf4851d408f11b470745b87d3635a71dfcedf
1 /* Token-reader for Bison's input parser,
2 Copyright (C) 1984, 1986, 1989, 1992 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 lex is the entry point. It is called from reader.c.
23 It returns one of the token-type codes defined in lex.h.
24 When an identifier is seen, the code IDENTIFIER is returned
25 and the name is looked up in the symbol table using symtab.c;
26 symval is set to a pointer to the entry found. */
31 #include "getopt.h" /* for optarg */
36 /* flags set by % directives */
37 extern int definesflag
; /* for -d */
38 extern int toknumflag
; /* for -k */
39 extern int noparserflag
; /* for -n */
40 extern int fixed_outfiles
; /* for -y */
41 extern int nolinesflag
; /* for -l */
42 extern int rawtoknumflag
; /* for -r */
43 extern int verboseflag
; /* for -v */
44 extern int debugflag
; /* for -t */
45 extern char *spec_name_prefix
; /* for -p */
46 extern char *spec_file_prefix
; /* for -b */
47 /*spec_outfile is declared in files.h, for -o */
50 extern int translations
;
52 void init_lex
PARAMS((void));
53 char *grow_token_buffer
PARAMS((char *));
54 int skip_white_space
PARAMS((void));
55 int safegetc
PARAMS((FILE *));
56 int literalchar
PARAMS((char **, int *, char));
57 void unlex
PARAMS((int));
58 int lex
PARAMS((void));
59 int parse_percent_token
PARAMS((void));
61 /* functions from main.c */
62 extern char *printable_version
PARAMS((int));
63 extern void fatal
PARAMS((char *));
64 extern void warn
PARAMS((char *));
65 extern void warni
PARAMS((char *, int));
66 extern void warns
PARAMS((char *, char *));
68 /* Buffer for storing the current token. */
71 /* Allocated size of token_buffer, not including space for terminator. */
77 static int unlexed
; /* these two describe a token to be reread */
78 static bucket
*unlexed_symval
; /* by the next call to lex */
85 token_buffer
= NEW2 (maxtoken
+ 1, char);
91 grow_token_buffer (char *p
)
93 int offset
= p
- token_buffer
;
95 token_buffer
= (char *) xrealloc(token_buffer
, maxtoken
+ 1);
96 return token_buffer
+ offset
;
101 skip_white_space (void)
116 if (c
!= '*' && c
!= '/')
118 warn(_("unexpected `/' found and ignored"));
121 cplus_comment
= (c
== '/');
128 if (!cplus_comment
&& c
== '*')
147 fatal(_("unterminated comment"));
169 /* do a getc, but give error message if EOF encountered */
173 register int c
= getc(f
);
175 fatal(_("Unexpected end of file"));
179 /* read one literal character from finput. process \ escapes.
180 append the normalized string version of the char to *pp.
181 assign the character code to *pcode
182 return 1 unless the character is an unescaped `term' or \n
186 literalchar (char **pp
, int *pcode
, char term
)
193 c
= safegetc(finput
);
196 warn(_("unescaped newline in constant"));
209 c
= safegetc(finput
);
210 if (c
== 't') code
= '\t';
211 else if (c
== 'n') code
= '\n';
212 else if (c
== 'a') code
= '\007';
213 else if (c
== 'r') code
= '\r';
214 else if (c
== 'f') code
= '\f';
215 else if (c
== 'b') code
= '\b';
216 else if (c
== 'v') code
= '\013';
217 else if (c
== '\\') code
= '\\';
218 else if (c
== '\'') code
= '\'';
219 else if (c
== '\"') code
= '\"';
220 else if (c
<= '7' && c
>= '0')
223 while (c
<= '7' && c
>= '0')
225 code
= (code
* 8) + (c
- '0');
226 if (code
>= 256 || code
< 0)
228 warni(_("octal value outside range 0...255: `\\%o'"), code
);
232 c
= safegetc(finput
);
238 c
= safegetc(finput
);
242 if (c
>= '0' && c
<= '9')
243 code
*= 16, code
+= c
- '0';
244 else if (c
>= 'a' && c
<= 'f')
245 code
*= 16, code
+= c
- 'a' + 10;
246 else if (c
>= 'A' && c
<= 'F')
247 code
*= 16, code
+= c
- 'A' + 10;
250 if (code
>= 256 || code
<0)
252 warni(_("hexadecimal value above 255: `\\x%x'"), code
);
256 c
= safegetc(finput
);
262 warns (_("unknown escape sequence: `\\' followed by `%s'"),
263 printable_version(c
));
268 /* now fill token_buffer with the canonical name for this character
269 as a literal token. Do not use what the user typed,
270 so that `\012' and `\n' can be interchangeable. */
273 if (code
== '\\') {*p
++ = '\\'; *p
++ = '\\';}
274 else if (code
== '\'') {*p
++ = '\\'; *p
++ = '\'';}
275 else if (code
== '\"') {*p
++ = '\\'; *p
++ = '\"';}
276 else if (code
>= 040 && code
< 0177)
278 else if (code
== '\t') {*p
++ = '\\'; *p
++ = 't';}
279 else if (code
== '\n') {*p
++ = '\\'; *p
++ = 'n';}
280 else if (code
== '\r') {*p
++ = '\\'; *p
++ = 'r';}
281 else if (code
== '\v') {*p
++ = '\\'; *p
++ = 'v';}
282 else if (code
== '\b') {*p
++ = '\\'; *p
++ = 'b';}
283 else if (code
== '\f') {*p
++ = '\\'; *p
++ = 'f';}
287 *p
++ = code
/ 0100 + '0';
288 *p
++ = ((code
/ 010) & 07) + '0';
289 *p
++ = (code
& 07) + '0';
301 unlexed_symval
= symval
;
313 symval
= unlexed_symval
;
319 c
= skip_white_space();
320 *token_buffer
= c
; /* for error messages (token buffer always valid) */
326 strcpy(token_buffer
, "EOF");
329 case 'A': case 'B': case 'C': case 'D': case 'E':
330 case 'F': case 'G': case 'H': case 'I': case 'J':
331 case 'K': case 'L': case 'M': case 'N': case 'O':
332 case 'P': case 'Q': case 'R': case 'S': case 'T':
333 case 'U': case 'V': case 'W': case 'X': case 'Y':
335 case 'a': case 'b': case 'c': case 'd': case 'e':
336 case 'f': case 'g': case 'h': case 'i': case 'j':
337 case 'k': case 'l': case 'm': case 'n': case 'o':
338 case 'p': case 'q': case 'r': case 's': case 't':
339 case 'u': case 'v': case 'w': case 'x': case 'y':
343 while (isalnum(c
) || c
== '_' || c
== '.')
345 if (p
== token_buffer
+ maxtoken
)
346 p
= grow_token_buffer(p
);
354 symval
= getsym(token_buffer
);
357 case '0': case '1': case '2': case '3': case '4':
358 case '5': case '6': case '7': case '8': case '9':
365 if (p
== token_buffer
+ maxtoken
)
366 p
= grow_token_buffer(p
);
369 numval
= numval
*10 + c
- '0';
379 /* parse the literal token and compute character code in code */
384 char discard
[10], *dp
;
388 literalchar(&p
, &code
, '\'');
393 warn(_("use \"...\" for multi-character literal tokens"));
397 if (! literalchar(&dp
, &discode
, '\''))
403 symval
= getsym(token_buffer
);
404 symval
->class = STOKEN
;
405 if (! symval
->user_token_number
)
406 symval
->user_token_number
= code
;
412 /* parse the literal string token and treat as an identifier */
416 int code
; /* ignored here */
419 while (literalchar(&p
, &code
, '\"')) /* read up to and including " */
421 if (p
>= token_buffer
+ maxtoken
- 4)
422 p
= grow_token_buffer(p
);
426 symval
= getsym(token_buffer
);
427 symval
->class = STOKEN
;
451 if (c
== '\n') lineno
++;
453 while(c
==' ' || c
=='\n' || c
=='\t');
457 strcpy(token_buffer
, "={");
472 fatal(_("unterminated type name at end of file"));
475 warn(_("unterminated type name"));
480 if (p
== token_buffer
+ maxtoken
)
481 p
= grow_token_buffer(p
);
491 return (parse_percent_token());
498 /* the following table dictates the action taken for the various
499 % directives. A setflag value causes the named flag to be
500 set. A retval action returns the code.
502 struct percent_table_struct
{
508 {"token", NULL
, TOKEN
},
509 {"term", NULL
, TOKEN
},
510 {"nterm", NULL
, NTERM
},
511 {"type", NULL
, TYPE
},
512 {"guard", NULL
, GUARD
},
513 {"union", NULL
, UNION
},
514 {"expect", NULL
, EXPECT
},
515 {"thong", NULL
, THONG
},
516 {"start", NULL
, START
},
517 {"left", NULL
, LEFT
},
518 {"right", NULL
, RIGHT
},
519 {"nonassoc", NULL
, NONASSOC
},
520 {"binary", NULL
, NONASSOC
},
521 {"semantic_parser", NULL
, SEMANTIC_PARSER
},
522 {"pure_parser", NULL
, PURE_PARSER
},
523 {"prec", NULL
, PREC
},
525 {"no_lines", &nolinesflag
, NOOP
}, /* -l */
526 {"raw", &rawtoknumflag
, NOOP
}, /* -r */
527 {"token_table", &toknumflag
, NOOP
}, /* -k */
530 /* These can be utilized after main is reoganized so
531 open_files() is deferred 'til after read_declarations().
532 But %{ and %union both put information into files
533 that have to be opened before read_declarations().
535 {"yacc", &fixed_outfiles
, NOOP
}, /* -y */
536 {"fixed_output_files", &fixed_outfiles
, NOOP
}, /* -y */
537 {"defines", &definesflag
, NOOP
}, /* -d */
538 {"no_parser", &noparserflag
, NOOP
}, /* -n */
539 {"output_file", &spec_outfile
, SETOPT
}, /* -o */
540 {"file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
541 {"name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
543 /* These would be acceptable, but they do not affect processing */
544 {"verbose", &verboseflag
, NOOP
}, /* -v */
545 {"debug", &debugflag
, NOOP
}, /* -t */
546 /* {"help", <print usage stmt>, NOOP},*/ /* -h */
547 /* {"version", <print version number> , NOOP},*/ /* -V */
550 {NULL
, NULL
, ILLEGAL
}
553 /* Parse a token which starts with %.
554 Assumes the % has already been read and discarded. */
557 parse_percent_token (void)
561 register struct percent_table_struct
*tx
;
566 *p
++ = c
; /* for error msg */
572 return (TWO_PERCENTS
);
575 return (PERCENT_LEFT_CURLY
);
597 while (isalpha(c
) || c
== '_' || c
== '-')
599 if (p
== token_buffer
+ maxtoken
)
600 p
= grow_token_buffer(p
);
602 if (c
== '-') c
= '_';
611 /* table lookup % directive */
612 for (tx
= percent_table
; tx
->name
; tx
++)
613 if (strcmp(token_buffer
+1, tx
->name
) == 0)
615 if (tx
->retval
== SETOPT
)
617 *((char **)(tx
->setflag
)) = optarg
;
622 *((int *)(tx
->setflag
)) = 1;