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