]>
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 warns (_("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
== '\\') {*p
++ = '\\'; *p
++ = '\\';}
272 else if (code
== '\'') {*p
++ = '\\'; *p
++ = '\'';}
273 else if (code
== '\"') {*p
++ = '\\'; *p
++ = '\"';}
274 else if (code
>= 040 && code
< 0177)
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
;
387 literalchar(&p
, &code
, '\'');
392 warn(_("use \"...\" for multi-character literal tokens"));
396 if (! literalchar(&dp
, &discode
, '\''))
402 symval
= getsym(token_buffer
);
403 symval
->class = STOKEN
;
404 if (! symval
->user_token_number
)
405 symval
->user_token_number
= code
;
411 /* parse the literal string token and treat as an identifier */
415 int code
; /* ignored here */
418 while (literalchar(&p
, &code
, '\"')) /* read up to and including " */
420 if (p
>= token_buffer
+ maxtoken
- 4)
421 p
= grow_token_buffer(p
);
425 symval
= getsym(token_buffer
);
426 symval
->class = STOKEN
;
450 if (c
== '\n') lineno
++;
452 while(c
==' ' || c
=='\n' || c
=='\t');
456 strcpy(token_buffer
, "={");
471 fatal(_("unterminated type name at end of file"));
474 warn(_("unterminated type name"));
479 if (p
== token_buffer
+ maxtoken
)
480 p
= grow_token_buffer(p
);
490 return (parse_percent_token());
497 /* the following table dictates the action taken for the various
498 % directives. A setflag value causes the named flag to be
499 set. A retval action returns the code.
501 struct percent_table_struct
{
507 {"token", NULL
, TOKEN
},
508 {"term", NULL
, TOKEN
},
509 {"nterm", NULL
, NTERM
},
510 {"type", NULL
, TYPE
},
511 {"guard", NULL
, GUARD
},
512 {"union", NULL
, UNION
},
513 {"expect", NULL
, EXPECT
},
514 {"thong", NULL
, THONG
},
515 {"start", NULL
, START
},
516 {"left", NULL
, LEFT
},
517 {"right", NULL
, RIGHT
},
518 {"nonassoc", NULL
, NONASSOC
},
519 {"binary", NULL
, NONASSOC
},
520 {"semantic_parser", NULL
, SEMANTIC_PARSER
},
521 {"pure_parser", NULL
, PURE_PARSER
},
522 {"prec", NULL
, PREC
},
524 {"no_lines", &nolinesflag
, NOOP
}, /* -l */
525 {"raw", &rawtoknumflag
, NOOP
}, /* -r */
526 {"token_table", &toknumflag
, NOOP
}, /* -k */
529 /* These can be utilized after main is reoganized so
530 open_files() is deferred 'til after read_declarations().
531 But %{ and %union both put information into files
532 that have to be opened before read_declarations().
534 {"yacc", &fixed_outfiles
, NOOP
}, /* -y */
535 {"fixed_output_files", &fixed_outfiles
, NOOP
}, /* -y */
536 {"defines", &definesflag
, NOOP
}, /* -d */
537 {"no_parser", &noparserflag
, NOOP
}, /* -n */
538 {"output_file", &spec_outfile
, SETOPT
}, /* -o */
539 {"file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
540 {"name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
542 /* These would be acceptable, but they do not affect processing */
543 {"verbose", &verboseflag
, NOOP
}, /* -v */
544 {"debug", &debugflag
, NOOP
}, /* -t */
545 /* {"help", <print usage stmt>, NOOP}, /* -h */
546 /* {"version", <print version number> , NOOP}, /* -V */
549 {NULL
, NULL
, ILLEGAL
}
552 /* Parse a token which starts with %.
553 Assumes the % has already been read and discarded. */
556 parse_percent_token ()
560 register struct percent_table_struct
*tx
;
565 *p
++ = c
; /* for error msg */
571 return (TWO_PERCENTS
);
574 return (PERCENT_LEFT_CURLY
);
596 while (isalpha(c
) || c
== '_' || c
== '-')
598 if (p
== token_buffer
+ maxtoken
)
599 p
= grow_token_buffer(p
);
601 if (c
== '-') c
= '_';
610 /* table lookup % directive */
611 for (tx
= percent_table
; tx
->name
; tx
++)
612 if (strcmp(token_buffer
+1, tx
->name
) == 0)
614 if (tx
->retval
== SETOPT
)
616 *((char **)(tx
->setflag
)) = optarg
;
621 *((int *)(tx
->setflag
)) = 1;