]>
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 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. */
23 lex is the entry point. It is called from reader.c.
24 It returns one of the token-type codes defined in lex.h.
25 When an identifier is seen, the code IDENTIFIER is returned
26 and the name is looked up in the symbol table using symtab.c;
27 symval is set to a pointer to the entry found. */
32 #include "getopt.h" /* for optarg */
37 /* flags set by % directives */
38 extern int definesflag
; /* for -d */
39 extern int toknumflag
; /* for -k */
40 extern int noparserflag
; /* for -n */
41 extern int fixed_outfiles
; /* for -y */
42 extern int nolinesflag
; /* for -l */
43 extern int rawtoknumflag
; /* for -r */
44 extern int verboseflag
; /* for -v */
45 extern int debugflag
; /* for -t */
46 extern char *spec_name_prefix
; /* for -p */
47 extern char *spec_file_prefix
; /* for -b */
48 /*spec_outfile is declared in files.h, for -o */
51 extern int translations
;
53 void init_lex
PARAMS((void));
54 char *grow_token_buffer
PARAMS((char *));
55 int skip_white_space
PARAMS((void));
56 int safegetc
PARAMS((FILE *));
57 int literalchar
PARAMS((char **, int *, char));
58 void unlex
PARAMS((int));
59 int lex
PARAMS((void));
60 int parse_percent_token
PARAMS((void));
62 /* functions from main.c */
63 extern char *printable_version
PARAMS((int));
64 extern void fatal
PARAMS((char *));
65 extern void warn
PARAMS((char *));
66 extern void warni
PARAMS((char *, int));
67 extern void warns
PARAMS((char *, char *));
69 /* Buffer for storing the current token. */
72 /* Allocated size of token_buffer, not including space for terminator. */
78 static int unlexed
; /* these two describe a token to be reread */
79 static bucket
*unlexed_symval
; /* by the next call to lex */
86 token_buffer
= NEW2 (maxtoken
+ 1, char);
92 grow_token_buffer (char *p
)
94 int offset
= p
- token_buffer
;
96 token_buffer
= (char *) xrealloc(token_buffer
, maxtoken
+ 1);
97 return token_buffer
+ offset
;
102 skip_white_space (void)
117 if (c
!= '*' && c
!= '/')
119 warn(_("unexpected `/' found and ignored"));
122 cplus_comment
= (c
== '/');
129 if (!cplus_comment
&& c
== '*')
148 fatal(_("unterminated comment"));
170 /* do a getc, but give error message if EOF encountered */
174 register int c
= getc(f
);
176 fatal(_("Unexpected end of file"));
180 /* read one literal character from finput. process \ escapes.
181 append the normalized string version of the char to *pp.
182 assign the character code to *pcode
183 return 1 unless the character is an unescaped `term' or \n
187 literalchar (char **pp
, int *pcode
, char term
)
194 c
= safegetc(finput
);
197 warn(_("unescaped newline in constant"));
210 c
= safegetc(finput
);
211 if (c
== 't') code
= '\t';
212 else if (c
== 'n') code
= '\n';
213 else if (c
== 'a') code
= '\007';
214 else if (c
== 'r') code
= '\r';
215 else if (c
== 'f') code
= '\f';
216 else if (c
== 'b') code
= '\b';
217 else if (c
== 'v') code
= '\013';
218 else if (c
== '\\') code
= '\\';
219 else if (c
== '\'') code
= '\'';
220 else if (c
== '\"') code
= '\"';
221 else if (c
<= '7' && c
>= '0')
224 while (c
<= '7' && c
>= '0')
226 code
= (code
* 8) + (c
- '0');
227 if (code
>= 256 || code
< 0)
229 warni(_("octal value outside range 0...255: `\\%o'"), code
);
233 c
= safegetc(finput
);
239 c
= safegetc(finput
);
243 if (c
>= '0' && c
<= '9')
244 code
*= 16, code
+= c
- '0';
245 else if (c
>= 'a' && c
<= 'f')
246 code
*= 16, code
+= c
- 'a' + 10;
247 else if (c
>= 'A' && c
<= 'F')
248 code
*= 16, code
+= c
- 'A' + 10;
251 if (code
>= 256 || code
<0)
253 warni(_("hexadecimal value above 255: `\\x%x'"), code
);
257 c
= safegetc(finput
);
263 warns (_("unknown escape sequence: `\\' followed by `%s'"),
264 printable_version(c
));
269 /* now fill token_buffer with the canonical name for this character
270 as a literal token. Do not use what the user typed,
271 so that `\012' and `\n' can be interchangeable. */
274 if (code
== term
&& wasquote
)
276 else if (code
== '\\') {*p
++ = '\\'; *p
++ = '\\';}
277 else if (code
== '\'') {*p
++ = '\\'; *p
++ = '\'';}
278 else if (code
== '\"') {*p
++ = '\\'; *p
++ = '\"';}
279 else if (code
>= 040 && code
< 0177)
281 else if (code
== '\t') {*p
++ = '\\'; *p
++ = 't';}
282 else if (code
== '\n') {*p
++ = '\\'; *p
++ = 'n';}
283 else if (code
== '\r') {*p
++ = '\\'; *p
++ = 'r';}
284 else if (code
== '\v') {*p
++ = '\\'; *p
++ = 'v';}
285 else if (code
== '\b') {*p
++ = '\\'; *p
++ = 'b';}
286 else if (code
== '\f') {*p
++ = '\\'; *p
++ = 'f';}
290 *p
++ = code
/ 0100 + '0';
291 *p
++ = ((code
/ 010) & 07) + '0';
292 *p
++ = (code
& 07) + '0';
304 unlexed_symval
= symval
;
316 symval
= unlexed_symval
;
322 c
= skip_white_space();
323 *token_buffer
= c
; /* for error messages (token buffer always valid) */
329 strcpy(token_buffer
, "EOF");
332 case 'A': case 'B': case 'C': case 'D': case 'E':
333 case 'F': case 'G': case 'H': case 'I': case 'J':
334 case 'K': case 'L': case 'M': case 'N': case 'O':
335 case 'P': case 'Q': case 'R': case 'S': case 'T':
336 case 'U': case 'V': case 'W': case 'X': case 'Y':
338 case 'a': case 'b': case 'c': case 'd': case 'e':
339 case 'f': case 'g': case 'h': case 'i': case 'j':
340 case 'k': case 'l': case 'm': case 'n': case 'o':
341 case 'p': case 'q': case 'r': case 's': case 't':
342 case 'u': case 'v': case 'w': case 'x': case 'y':
346 while (isalnum(c
) || c
== '_' || c
== '.')
348 if (p
== token_buffer
+ maxtoken
)
349 p
= grow_token_buffer(p
);
357 symval
= getsym(token_buffer
);
360 case '0': case '1': case '2': case '3': case '4':
361 case '5': case '6': case '7': case '8': case '9':
368 if (p
== token_buffer
+ maxtoken
)
369 p
= grow_token_buffer(p
);
372 numval
= numval
*10 + c
- '0';
382 /* parse the literal token and compute character code in code */
387 char discard
[10], *dp
;
391 literalchar(&p
, &code
, '\'');
396 warn(_("use \"...\" for multi-character literal tokens"));
400 if (! literalchar(&dp
, &discode
, '\''))
406 symval
= getsym(token_buffer
);
407 symval
->class = STOKEN
;
408 if (! symval
->user_token_number
)
409 symval
->user_token_number
= code
;
415 /* parse the literal string token and treat as an identifier */
419 int code
; /* ignored here */
422 while (literalchar(&p
, &code
, '\"')) /* read up to and including " */
424 if (p
>= token_buffer
+ maxtoken
- 4)
425 p
= grow_token_buffer(p
);
429 symval
= getsym(token_buffer
);
430 symval
->class = STOKEN
;
454 if (c
== '\n') lineno
++;
456 while(c
==' ' || c
=='\n' || c
=='\t');
460 strcpy(token_buffer
, "={");
475 fatal(_("unterminated type name at end of file"));
478 warn(_("unterminated type name"));
483 if (p
== token_buffer
+ maxtoken
)
484 p
= grow_token_buffer(p
);
494 return (parse_percent_token());
501 /* the following table dictates the action taken for the various
502 % directives. A setflag value causes the named flag to be
503 set. A retval action returns the code.
505 struct percent_table_struct
{
511 {"token", NULL
, TOKEN
},
512 {"term", NULL
, TOKEN
},
513 {"nterm", NULL
, NTERM
},
514 {"type", NULL
, TYPE
},
515 {"guard", NULL
, GUARD
},
516 {"union", NULL
, UNION
},
517 {"expect", NULL
, EXPECT
},
518 {"thong", NULL
, THONG
},
519 {"start", NULL
, START
},
520 {"left", NULL
, LEFT
},
521 {"right", NULL
, RIGHT
},
522 {"nonassoc", NULL
, NONASSOC
},
523 {"binary", NULL
, NONASSOC
},
524 {"semantic_parser", NULL
, SEMANTIC_PARSER
},
525 {"pure_parser", NULL
, PURE_PARSER
},
526 {"prec", NULL
, PREC
},
528 {"no_lines", &nolinesflag
, NOOP
}, /* -l */
529 {"raw", &rawtoknumflag
, NOOP
}, /* -r */
530 {"token_table", &toknumflag
, NOOP
}, /* -k */
533 /* These can be utilized after main is reoganized so
534 open_files() is deferred 'til after read_declarations().
535 But %{ and %union both put information into files
536 that have to be opened before read_declarations().
538 {"yacc", &fixed_outfiles
, NOOP
}, /* -y */
539 {"fixed_output_files", &fixed_outfiles
, NOOP
}, /* -y */
540 {"defines", &definesflag
, NOOP
}, /* -d */
541 {"no_parser", &noparserflag
, NOOP
}, /* -n */
542 {"output_file", &spec_outfile
, SETOPT
}, /* -o */
543 {"file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
544 {"name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
546 /* These would be acceptable, but they do not affect processing */
547 {"verbose", &verboseflag
, NOOP
}, /* -v */
548 {"debug", &debugflag
, NOOP
}, /* -t */
549 /* {"help", <print usage stmt>, NOOP},*/ /* -h */
550 /* {"version", <print version number> , NOOP},*/ /* -V */
553 {NULL
, NULL
, ILLEGAL
}
556 /* Parse a token which starts with %.
557 Assumes the % has already been read and discarded. */
560 parse_percent_token (void)
564 register struct percent_table_struct
*tx
;
569 *p
++ = c
; /* for error msg */
575 return (TWO_PERCENTS
);
578 return (PERCENT_LEFT_CURLY
);
600 while (isalpha(c
) || c
== '_' || c
== '-')
602 if (p
== token_buffer
+ maxtoken
)
603 p
= grow_token_buffer(p
);
605 if (c
== '-') c
= '_';
614 /* table lookup % directive */
615 for (tx
= percent_table
; tx
->name
; tx
++)
616 if (strcmp(token_buffer
+1, tx
->name
) == 0)
618 if (tx
->retval
== SETOPT
)
620 *((char **)(tx
->setflag
)) = optarg
;
625 *((int *)(tx
->setflag
)) = 1;