gdb/f-exp.y - gdb


  1. /* YACC parser for Fortran expressions, for GDB.
  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    Contributed by Motorola.  Adapted from the C parser by Farooq Butt
  4.    (fmbutt@engage.sps.mot.com).

  5.    This file is part of GDB.

  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 3 of the License, or
  9.    (at your option) any later version.

  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.

  14.    You should have received a copy of the GNU General Public License
  15.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  16. /* This was blantantly ripped off the C expression parser, please
  17.    be aware of that as you look at its basic structure -FMB */

  18. /* Parse a F77 expression from text in a string,
  19.    and return the result as a  struct expression  pointer.
  20.    That structure contains arithmetic operations in reverse polish,
  21.    with constants represented by operations that are followed by special data.
  22.    See expression.h for the details of the format.
  23.    What is important here is that it can be built up sequentially
  24.    during the process of parsing; the lower levels of the tree always
  25.    come first in the result.

  26.    Note that malloc's and realloc's in this file are transformed to
  27.    xmalloc and xrealloc respectively by the same sed command in the
  28.    makefile that remaps any other malloc/realloc inserted by the parser
  29.    generator.  Doing this with #defines and trying to control the interaction
  30.    with include files (<malloc.h> and <stdlib.h> for example) just became
  31.    too messy, particularly when such includes can be inserted at random
  32.    times by the parser generator.  */

  33. %{

  34. #include "defs.h"
  35. #include "expression.h"
  36. #include "value.h"
  37. #include "parser-defs.h"
  38. #include "language.h"
  39. #include "f-lang.h"
  40. #include "bfd.h" /* Required by objfiles.h.  */
  41. #include "symfile.h" /* Required by objfiles.h.  */
  42. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  43. #include "block.h"
  44. #include <ctype.h>

  45. #define parse_type(ps) builtin_type (parse_gdbarch (ps))
  46. #define parse_f_type(ps) builtin_f_type (parse_gdbarch (ps))

  47. /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
  48.    as well as gratuitiously global symbol names, so we can have multiple
  49.    yacc generated parsers in gdb.  Note that these are only the variables
  50.    produced by yacc.  If other parser generators (bison, byacc, etc) produce
  51.    additional global names that conflict at link time, then those parser
  52.    generators need to be fixed instead of adding those names to this list.  */

  53. #define        yymaxdepth f_maxdepth
  54. #define        yyparse f_parse_internal
  55. #define        yylex        f_lex
  56. #define        yyerror        f_error
  57. #define        yylval        f_lval
  58. #define        yychar        f_char
  59. #define        yydebug        f_debug
  60. #define        yypact        f_pact
  61. #define        yyr1        f_r1
  62. #define        yyr2        f_r2
  63. #define        yydef        f_def
  64. #define        yychk        f_chk
  65. #define        yypgo        f_pgo
  66. #define        yyact        f_act
  67. #define        yyexca        f_exca
  68. #define yyerrflag f_errflag
  69. #define yynerrs        f_nerrs
  70. #define        yyps        f_ps
  71. #define        yypv        f_pv
  72. #define        yys        f_s
  73. #define        yy_yys        f_yys
  74. #define        yystate        f_state
  75. #define        yytmp        f_tmp
  76. #define        yyv        f_v
  77. #define        yy_yyv        f_yyv
  78. #define        yyval        f_val
  79. #define        yylloc        f_lloc
  80. #define yyreds        f_reds                /* With YYDEBUG defined */
  81. #define yytoks        f_toks                /* With YYDEBUG defined */
  82. #define yyname        f_name                /* With YYDEBUG defined */
  83. #define yyrule        f_rule                /* With YYDEBUG defined */
  84. #define yylhs        f_yylhs
  85. #define yylen        f_yylen
  86. #define yydefred f_yydefred
  87. #define yydgoto        f_yydgoto
  88. #define yysindex f_yysindex
  89. #define yyrindex f_yyrindex
  90. #define yygindex f_yygindex
  91. #define yytable         f_yytable
  92. #define yycheck         f_yycheck
  93. #define yyss        f_yyss
  94. #define yysslim        f_yysslim
  95. #define yyssp        f_yyssp
  96. #define yystacksize f_yystacksize
  97. #define yyvs        f_yyvs
  98. #define yyvsp        f_yyvsp

  99. #ifndef YYDEBUG
  100. #define        YYDEBUG        1                /* Default to yydebug support */
  101. #endif

  102. #define YYFPRINTF parser_fprintf

  103. /* The state of the parser, used internally when we are parsing the
  104.    expression.  */

  105. static struct parser_state *pstate = NULL;

  106. int yyparse (void);

  107. static int yylex (void);

  108. void yyerror (char *);

  109. static void growbuf_by_size (int);

  110. static int match_string_literal (void);

  111. %}

  112. /* Although the yacc "value" of an expression is not used,
  113.    since the result is stored in the structure being created,
  114.    other node types do have values.  */

  115. %union
  116.   {
  117.     LONGEST lval;
  118.     struct {
  119.       LONGEST val;
  120.       struct type *type;
  121.     } typed_val;
  122.     DOUBLEST dval;
  123.     struct symbol *sym;
  124.     struct type *tval;
  125.     struct stoken sval;
  126.     struct ttype tsym;
  127.     struct symtoken ssym;
  128.     int voidval;
  129.     struct block *bval;
  130.     enum exp_opcode opcode;
  131.     struct internalvar *ivar;

  132.     struct type **tvec;
  133.     int *ivec;
  134.   }

  135. %{
  136. /* YYSTYPE gets defined by %union */
  137. static int parse_number (struct parser_state *, const char *, int,
  138.                          int, YYSTYPE *);
  139. %}

  140. %type <voidval> exp  type_exp start variable
  141. %type <tval> type typebase
  142. %type <tvec> nonempty_typelist
  143. /* %type <bval> block */

  144. /* Fancy type parsing.  */
  145. %type <voidval> func_mod direct_abs_decl abs_decl
  146. %type <tval> ptype

  147. %token <typed_val> INT
  148. %token <dval> FLOAT

  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. %token <sval> STRING_LITERAL
  157. %token <lval> BOOLEAN_LITERAL
  158. %token <ssym> NAME
  159. %token <tsym> TYPENAME
  160. %type <sval> name
  161. %type <ssym> name_not_typename

  162. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  163.    but which would parse as a valid number in the current input radix.
  164.    E.g. "c" when input_radix==16.  Depending on the parse, it will be
  165.    turned into a name or into a number.  */

  166. %token <ssym> NAME_OR_INT

  167. %token  SIZEOF
  168. %token ERROR

  169. /* Special type cases, put in to allow the parser to distinguish different
  170.    legal basetypes.  */
  171. %token INT_KEYWORD INT_S2_KEYWORD LOGICAL_S1_KEYWORD LOGICAL_S2_KEYWORD
  172. %token LOGICAL_S8_KEYWORD
  173. %token LOGICAL_KEYWORD REAL_KEYWORD REAL_S8_KEYWORD REAL_S16_KEYWORD
  174. %token COMPLEX_S8_KEYWORD COMPLEX_S16_KEYWORD COMPLEX_S32_KEYWORD
  175. %token BOOL_AND BOOL_OR BOOL_NOT
  176. %token <lval> CHARACTER

  177. %token <voidval> VARIABLE

  178. %token <opcode> ASSIGN_MODIFY

  179. %left ','
  180. %left ABOVE_COMMA
  181. %right '=' ASSIGN_MODIFY
  182. %right '?'
  183. %left BOOL_OR
  184. %right BOOL_NOT
  185. %left BOOL_AND
  186. %left '|'
  187. %left '^'
  188. %left '&'
  189. %left EQUAL NOTEQUAL
  190. %left LESSTHAN GREATERTHAN LEQ GEQ
  191. %left LSH RSH
  192. %left '@'
  193. %left '+' '-'
  194. %left '*' '/'
  195. %right STARSTAR
  196. %right '%'
  197. %right UNARY
  198. %right '('


  199. %%

  200. start   :        exp
  201.         |        type_exp
  202.         ;

  203. type_exp:        type
  204.                         { write_exp_elt_opcode (pstate, OP_TYPE);
  205.                           write_exp_elt_type (pstate, $1);
  206.                           write_exp_elt_opcode (pstate, OP_TYPE); }
  207.         ;

  208. exp     :       '(' exp ')'
  209.                         { }
  210.         ;

  211. /* Expressions, not including the comma operator.  */
  212. exp        :        '*' exp    %prec UNARY
  213.                         { write_exp_elt_opcode (pstate, UNOP_IND); }
  214.         ;

  215. exp        :        '&' exp    %prec UNARY
  216.                         { write_exp_elt_opcode (pstate, UNOP_ADDR); }
  217.         ;

  218. exp        :        '-' exp    %prec UNARY
  219.                         { write_exp_elt_opcode (pstate, UNOP_NEG); }
  220.         ;

  221. exp        :        BOOL_NOT exp    %prec UNARY
  222.                         { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
  223.         ;

  224. exp        :        '~' exp    %prec UNARY
  225.                         { write_exp_elt_opcode (pstate, UNOP_COMPLEMENT); }
  226.         ;

  227. exp        :        SIZEOF exp       %prec UNARY
  228.                         { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
  229.         ;

  230. /* No more explicit array operators, we treat everything in F77 as
  231.    a function call.  The disambiguation as to whether we are
  232.    doing a subscript operation or a function call is done
  233.    later in eval.c.  */

  234. exp        :        exp '('
  235.                         { start_arglist (); }
  236.                 arglist ')'
  237.                         { write_exp_elt_opcode (pstate,
  238.                                                 OP_F77_UNDETERMINED_ARGLIST);
  239.                           write_exp_elt_longcst (pstate,
  240.                                                  (LONGEST) end_arglist ());
  241.                           write_exp_elt_opcode (pstate,
  242.                                               OP_F77_UNDETERMINED_ARGLIST); }
  243.         ;

  244. arglist        :
  245.         ;

  246. arglist        :        exp
  247.                         { arglist_len = 1; }
  248.         ;

  249. arglist :        subrange
  250.                         { arglist_len = 1; }
  251.         ;

  252. arglist        :        arglist ',' exp   %prec ABOVE_COMMA
  253.                         { arglist_len++; }
  254.         ;

  255. /* There are four sorts of subrange types in F90.  */

  256. subrange:        exp ':' exp        %prec ABOVE_COMMA
  257.                         { write_exp_elt_opcode (pstate, OP_F90_RANGE);
  258.                           write_exp_elt_longcst (pstate, NONE_BOUND_DEFAULT);
  259.                           write_exp_elt_opcode (pstate, OP_F90_RANGE); }
  260.         ;

  261. subrange:        exp ':'        %prec ABOVE_COMMA
  262.                         { write_exp_elt_opcode (pstate, OP_F90_RANGE);
  263.                           write_exp_elt_longcst (pstate, HIGH_BOUND_DEFAULT);
  264.                           write_exp_elt_opcode (pstate, OP_F90_RANGE); }
  265.         ;

  266. subrange:        ':' exp        %prec ABOVE_COMMA
  267.                         { write_exp_elt_opcode (pstate, OP_F90_RANGE);
  268.                           write_exp_elt_longcst (pstate, LOW_BOUND_DEFAULT);
  269.                           write_exp_elt_opcode (pstate, OP_F90_RANGE); }
  270.         ;

  271. subrange:        ':'        %prec ABOVE_COMMA
  272.                         { write_exp_elt_opcode (pstate, OP_F90_RANGE);
  273.                           write_exp_elt_longcst (pstate, BOTH_BOUND_DEFAULT);
  274.                           write_exp_elt_opcode (pstate, OP_F90_RANGE); }
  275.         ;

  276. complexnum:     exp ',' exp
  277.                         { }
  278.         ;

  279. exp        :        '(' complexnum ')'
  280.                         { write_exp_elt_opcode (pstate, OP_COMPLEX);
  281.                           write_exp_elt_type (pstate,
  282.                                               parse_f_type (pstate)
  283.                                               ->builtin_complex_s16);
  284.                           write_exp_elt_opcode (pstate, OP_COMPLEX); }
  285.         ;

  286. exp        :        '(' type ')' exp  %prec UNARY
  287.                         { write_exp_elt_opcode (pstate, UNOP_CAST);
  288.                           write_exp_elt_type (pstate, $2);
  289.                           write_exp_elt_opcode (pstate, UNOP_CAST); }
  290.         ;

  291. exp     :       exp '%' name
  292.                         { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  293.                           write_exp_string (pstate, $3);
  294.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
  295.         ;

  296. /* Binary operators in order of decreasing precedence.  */

  297. exp        :        exp '@' exp
  298.                         { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
  299.         ;

  300. exp        :        exp STARSTAR exp
  301.                         { write_exp_elt_opcode (pstate, BINOP_EXP); }
  302.         ;

  303. exp        :        exp '*' exp
  304.                         { write_exp_elt_opcode (pstate, BINOP_MUL); }
  305.         ;

  306. exp        :        exp '/' exp
  307.                         { write_exp_elt_opcode (pstate, BINOP_DIV); }
  308.         ;

  309. exp        :        exp '+' exp
  310.                         { write_exp_elt_opcode (pstate, BINOP_ADD); }
  311.         ;

  312. exp        :        exp '-' exp
  313.                         { write_exp_elt_opcode (pstate, BINOP_SUB); }
  314.         ;

  315. exp        :        exp LSH exp
  316.                         { write_exp_elt_opcode (pstate, BINOP_LSH); }
  317.         ;

  318. exp        :        exp RSH exp
  319.                         { write_exp_elt_opcode (pstate, BINOP_RSH); }
  320.         ;

  321. exp        :        exp EQUAL exp
  322.                         { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
  323.         ;

  324. exp        :        exp NOTEQUAL exp
  325.                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
  326.         ;

  327. exp        :        exp LEQ exp
  328.                         { write_exp_elt_opcode (pstate, BINOP_LEQ); }
  329.         ;

  330. exp        :        exp GEQ exp
  331.                         { write_exp_elt_opcode (pstate, BINOP_GEQ); }
  332.         ;

  333. exp        :        exp LESSTHAN exp
  334.                         { write_exp_elt_opcode (pstate, BINOP_LESS); }
  335.         ;

  336. exp        :        exp GREATERTHAN exp
  337.                         { write_exp_elt_opcode (pstate, BINOP_GTR); }
  338.         ;

  339. exp        :        exp '&' exp
  340.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_AND); }
  341.         ;

  342. exp        :        exp '^' exp
  343.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_XOR); }
  344.         ;

  345. exp        :        exp '|' exp
  346.                         { write_exp_elt_opcode (pstate, BINOP_BITWISE_IOR); }
  347.         ;

  348. exp     :       exp BOOL_AND exp
  349.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
  350.         ;


  351. exp        :        exp BOOL_OR exp
  352.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
  353.         ;

  354. exp        :        exp '=' exp
  355.                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
  356.         ;

  357. exp        :        exp ASSIGN_MODIFY exp
  358.                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
  359.                           write_exp_elt_opcode (pstate, $2);
  360.                           write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY); }
  361.         ;

  362. exp        :        INT
  363.                         { write_exp_elt_opcode (pstate, OP_LONG);
  364.                           write_exp_elt_type (pstate, $1.type);
  365.                           write_exp_elt_longcst (pstate, (LONGEST) ($1.val));
  366.                           write_exp_elt_opcode (pstate, OP_LONG); }
  367.         ;

  368. exp        :        NAME_OR_INT
  369.                         { YYSTYPE val;
  370.                           parse_number (pstate, $1.stoken.ptr,
  371.                                         $1.stoken.length, 0, &val);
  372.                           write_exp_elt_opcode (pstate, OP_LONG);
  373.                           write_exp_elt_type (pstate, val.typed_val.type);
  374.                           write_exp_elt_longcst (pstate,
  375.                                                  (LONGEST)val.typed_val.val);
  376.                           write_exp_elt_opcode (pstate, OP_LONG); }
  377.         ;

  378. exp        :        FLOAT
  379.                         { write_exp_elt_opcode (pstate, OP_DOUBLE);
  380.                           write_exp_elt_type (pstate,
  381.                                               parse_f_type (pstate)
  382.                                               ->builtin_real_s8);
  383.                           write_exp_elt_dblcst (pstate, $1);
  384.                           write_exp_elt_opcode (pstate, OP_DOUBLE); }
  385.         ;

  386. exp        :        variable
  387.         ;

  388. exp        :        VARIABLE
  389.         ;

  390. exp        :        SIZEOF '(' type ')'        %prec UNARY
  391.                         { write_exp_elt_opcode (pstate, OP_LONG);
  392.                           write_exp_elt_type (pstate,
  393.                                               parse_f_type (pstate)
  394.                                               ->builtin_integer);
  395.                           CHECK_TYPEDEF ($3);
  396.                           write_exp_elt_longcst (pstate,
  397.                                                  (LONGEST) TYPE_LENGTH ($3));
  398.                           write_exp_elt_opcode (pstate, OP_LONG); }
  399.         ;

  400. exp     :       BOOLEAN_LITERAL
  401.                         { write_exp_elt_opcode (pstate, OP_BOOL);
  402.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  403.                           write_exp_elt_opcode (pstate, OP_BOOL);
  404.                         }
  405.         ;

  406. exp        :        STRING_LITERAL
  407.                         {
  408.                           write_exp_elt_opcode (pstate, OP_STRING);
  409.                           write_exp_string (pstate, $1);
  410.                           write_exp_elt_opcode (pstate, OP_STRING);
  411.                         }
  412.         ;

  413. variable:        name_not_typename
  414.                         { struct symbol *sym = $1.sym;

  415.                           if (sym)
  416.                             {
  417.                               if (symbol_read_needs_frame (sym))
  418.                                 {
  419.                                   if (innermost_block == 0
  420.                                       || contained_in (block_found,
  421.                                                        innermost_block))
  422.                                     innermost_block = block_found;
  423.                                 }
  424.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  425.                               /* We want to use the selected frame, not
  426.                                  another more inner frame which happens to
  427.                                  be in the same block.  */
  428.                               write_exp_elt_block (pstate, NULL);
  429.                               write_exp_elt_sym (pstate, sym);
  430.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  431.                               break;
  432.                             }
  433.                           else
  434.                             {
  435.                               struct bound_minimal_symbol msymbol;
  436.                               char *arg = copy_name ($1.stoken);

  437.                               msymbol =
  438.                                 lookup_bound_minimal_symbol (arg);
  439.                               if (msymbol.minsym != NULL)
  440.                                 write_exp_msymbol (pstate, msymbol);
  441.                               else if (!have_full_symbols () && !have_partial_symbols ())
  442.                                 error (_("No symbol table is loaded.  Use the \"file\" command."));
  443.                               else
  444.                                 error (_("No symbol \"%s\" in current context."),
  445.                                        copy_name ($1.stoken));
  446.                             }
  447.                         }
  448.         ;


  449. type    :       ptype
  450.         ;

  451. ptype        :        typebase
  452.         |        typebase abs_decl
  453.                 {
  454.                   /* This is where the interesting stuff happens.  */
  455.                   int done = 0;
  456.                   int array_size;
  457.                   struct type *follow_type = $1;
  458.                   struct type *range_type;

  459.                   while (!done)
  460.                     switch (pop_type ())
  461.                       {
  462.                       case tp_end:
  463.                         done = 1;
  464.                         break;
  465.                       case tp_pointer:
  466.                         follow_type = lookup_pointer_type (follow_type);
  467.                         break;
  468.                       case tp_reference:
  469.                         follow_type = lookup_reference_type (follow_type);
  470.                         break;
  471.                       case tp_array:
  472.                         array_size = pop_type_int ();
  473.                         if (array_size != -1)
  474.                           {
  475.                             range_type =
  476.                               create_static_range_type ((struct type *) NULL,
  477.                                                         parse_f_type (pstate)
  478.                                                         ->builtin_integer,
  479.                                                         0, array_size - 1);
  480.                             follow_type =
  481.                               create_array_type ((struct type *) NULL,
  482.                                                  follow_type, range_type);
  483.                           }
  484.                         else
  485.                           follow_type = lookup_pointer_type (follow_type);
  486.                         break;
  487.                       case tp_function:
  488.                         follow_type = lookup_function_type (follow_type);
  489.                         break;
  490.                       }
  491.                   $$ = follow_type;
  492.                 }
  493.         ;

  494. abs_decl:        '*'
  495.                         { push_type (tp_pointer); $$ = 0; }
  496.         |        '*' abs_decl
  497.                         { push_type (tp_pointer); $$ = $2; }
  498.         |        '&'
  499.                         { push_type (tp_reference); $$ = 0; }
  500.         |        '&' abs_decl
  501.                         { push_type (tp_reference); $$ = $2; }
  502.         |        direct_abs_decl
  503.         ;

  504. direct_abs_decl: '(' abs_decl ')'
  505.                         { $$ = $2; }
  506.         |         direct_abs_decl func_mod
  507.                         { push_type (tp_function); }
  508.         |        func_mod
  509.                         { push_type (tp_function); }
  510.         ;

  511. func_mod:        '(' ')'
  512.                         { $$ = 0; }
  513.         |        '(' nonempty_typelist ')'
  514.                         { free ($2); $$ = 0; }
  515.         ;

  516. typebase  /* Implements (approximately): (type-qualifier)* type-specifier */
  517.         :        TYPENAME
  518.                         { $$ = $1.type; }
  519.         |        INT_KEYWORD
  520.                         { $$ = parse_f_type (pstate)->builtin_integer; }
  521.         |        INT_S2_KEYWORD
  522.                         { $$ = parse_f_type (pstate)->builtin_integer_s2; }
  523.         |        CHARACTER
  524.                         { $$ = parse_f_type (pstate)->builtin_character; }
  525.         |        LOGICAL_S8_KEYWORD
  526.                         { $$ = parse_f_type (pstate)->builtin_logical_s8; }
  527.         |        LOGICAL_KEYWORD
  528.                         { $$ = parse_f_type (pstate)->builtin_logical; }
  529.         |        LOGICAL_S2_KEYWORD
  530.                         { $$ = parse_f_type (pstate)->builtin_logical_s2; }
  531.         |        LOGICAL_S1_KEYWORD
  532.                         { $$ = parse_f_type (pstate)->builtin_logical_s1; }
  533.         |        REAL_KEYWORD
  534.                         { $$ = parse_f_type (pstate)->builtin_real; }
  535.         |       REAL_S8_KEYWORD
  536.                         { $$ = parse_f_type (pstate)->builtin_real_s8; }
  537.         |        REAL_S16_KEYWORD
  538.                         { $$ = parse_f_type (pstate)->builtin_real_s16; }
  539.         |        COMPLEX_S8_KEYWORD
  540.                         { $$ = parse_f_type (pstate)->builtin_complex_s8; }
  541.         |        COMPLEX_S16_KEYWORD
  542.                         { $$ = parse_f_type (pstate)->builtin_complex_s16; }
  543.         |        COMPLEX_S32_KEYWORD
  544.                         { $$ = parse_f_type (pstate)->builtin_complex_s32; }
  545.         ;

  546. nonempty_typelist
  547.         :        type
  548.                 { $$ = (struct type **) malloc (sizeof (struct type *) * 2);
  549.                   $<ivec>$[0] = 1;        /* Number of types in vector */
  550.                   $$[1] = $1;
  551.                 }
  552.         |        nonempty_typelist ',' type
  553.                 { int len = sizeof (struct type *) * (++($<ivec>1[0]) + 1);
  554.                   $$ = (struct type **) realloc ((char *) $1, len);
  555.                   $$[$<ivec>$[0]] = $3;
  556.                 }
  557.         ;

  558. name        :        NAME
  559.                 {  $$ = $1.stoken; }
  560.         ;

  561. name_not_typename :        NAME
  562. /* These would be useful if name_not_typename was useful, but it is just
  563.    a fake for "variable", so these cause reduce/reduce conflicts because
  564.    the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  565.    =exp) or just an exp.  If name_not_typename was ever used in an lvalue
  566.    context where only a name could occur, this might be useful.
  567.           |        NAME_OR_INT
  568.    */
  569.         ;

  570. %%

  571. /* Take care of parsing a number (anything that starts with a digit).
  572.    Set yylval and return the token type; update lexptr.
  573.    LEN is the number of characters in it.  */

  574. /*** Needs some error checking for the float case ***/

  575. static int
  576. parse_number (struct parser_state *par_state,
  577.               const char *p, int len, int parsed_float, YYSTYPE *putithere)
  578. {
  579.   LONGEST n = 0;
  580.   LONGEST prevn = 0;
  581.   int c;
  582.   int base = input_radix;
  583.   int unsigned_p = 0;
  584.   int long_p = 0;
  585.   ULONGEST high_bit;
  586.   struct type *signed_type;
  587.   struct type *unsigned_type;

  588.   if (parsed_float)
  589.     {
  590.       /* It's a float since it contains a point or an exponent.  */
  591.       /* [dD] is not understood as an exponent by atof, change it to 'e'.  */
  592.       char *tmp, *tmp2;

  593.       tmp = xstrdup (p);
  594.       for (tmp2 = tmp; *tmp2; ++tmp2)
  595.         if (*tmp2 == 'd' || *tmp2 == 'D')
  596.           *tmp2 = 'e';
  597.       putithere->dval = atof (tmp);
  598.       free (tmp);
  599.       return FLOAT;
  600.     }

  601.   /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  602.   if (p[0] == '0')
  603.     switch (p[1])
  604.       {
  605.       case 'x':
  606.       case 'X':
  607.         if (len >= 3)
  608.           {
  609.             p += 2;
  610.             base = 16;
  611.             len -= 2;
  612.           }
  613.         break;

  614.       case 't':
  615.       case 'T':
  616.       case 'd':
  617.       case 'D':
  618.         if (len >= 3)
  619.           {
  620.             p += 2;
  621.             base = 10;
  622.             len -= 2;
  623.           }
  624.         break;

  625.       default:
  626.         base = 8;
  627.         break;
  628.       }

  629.   while (len-- > 0)
  630.     {
  631.       c = *p++;
  632.       if (isupper (c))
  633.         c = tolower (c);
  634.       if (len == 0 && c == 'l')
  635.         long_p = 1;
  636.       else if (len == 0 && c == 'u')
  637.         unsigned_p = 1;
  638.       else
  639.         {
  640.           int i;
  641.           if (c >= '0' && c <= '9')
  642.             i = c - '0';
  643.           else if (c >= 'a' && c <= 'f')
  644.             i = c - 'a' + 10;
  645.           else
  646.             return ERROR;        /* Char not a digit */
  647.           if (i >= base)
  648.             return ERROR;                /* Invalid digit in this base */
  649.           n *= base;
  650.           n += i;
  651.         }
  652.       /* Portably test for overflow (only works for nonzero values, so make
  653.          a second check for zero).  */
  654.       if ((prevn >= n) && n != 0)
  655.         unsigned_p=1;                /* Try something unsigned */
  656.       /* If range checking enabled, portably test for unsigned overflow.  */
  657.       if (RANGE_CHECK && n != 0)
  658.         {
  659.           if ((unsigned_p && (unsigned)prevn >= (unsigned)n))
  660.             range_error (_("Overflow on numeric constant."));
  661.         }
  662.       prevn = n;
  663.     }

  664.   /* If the number is too big to be an int, or it's got an l suffix
  665.      then it's a long.  Work out if this has to be a long by
  666.      shifting right and seeing if anything remains, and the
  667.      target int size is different to the target long size.

  668.      In the expression below, we could have tested
  669.      (n >> gdbarch_int_bit (parse_gdbarch))
  670.      to see if it was zero,
  671.      but too many compilers warn about that, when ints and longs
  672.      are the same size.  So we shift it twice, with fewer bits
  673.      each time, for the same result.  */

  674.   if ((gdbarch_int_bit (parse_gdbarch (par_state))
  675.        != gdbarch_long_bit (parse_gdbarch (par_state))
  676.        && ((n >> 2)
  677.            >> (gdbarch_int_bit (parse_gdbarch (par_state))-2))) /* Avoid
  678.                                                             shift warning */
  679.       || long_p)
  680.     {
  681.       high_bit = ((ULONGEST)1)
  682.       << (gdbarch_long_bit (parse_gdbarch (par_state))-1);
  683.       unsigned_type = parse_type (par_state)->builtin_unsigned_long;
  684.       signed_type = parse_type (par_state)->builtin_long;
  685.     }
  686.   else
  687.     {
  688.       high_bit =
  689.         ((ULONGEST)1) << (gdbarch_int_bit (parse_gdbarch (par_state)) - 1);
  690.       unsigned_type = parse_type (par_state)->builtin_unsigned_int;
  691.       signed_type = parse_type (par_state)->builtin_int;
  692.     }

  693.   putithere->typed_val.val = n;

  694.   /* If the high bit of the worked out type is set then this number
  695.      has to be unsigned.  */

  696.   if (unsigned_p || (n & high_bit))
  697.     putithere->typed_val.type = unsigned_type;
  698.   else
  699.     putithere->typed_val.type = signed_type;

  700.   return INT;
  701. }

  702. struct token
  703. {
  704.   char *operator;
  705.   int token;
  706.   enum exp_opcode opcode;
  707. };

  708. static const struct token dot_ops[] =
  709. {
  710.   { ".and.", BOOL_AND, BINOP_END },
  711.   { ".AND.", BOOL_AND, BINOP_END },
  712.   { ".or.", BOOL_OR, BINOP_END },
  713.   { ".OR.", BOOL_OR, BINOP_END },
  714.   { ".not.", BOOL_NOT, BINOP_END },
  715.   { ".NOT.", BOOL_NOT, BINOP_END },
  716.   { ".eq.", EQUAL, BINOP_END },
  717.   { ".EQ.", EQUAL, BINOP_END },
  718.   { ".eqv.", EQUAL, BINOP_END },
  719.   { ".NEQV.", NOTEQUAL, BINOP_END },
  720.   { ".neqv.", NOTEQUAL, BINOP_END },
  721.   { ".EQV.", EQUAL, BINOP_END },
  722.   { ".ne.", NOTEQUAL, BINOP_END },
  723.   { ".NE.", NOTEQUAL, BINOP_END },
  724.   { ".le.", LEQ, BINOP_END },
  725.   { ".LE.", LEQ, BINOP_END },
  726.   { ".ge.", GEQ, BINOP_END },
  727.   { ".GE.", GEQ, BINOP_END },
  728.   { ".gt.", GREATERTHAN, BINOP_END },
  729.   { ".GT.", GREATERTHAN, BINOP_END },
  730.   { ".lt.", LESSTHAN, BINOP_END },
  731.   { ".LT.", LESSTHAN, BINOP_END },
  732.   { NULL, 0, 0 }
  733. };

  734. struct f77_boolean_val
  735. {
  736.   char *name;
  737.   int value;
  738. };

  739. static const struct f77_boolean_val boolean_values[]  =
  740. {
  741.   { ".true.", 1 },
  742.   { ".TRUE.", 1 },
  743.   { ".false.", 0 },
  744.   { ".FALSE.", 0 },
  745.   { NULL, 0 }
  746. };

  747. static const struct token f77_keywords[] =
  748. {
  749.   { "complex_16", COMPLEX_S16_KEYWORD, BINOP_END },
  750.   { "complex_32", COMPLEX_S32_KEYWORD, BINOP_END },
  751.   { "character", CHARACTER, BINOP_END },
  752.   { "integer_2", INT_S2_KEYWORD, BINOP_END },
  753.   { "logical_1", LOGICAL_S1_KEYWORD, BINOP_END },
  754.   { "logical_2", LOGICAL_S2_KEYWORD, BINOP_END },
  755.   { "logical_8", LOGICAL_S8_KEYWORD, BINOP_END },
  756.   { "complex_8", COMPLEX_S8_KEYWORD, BINOP_END },
  757.   { "integer", INT_KEYWORD, BINOP_END },
  758.   { "logical", LOGICAL_KEYWORD, BINOP_END },
  759.   { "real_16", REAL_S16_KEYWORD, BINOP_END },
  760.   { "complex", COMPLEX_S8_KEYWORD, BINOP_END },
  761.   { "sizeof", SIZEOF, BINOP_END },
  762.   { "real_8", REAL_S8_KEYWORD, BINOP_END },
  763.   { "real", REAL_KEYWORD, BINOP_END },
  764.   { NULL, 0, 0 }
  765. };

  766. /* Implementation of a dynamically expandable buffer for processing input
  767.    characters acquired through lexptr and building a value to return in
  768.    yylval.  Ripped off from ch-exp.y */

  769. static char *tempbuf;                /* Current buffer contents */
  770. static int tempbufsize;                /* Size of allocated buffer */
  771. static int tempbufindex;        /* Current index into buffer */

  772. #define GROWBY_MIN_SIZE 64        /* Minimum amount to grow buffer by */

  773. #define CHECKBUF(size) \
  774.   do { \
  775.     if (tempbufindex + (size) >= tempbufsize) \
  776.       { \
  777.         growbuf_by_size (size); \
  778.       } \
  779.   } while (0);


  780. /* Grow the static temp buffer if necessary, including allocating the
  781.    first one on demand.  */

  782. static void
  783. growbuf_by_size (int count)
  784. {
  785.   int growby;

  786.   growby = max (count, GROWBY_MIN_SIZE);
  787.   tempbufsize += growby;
  788.   if (tempbuf == NULL)
  789.     tempbuf = (char *) malloc (tempbufsize);
  790.   else
  791.     tempbuf = (char *) realloc (tempbuf, tempbufsize);
  792. }

  793. /* Blatantly ripped off from ch-exp.y. This routine recognizes F77
  794.    string-literals.

  795.    Recognize a string literal.  A string literal is a nonzero sequence
  796.    of characters enclosed in matching single quotes, except that
  797.    a single character inside single quotes is a character literal, which
  798.    we reject as a string literal.  To embed the terminator character inside
  799.    a string, it is simply doubled (I.E. 'this''is''one''string') */

  800. static int
  801. match_string_literal (void)
  802. {
  803.   const char *tokptr = lexptr;

  804.   for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
  805.     {
  806.       CHECKBUF (1);
  807.       if (*tokptr == *lexptr)
  808.         {
  809.           if (*(tokptr + 1) == *lexptr)
  810.             tokptr++;
  811.           else
  812.             break;
  813.         }
  814.       tempbuf[tempbufindex++] = *tokptr;
  815.     }
  816.   if (*tokptr == '\0'                                        /* no terminator */
  817.       || tempbufindex == 0)                                /* no string */
  818.     return 0;
  819.   else
  820.     {
  821.       tempbuf[tempbufindex] = '\0';
  822.       yylval.sval.ptr = tempbuf;
  823.       yylval.sval.length = tempbufindex;
  824.       lexptr = ++tokptr;
  825.       return STRING_LITERAL;
  826.     }
  827. }

  828. /* Read one token, getting characters through lexptr.  */

  829. static int
  830. yylex (void)
  831. {
  832.   int c;
  833.   int namelen;
  834.   unsigned int i,token;
  835.   const char *tokstart;

  836. retry:

  837.   prev_lexptr = lexptr;

  838.   tokstart = lexptr;

  839.   /* First of all, let us make sure we are not dealing with the
  840.      special tokens .true. and .false. which evaluate to 1 and 0.  */

  841.   if (*lexptr == '.')
  842.     {
  843.       for (i = 0; boolean_values[i].name != NULL; i++)
  844.         {
  845.           if (strncmp (tokstart, boolean_values[i].name,
  846.                        strlen (boolean_values[i].name)) == 0)
  847.             {
  848.               lexptr += strlen (boolean_values[i].name);
  849.               yylval.lval = boolean_values[i].value;
  850.               return BOOLEAN_LITERAL;
  851.             }
  852.         }
  853.     }

  854.   /* See if it is a special .foo. operator.  */

  855.   for (i = 0; dot_ops[i].operator != NULL; i++)
  856.     if (strncmp (tokstart, dot_ops[i].operator,
  857.                  strlen (dot_ops[i].operator)) == 0)
  858.       {
  859.         lexptr += strlen (dot_ops[i].operator);
  860.         yylval.opcode = dot_ops[i].opcode;
  861.         return dot_ops[i].token;
  862.       }

  863.   /* See if it is an exponentiation operator.  */

  864.   if (strncmp (tokstart, "**", 2) == 0)
  865.     {
  866.       lexptr += 2;
  867.       yylval.opcode = BINOP_EXP;
  868.       return STARSTAR;
  869.     }

  870.   switch (c = *tokstart)
  871.     {
  872.     case 0:
  873.       return 0;

  874.     case ' ':
  875.     case '\t':
  876.     case '\n':
  877.       lexptr++;
  878.       goto retry;

  879.     case '\'':
  880.       token = match_string_literal ();
  881.       if (token != 0)
  882.         return (token);
  883.       break;

  884.     case '(':
  885.       paren_depth++;
  886.       lexptr++;
  887.       return c;

  888.     case ')':
  889.       if (paren_depth == 0)
  890.         return 0;
  891.       paren_depth--;
  892.       lexptr++;
  893.       return c;

  894.     case ',':
  895.       if (comma_terminates && paren_depth == 0)
  896.         return 0;
  897.       lexptr++;
  898.       return c;

  899.     case '.':
  900.       /* Might be a floating point number.  */
  901.       if (lexptr[1] < '0' || lexptr[1] > '9')
  902.         goto symbol;                /* Nope, must be a symbol.  */
  903.       /* FALL THRU into number case.  */

  904.     case '0':
  905.     case '1':
  906.     case '2':
  907.     case '3':
  908.     case '4':
  909.     case '5':
  910.     case '6':
  911.     case '7':
  912.     case '8':
  913.     case '9':
  914.       {
  915.         /* It's a number.  */
  916.         int got_dot = 0, got_e = 0, got_d = 0, toktype;
  917.         const char *p = tokstart;
  918.         int hex = input_radix > 10;

  919.         if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  920.           {
  921.             p += 2;
  922.             hex = 1;
  923.           }
  924.         else if (c == '0' && (p[1]=='t' || p[1]=='T'
  925.                               || p[1]=='d' || p[1]=='D'))
  926.           {
  927.             p += 2;
  928.             hex = 0;
  929.           }

  930.         for (;; ++p)
  931.           {
  932.             if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  933.               got_dot = got_e = 1;
  934.             else if (!hex && !got_d && (*p == 'd' || *p == 'D'))
  935.               got_dot = got_d = 1;
  936.             else if (!hex && !got_dot && *p == '.')
  937.               got_dot = 1;
  938.             else if (((got_e && (p[-1] == 'e' || p[-1] == 'E'))
  939.                      || (got_d && (p[-1] == 'd' || p[-1] == 'D')))
  940.                      && (*p == '-' || *p == '+'))
  941.               /* This is the sign of the exponent, not the end of the
  942.                  number.  */
  943.               continue;
  944.             /* We will take any letters or digits.  parse_number will
  945.                complain if past the radix, or if L or U are not final.  */
  946.             else if ((*p < '0' || *p > '9')
  947.                      && ((*p < 'a' || *p > 'z')
  948.                          && (*p < 'A' || *p > 'Z')))
  949.               break;
  950.           }
  951.         toktype = parse_number (pstate, tokstart, p - tokstart,
  952.                                 got_dot|got_e|got_d,
  953.                                 &yylval);
  954.         if (toktype == ERROR)
  955.           {
  956.             char *err_copy = (char *) alloca (p - tokstart + 1);

  957.             memcpy (err_copy, tokstart, p - tokstart);
  958.             err_copy[p - tokstart] = 0;
  959.             error (_("Invalid number \"%s\"."), err_copy);
  960.           }
  961.         lexptr = p;
  962.         return toktype;
  963.       }

  964.     case '+':
  965.     case '-':
  966.     case '*':
  967.     case '/':
  968.     case '%':
  969.     case '|':
  970.     case '&':
  971.     case '^':
  972.     case '~':
  973.     case '!':
  974.     case '@':
  975.     case '<':
  976.     case '>':
  977.     case '[':
  978.     case ']':
  979.     case '?':
  980.     case ':':
  981.     case '=':
  982.     case '{':
  983.     case '}':
  984.     symbol:
  985.       lexptr++;
  986.       return c;
  987.     }

  988.   if (!(c == '_' || c == '$' || c ==':'
  989.         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  990.     /* We must have come across a bad character (e.g. ';').  */
  991.     error (_("Invalid character '%c' in expression."), c);

  992.   namelen = 0;
  993.   for (c = tokstart[namelen];
  994.        (c == '_' || c == '$' || c == ':' || (c >= '0' && c <= '9')
  995.         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  996.        c = tokstart[++namelen]);

  997.   /* The token "if" terminates the expression and is NOT
  998.      removed from the input stream.  */

  999.   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  1000.     return 0;

  1001.   lexptr += namelen;

  1002.   /* Catch specific keywords.  */

  1003.   for (i = 0; f77_keywords[i].operator != NULL; i++)
  1004.     if (strlen (f77_keywords[i].operator) == namelen
  1005.         && strncmp (tokstart, f77_keywords[i].operator, namelen) == 0)
  1006.       {
  1007.         /*         lexptr += strlen(f77_keywords[i].operator); */
  1008.         yylval.opcode = f77_keywords[i].opcode;
  1009.         return f77_keywords[i].token;
  1010.       }

  1011.   yylval.sval.ptr = tokstart;
  1012.   yylval.sval.length = namelen;

  1013.   if (*tokstart == '$')
  1014.     {
  1015.       write_dollar_variable (pstate, yylval.sval);
  1016.       return VARIABLE;
  1017.     }

  1018.   /* Use token-type TYPENAME for symbols that happen to be defined
  1019.      currently as names of types; NAME for other symbols.
  1020.      The caller is not constrained to care about the distinction.  */
  1021.   {
  1022.     char *tmp = copy_name (yylval.sval);
  1023.     struct symbol *sym;
  1024.     struct field_of_this_result is_a_field_of_this;
  1025.     enum domain_enum_tag lookup_domains[] =
  1026.     {
  1027.       STRUCT_DOMAIN,
  1028.       VAR_DOMAIN,
  1029.       MODULE_DOMAIN
  1030.     };
  1031.     int i;
  1032.     int hextype;

  1033.     for (i = 0; i < ARRAY_SIZE (lookup_domains); ++i)
  1034.       {
  1035.         /* Initialize this in case we *don't* use it in this call; that
  1036.            way we can refer to it unconditionally below.  */
  1037.         memset (&is_a_field_of_this, 0, sizeof (is_a_field_of_this));

  1038.         sym = lookup_symbol (tmp, expression_context_block,
  1039.                              lookup_domains[i],
  1040.                              parse_language (pstate)->la_language
  1041.                              == language_cplus ? &is_a_field_of_this : NULL);
  1042.         if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  1043.           {
  1044.             yylval.tsym.type = SYMBOL_TYPE (sym);
  1045.             return TYPENAME;
  1046.           }

  1047.         if (sym)
  1048.           break;
  1049.       }

  1050.     yylval.tsym.type
  1051.       = language_lookup_primitive_type (parse_language (pstate),
  1052.                                         parse_gdbarch (pstate), tmp);
  1053.     if (yylval.tsym.type != NULL)
  1054.       return TYPENAME;

  1055.     /* Input names that aren't symbols but ARE valid hex numbers,
  1056.        when the input radix permits them, can be names or numbers
  1057.        depending on the parse.  Note we support radixes > 16 here.  */
  1058.     if (!sym
  1059.         && ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10)
  1060.             || (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  1061.       {
  1062.          YYSTYPE newlval;        /* Its value is ignored.  */
  1063.         hextype = parse_number (pstate, tokstart, namelen, 0, &newlval);
  1064.         if (hextype == INT)
  1065.           {
  1066.             yylval.ssym.sym = sym;
  1067.             yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1068.             return NAME_OR_INT;
  1069.           }
  1070.       }

  1071.     /* Any other kind of symbol */
  1072.     yylval.ssym.sym = sym;
  1073.     yylval.ssym.is_a_field_of_this = is_a_field_of_this.type != NULL;
  1074.     return NAME;
  1075.   }
  1076. }

  1077. int
  1078. f_parse (struct parser_state *par_state)
  1079. {
  1080.   int result;
  1081.   struct cleanup *c = make_cleanup_clear_parser_state (&pstate);

  1082.   /* Setting up the parser state.  */
  1083.   gdb_assert (par_state != NULL);
  1084.   pstate = par_state;

  1085.   result = yyparse ();
  1086.   do_cleanups (c);
  1087.   return result;
  1088. }

  1089. void
  1090. yyerror (char *msg)
  1091. {
  1092.   if (prev_lexptr)
  1093.     lexptr = prev_lexptr;

  1094.   error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
  1095. }