]>
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. */
24 #include "getopt.h" /* for optarg */
30 /*spec_outfile is declared in files.h, for -o */
32 extern int translations
;
34 /* functions from main.c */
35 extern char *printable_version
PARAMS ((int));
37 /* Buffer for storing the current token. */
40 /* Allocated size of token_buffer, not including space for terminator. */
46 static int unlexed
; /* these two describe a token to be reread */
47 static bucket
*unlexed_symval
; /* by the next call to lex */
54 token_buffer
= NEW2 (maxtoken
+ 1, char);
60 grow_token_buffer (char *p
)
62 int offset
= p
- token_buffer
;
64 token_buffer
= (char *) xrealloc (token_buffer
, maxtoken
+ 1);
65 return token_buffer
+ offset
;
70 skip_white_space (void)
85 if (c
!= '*' && c
!= '/')
87 complain (_("unexpected `/' found and ignored"));
90 cplus_comment
= (c
== '/');
97 if (!cplus_comment
&& c
== '*')
116 fatal (_("unterminated comment"));
138 /* do a getc, but give error message if EOF encountered */
144 fatal (_("unexpected end of file"));
149 /*------------------------------------------------------------------.
150 | Read one literal character from finput. Process \ escapes. |
151 | Append the normalized string version of the char to *PP. Assign |
152 | the character code to *PCODE. Return 1 unless the character is an |
153 | unescaped `term' or \n report error for \n |
154 `------------------------------------------------------------------*/
157 literalchar (char **pp
, int *pcode
, char term
)
167 complain (_("unescaped newline in constant"));
201 else if (c
<= '7' && c
>= '0')
204 while (c
<= '7' && c
>= '0')
206 code
= (code
* 8) + (c
- '0');
207 if (code
>= 256 || code
< 0)
209 complain (_("octal value outside range 0...255: `\\%o'"),
224 if (c
>= '0' && c
<= '9')
225 code
*= 16, code
+= c
- '0';
226 else if (c
>= 'a' && c
<= 'f')
227 code
*= 16, code
+= c
- 'a' + 10;
228 else if (c
>= 'A' && c
<= 'F')
229 code
*= 16, code
+= c
- 'A' + 10;
232 if (code
>= 256 || code
< 0)
234 complain (_("hexadecimal value above 255: `\\x%x'"), code
);
244 complain (_("unknown escape sequence: `\\' followed by `%s'"),
245 printable_version (c
));
250 /* now fill token_buffer with the canonical name for this character
251 as a literal token. Do not use what the user typed,
252 so that `\012' and `\n' can be interchangeable. */
255 if (code
== term
&& wasquote
)
257 else if (code
== '\\')
262 else if (code
== '\'')
267 else if (code
== '\"')
272 else if (code
>= 040 && code
< 0177)
274 else if (code
== '\t')
279 else if (code
== '\n')
284 else if (code
== '\r')
289 else if (code
== '\v')
294 else if (code
== '\b')
299 else if (code
== '\f')
307 *p
++ = code
/ 0100 + '0';
308 *p
++ = ((code
/ 010) & 07) + '0';
309 *p
++ = (code
& 07) + '0';
321 unlexed_symval
= symval
;
333 symval
= unlexed_symval
;
339 c
= skip_white_space ();
340 *token_buffer
= c
; /* for error messages (token buffer always valid) */
346 strcpy (token_buffer
, "EOF");
349 case 'A': case 'B': case 'C': case 'D': case 'E':
350 case 'F': case 'G': case 'H': case 'I': case 'J':
351 case 'K': case 'L': case 'M': case 'N': case 'O':
352 case 'P': case 'Q': case 'R': case 'S': case 'T':
353 case 'U': case 'V': case 'W': case 'X': case 'Y':
355 case 'a': case 'b': case 'c': case 'd': case 'e':
356 case 'f': case 'g': case 'h': case 'i': case 'j':
357 case 'k': case 'l': case 'm': case 'n': case 'o':
358 case 'p': case 'q': case 'r': case 's': case 't':
359 case 'u': case 'v': case 'w': case 'x': case 'y':
364 while (isalnum (c
) || c
== '_' || c
== '.')
366 if (p
== token_buffer
+ maxtoken
)
367 p
= grow_token_buffer (p
);
375 symval
= getsym (token_buffer
);
378 case '0': case '1': case '2': case '3': case '4':
379 case '5': case '6': case '7': case '8': case '9':
386 if (p
== token_buffer
+ maxtoken
)
387 p
= grow_token_buffer (p
);
390 numval
= numval
* 10 + c
- '0';
399 /* parse the literal token and compute character code in code */
404 char discard
[10], *dp
;
408 literalchar (&p
, &code
, '\'');
413 complain (_("use \"...\" for multi-character literal tokens"));
417 if (!literalchar (&dp
, &discode
, '\''))
423 symval
= getsym (token_buffer
);
424 symval
->class = STOKEN
;
425 if (!symval
->user_token_number
)
426 symval
->user_token_number
= code
;
431 /* parse the literal string token and treat as an identifier */
435 int code
; /* ignored here */
438 while (literalchar (&p
, &code
, '\"')) /* read up to and including " */
440 if (p
>= token_buffer
+ maxtoken
- 4)
441 p
= grow_token_buffer (p
);
445 symval
= getsym (token_buffer
);
446 symval
->class = STOKEN
;
473 while (c
== ' ' || c
== '\n' || c
== '\t');
477 strcpy (token_buffer
, "={");
492 fatal (_("unterminated type name at end of file"));
495 complain (_("unterminated type name"));
500 if (p
== token_buffer
+ maxtoken
)
501 p
= grow_token_buffer (p
);
511 return parse_percent_token ();
518 /* the following table dictates the action taken for the various %
519 directives. A setflag value causes the named flag to be set. A
520 retval action returns the code. */
521 struct percent_table_struct
529 { "token", NULL
, TOKEN
},
530 { "term", NULL
, TOKEN
},
531 { "nterm", NULL
, NTERM
},
532 { "type", NULL
, TYPE
},
533 { "guard", NULL
, GUARD
},
534 { "union", NULL
, UNION
},
535 { "expect", NULL
, EXPECT
},
536 { "thong", NULL
, THONG
},
537 { "start", NULL
, START
},
538 { "left", NULL
, LEFT
},
539 { "right", NULL
, RIGHT
},
540 { "nonassoc", NULL
, NONASSOC
},
541 { "binary", NULL
, NONASSOC
},
542 { "semantic_parser", NULL
, SEMANTIC_PARSER
},
543 { "pure_parser", NULL
, PURE_PARSER
},
544 { "prec", NULL
, PREC
},
545 { "no_lines", &nolinesflag
, NOOP
}, /* -l */
546 { "raw", &rawtoknumflag
, NOOP
}, /* -r */
547 { "token_table", &toknumflag
, NOOP
}, /* -k */
549 /* These can be utilized after main is reoganized so
550 open_files() is deferred 'til after read_declarations().
551 But %{ and %union both put information into files
552 that have to be opened before read_declarations().
554 { "yacc", &fixed_outfiles
, NOOP
}, /* -y */
555 { "fixed_output_files", &fixed_outfiles
, NOOP
}, /* -y */
556 { "defines", &definesflag
, NOOP
}, /* -d */
557 { "no_parser", &noparserflag
, NOOP
}, /* -n */
558 { "output_file", &spec_outfile
, SETOPT
}, /* -o */
559 { "file_prefix", &spec_file_prefix
, SETOPT
}, /* -b */
560 { "name_prefix", &spec_name_prefix
, SETOPT
}, /* -p */
561 /* These would be acceptable, but they do not affect processing */
562 { "verbose", &verboseflag
, NOOP
}, /* -v */
563 { "debug", &debugflag
, NOOP
}, /* -t */
564 /* {"help", <print usage stmt>, NOOP}, *//* -h */
565 /* {"version", <print version number> , NOOP}, *//* -V */
567 { NULL
, NULL
, ILLEGAL
}
570 /* Parse a token which starts with %.
571 Assumes the % has already been read and discarded. */
574 parse_percent_token (void)
578 struct percent_table_struct
*tx
;
583 *p
++ = c
; /* for error msg */
592 return PERCENT_LEFT_CURLY
;
614 while (isalpha (c
) || c
== '_' || c
== '-')
616 if (p
== token_buffer
+ maxtoken
)
617 p
= grow_token_buffer (p
);
629 /* table lookup % directive */
630 for (tx
= percent_table
; tx
->name
; tx
++)
631 if (strcmp (token_buffer
+ 1, tx
->name
) == 0)
633 if (tx
->retval
== SETOPT
)
635 *((char **) (tx
->setflag
)) = optarg
;
640 *((int *) (tx
->setflag
)) = 1;