Revert previous change. Not obvious.
[external/binutils.git] / gdb / c-exp.y
1 /* YACC parser for C expressions, for GDB.
2    Copyright 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3    1998, 1999, 2000
4    Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 /* Parse a C expression from text in a string,
23    and return the result as a  struct expression  pointer.
24    That structure contains arithmetic operations in reverse polish,
25    with constants represented by operations that are followed by special data.
26    See expression.h for the details of the format.
27    What is important here is that it can be built up sequentially
28    during the process of parsing; the lower levels of the tree always
29    come first in the result.
30
31    Note that malloc's and realloc's in this file are transformed to
32    xmalloc and xrealloc respectively by the same sed command in the
33    makefile that remaps any other malloc/realloc inserted by the parser
34    generator.  Doing this with #defines and trying to control the interaction
35    with include files (<malloc.h> and <stdlib.h> for example) just became
36    too messy, particularly when such includes can be inserted at random
37    times by the parser generator.  */
38    
39 %{
40
41 #include "defs.h"
42 #include "gdb_string.h"
43 #include <ctype.h>
44 #include "expression.h"
45 #include "value.h"
46 #include "parser-defs.h"
47 #include "language.h"
48 #include "c-lang.h"
49 #include "bfd.h" /* Required by objfiles.h.  */
50 #include "symfile.h" /* Required by objfiles.h.  */
51 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
52
53 /* Flag indicating we're dealing with HP-compiled objects */ 
54 extern int hp_som_som_object_present;
55
56 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
57    as well as gratuitiously global symbol names, so we can have multiple
58    yacc generated parsers in gdb.  Note that these are only the variables
59    produced by yacc.  If other parser generators (bison, byacc, etc) produce
60    additional global names that conflict at link time, then those parser
61    generators need to be fixed instead of adding those names to this list. */
62
63 #define yymaxdepth c_maxdepth
64 #define yyparse c_parse
65 #define yylex   c_lex
66 #define yyerror c_error
67 #define yylval  c_lval
68 #define yychar  c_char
69 #define yydebug c_debug
70 #define yypact  c_pact  
71 #define yyr1    c_r1                    
72 #define yyr2    c_r2                    
73 #define yydef   c_def           
74 #define yychk   c_chk           
75 #define yypgo   c_pgo           
76 #define yyact   c_act           
77 #define yyexca  c_exca
78 #define yyerrflag c_errflag
79 #define yynerrs c_nerrs
80 #define yyps    c_ps
81 #define yypv    c_pv
82 #define yys     c_s
83 #define yy_yys  c_yys
84 #define yystate c_state
85 #define yytmp   c_tmp
86 #define yyv     c_v
87 #define yy_yyv  c_yyv
88 #define yyval   c_val
89 #define yylloc  c_lloc
90 #define yyreds  c_reds          /* With YYDEBUG defined */
91 #define yytoks  c_toks          /* With YYDEBUG defined */
92 #define yyname  c_name          /* With YYDEBUG defined */
93 #define yyrule  c_rule          /* With YYDEBUG defined */
94 #define yylhs   c_yylhs
95 #define yylen   c_yylen
96 #define yydefred c_yydefred
97 #define yydgoto c_yydgoto
98 #define yysindex c_yysindex
99 #define yyrindex c_yyrindex
100 #define yygindex c_yygindex
101 #define yytable  c_yytable
102 #define yycheck  c_yycheck
103
104 #ifndef YYDEBUG
105 #define YYDEBUG 1               /* Default to yydebug support */
106 #endif
107
108 #define YYFPRINTF parser_fprintf
109
110 int yyparse (void);
111
112 static int yylex (void);
113
114 void yyerror (char *);
115
116 %}
117
118 /* Although the yacc "value" of an expression is not used,
119    since the result is stored in the structure being created,
120    other node types do have values.  */
121
122 %union
123   {
124     LONGEST lval;
125     struct {
126       LONGEST val;
127       struct type *type;
128     } typed_val_int;
129     struct {
130       DOUBLEST dval;
131       struct type *type;
132     } typed_val_float;
133     struct symbol *sym;
134     struct type *tval;
135     struct stoken sval;
136     struct ttype tsym;
137     struct symtoken ssym;
138     int voidval;
139     struct block *bval;
140     enum exp_opcode opcode;
141     struct internalvar *ivar;
142
143     struct type **tvec;
144     int *ivec;
145   }
146
147 %{
148 /* YYSTYPE gets defined by %union */
149 static int parse_number (char *, int, int, YYSTYPE *);
150 %}
151
152 %type <voidval> exp exp1 type_exp start variable qualified_name lcurly
153 %type <lval> rcurly
154 %type <tval> type typebase
155 %type <tvec> nonempty_typelist
156 /* %type <bval> block */
157
158 /* Fancy type parsing.  */
159 %type <voidval> func_mod direct_abs_decl abs_decl
160 %type <tval> ptype
161 %type <lval> array_mod
162
163 %token <typed_val_int> INT
164 %token <typed_val_float> FLOAT
165
166 /* Both NAME and TYPENAME tokens represent symbols in the input,
167    and both convey their data as strings.
168    But a TYPENAME is a string that happens to be defined as a typedef
169    or builtin type name (such as int or char)
170    and a NAME is any other symbol.
171    Contexts where this distinction is not important can use the
172    nonterminal "name", which matches either NAME or TYPENAME.  */
173
174 %token <sval> STRING
175 %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
176 %token <tsym> TYPENAME
177 %type <sval> name
178 %type <ssym> name_not_typename
179 %type <tsym> typename
180
181 /* A NAME_OR_INT is a symbol which is not known in the symbol table,
182    but which would parse as a valid number in the current input radix.
183    E.g. "c" when input_radix==16.  Depending on the parse, it will be
184    turned into a name or into a number.  */
185
186 %token <ssym> NAME_OR_INT 
187
188 %token STRUCT CLASS UNION ENUM SIZEOF UNSIGNED COLONCOLON
189 %token TEMPLATE
190 %token ERROR
191
192 /* Special type cases, put in to allow the parser to distinguish different
193    legal basetypes.  */
194 %token SIGNED_KEYWORD LONG SHORT INT_KEYWORD CONST_KEYWORD VOLATILE_KEYWORD DOUBLE_KEYWORD
195
196 %token <voidval> VARIABLE
197
198 %token <opcode> ASSIGN_MODIFY
199
200 /* C++ */
201 %token THIS
202 %token TRUEKEYWORD
203 %token FALSEKEYWORD
204
205
206 %left ','
207 %left ABOVE_COMMA
208 %right '=' ASSIGN_MODIFY
209 %right '?'
210 %left OROR
211 %left ANDAND
212 %left '|'
213 %left '^'
214 %left '&'
215 %left EQUAL NOTEQUAL
216 %left '<' '>' LEQ GEQ
217 %left LSH RSH
218 %left '@'
219 %left '+' '-'
220 %left '*' '/' '%'
221 %right UNARY INCREMENT DECREMENT
222 %right ARROW '.' '[' '('
223 %token <ssym> BLOCKNAME 
224 %token <bval> FILENAME
225 %type <bval> block
226 %left COLONCOLON
227
228 \f
229 %%
230
231 start   :       exp1
232         |       type_exp
233         ;
234
235 type_exp:       type
236                         { write_exp_elt_opcode(OP_TYPE);
237                           write_exp_elt_type($1);
238                           write_exp_elt_opcode(OP_TYPE);}
239         ;
240
241 /* Expressions, including the comma operator.  */
242 exp1    :       exp
243         |       exp1 ',' exp
244                         { write_exp_elt_opcode (BINOP_COMMA); }
245         ;
246
247 /* Expressions, not including the comma operator.  */
248 exp     :       '*' exp    %prec UNARY
249                         { write_exp_elt_opcode (UNOP_IND); }
250
251 exp     :       '&' exp    %prec UNARY
252                         { write_exp_elt_opcode (UNOP_ADDR); }
253
254 exp     :       '-' exp    %prec UNARY
255                         { write_exp_elt_opcode (UNOP_NEG); }
256         ;
257
258 exp     :       '!' exp    %prec UNARY
259                         { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
260         ;
261
262 exp     :       '~' exp    %prec UNARY
263                         { write_exp_elt_opcode (UNOP_COMPLEMENT); }
264         ;
265
266 exp     :       INCREMENT exp    %prec UNARY
267                         { write_exp_elt_opcode (UNOP_PREINCREMENT); }
268         ;
269
270 exp     :       DECREMENT exp    %prec UNARY
271                         { write_exp_elt_opcode (UNOP_PREDECREMENT); }
272         ;
273
274 exp     :       exp INCREMENT    %prec UNARY
275                         { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
276         ;
277
278 exp     :       exp DECREMENT    %prec UNARY
279                         { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
280         ;
281
282 exp     :       SIZEOF exp       %prec UNARY
283                         { write_exp_elt_opcode (UNOP_SIZEOF); }
284         ;
285
286 exp     :       exp ARROW name
287                         { write_exp_elt_opcode (STRUCTOP_PTR);
288                           write_exp_string ($3);
289                           write_exp_elt_opcode (STRUCTOP_PTR); }
290         ;
291
292 exp     :       exp ARROW qualified_name
293                         { /* exp->type::name becomes exp->*(&type::name) */
294                           /* Note: this doesn't work if name is a
295                              static member!  FIXME */
296                           write_exp_elt_opcode (UNOP_ADDR);
297                           write_exp_elt_opcode (STRUCTOP_MPTR); }
298         ;
299
300 exp     :       exp ARROW '*' exp
301                         { write_exp_elt_opcode (STRUCTOP_MPTR); }
302         ;
303
304 exp     :       exp '.' name
305                         { write_exp_elt_opcode (STRUCTOP_STRUCT);
306                           write_exp_string ($3);
307                           write_exp_elt_opcode (STRUCTOP_STRUCT); }
308         ;
309
310 exp     :       exp '.' qualified_name
311                         { /* exp.type::name becomes exp.*(&type::name) */
312                           /* Note: this doesn't work if name is a
313                              static member!  FIXME */
314                           write_exp_elt_opcode (UNOP_ADDR);
315                           write_exp_elt_opcode (STRUCTOP_MEMBER); }
316         ;
317
318 exp     :       exp '.' '*' exp
319                         { write_exp_elt_opcode (STRUCTOP_MEMBER); }
320         ;
321
322 exp     :       exp '[' exp1 ']'
323                         { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
324         ;
325
326 exp     :       exp '(' 
327                         /* This is to save the value of arglist_len
328                            being accumulated by an outer function call.  */
329                         { start_arglist (); }
330                 arglist ')'     %prec ARROW
331                         { write_exp_elt_opcode (OP_FUNCALL);
332                           write_exp_elt_longcst ((LONGEST) end_arglist ());
333                           write_exp_elt_opcode (OP_FUNCALL); }
334         ;
335
336 lcurly  :       '{'
337                         { start_arglist (); }
338         ;
339
340 arglist :
341         ;
342
343 arglist :       exp
344                         { arglist_len = 1; }
345         ;
346
347 arglist :       arglist ',' exp   %prec ABOVE_COMMA
348                         { arglist_len++; }
349         ;
350
351 rcurly  :       '}'
352                         { $$ = end_arglist () - 1; }
353         ;
354 exp     :       lcurly arglist rcurly   %prec ARROW
355                         { write_exp_elt_opcode (OP_ARRAY);
356                           write_exp_elt_longcst ((LONGEST) 0);
357                           write_exp_elt_longcst ((LONGEST) $3);
358                           write_exp_elt_opcode (OP_ARRAY); }
359         ;
360
361 exp     :       lcurly type rcurly exp  %prec UNARY
362                         { write_exp_elt_opcode (UNOP_MEMVAL);
363                           write_exp_elt_type ($2);
364                           write_exp_elt_opcode (UNOP_MEMVAL); }
365         ;
366
367 exp     :       '(' type ')' exp  %prec UNARY
368                         { write_exp_elt_opcode (UNOP_CAST);
369                           write_exp_elt_type ($2);
370                           write_exp_elt_opcode (UNOP_CAST); }
371         ;
372
373 exp     :       '(' exp1 ')'
374                         { }
375         ;
376
377 /* Binary operators in order of decreasing precedence.  */
378
379 exp     :       exp '@' exp
380                         { write_exp_elt_opcode (BINOP_REPEAT); }
381         ;
382
383 exp     :       exp '*' exp
384                         { write_exp_elt_opcode (BINOP_MUL); }
385         ;
386
387 exp     :       exp '/' exp
388                         { write_exp_elt_opcode (BINOP_DIV); }
389         ;
390
391 exp     :       exp '%' exp
392                         { write_exp_elt_opcode (BINOP_REM); }
393         ;
394
395 exp     :       exp '+' exp
396                         { write_exp_elt_opcode (BINOP_ADD); }
397         ;
398
399 exp     :       exp '-' exp
400                         { write_exp_elt_opcode (BINOP_SUB); }
401         ;
402
403 exp     :       exp LSH exp
404                         { write_exp_elt_opcode (BINOP_LSH); }
405         ;
406
407 exp     :       exp RSH exp
408                         { write_exp_elt_opcode (BINOP_RSH); }
409         ;
410
411 exp     :       exp EQUAL exp
412                         { write_exp_elt_opcode (BINOP_EQUAL); }
413         ;
414
415 exp     :       exp NOTEQUAL exp
416                         { write_exp_elt_opcode (BINOP_NOTEQUAL); }
417         ;
418
419 exp     :       exp LEQ exp
420                         { write_exp_elt_opcode (BINOP_LEQ); }
421         ;
422
423 exp     :       exp GEQ exp
424                         { write_exp_elt_opcode (BINOP_GEQ); }
425         ;
426
427 exp     :       exp '<' exp
428                         { write_exp_elt_opcode (BINOP_LESS); }
429         ;
430
431 exp     :       exp '>' exp
432                         { write_exp_elt_opcode (BINOP_GTR); }
433         ;
434
435 exp     :       exp '&' exp
436                         { write_exp_elt_opcode (BINOP_BITWISE_AND); }
437         ;
438
439 exp     :       exp '^' exp
440                         { write_exp_elt_opcode (BINOP_BITWISE_XOR); }
441         ;
442
443 exp     :       exp '|' exp
444                         { write_exp_elt_opcode (BINOP_BITWISE_IOR); }
445         ;
446
447 exp     :       exp ANDAND exp
448                         { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
449         ;
450
451 exp     :       exp OROR exp
452                         { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
453         ;
454
455 exp     :       exp '?' exp ':' exp     %prec '?'
456                         { write_exp_elt_opcode (TERNOP_COND); }
457         ;
458                           
459 exp     :       exp '=' exp
460                         { write_exp_elt_opcode (BINOP_ASSIGN); }
461         ;
462
463 exp     :       exp ASSIGN_MODIFY exp
464                         { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
465                           write_exp_elt_opcode ($2);
466                           write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
467         ;
468
469 exp     :       INT
470                         { write_exp_elt_opcode (OP_LONG);
471                           write_exp_elt_type ($1.type);
472                           write_exp_elt_longcst ((LONGEST)($1.val));
473                           write_exp_elt_opcode (OP_LONG); }
474         ;
475
476 exp     :       NAME_OR_INT
477                         { YYSTYPE val;
478                           parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
479                           write_exp_elt_opcode (OP_LONG);
480                           write_exp_elt_type (val.typed_val_int.type);
481                           write_exp_elt_longcst ((LONGEST)val.typed_val_int.val);
482                           write_exp_elt_opcode (OP_LONG);
483                         }
484         ;
485
486
487 exp     :       FLOAT
488                         { write_exp_elt_opcode (OP_DOUBLE);
489                           write_exp_elt_type ($1.type);
490                           write_exp_elt_dblcst ($1.dval);
491                           write_exp_elt_opcode (OP_DOUBLE); }
492         ;
493
494 exp     :       variable
495         ;
496
497 exp     :       VARIABLE
498                         /* Already written by write_dollar_variable. */
499         ;
500
501 exp     :       SIZEOF '(' type ')'     %prec UNARY
502                         { write_exp_elt_opcode (OP_LONG);
503                           write_exp_elt_type (builtin_type_int);
504                           CHECK_TYPEDEF ($3);
505                           write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
506                           write_exp_elt_opcode (OP_LONG); }
507         ;
508
509 exp     :       STRING
510                         { /* C strings are converted into array constants with
511                              an explicit null byte added at the end.  Thus
512                              the array upper bound is the string length.
513                              There is no such thing in C as a completely empty
514                              string. */
515                           char *sp = $1.ptr; int count = $1.length;
516                           while (count-- > 0)
517                             {
518                               write_exp_elt_opcode (OP_LONG);
519                               write_exp_elt_type (builtin_type_char);
520                               write_exp_elt_longcst ((LONGEST)(*sp++));
521                               write_exp_elt_opcode (OP_LONG);
522                             }
523                           write_exp_elt_opcode (OP_LONG);
524                           write_exp_elt_type (builtin_type_char);
525                           write_exp_elt_longcst ((LONGEST)'\0');
526                           write_exp_elt_opcode (OP_LONG);
527                           write_exp_elt_opcode (OP_ARRAY);
528                           write_exp_elt_longcst ((LONGEST) 0);
529                           write_exp_elt_longcst ((LONGEST) ($1.length));
530                           write_exp_elt_opcode (OP_ARRAY); }
531         ;
532
533 /* C++.  */
534 exp     :       THIS
535                         { write_exp_elt_opcode (OP_THIS);
536                           write_exp_elt_opcode (OP_THIS); }
537         ;
538
539 exp     :       TRUEKEYWORD    
540                         { write_exp_elt_opcode (OP_LONG);
541                           write_exp_elt_type (builtin_type_bool);
542                           write_exp_elt_longcst ((LONGEST) 1);
543                           write_exp_elt_opcode (OP_LONG); }
544         ;
545
546 exp     :       FALSEKEYWORD   
547                         { write_exp_elt_opcode (OP_LONG);
548                           write_exp_elt_type (builtin_type_bool);
549                           write_exp_elt_longcst ((LONGEST) 0);
550                           write_exp_elt_opcode (OP_LONG); }
551         ;
552
553 /* end of C++.  */
554
555 block   :       BLOCKNAME
556                         {
557                           if ($1.sym)
558                             $$ = SYMBOL_BLOCK_VALUE ($1.sym);
559                           else
560                             error ("No file or function \"%s\".",
561                                    copy_name ($1.stoken));
562                         }
563         |       FILENAME
564                         {
565                           $$ = $1;
566                         }
567         ;
568
569 block   :       block COLONCOLON name
570                         { struct symbol *tem
571                             = lookup_symbol (copy_name ($3), $1,
572                                              VAR_NAMESPACE, (int *) NULL,
573                                              (struct symtab **) NULL);
574                           if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
575                             error ("No function \"%s\" in specified context.",
576                                    copy_name ($3));
577                           $$ = SYMBOL_BLOCK_VALUE (tem); }
578         ;
579
580 variable:       block COLONCOLON name
581                         { struct symbol *sym;
582                           sym = lookup_symbol (copy_name ($3), $1,
583                                                VAR_NAMESPACE, (int *) NULL,
584                                                (struct symtab **) NULL);
585                           if (sym == 0)
586                             error ("No symbol \"%s\" in specified context.",
587                                    copy_name ($3));
588
589                           write_exp_elt_opcode (OP_VAR_VALUE);
590                           /* block_found is set by lookup_symbol.  */
591                           write_exp_elt_block (block_found);
592                           write_exp_elt_sym (sym);
593                           write_exp_elt_opcode (OP_VAR_VALUE); }
594         ;
595
596 qualified_name: typebase COLONCOLON name
597                         {
598                           struct type *type = $1;
599                           if (TYPE_CODE (type) != TYPE_CODE_STRUCT
600                               && TYPE_CODE (type) != TYPE_CODE_UNION)
601                             error ("`%s' is not defined as an aggregate type.",
602                                    TYPE_NAME (type));
603
604                           write_exp_elt_opcode (OP_SCOPE);
605                           write_exp_elt_type (type);
606                           write_exp_string ($3);
607                           write_exp_elt_opcode (OP_SCOPE);
608                         }
609         |       typebase COLONCOLON '~' name
610                         {
611                           struct type *type = $1;
612                           struct stoken tmp_token;
613                           if (TYPE_CODE (type) != TYPE_CODE_STRUCT
614                               && TYPE_CODE (type) != TYPE_CODE_UNION)
615                             error ("`%s' is not defined as an aggregate type.",
616                                    TYPE_NAME (type));
617
618                           tmp_token.ptr = (char*) alloca ($4.length + 2);
619                           tmp_token.length = $4.length + 1;
620                           tmp_token.ptr[0] = '~';
621                           memcpy (tmp_token.ptr+1, $4.ptr, $4.length);
622                           tmp_token.ptr[tmp_token.length] = 0;
623
624                           /* Check for valid destructor name.  */
625                           destructor_name_p (tmp_token.ptr, type);
626                           write_exp_elt_opcode (OP_SCOPE);
627                           write_exp_elt_type (type);
628                           write_exp_string (tmp_token);
629                           write_exp_elt_opcode (OP_SCOPE);
630                         }
631         ;
632
633 variable:       qualified_name
634         |       COLONCOLON name
635                         {
636                           char *name = copy_name ($2);
637                           struct symbol *sym;
638                           struct minimal_symbol *msymbol;
639
640                           sym =
641                             lookup_symbol (name, (const struct block *) NULL,
642                                            VAR_NAMESPACE, (int *) NULL,
643                                            (struct symtab **) NULL);
644                           if (sym)
645                             {
646                               write_exp_elt_opcode (OP_VAR_VALUE);
647                               write_exp_elt_block (NULL);
648                               write_exp_elt_sym (sym);
649                               write_exp_elt_opcode (OP_VAR_VALUE);
650                               break;
651                             }
652
653                           msymbol = lookup_minimal_symbol (name, NULL, NULL);
654                           if (msymbol != NULL)
655                             {
656                               write_exp_msymbol (msymbol,
657                                                  lookup_function_type (builtin_type_int),
658                                                  builtin_type_int);
659                             }
660                           else
661                             if (!have_full_symbols () && !have_partial_symbols ())
662                               error ("No symbol table is loaded.  Use the \"file\" command.");
663                             else
664                               error ("No symbol \"%s\" in current context.", name);
665                         }
666         ;
667
668 variable:       name_not_typename
669                         { struct symbol *sym = $1.sym;
670
671                           if (sym)
672                             {
673                               if (symbol_read_needs_frame (sym))
674                                 {
675                                   if (innermost_block == 0 ||
676                                       contained_in (block_found, 
677                                                     innermost_block))
678                                     innermost_block = block_found;
679                                 }
680
681                               write_exp_elt_opcode (OP_VAR_VALUE);
682                               /* We want to use the selected frame, not
683                                  another more inner frame which happens to
684                                  be in the same block.  */
685                               write_exp_elt_block (NULL);
686                               write_exp_elt_sym (sym);
687                               write_exp_elt_opcode (OP_VAR_VALUE);
688                             }
689                           else if ($1.is_a_field_of_this)
690                             {
691                               /* C++: it hangs off of `this'.  Must
692                                  not inadvertently convert from a method call
693                                  to data ref.  */
694                               if (innermost_block == 0 || 
695                                   contained_in (block_found, innermost_block))
696                                 innermost_block = block_found;
697                               write_exp_elt_opcode (OP_THIS);
698                               write_exp_elt_opcode (OP_THIS);
699                               write_exp_elt_opcode (STRUCTOP_PTR);
700                               write_exp_string ($1.stoken);
701                               write_exp_elt_opcode (STRUCTOP_PTR);
702                             }
703                           else
704                             {
705                               struct minimal_symbol *msymbol;
706                               register char *arg = copy_name ($1.stoken);
707
708                               msymbol =
709                                 lookup_minimal_symbol (arg, NULL, NULL);
710                               if (msymbol != NULL)
711                                 {
712                                   write_exp_msymbol (msymbol,
713                                                      lookup_function_type (builtin_type_int),
714                                                      builtin_type_int);
715                                 }
716                               else if (!have_full_symbols () && !have_partial_symbols ())
717                                 error ("No symbol table is loaded.  Use the \"file\" command.");
718                               else
719                                 error ("No symbol \"%s\" in current context.",
720                                        copy_name ($1.stoken));
721                             }
722                         }
723         ;
724
725 space_identifier : '@' NAME
726                 { push_type_address_space (copy_name ($2.stoken));
727                   push_type (tp_space_identifier);
728                 }
729         ;
730
731 const_or_volatile: const_or_volatile_noopt
732         |
733         ;
734
735 cv_with_space_id : const_or_volatile space_identifier const_or_volatile
736         ;
737
738 const_or_volatile_or_space_identifier_noopt: cv_with_space_id
739         | const_or_volatile_noopt 
740         ;
741
742 const_or_volatile_or_space_identifier: 
743                 const_or_volatile_or_space_identifier_noopt
744         |
745         ;
746
747 abs_decl:       '*'
748                         { push_type (tp_pointer); $$ = 0; }
749         |       '*' abs_decl
750                         { push_type (tp_pointer); $$ = $2; }
751         |       '&'
752                         { push_type (tp_reference); $$ = 0; }
753         |       '&' abs_decl
754                         { push_type (tp_reference); $$ = $2; }
755         |       direct_abs_decl
756         ;
757
758 direct_abs_decl: '(' abs_decl ')'
759                         { $$ = $2; }
760         |       direct_abs_decl array_mod
761                         {
762                           push_type_int ($2);
763                           push_type (tp_array);
764                         }
765         |       array_mod
766                         {
767                           push_type_int ($1);
768                           push_type (tp_array);
769                           $$ = 0;
770                         }
771
772         |       direct_abs_decl func_mod
773                         { push_type (tp_function); }
774         |       func_mod
775                         { push_type (tp_function); }
776         ;
777
778 array_mod:      '[' ']'
779                         { $$ = -1; }
780         |       '[' INT ']'
781                         { $$ = $2.val; }
782         ;
783
784 func_mod:       '(' ')'
785                         { $$ = 0; }
786         |       '(' nonempty_typelist ')'
787                         { free ((PTR)$2); $$ = 0; }
788         ;
789
790 /* We used to try to recognize more pointer to member types here, but
791    that didn't work (shift/reduce conflicts meant that these rules never
792    got executed).  The problem is that
793      int (foo::bar::baz::bizzle)
794    is a function type but
795      int (foo::bar::baz::bizzle::*)
796    is a pointer to member type.  Stroustrup loses again!  */
797
798 type    :       ptype
799         |       typebase COLONCOLON '*'
800                         { $$ = lookup_member_type (builtin_type_int, $1); }
801         ;
802
803 typebase  /* Implements (approximately): (type-qualifier)* type-specifier */
804         :       TYPENAME
805                         { $$ = $1.type; }
806         |       INT_KEYWORD
807                         { $$ = builtin_type_int; }
808         |       LONG
809                         { $$ = builtin_type_long; }
810         |       SHORT
811                         { $$ = builtin_type_short; }
812         |       LONG INT_KEYWORD
813                         { $$ = builtin_type_long; }
814         |       LONG SIGNED_KEYWORD INT_KEYWORD
815                         { $$ = builtin_type_long; }
816         |       LONG SIGNED_KEYWORD
817                         { $$ = builtin_type_long; }
818         |       SIGNED_KEYWORD LONG INT_KEYWORD
819                         { $$ = builtin_type_long; }
820         |       UNSIGNED LONG INT_KEYWORD
821                         { $$ = builtin_type_unsigned_long; }
822         |       LONG UNSIGNED INT_KEYWORD
823                         { $$ = builtin_type_unsigned_long; }
824         |       LONG UNSIGNED
825                         { $$ = builtin_type_unsigned_long; }
826         |       LONG LONG
827                         { $$ = builtin_type_long_long; }
828         |       LONG LONG INT_KEYWORD
829                         { $$ = builtin_type_long_long; }
830         |       LONG LONG SIGNED_KEYWORD INT_KEYWORD
831                         { $$ = builtin_type_long_long; }
832         |       LONG LONG SIGNED_KEYWORD
833                         { $$ = builtin_type_long_long; }
834         |       SIGNED_KEYWORD LONG LONG
835                         { $$ = builtin_type_long_long; }
836         |       UNSIGNED LONG LONG
837                         { $$ = builtin_type_unsigned_long_long; }
838         |       UNSIGNED LONG LONG INT_KEYWORD
839                         { $$ = builtin_type_unsigned_long_long; }
840         |       LONG LONG UNSIGNED
841                         { $$ = builtin_type_unsigned_long_long; }
842         |       LONG LONG UNSIGNED INT_KEYWORD
843                         { $$ = builtin_type_unsigned_long_long; }
844         |       SIGNED_KEYWORD LONG LONG
845                         { $$ = lookup_signed_typename ("long long"); }
846         |       SIGNED_KEYWORD LONG LONG INT_KEYWORD
847                         { $$ = lookup_signed_typename ("long long"); }
848         |       SHORT INT_KEYWORD
849                         { $$ = builtin_type_short; }
850         |       SHORT SIGNED_KEYWORD INT_KEYWORD
851                         { $$ = builtin_type_short; }
852         |       SHORT SIGNED_KEYWORD
853                         { $$ = builtin_type_short; }
854         |       UNSIGNED SHORT INT_KEYWORD
855                         { $$ = builtin_type_unsigned_short; }
856         |       SHORT UNSIGNED 
857                         { $$ = builtin_type_unsigned_short; }
858         |       SHORT UNSIGNED INT_KEYWORD
859                         { $$ = builtin_type_unsigned_short; }
860         |       DOUBLE_KEYWORD
861                         { $$ = builtin_type_double; }
862         |       LONG DOUBLE_KEYWORD
863                         { $$ = builtin_type_long_double; }
864         |       STRUCT name
865                         { $$ = lookup_struct (copy_name ($2),
866                                               expression_context_block); }
867         |       CLASS name
868                         { $$ = lookup_struct (copy_name ($2),
869                                               expression_context_block); }
870         |       UNION name
871                         { $$ = lookup_union (copy_name ($2),
872                                              expression_context_block); }
873         |       ENUM name
874                         { $$ = lookup_enum (copy_name ($2),
875                                             expression_context_block); }
876         |       UNSIGNED typename
877                         { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
878         |       UNSIGNED
879                         { $$ = builtin_type_unsigned_int; }
880         |       SIGNED_KEYWORD typename
881                         { $$ = lookup_signed_typename (TYPE_NAME($2.type)); }
882         |       SIGNED_KEYWORD
883                         { $$ = builtin_type_int; }
884                 /* It appears that this rule for templates is never
885                    reduced; template recognition happens by lookahead
886                    in the token processing code in yylex. */         
887         |       TEMPLATE name '<' type '>'
888                         { $$ = lookup_template_type(copy_name($2), $4,
889                                                     expression_context_block);
890                         }
891         | const_or_volatile_or_space_identifier_noopt typebase 
892                         { $$ = follow_types ($2); }
893         | typebase const_or_volatile_or_space_identifier_noopt 
894                         { $$ = follow_types ($1); }
895         ;
896
897 typename:       TYPENAME
898         |       INT_KEYWORD
899                 {
900                   $$.stoken.ptr = "int";
901                   $$.stoken.length = 3;
902                   $$.type = builtin_type_int;
903                 }
904         |       LONG
905                 {
906                   $$.stoken.ptr = "long";
907                   $$.stoken.length = 4;
908                   $$.type = builtin_type_long;
909                 }
910         |       SHORT
911                 {
912                   $$.stoken.ptr = "short";
913                   $$.stoken.length = 5;
914                   $$.type = builtin_type_short;
915                 }
916         ;
917
918 nonempty_typelist
919         :       type
920                 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
921                   $<ivec>$[0] = 1;      /* Number of types in vector */
922                   $$[1] = $1;
923                 }
924         |       nonempty_typelist ',' type
925                 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
926                   $$ = (struct type **) realloc ((char *) $1, len);
927                   $$[$<ivec>$[0]] = $3;
928                 }
929         ;
930
931 ptype   :       typebase
932         |       ptype const_or_volatile_or_space_identifier abs_decl const_or_volatile_or_space_identifier
933                 { $$ = follow_types ($1); }
934         ;
935
936 const_and_volatile:     CONST_KEYWORD VOLATILE_KEYWORD
937         |               VOLATILE_KEYWORD CONST_KEYWORD
938         ;
939
940 const_or_volatile_noopt:        const_and_volatile 
941                         { push_type (tp_const);
942                           push_type (tp_volatile); 
943                         }
944         |               CONST_KEYWORD
945                         { push_type (tp_const); }
946         |               VOLATILE_KEYWORD
947                         { push_type (tp_volatile); }
948         ;
949
950 name    :       NAME { $$ = $1.stoken; }
951         |       BLOCKNAME { $$ = $1.stoken; }
952         |       TYPENAME { $$ = $1.stoken; }
953         |       NAME_OR_INT  { $$ = $1.stoken; }
954         ;
955
956 name_not_typename :     NAME
957         |       BLOCKNAME
958 /* These would be useful if name_not_typename was useful, but it is just
959    a fake for "variable", so these cause reduce/reduce conflicts because
960    the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
961    =exp) or just an exp.  If name_not_typename was ever used in an lvalue
962    context where only a name could occur, this might be useful.
963         |       NAME_OR_INT
964  */
965         ;
966
967 %%
968
969 /* Take care of parsing a number (anything that starts with a digit).
970    Set yylval and return the token type; update lexptr.
971    LEN is the number of characters in it.  */
972
973 /*** Needs some error checking for the float case ***/
974
975 static int
976 parse_number (p, len, parsed_float, putithere)
977      register char *p;
978      register int len;
979      int parsed_float;
980      YYSTYPE *putithere;
981 {
982   /* FIXME: Shouldn't these be unsigned?  We don't deal with negative values
983      here, and we do kind of silly things like cast to unsigned.  */
984   register LONGEST n = 0;
985   register LONGEST prevn = 0;
986   ULONGEST un;
987
988   register int i = 0;
989   register int c;
990   register int base = input_radix;
991   int unsigned_p = 0;
992
993   /* Number of "L" suffixes encountered.  */
994   int long_p = 0;
995
996   /* We have found a "L" or "U" suffix.  */
997   int found_suffix = 0;
998
999   ULONGEST high_bit;
1000   struct type *signed_type;
1001   struct type *unsigned_type;
1002
1003   if (parsed_float)
1004     {
1005       /* It's a float since it contains a point or an exponent.  */
1006       char c;
1007       int num = 0;      /* number of tokens scanned by scanf */
1008       char saved_char = p[len];
1009
1010       p[len] = 0;       /* null-terminate the token */
1011       if (sizeof (putithere->typed_val_float.dval) <= sizeof (float))
1012         num = sscanf (p, "%g%c", (float *) &putithere->typed_val_float.dval,&c);
1013       else if (sizeof (putithere->typed_val_float.dval) <= sizeof (double))
1014         num = sscanf (p, "%lg%c", (double *) &putithere->typed_val_float.dval,&c);
1015       else
1016         {
1017 #ifdef SCANF_HAS_LONG_DOUBLE
1018           num = sscanf (p, "%Lg%c", &putithere->typed_val_float.dval,&c);
1019 #else
1020           /* Scan it into a double, then assign it to the long double.
1021              This at least wins with values representable in the range
1022              of doubles. */
1023           double temp;
1024           num = sscanf (p, "%lg%c", &temp,&c);
1025           putithere->typed_val_float.dval = temp;
1026 #endif
1027         }
1028       p[len] = saved_char;      /* restore the input stream */
1029       if (num != 1)             /* check scanf found ONLY a float ... */
1030         return ERROR;
1031       /* See if it has `f' or `l' suffix (float or long double).  */
1032
1033       c = tolower (p[len - 1]);
1034
1035       if (c == 'f')
1036         putithere->typed_val_float.type = builtin_type_float;
1037       else if (c == 'l')
1038         putithere->typed_val_float.type = builtin_type_long_double;
1039       else if (isdigit (c) || c == '.')
1040         putithere->typed_val_float.type = builtin_type_double;
1041       else
1042         return ERROR;
1043
1044       return FLOAT;
1045     }
1046
1047   /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
1048   if (p[0] == '0')
1049     switch (p[1])
1050       {
1051       case 'x':
1052       case 'X':
1053         if (len >= 3)
1054           {
1055             p += 2;
1056             base = 16;
1057             len -= 2;
1058           }
1059         break;
1060
1061       case 't':
1062       case 'T':
1063       case 'd':
1064       case 'D':
1065         if (len >= 3)
1066           {
1067             p += 2;
1068             base = 10;
1069             len -= 2;
1070           }
1071         break;
1072
1073       default:
1074         base = 8;
1075         break;
1076       }
1077
1078   while (len-- > 0)
1079     {
1080       c = *p++;
1081       if (c >= 'A' && c <= 'Z')
1082         c += 'a' - 'A';
1083       if (c != 'l' && c != 'u')
1084         n *= base;
1085       if (c >= '0' && c <= '9')
1086         {
1087           if (found_suffix)
1088             return ERROR;
1089           n += i = c - '0';
1090         }
1091       else
1092         {
1093           if (base > 10 && c >= 'a' && c <= 'f')
1094             {
1095               if (found_suffix)
1096                 return ERROR;
1097               n += i = c - 'a' + 10;
1098             }
1099           else if (c == 'l')
1100             {
1101               ++long_p;
1102               found_suffix = 1;
1103             }
1104           else if (c == 'u')
1105             {
1106               unsigned_p = 1;
1107               found_suffix = 1;
1108             }
1109           else
1110             return ERROR;       /* Char not a digit */
1111         }
1112       if (i >= base)
1113         return ERROR;           /* Invalid digit in this base */
1114
1115       /* Portably test for overflow (only works for nonzero values, so make
1116          a second check for zero).  FIXME: Can't we just make n and prevn
1117          unsigned and avoid this?  */
1118       if (c != 'l' && c != 'u' && (prevn >= n) && n != 0)
1119         unsigned_p = 1;         /* Try something unsigned */
1120
1121       /* Portably test for unsigned overflow.
1122          FIXME: This check is wrong; for example it doesn't find overflow
1123          on 0x123456789 when LONGEST is 32 bits.  */
1124       if (c != 'l' && c != 'u' && n != 0)
1125         {       
1126           if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n))
1127             error ("Numeric constant too large.");
1128         }
1129       prevn = n;
1130     }
1131
1132   /* An integer constant is an int, a long, or a long long.  An L
1133      suffix forces it to be long; an LL suffix forces it to be long
1134      long.  If not forced to a larger size, it gets the first type of
1135      the above that it fits in.  To figure out whether it fits, we
1136      shift it right and see whether anything remains.  Note that we
1137      can't shift sizeof (LONGEST) * HOST_CHAR_BIT bits or more in one
1138      operation, because many compilers will warn about such a shift
1139      (which always produces a zero result).  Sometimes TARGET_INT_BIT
1140      or TARGET_LONG_BIT will be that big, sometimes not.  To deal with
1141      the case where it is we just always shift the value more than
1142      once, with fewer bits each time.  */
1143
1144   un = (ULONGEST)n >> 2;
1145   if (long_p == 0
1146       && (un >> (TARGET_INT_BIT - 2)) == 0)
1147     {
1148       high_bit = ((ULONGEST)1) << (TARGET_INT_BIT-1);
1149
1150       /* A large decimal (not hex or octal) constant (between INT_MAX
1151          and UINT_MAX) is a long or unsigned long, according to ANSI,
1152          never an unsigned int, but this code treats it as unsigned
1153          int.  This probably should be fixed.  GCC gives a warning on
1154          such constants.  */
1155
1156       unsigned_type = builtin_type_unsigned_int;
1157       signed_type = builtin_type_int;
1158     }
1159   else if (long_p <= 1
1160            && (un >> (TARGET_LONG_BIT - 2)) == 0)
1161     {
1162       high_bit = ((ULONGEST)1) << (TARGET_LONG_BIT-1);
1163       unsigned_type = builtin_type_unsigned_long;
1164       signed_type = builtin_type_long;
1165     }
1166   else
1167     {
1168       int shift;
1169       if (sizeof (ULONGEST) * HOST_CHAR_BIT < TARGET_LONG_LONG_BIT)
1170         /* A long long does not fit in a LONGEST.  */
1171         shift = (sizeof (ULONGEST) * HOST_CHAR_BIT - 1);
1172       else
1173         shift = (TARGET_LONG_LONG_BIT - 1);
1174       high_bit = (ULONGEST) 1 << shift;
1175       unsigned_type = builtin_type_unsigned_long_long;
1176       signed_type = builtin_type_long_long;
1177     }
1178
1179    putithere->typed_val_int.val = n;
1180
1181    /* If the high bit of the worked out type is set then this number
1182       has to be unsigned. */
1183
1184    if (unsigned_p || (n & high_bit)) 
1185      {
1186        putithere->typed_val_int.type = unsigned_type;
1187      }
1188    else 
1189      {
1190        putithere->typed_val_int.type = signed_type;
1191      }
1192
1193    return INT;
1194 }
1195
1196 struct token
1197 {
1198   char *operator;
1199   int token;
1200   enum exp_opcode opcode;
1201 };
1202
1203 static const struct token tokentab3[] =
1204   {
1205     {">>=", ASSIGN_MODIFY, BINOP_RSH},
1206     {"<<=", ASSIGN_MODIFY, BINOP_LSH}
1207   };
1208
1209 static const struct token tokentab2[] =
1210   {
1211     {"+=", ASSIGN_MODIFY, BINOP_ADD},
1212     {"-=", ASSIGN_MODIFY, BINOP_SUB},
1213     {"*=", ASSIGN_MODIFY, BINOP_MUL},
1214     {"/=", ASSIGN_MODIFY, BINOP_DIV},
1215     {"%=", ASSIGN_MODIFY, BINOP_REM},
1216     {"|=", ASSIGN_MODIFY, BINOP_BITWISE_IOR},
1217     {"&=", ASSIGN_MODIFY, BINOP_BITWISE_AND},
1218     {"^=", ASSIGN_MODIFY, BINOP_BITWISE_XOR},
1219     {"++", INCREMENT, BINOP_END},
1220     {"--", DECREMENT, BINOP_END},
1221     {"->", ARROW, BINOP_END},
1222     {"&&", ANDAND, BINOP_END},
1223     {"||", OROR, BINOP_END},
1224     {"::", COLONCOLON, BINOP_END},
1225     {"<<", LSH, BINOP_END},
1226     {">>", RSH, BINOP_END},
1227     {"==", EQUAL, BINOP_END},
1228     {"!=", NOTEQUAL, BINOP_END},
1229     {"<=", LEQ, BINOP_END},
1230     {">=", GEQ, BINOP_END}
1231   };
1232
1233 /* Read one token, getting characters through lexptr.  */
1234
1235 static int
1236 yylex ()
1237 {
1238   int c;
1239   int namelen;
1240   unsigned int i;
1241   char *tokstart;
1242   char *tokptr;
1243   int tempbufindex;
1244   static char *tempbuf;
1245   static int tempbufsize;
1246   struct symbol * sym_class = NULL;
1247   char * token_string = NULL;
1248   int class_prefix = 0;
1249   int unquoted_expr;
1250    
1251  retry:
1252
1253   /* Check if this is a macro invocation that we need to expand.  */
1254   if (! scanning_macro_expansion ())
1255     {
1256       char *expanded = macro_expand_next (&lexptr,
1257                                           expression_macro_lookup_func,
1258                                           expression_macro_lookup_baton);
1259
1260       if (expanded)
1261         scan_macro_expansion (expanded);
1262     }
1263
1264   prev_lexptr = lexptr;
1265   unquoted_expr = 1;
1266
1267   tokstart = lexptr;
1268   /* See if it is a special token of length 3.  */
1269   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
1270     if (STREQN (tokstart, tokentab3[i].operator, 3))
1271       {
1272         lexptr += 3;
1273         yylval.opcode = tokentab3[i].opcode;
1274         return tokentab3[i].token;
1275       }
1276
1277   /* See if it is a special token of length 2.  */
1278   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
1279     if (STREQN (tokstart, tokentab2[i].operator, 2))
1280       {
1281         lexptr += 2;
1282         yylval.opcode = tokentab2[i].opcode;
1283         return tokentab2[i].token;
1284       }
1285
1286   switch (c = *tokstart)
1287     {
1288     case 0:
1289       /* If we were just scanning the result of a macro expansion,
1290          then we need to resume scanning the original text.
1291          Otherwise, we were already scanning the original text, and
1292          we're really done.  */
1293       if (scanning_macro_expansion ())
1294         {
1295           finished_macro_expansion ();
1296           goto retry;
1297         }
1298       else
1299         return 0;
1300
1301     case ' ':
1302     case '\t':
1303     case '\n':
1304       lexptr++;
1305       goto retry;
1306
1307     case '\'':
1308       /* We either have a character constant ('0' or '\177' for example)
1309          or we have a quoted symbol reference ('foo(int,int)' in C++
1310          for example). */
1311       lexptr++;
1312       c = *lexptr++;
1313       if (c == '\\')
1314         c = parse_escape (&lexptr);
1315       else if (c == '\'')
1316         error ("Empty character constant.");
1317
1318       yylval.typed_val_int.val = c;
1319       yylval.typed_val_int.type = builtin_type_char;
1320
1321       c = *lexptr++;
1322       if (c != '\'')
1323         {
1324           namelen = skip_quoted (tokstart) - tokstart;
1325           if (namelen > 2)
1326             {
1327               lexptr = tokstart + namelen;
1328               unquoted_expr = 0;
1329               if (lexptr[-1] != '\'')
1330                 error ("Unmatched single quote.");
1331               namelen -= 2;
1332               tokstart++;
1333               goto tryname;
1334             }
1335           error ("Invalid character constant.");
1336         }
1337       return INT;
1338
1339     case '(':
1340       paren_depth++;
1341       lexptr++;
1342       return c;
1343
1344     case ')':
1345       if (paren_depth == 0)
1346         return 0;
1347       paren_depth--;
1348       lexptr++;
1349       return c;
1350
1351     case ',':
1352       if (comma_terminates
1353           && paren_depth == 0
1354           && ! scanning_macro_expansion ())
1355         return 0;
1356       lexptr++;
1357       return c;
1358
1359     case '.':
1360       /* Might be a floating point number.  */
1361       if (lexptr[1] < '0' || lexptr[1] > '9')
1362         goto symbol;            /* Nope, must be a symbol. */
1363       /* FALL THRU into number case.  */
1364
1365     case '0':
1366     case '1':
1367     case '2':
1368     case '3':
1369     case '4':
1370     case '5':
1371     case '6':
1372     case '7':
1373     case '8':
1374     case '9':
1375       {
1376         /* It's a number.  */
1377         int got_dot = 0, got_e = 0, toktype;
1378         register char *p = tokstart;
1379         int hex = input_radix > 10;
1380
1381         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
1382           {
1383             p += 2;
1384             hex = 1;
1385           }
1386         else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
1387           {
1388             p += 2;
1389             hex = 0;
1390           }
1391
1392         for (;; ++p)
1393           {
1394             /* This test includes !hex because 'e' is a valid hex digit
1395                and thus does not indicate a floating point number when
1396                the radix is hex.  */
1397             if (!hex && !got_e && (*p == 'e' || *p == 'E'))
1398               got_dot = got_e = 1;
1399             /* This test does not include !hex, because a '.' always indicates
1400                a decimal floating point number regardless of the radix.  */
1401             else if (!got_dot && *p == '.')
1402               got_dot = 1;
1403             else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
1404                      && (*p == '-' || *p == '+'))
1405               /* This is the sign of the exponent, not the end of the
1406                  number.  */
1407               continue;
1408             /* We will take any letters or digits.  parse_number will
1409                complain if past the radix, or if L or U are not final.  */
1410             else if ((*p < '0' || *p > '9')
1411                      && ((*p < 'a' || *p > 'z')
1412                                   && (*p < 'A' || *p > 'Z')))
1413               break;
1414           }
1415         toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
1416         if (toktype == ERROR)
1417           {
1418             char *err_copy = (char *) alloca (p - tokstart + 1);
1419
1420             memcpy (err_copy, tokstart, p - tokstart);
1421             err_copy[p - tokstart] = 0;
1422             error ("Invalid number \"%s\".", err_copy);
1423           }
1424         lexptr = p;
1425         return toktype;
1426       }
1427
1428     case '+':
1429     case '-':
1430     case '*':
1431     case '/':
1432     case '%':
1433     case '|':
1434     case '&':
1435     case '^':
1436     case '~':
1437     case '!':
1438     case '@':
1439     case '<':
1440     case '>':
1441     case '[':
1442     case ']':
1443     case '?':
1444     case ':':
1445     case '=':
1446     case '{':
1447     case '}':
1448     symbol:
1449       lexptr++;
1450       return c;
1451
1452     case '"':
1453
1454       /* Build the gdb internal form of the input string in tempbuf,
1455          translating any standard C escape forms seen.  Note that the
1456          buffer is null byte terminated *only* for the convenience of
1457          debugging gdb itself and printing the buffer contents when
1458          the buffer contains no embedded nulls.  Gdb does not depend
1459          upon the buffer being null byte terminated, it uses the length
1460          string instead.  This allows gdb to handle C strings (as well
1461          as strings in other languages) with embedded null bytes */
1462
1463       tokptr = ++tokstart;
1464       tempbufindex = 0;
1465
1466       do {
1467         /* Grow the static temp buffer if necessary, including allocating
1468            the first one on demand. */
1469         if (tempbufindex + 1 >= tempbufsize)
1470           {
1471             tempbuf = (char *) realloc (tempbuf, tempbufsize += 64);
1472           }
1473         switch (*tokptr)
1474           {
1475           case '\0':
1476           case '"':
1477             /* Do nothing, loop will terminate. */
1478             break;
1479           case '\\':
1480             tokptr++;
1481             c = parse_escape (&tokptr);
1482             if (c == -1)
1483               {
1484                 continue;
1485               }
1486             tempbuf[tempbufindex++] = c;
1487             break;
1488           default:
1489             tempbuf[tempbufindex++] = *tokptr++;
1490             break;
1491           }
1492       } while ((*tokptr != '"') && (*tokptr != '\0'));
1493       if (*tokptr++ != '"')
1494         {
1495           error ("Unterminated string in expression.");
1496         }
1497       tempbuf[tempbufindex] = '\0';     /* See note above */
1498       yylval.sval.ptr = tempbuf;
1499       yylval.sval.length = tempbufindex;
1500       lexptr = tokptr;
1501       return (STRING);
1502     }
1503
1504   if (!(c == '_' || c == '$'
1505         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
1506     /* We must have come across a bad character (e.g. ';').  */
1507     error ("Invalid character '%c' in expression.", c);
1508
1509   /* It's a name.  See how long it is.  */
1510   namelen = 0;
1511   for (c = tokstart[namelen];
1512        (c == '_' || c == '$' || (c >= '0' && c <= '9')
1513         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '<');)
1514     {
1515       /* Template parameter lists are part of the name.
1516          FIXME: This mishandles `print $a<4&&$a>3'.  */
1517
1518       if (c == '<')
1519         { 
1520                /* Scan ahead to get rest of the template specification.  Note
1521                   that we look ahead only when the '<' adjoins non-whitespace
1522                   characters; for comparison expressions, e.g. "a < b > c",
1523                   there must be spaces before the '<', etc. */
1524                
1525                char * p = find_template_name_end (tokstart + namelen);
1526                if (p)
1527                  namelen = p - tokstart;
1528                break;
1529         }
1530       c = tokstart[++namelen];
1531     }
1532
1533   /* The token "if" terminates the expression and is NOT removed from
1534      the input stream.  It doesn't count if it appears in the
1535      expansion of a macro.  */
1536   if (namelen == 2
1537       && tokstart[0] == 'i'
1538       && tokstart[1] == 'f'
1539       && ! scanning_macro_expansion ())
1540     {
1541       return 0;
1542     }
1543
1544   lexptr += namelen;
1545
1546   tryname:
1547
1548   /* Catch specific keywords.  Should be done with a data structure.  */
1549   switch (namelen)
1550     {
1551     case 8:
1552       if (STREQN (tokstart, "unsigned", 8))
1553         return UNSIGNED;
1554       if (current_language->la_language == language_cplus
1555           && STREQN (tokstart, "template", 8))
1556         return TEMPLATE;
1557       if (STREQN (tokstart, "volatile", 8))
1558         return VOLATILE_KEYWORD;
1559       break;
1560     case 6:
1561       if (STREQN (tokstart, "struct", 6))
1562         return STRUCT;
1563       if (STREQN (tokstart, "signed", 6))
1564         return SIGNED_KEYWORD;
1565       if (STREQN (tokstart, "sizeof", 6))      
1566         return SIZEOF;
1567       if (STREQN (tokstart, "double", 6))      
1568         return DOUBLE_KEYWORD;
1569       break;
1570     case 5:
1571       if (current_language->la_language == language_cplus)
1572         {
1573           if (STREQN (tokstart, "false", 5))
1574             return FALSEKEYWORD;
1575           if (STREQN (tokstart, "class", 5))
1576             return CLASS;
1577         }
1578       if (STREQN (tokstart, "union", 5))
1579         return UNION;
1580       if (STREQN (tokstart, "short", 5))
1581         return SHORT;
1582       if (STREQN (tokstart, "const", 5))
1583         return CONST_KEYWORD;
1584       break;
1585     case 4:
1586       if (STREQN (tokstart, "enum", 4))
1587         return ENUM;
1588       if (STREQN (tokstart, "long", 4))
1589         return LONG;
1590       if (current_language->la_language == language_cplus)
1591           {
1592             if (STREQN (tokstart, "true", 4))
1593               return TRUEKEYWORD;
1594
1595             if (STREQN (tokstart, "this", 4))
1596               {
1597                 static const char this_name[] =
1598                 { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
1599                 
1600                 if (lookup_symbol (this_name, expression_context_block,
1601                                    VAR_NAMESPACE, (int *) NULL,
1602                                    (struct symtab **) NULL))
1603                   return THIS;
1604               }
1605           }
1606       break;
1607     case 3:
1608       if (STREQN (tokstart, "int", 3))
1609         return INT_KEYWORD;
1610       break;
1611     default:
1612       break;
1613     }
1614
1615   yylval.sval.ptr = tokstart;
1616   yylval.sval.length = namelen;
1617
1618   if (*tokstart == '$')
1619     {
1620       write_dollar_variable (yylval.sval);
1621       return VARIABLE;
1622     }
1623   
1624   /* Look ahead and see if we can consume more of the input
1625      string to get a reasonable class/namespace spec or a
1626      fully-qualified name.  This is a kludge to get around the
1627      HP aCC compiler's generation of symbol names with embedded
1628      colons for namespace and nested classes. */ 
1629   if (unquoted_expr)
1630     {
1631       /* Only do it if not inside single quotes */ 
1632       sym_class = parse_nested_classes_for_hpacc (yylval.sval.ptr, yylval.sval.length,
1633                                                   &token_string, &class_prefix, &lexptr);
1634       if (sym_class)
1635         {
1636           /* Replace the current token with the bigger one we found */ 
1637           yylval.sval.ptr = token_string;
1638           yylval.sval.length = strlen (token_string);
1639         }
1640     }
1641   
1642   /* Use token-type BLOCKNAME for symbols that happen to be defined as
1643      functions or symtabs.  If this is not so, then ...
1644      Use token-type TYPENAME for symbols that happen to be defined
1645      currently as names of types; NAME for other symbols.
1646      The caller is not constrained to care about the distinction.  */
1647   {
1648     char *tmp = copy_name (yylval.sval);
1649     struct symbol *sym;
1650     int is_a_field_of_this = 0;
1651     int hextype;
1652
1653     sym = lookup_symbol (tmp, expression_context_block,
1654                          VAR_NAMESPACE,
1655                          current_language->la_language == language_cplus
1656                          ? &is_a_field_of_this : (int *) NULL,
1657                          (struct symtab **) NULL);
1658     /* Call lookup_symtab, not lookup_partial_symtab, in case there are
1659        no psymtabs (coff, xcoff, or some future change to blow away the
1660        psymtabs once once symbols are read).  */
1661     if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1662       {
1663         yylval.ssym.sym = sym;
1664         yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1665         return BLOCKNAME;
1666       }
1667     else if (!sym)
1668       {                         /* See if it's a file name. */
1669         struct symtab *symtab;
1670
1671         symtab = lookup_symtab (tmp);
1672
1673         if (symtab)
1674           {
1675             yylval.bval = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), STATIC_BLOCK);
1676             return FILENAME;
1677           }
1678       }
1679
1680     if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
1681         {
1682 #if 1
1683           /* Despite the following flaw, we need to keep this code enabled.
1684              Because we can get called from check_stub_method, if we don't
1685              handle nested types then it screws many operations in any
1686              program which uses nested types.  */
1687           /* In "A::x", if x is a member function of A and there happens
1688              to be a type (nested or not, since the stabs don't make that
1689              distinction) named x, then this code incorrectly thinks we
1690              are dealing with nested types rather than a member function.  */
1691
1692           char *p;
1693           char *namestart;
1694           struct symbol *best_sym;
1695
1696           /* Look ahead to detect nested types.  This probably should be
1697              done in the grammar, but trying seemed to introduce a lot
1698              of shift/reduce and reduce/reduce conflicts.  It's possible
1699              that it could be done, though.  Or perhaps a non-grammar, but
1700              less ad hoc, approach would work well.  */
1701
1702           /* Since we do not currently have any way of distinguishing
1703              a nested type from a non-nested one (the stabs don't tell
1704              us whether a type is nested), we just ignore the
1705              containing type.  */
1706
1707           p = lexptr;
1708           best_sym = sym;
1709           while (1)
1710             {
1711               /* Skip whitespace.  */
1712               while (*p == ' ' || *p == '\t' || *p == '\n')
1713                 ++p;
1714               if (*p == ':' && p[1] == ':')
1715                 {
1716                   /* Skip the `::'.  */
1717                   p += 2;
1718                   /* Skip whitespace.  */
1719                   while (*p == ' ' || *p == '\t' || *p == '\n')
1720                     ++p;
1721                   namestart = p;
1722                   while (*p == '_' || *p == '$' || (*p >= '0' && *p <= '9')
1723                          || (*p >= 'a' && *p <= 'z')
1724                          || (*p >= 'A' && *p <= 'Z'))
1725                     ++p;
1726                   if (p != namestart)
1727                     {
1728                       struct symbol *cur_sym;
1729                       /* As big as the whole rest of the expression, which is
1730                          at least big enough.  */
1731                       char *ncopy = alloca (strlen (tmp)+strlen (namestart)+3);
1732                       char *tmp1;
1733
1734                       tmp1 = ncopy;
1735                       memcpy (tmp1, tmp, strlen (tmp));
1736                       tmp1 += strlen (tmp);
1737                       memcpy (tmp1, "::", 2);
1738                       tmp1 += 2;
1739                       memcpy (tmp1, namestart, p - namestart);
1740                       tmp1[p - namestart] = '\0';
1741                       cur_sym = lookup_symbol (ncopy, expression_context_block,
1742                                                VAR_NAMESPACE, (int *) NULL,
1743                                                (struct symtab **) NULL);
1744                       if (cur_sym)
1745                         {
1746                           if (SYMBOL_CLASS (cur_sym) == LOC_TYPEDEF)
1747                             {
1748                               best_sym = cur_sym;
1749                               lexptr = p;
1750                             }
1751                           else
1752                             break;
1753                         }
1754                       else
1755                         break;
1756                     }
1757                   else
1758                     break;
1759                 }
1760               else
1761                 break;
1762             }
1763
1764           yylval.tsym.type = SYMBOL_TYPE (best_sym);
1765 #else /* not 0 */
1766           yylval.tsym.type = SYMBOL_TYPE (sym);
1767 #endif /* not 0 */
1768           return TYPENAME;
1769         }
1770     if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
1771       return TYPENAME;
1772
1773     /* Input names that aren't symbols but ARE valid hex numbers,
1774        when the input radix permits them, can be names or numbers
1775        depending on the parse.  Note we support radixes > 16 here.  */
1776     if (!sym && 
1777         ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
1778          (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
1779       {
1780         YYSTYPE newlval;        /* Its value is ignored.  */
1781         hextype = parse_number (tokstart, namelen, 0, &newlval);
1782         if (hextype == INT)
1783           {
1784             yylval.ssym.sym = sym;
1785             yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1786             return NAME_OR_INT;
1787           }
1788       }
1789
1790     /* Any other kind of symbol */
1791     yylval.ssym.sym = sym;
1792     yylval.ssym.is_a_field_of_this = is_a_field_of_this;
1793     return NAME;
1794   }
1795 }
1796
1797 void
1798 yyerror (msg)
1799      char *msg;
1800 {
1801   if (prev_lexptr)
1802     lexptr = prev_lexptr;
1803
1804   error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
1805 }