]>
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, 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 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));
65 /* Buffer for storing the current token. */
68 /* Allocated size of token_buffer, not including space for terminator. */
74 static int unlexed
; /* these two describe a token to be reread */
75 static bucket
*unlexed_symval
; /* by the next call to lex */
82 token_buffer
= NEW2 (maxtoken
+ 1, char);
88 grow_token_buffer (char *p
)
90 int offset
= p
- token_buffer
;
92 token_buffer
= (char *) xrealloc(token_buffer
, maxtoken
+ 1);
93 return token_buffer
+ offset
;
98 skip_white_space (void)
113 if (c
!= '*' && c
!= '/')
115 complain (_("unexpected `/' found and ignored"));
118 cplus_comment
= (c
== '/');
125 if (!cplus_comment
&& c
== '*')
144 fatal (_("unterminated comment"));
166 /* do a getc, but give error message if EOF encountered */
170 register int c
= getc(f
);
172 fatal (_("unexpected end of file"));
176 /* read one literal character from finput. process \ escapes.
177 append the normalized string version of the char to *pp.
178 assign the character code to *pcode
179 return 1 unless the character is an unescaped `term' or \n
183 literalchar (char **pp
, int *pcode
, char term
)
190 c
= safegetc(finput
);
193 complain (_("unescaped newline in constant"));
206 c
= safegetc(finput
);
207 if (c
== 't') code
= '\t';
208 else if (c
== 'n') code
= '\n';
209 else if (c
== 'a') code
= '\007';
210 else if (c
== 'r') code
= '\r';
211 else if (c
== 'f') code
= '\f';
212 else if (c
== 'b') code
= '\b';
213 else if (c
== 'v') code
= '\013';
214 else if (c
== '\\') code
= '\\';
215 else if (c
== '\'') code
= '\'';
216 else if (c
== '\"') code
= '\"';
217 else if (c
<= '7' && c
>= '0')
220 while (c
<= '7' && c
>= '0')
222 code
= (code
* 8) + (c
- '0');
223 if (code
>= 256 || code
< 0)
225 complain (_("octal value outside range 0...255: `\\%o'"),
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 complain (_("hexadecimal value above 255: `\\x%x'"),
255 c
= safegetc(finput
);
261 complain (_("unknown escape sequence: `\\' followed by `%s'"),
262 printable_version(c
));
267 /* now fill token_buffer with the canonical name for this character
268 as a literal token. Do not use what the user typed,
269 so that `\012' and `\n' can be interchangeable. */
272 if (code
== term
&& wasquote
)
274 else 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 complain (_("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 complain (_("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 */
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;