]>
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, 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. */
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 int parse_percent_token();
55 /* functions from main.c */
56 extern char *printable_version();
61 /* Buffer for storing the current token. */
64 /* Allocated size of token_buffer, not including space for terminator. */
70 static int unlexed
; /* these two describe a token to be reread */
71 static bucket
*unlexed_symval
; /* by the next call to lex */
78 token_buffer
= NEW2 (maxtoken
+ 1, char);
87 int offset
= p
- token_buffer
;
89 token_buffer
= (char *) xrealloc(token_buffer
, maxtoken
+ 1);
90 return token_buffer
+ offset
;
110 if (c
!= '*' && c
!= '/')
112 warn("unexpected `/' found and ignored");
115 cplus_comment
= (c
== '/');
122 if (!cplus_comment
&& c
== '*')
141 fatal("unterminated comment");
163 /* do a getc, but give error message if EOF encountered */
168 register int c
= getc(f
);
170 fatal("Unexpected end of file");
174 /* read one literal character from finput. process \ escapes.
175 append the normalized string version of the char to *pp.
176 assign the character code to *pcode
177 return 1 unless the character is an unescaped `term' or \n
181 literalchar(pp
, pcode
, term
)
191 c
= safegetc(finput
);
194 warn("unescaped newline in constant");
207 c
= safegetc(finput
);
208 if (c
== 't') code
= '\t';
209 else if (c
== 'n') code
= '\n';
210 else if (c
== 'a') code
= '\007';
211 else if (c
== 'r') code
= '\r';
212 else if (c
== 'f') code
= '\f';
213 else if (c
== 'b') code
= '\b';
214 else if (c
== 'v') code
= 013;
215 else if (c
== '\\') code
= '\\';
216 else if (c
== '\'') code
= '\'';
217 else if (c
== '\"') code
= '\"';
218 else if (c
<= '7' && c
>= '0')
221 while (c
<= '7' && c
>= '0')
223 code
= (code
* 8) + (c
- '0');
224 if (code
>= 256 || code
< 0)
226 warni("octal value outside range 0...255: `\\%o'", code
);
230 c
= safegetc(finput
);
236 c
= safegetc(finput
);
240 if (c
>= '0' && c
<= '9')
241 code
*= 16, code
+= c
- '0';
242 else if (c
>= 'a' && c
<= 'f')
243 code
*= 16, code
+= c
- 'a' + 10;
244 else if (c
>= 'A' && c
<= 'F')
245 code
*= 16, code
+= c
- 'A' + 10;
248 if (code
>= 256 || code
<0)
250 warni("hexadecimal value above 255: `\\x%x'", code
);
254 c
= safegetc(finput
);
260 warni ("unknown escape sequence: `\\' followed by `%s'",
261 printable_version(c
));
266 /* now fill token_buffer with the canonical name for this character
267 as a literal token. Do not use what the user typed,
268 so that `\012' and `\n' can be interchangeable. */
271 if (code
>= 040 && code
< 0177)
273 else if (code
== '\\') {*p
++ = '\\'; *p
++ = '\\';}
274 else if (code
== '\'') {*p
++ = '\\'; *p
++ = '\'';}
275 else if (code
== '\"') {*p
++ = '\\'; *p
++ = '\"';}
276 else if (code
== '\t') {*p
++ = '\\'; *p
++ = 't';}
277 else if (code
== '\n') {*p
++ = '\\'; *p
++ = 'n';}
278 else if (code
== '\r') {*p
++ = '\\'; *p
++ = 'r';}
279 else if (code
== '\v') {*p
++ = '\\'; *p
++ = 'v';}
280 else if (code
== '\b') {*p
++ = '\\'; *p
++ = 'b';}
281 else if (code
== '\f') {*p
++ = '\\'; *p
++ = 'f';}
285 *p
++ = code
/ 0100 + '0';
286 *p
++ = ((code
/ 010) & 07) + '0';
287 *p
++ = (code
& 07) + '0';
300 unlexed_symval
= symval
;
312 symval
= unlexed_symval
;
318 c
= skip_white_space();
319 *token_buffer
= c
; /* for error messages (token buffer always valid) */
325 strcpy(token_buffer
, "EOF");
328 case 'A': case 'B': case 'C': case 'D': case 'E':
329 case 'F': case 'G': case 'H': case 'I': case 'J':
330 case 'K': case 'L': case 'M': case 'N': case 'O':
331 case 'P': case 'Q': case 'R': case 'S': case 'T':
332 case 'U': case 'V': case 'W': case 'X': case 'Y':
334 case 'a': case 'b': case 'c': case 'd': case 'e':
335 case 'f': case 'g': case 'h': case 'i': case 'j':
336 case 'k': case 'l': case 'm': case 'n': case 'o':
337 case 'p': case 'q': case 'r': case 's': case 't':
338 case 'u': case 'v': case 'w': case 'x': case 'y':
342 while (isalnum(c
) || c
== '_' || c
== '.')
344 if (p
== token_buffer
+ maxtoken
)
345 p
= grow_token_buffer(p
);
353 symval
= getsym(token_buffer
);
356 case '0': case '1': case '2': case '3': case '4':
357 case '5': case '6': case '7': case '8': case '9':
364 if (p
== token_buffer
+ maxtoken
)
365 p
= grow_token_buffer(p
);
368 numval
= numval
*10 + c
- '0';
378 /* parse the literal token and compute character code in code */
383 char discard
[10], *dp
;
386 literalchar(&p
, &code
, '\'');
391 warn("use \"...\" for multi-character literal tokens");
393 while (literalchar(&dp
, &discode
, '\'')) {}
397 symval
= getsym(token_buffer
);
398 symval
->class = STOKEN
;
399 if (! symval
->user_token_number
)
400 symval
->user_token_number
= code
;
406 /* parse the literal string token and treat as an identifier */
410 int code
; /* ignored here */
413 while (literalchar(&p
, &code
, '\"')) /* read up to and including " */
415 if (p
>= token_buffer
+ maxtoken
- 4)
416 p
= grow_token_buffer(p
);
420 symval
= getsym(token_buffer
);
421 symval
->class = STOKEN
;
445 if (c
== '\n') lineno
++;
447 while(c
==' ' || c
=='\n' || c
=='\t');
451 strcpy(token_buffer
, "={");
466 fatal("unterminated type name at end of file");
469 warn("unterminated type name");
474 if (p
== token_buffer
+ maxtoken
)
475 p
= grow_token_buffer(p
);
485 return (parse_percent_token());
492 /* the following table dictates the action taken for the various
493 % directives. A setflag value causes the named flag to be
494 set. A retval action returns the code.
496 struct percent_table_struct
{
502 {"token", NULL
, TOKEN
},
503 {"term", NULL
, TOKEN
},
504 {"nterm", NULL
, NTERM
},
505 {"type", NULL
, TYPE
},
506 {"guard", NULL
, GUARD
},
507 {"union", NULL
, UNION
},
508 {"expect", NULL
, EXPECT
},
509 {"thong", NULL
, THONG
},
510 {"start", NULL
, START
},
511 {"left", NULL
, LEFT
},
512 {"right", NULL
, RIGHT
},
513 {"nonassoc", NULL
, NONASSOC
},
514 {"binary", NULL
, NONASSOC
},
515 {"semantic_parser", NULL
, SEMANTIC_PARSER
},
516 {"pure_parser", NULL
, PURE_PARSER
},
517 {"prec", NULL
, PREC
},
519 {"no_lines", &nolinesflag
, NOOP
}, /* -l */
520 {"raw", &rawtoknumflag
, NOOP
}, /* -r */
521 {"token_table", &toknumflag
, NOOP
}, /* -k */
524 /* These can be utilized after main is reoganized so
525 open_files() is deferred 'til after read_declarations().
526 But %{ and %union both put information into files
527 that have to be opened before read_declarations().
529 {"yacc", &fixed_outfiles
, NOOP
}, /* -y */
530 {"fixed_output_files", &fixed_outfiles
, NOOP
}, /* -y */
531 {"defines", &definesflag
, NOOP
}, /* -d */
532 {"no_parser", &noparserflag
, NOOP
}, /* -n */
533 {"output_file", &spec_outfile
, SETOPT
}, /* -o */
534 {"file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
535 {"name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
537 /* These would be acceptable, but they do not affect processing */
538 {"verbose", &verboseflag
, NOOP
}, /* -v */
539 {"debug", &debugflag
, NOOP
}, /* -t */
540 /* {"help", <print usage stmt>, NOOP}, /* -h */
541 /* {"version", <print version number> , NOOP}, /* -V */
544 {NULL
, NULL
, ILLEGAL
}
547 /* Parse a token which starts with %.
548 Assumes the % has already been read and discarded. */
551 parse_percent_token ()
555 register struct percent_table_struct
*tx
;
560 *p
++ = c
; /* for error msg */
566 return (TWO_PERCENTS
);
569 return (PERCENT_LEFT_CURLY
);
591 while (isalpha(c
) || c
== '_' || c
== '-')
593 if (p
== token_buffer
+ maxtoken
)
594 p
= grow_token_buffer(p
);
596 if (c
== '-') c
= '_';
605 /* table lookup % directive */
606 for (tx
= percent_table
; tx
->name
; tx
++)
607 if (strcmp(token_buffer
+1, tx
->name
) == 0)
609 if (tx
->retval
== SETOPT
)
611 *((char **)(tx
->setflag
)) = optarg
;
616 *((int *)(tx
->setflag
)) = 1;