]>
git.saurik.com Git - bison.git/blob - src/lex.c
a1e8580b261ef2788e70a2c2213c0640412de9e6
1 /* Token-reader for Bison's input parser,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000 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 */
38 /* flags set by % directives */
39 extern int definesflag
; /* for -d */
40 extern int toknumflag
; /* for -k */
41 extern int noparserflag
; /* for -n */
42 extern int fixed_outfiles
; /* for -y */
43 extern int nolinesflag
; /* for -l */
44 extern int rawtoknumflag
; /* for -r */
45 extern int verboseflag
; /* for -v */
46 extern int debugflag
; /* for -t */
47 extern char *spec_name_prefix
; /* for -p */
48 extern char *spec_file_prefix
; /* for -b */
49 /*spec_outfile is declared in files.h, for -o */
51 extern int translations
;
53 extern void init_lex
PARAMS((void));
54 extern char *grow_token_buffer
PARAMS((char *));
55 extern int skip_white_space
PARAMS((void));
56 extern void unlex
PARAMS((int));
57 extern int lex
PARAMS((void));
58 extern int parse_percent_token
PARAMS((void));
60 static int safegetc
PARAMS((FILE *));
61 static int literalchar
PARAMS((char **, int *, char));
63 /* functions from main.c */
64 extern char *printable_version
PARAMS((int));
66 /* Buffer for storing the current token. */
69 /* Allocated size of token_buffer, not including space for terminator. */
75 static int unlexed
; /* these two describe a token to be reread */
76 static bucket
*unlexed_symval
; /* by the next call to lex */
83 token_buffer
= NEW2 (maxtoken
+ 1, char);
89 grow_token_buffer (char *p
)
91 int offset
= p
- token_buffer
;
93 token_buffer
= (char *) xrealloc(token_buffer
, maxtoken
+ 1);
94 return token_buffer
+ offset
;
99 skip_white_space (void)
114 if (c
!= '*' && c
!= '/')
116 complain (_("unexpected `/' found and ignored"));
119 cplus_comment
= (c
== '/');
126 if (!cplus_comment
&& c
== '*')
145 fatal (_("unterminated comment"));
167 /* do a getc, but give error message if EOF encountered */
171 register int c
= getc(f
);
173 fatal (_("unexpected end of file"));
177 /* read one literal character from finput. process \ escapes.
178 append the normalized string version of the char to *pp.
179 assign the character code to *pcode
180 return 1 unless the character is an unescaped `term' or \n
184 literalchar (char **pp
, int *pcode
, char term
)
191 c
= safegetc(finput
);
194 complain (_("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 complain (_("octal value outside range 0...255: `\\%o'"),
231 c
= safegetc(finput
);
237 c
= safegetc(finput
);
241 if (c
>= '0' && c
<= '9')
242 code
*= 16, code
+= c
- '0';
243 else if (c
>= 'a' && c
<= 'f')
244 code
*= 16, code
+= c
- 'a' + 10;
245 else if (c
>= 'A' && c
<= 'F')
246 code
*= 16, code
+= c
- 'A' + 10;
249 if (code
>= 256 || code
<0)
251 complain (_("hexadecimal value above 255: `\\x%x'"),
256 c
= safegetc(finput
);
262 complain (_("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
== term
&& wasquote
)
275 else if (code
== '\\') {*p
++ = '\\'; *p
++ = '\\';}
276 else if (code
== '\'') {*p
++ = '\\'; *p
++ = '\'';}
277 else if (code
== '\"') {*p
++ = '\\'; *p
++ = '\"';}
278 else if (code
>= 040 && code
< 0177)
280 else if (code
== '\t') {*p
++ = '\\'; *p
++ = 't';}
281 else if (code
== '\n') {*p
++ = '\\'; *p
++ = 'n';}
282 else if (code
== '\r') {*p
++ = '\\'; *p
++ = 'r';}
283 else if (code
== '\v') {*p
++ = '\\'; *p
++ = 'v';}
284 else if (code
== '\b') {*p
++ = '\\'; *p
++ = 'b';}
285 else if (code
== '\f') {*p
++ = '\\'; *p
++ = 'f';}
289 *p
++ = code
/ 0100 + '0';
290 *p
++ = ((code
/ 010) & 07) + '0';
291 *p
++ = (code
& 07) + '0';
303 unlexed_symval
= symval
;
315 symval
= unlexed_symval
;
321 c
= skip_white_space();
322 *token_buffer
= c
; /* for error messages (token buffer always valid) */
328 strcpy(token_buffer
, "EOF");
331 case 'A': case 'B': case 'C': case 'D': case 'E':
332 case 'F': case 'G': case 'H': case 'I': case 'J':
333 case 'K': case 'L': case 'M': case 'N': case 'O':
334 case 'P': case 'Q': case 'R': case 'S': case 'T':
335 case 'U': case 'V': case 'W': case 'X': case 'Y':
337 case 'a': case 'b': case 'c': case 'd': case 'e':
338 case 'f': case 'g': case 'h': case 'i': case 'j':
339 case 'k': case 'l': case 'm': case 'n': case 'o':
340 case 'p': case 'q': case 'r': case 's': case 't':
341 case 'u': case 'v': case 'w': case 'x': case 'y':
345 while (isalnum(c
) || c
== '_' || c
== '.')
347 if (p
== token_buffer
+ maxtoken
)
348 p
= grow_token_buffer(p
);
356 symval
= getsym(token_buffer
);
359 case '0': case '1': case '2': case '3': case '4':
360 case '5': case '6': case '7': case '8': case '9':
367 if (p
== token_buffer
+ maxtoken
)
368 p
= grow_token_buffer(p
);
371 numval
= numval
*10 + c
- '0';
381 /* parse the literal token and compute character code in code */
386 char discard
[10], *dp
;
390 literalchar(&p
, &code
, '\'');
395 complain (_("use \"...\" for multi-character literal tokens"));
399 if (! literalchar(&dp
, &discode
, '\''))
405 symval
= getsym(token_buffer
);
406 symval
->class = STOKEN
;
407 if (! symval
->user_token_number
)
408 symval
->user_token_number
= code
;
414 /* parse the literal string token and treat as an identifier */
418 int code
; /* ignored here */
421 while (literalchar(&p
, &code
, '\"')) /* read up to and including " */
423 if (p
>= token_buffer
+ maxtoken
- 4)
424 p
= grow_token_buffer(p
);
428 symval
= getsym(token_buffer
);
429 symval
->class = STOKEN
;
453 if (c
== '\n') lineno
++;
455 while(c
==' ' || c
=='\n' || c
=='\t');
459 strcpy(token_buffer
, "={");
474 fatal (_("unterminated type name at end of file"));
477 complain (_("unterminated type name"));
482 if (p
== token_buffer
+ maxtoken
)
483 p
= grow_token_buffer(p
);
493 return parse_percent_token();
500 /* the following table dictates the action taken for the various
501 % directives. A setflag value causes the named flag to be
502 set. A retval action returns the code.
504 struct percent_table_struct
{
510 {"token", NULL
, TOKEN
},
511 {"term", NULL
, TOKEN
},
512 {"nterm", NULL
, NTERM
},
513 {"type", NULL
, TYPE
},
514 {"guard", NULL
, GUARD
},
515 {"union", NULL
, UNION
},
516 {"expect", NULL
, EXPECT
},
517 {"thong", NULL
, THONG
},
518 {"start", NULL
, START
},
519 {"left", NULL
, LEFT
},
520 {"right", NULL
, RIGHT
},
521 {"nonassoc", NULL
, NONASSOC
},
522 {"binary", NULL
, NONASSOC
},
523 {"semantic_parser", NULL
, SEMANTIC_PARSER
},
524 {"pure_parser", NULL
, PURE_PARSER
},
525 {"prec", NULL
, PREC
},
527 {"no_lines", &nolinesflag
, NOOP
}, /* -l */
528 {"raw", &rawtoknumflag
, NOOP
}, /* -r */
529 {"token_table", &toknumflag
, NOOP
}, /* -k */
532 /* These can be utilized after main is reoganized so
533 open_files() is deferred 'til after read_declarations().
534 But %{ and %union both put information into files
535 that have to be opened before read_declarations().
537 {"yacc", &fixed_outfiles
, NOOP
}, /* -y */
538 {"fixed_output_files", &fixed_outfiles
, NOOP
}, /* -y */
539 {"defines", &definesflag
, NOOP
}, /* -d */
540 {"no_parser", &noparserflag
, NOOP
}, /* -n */
541 {"output_file", &spec_outfile
, SETOPT
}, /* -o */
542 {"file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
543 {"name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
545 /* These would be acceptable, but they do not affect processing */
546 {"verbose", &verboseflag
, NOOP
}, /* -v */
547 {"debug", &debugflag
, NOOP
}, /* -t */
548 /* {"help", <print usage stmt>, NOOP},*/ /* -h */
549 /* {"version", <print version number> , NOOP},*/ /* -V */
552 {NULL
, NULL
, ILLEGAL
}
555 /* Parse a token which starts with %.
556 Assumes the % has already been read and discarded. */
559 parse_percent_token (void)
563 register struct percent_table_struct
*tx
;
568 *p
++ = c
; /* for error msg */
577 return PERCENT_LEFT_CURLY
;
599 while (isalpha(c
) || c
== '_' || c
== '-')
601 if (p
== token_buffer
+ maxtoken
)
602 p
= grow_token_buffer(p
);
604 if (c
== '-') c
= '_';
613 /* table lookup % directive */
614 for (tx
= percent_table
; tx
->name
; tx
++)
615 if (strcmp(token_buffer
+1, tx
->name
) == 0)
617 if (tx
->retval
== SETOPT
)
619 *((char **)(tx
->setflag
)) = optarg
;
624 *((int *)(tx
->setflag
)) = 1;