]>
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
== '\\') {*p
++ = '\\'; *p
++ = '\\';}
275 else if (code
== '\'') {*p
++ = '\\'; *p
++ = '\'';}
276 else if (code
== '\"') {*p
++ = '\\'; *p
++ = '\"';}
277 else if (code
>= 040 && code
< 0177)
279 else if (code
== '\t') {*p
++ = '\\'; *p
++ = 't';}
280 else if (code
== '\n') {*p
++ = '\\'; *p
++ = 'n';}
281 else if (code
== '\r') {*p
++ = '\\'; *p
++ = 'r';}
282 else if (code
== '\v') {*p
++ = '\\'; *p
++ = 'v';}
283 else if (code
== '\b') {*p
++ = '\\'; *p
++ = 'b';}
284 else if (code
== '\f') {*p
++ = '\\'; *p
++ = 'f';}
288 *p
++ = code
/ 0100 + '0';
289 *p
++ = ((code
/ 010) & 07) + '0';
290 *p
++ = (code
& 07) + '0';
302 unlexed_symval
= symval
;
314 symval
= unlexed_symval
;
320 c
= skip_white_space();
321 *token_buffer
= c
; /* for error messages (token buffer always valid) */
327 strcpy(token_buffer
, "EOF");
330 case 'A': case 'B': case 'C': case 'D': case 'E':
331 case 'F': case 'G': case 'H': case 'I': case 'J':
332 case 'K': case 'L': case 'M': case 'N': case 'O':
333 case 'P': case 'Q': case 'R': case 'S': case 'T':
334 case 'U': case 'V': case 'W': case 'X': case 'Y':
336 case 'a': case 'b': case 'c': case 'd': case 'e':
337 case 'f': case 'g': case 'h': case 'i': case 'j':
338 case 'k': case 'l': case 'm': case 'n': case 'o':
339 case 'p': case 'q': case 'r': case 's': case 't':
340 case 'u': case 'v': case 'w': case 'x': case 'y':
344 while (isalnum(c
) || c
== '_' || c
== '.')
346 if (p
== token_buffer
+ maxtoken
)
347 p
= grow_token_buffer(p
);
355 symval
= getsym(token_buffer
);
358 case '0': case '1': case '2': case '3': case '4':
359 case '5': case '6': case '7': case '8': case '9':
366 if (p
== token_buffer
+ maxtoken
)
367 p
= grow_token_buffer(p
);
370 numval
= numval
*10 + c
- '0';
380 /* parse the literal token and compute character code in code */
385 char discard
[10], *dp
;
389 literalchar(&p
, &code
, '\'');
394 warn(_("use \"...\" for multi-character literal tokens"));
398 if (! literalchar(&dp
, &discode
, '\''))
404 symval
= getsym(token_buffer
);
405 symval
->class = STOKEN
;
406 if (! symval
->user_token_number
)
407 symval
->user_token_number
= code
;
413 /* parse the literal string token and treat as an identifier */
417 int code
; /* ignored here */
420 while (literalchar(&p
, &code
, '\"')) /* read up to and including " */
422 if (p
>= token_buffer
+ maxtoken
- 4)
423 p
= grow_token_buffer(p
);
427 symval
= getsym(token_buffer
);
428 symval
->class = STOKEN
;
452 if (c
== '\n') lineno
++;
454 while(c
==' ' || c
=='\n' || c
=='\t');
458 strcpy(token_buffer
, "={");
473 fatal(_("unterminated type name at end of file"));
476 warn(_("unterminated type name"));
481 if (p
== token_buffer
+ maxtoken
)
482 p
= grow_token_buffer(p
);
492 return (parse_percent_token());
499 /* the following table dictates the action taken for the various
500 % directives. A setflag value causes the named flag to be
501 set. A retval action returns the code.
503 struct percent_table_struct
{
509 {"token", NULL
, TOKEN
},
510 {"term", NULL
, TOKEN
},
511 {"nterm", NULL
, NTERM
},
512 {"type", NULL
, TYPE
},
513 {"guard", NULL
, GUARD
},
514 {"union", NULL
, UNION
},
515 {"expect", NULL
, EXPECT
},
516 {"thong", NULL
, THONG
},
517 {"start", NULL
, START
},
518 {"left", NULL
, LEFT
},
519 {"right", NULL
, RIGHT
},
520 {"nonassoc", NULL
, NONASSOC
},
521 {"binary", NULL
, NONASSOC
},
522 {"semantic_parser", NULL
, SEMANTIC_PARSER
},
523 {"pure_parser", NULL
, PURE_PARSER
},
524 {"prec", NULL
, PREC
},
526 {"no_lines", &nolinesflag
, NOOP
}, /* -l */
527 {"raw", &rawtoknumflag
, NOOP
}, /* -r */
528 {"token_table", &toknumflag
, NOOP
}, /* -k */
531 /* These can be utilized after main is reoganized so
532 open_files() is deferred 'til after read_declarations().
533 But %{ and %union both put information into files
534 that have to be opened before read_declarations().
536 {"yacc", &fixed_outfiles
, NOOP
}, /* -y */
537 {"fixed_output_files", &fixed_outfiles
, NOOP
}, /* -y */
538 {"defines", &definesflag
, NOOP
}, /* -d */
539 {"no_parser", &noparserflag
, NOOP
}, /* -n */
540 {"output_file", &spec_outfile
, SETOPT
}, /* -o */
541 {"file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
542 {"name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
544 /* These would be acceptable, but they do not affect processing */
545 {"verbose", &verboseflag
, NOOP
}, /* -v */
546 {"debug", &debugflag
, NOOP
}, /* -t */
547 /* {"help", <print usage stmt>, NOOP},*/ /* -h */
548 /* {"version", <print version number> , NOOP},*/ /* -V */
551 {NULL
, NULL
, ILLEGAL
}
554 /* Parse a token which starts with %.
555 Assumes the % has already been read and discarded. */
558 parse_percent_token (void)
562 register struct percent_table_struct
*tx
;
567 *p
++ = c
; /* for error msg */
573 return (TWO_PERCENTS
);
576 return (PERCENT_LEFT_CURLY
);
598 while (isalpha(c
) || c
== '_' || c
== '-')
600 if (p
== token_buffer
+ maxtoken
)
601 p
= grow_token_buffer(p
);
603 if (c
== '-') c
= '_';
612 /* table lookup % directive */
613 for (tx
= percent_table
; tx
->name
; tx
++)
614 if (strcmp(token_buffer
+1, tx
->name
) == 0)
616 if (tx
->retval
== SETOPT
)
618 *((char **)(tx
->setflag
)) = optarg
;
623 *((int *)(tx
->setflag
)) = 1;