home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / c-exp.y < prev    next >
Encoding:
GNU Bison Grammar  |  1995-02-10  |  42.0 KB  |  1,656 lines

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