gdb/m2-exp.y - gdb

  1. /* YACC grammar for Modula-2 expressions, for GDB.
  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.
  3.    Generated from expread.y (now c-exp.y) and contributed by the Department
  4.    of Computer Science at the State University of New York at Buffalo, 1991.

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

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

  31. %{

  32. #include "defs.h"
  33. #include "expression.h"
  34. #include "language.h"
  35. #include "value.h"
  36. #include "parser-defs.h"
  37. #include "m2-lang.h"
  38. #include "bfd.h" /* Required by objfiles.h.  */
  39. #include "symfile.h" /* Required by objfiles.h.  */
  40. #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
  41. #include "block.h"

  42. #define parse_type(ps) builtin_type (parse_gdbarch (ps))
  43. #define parse_m2_type(ps) builtin_m2_type (parse_gdbarch (ps))

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

  50. #define        yymaxdepth m2_maxdepth
  51. #define        yyparse        m2_parse_internal
  52. #define        yylex        m2_lex
  53. #define        yyerror        m2_error
  54. #define        yylval        m2_lval
  55. #define        yychar        m2_char
  56. #define        yydebug        m2_debug
  57. #define        yypact        m2_pact
  58. #define        yyr1        m2_r1
  59. #define        yyr2        m2_r2
  60. #define        yydef        m2_def
  61. #define        yychk        m2_chk
  62. #define        yypgo        m2_pgo
  63. #define        yyact        m2_act
  64. #define        yyexca        m2_exca
  65. #define        yyerrflag m2_errflag
  66. #define        yynerrs        m2_nerrs
  67. #define        yyps        m2_ps
  68. #define        yypv        m2_pv
  69. #define        yys        m2_s
  70. #define        yy_yys        m2_yys
  71. #define        yystate        m2_state
  72. #define        yytmp        m2_tmp
  73. #define        yyv        m2_v
  74. #define        yy_yyv        m2_yyv
  75. #define        yyval        m2_val
  76. #define        yylloc        m2_lloc
  77. #define        yyreds        m2_reds                /* With YYDEBUG defined */
  78. #define        yytoks        m2_toks                /* With YYDEBUG defined */
  79. #define yyname        m2_name                /* With YYDEBUG defined */
  80. #define yyrule        m2_rule                /* With YYDEBUG defined */
  81. #define yylhs        m2_yylhs
  82. #define yylen        m2_yylen
  83. #define yydefred m2_yydefred
  84. #define yydgoto        m2_yydgoto
  85. #define yysindex m2_yysindex
  86. #define yyrindex m2_yyrindex
  87. #define yygindex m2_yygindex
  88. #define yytable         m2_yytable
  89. #define yycheck         m2_yycheck
  90. #define yyss        m2_yyss
  91. #define yysslim        m2_yysslim
  92. #define yyssp        m2_yyssp
  93. #define yystacksize m2_yystacksize
  94. #define yyvs        m2_yyvs
  95. #define yyvsp        m2_yyvsp

  96. #ifndef YYDEBUG
  97. #define        YYDEBUG 1                /* Default to yydebug support */
  98. #endif

  99. #define YYFPRINTF parser_fprintf

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

  102. static struct parser_state *pstate = NULL;

  103. int yyparse (void);

  104. static int yylex (void);

  105. void yyerror (char *);

  106. static int parse_number (int);

  107. /* The sign of the number being parsed.  */
  108. static int number_sign = 1;

  109. %}

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

  113. %union
  114.   {
  115.     LONGEST lval;
  116.     ULONGEST ulval;
  117.     DOUBLEST dval;
  118.     struct symbol *sym;
  119.     struct type *tval;
  120.     struct stoken sval;
  121.     int voidval;
  122.     const struct block *bval;
  123.     enum exp_opcode opcode;
  124.     struct internalvar *ivar;

  125.     struct type **tvec;
  126.     int *ivec;
  127.   }

  128. %type <voidval> exp type_exp start set
  129. %type <voidval> variable
  130. %type <tval> type
  131. %type <bval> block
  132. %type <sym> fblock

  133. %token <lval> INT HEX ERROR
  134. %token <ulval> UINT M2_TRUE M2_FALSE CHAR
  135. %token <dval> FLOAT

  136. /* Both NAME and TYPENAME tokens represent symbols in the input,
  137.    and both convey their data as strings.
  138.    But a TYPENAME is a string that happens to be defined as a typedef
  139.    or builtin type name (such as int or char)
  140.    and a NAME is any other symbol.

  141.    Contexts where this distinction is not important can use the
  142.    nonterminal "name", which matches either NAME or TYPENAME.  */

  143. %token <sval> STRING
  144. %token <sval> NAME BLOCKNAME IDENT VARNAME
  145. %token <sval> TYPENAME

  146. %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
  147. %token TSIZE
  148. %token INC DEC INCL EXCL

  149. /* The GDB scope operator */
  150. %token COLONCOLON

  151. %token <voidval> INTERNAL_VAR

  152. /* M2 tokens */
  153. %left ','
  154. %left ABOVE_COMMA
  155. %nonassoc ASSIGN
  156. %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
  157. %left OROR
  158. %left LOGICAL_AND '&'
  159. %left '@'
  160. %left '+' '-'
  161. %left '*' '/' DIV MOD
  162. %right UNARY
  163. %right '^' DOT '[' '('
  164. %right NOT '~'
  165. %left COLONCOLON QID
  166. /* This is not an actual token ; it is used for precedence.
  167. %right QID
  168. */


  169. %%

  170. start   :        exp
  171.         |        type_exp
  172.         ;

  173. type_exp:        type
  174.                 { write_exp_elt_opcode (pstate, OP_TYPE);
  175.                   write_exp_elt_type (pstate, $1);
  176.                   write_exp_elt_opcode (pstate, OP_TYPE);
  177.                 }
  178.         ;

  179. /* Expressions */

  180. exp     :       exp '^'   %prec UNARY
  181.                         { write_exp_elt_opcode (pstate, UNOP_IND); }
  182.         ;

  183. exp        :        '-'
  184.                         { number_sign = -1; }
  185.                 exp    %prec UNARY
  186.                         { number_sign = 1;
  187.                           write_exp_elt_opcode (pstate, UNOP_NEG); }
  188.         ;

  189. exp        :        '+' exp    %prec UNARY
  190.                 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
  191.         ;

  192. exp        :        not_exp exp %prec UNARY
  193.                         { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
  194.         ;

  195. not_exp        :        NOT
  196.         |        '~'
  197.         ;

  198. exp        :        CAP '(' exp ')'
  199.                         { write_exp_elt_opcode (pstate, UNOP_CAP); }
  200.         ;

  201. exp        :        ORD '(' exp ')'
  202.                         { write_exp_elt_opcode (pstate, UNOP_ORD); }
  203.         ;

  204. exp        :        ABS '(' exp ')'
  205.                         { write_exp_elt_opcode (pstate, UNOP_ABS); }
  206.         ;

  207. exp        :         HIGH '(' exp ')'
  208.                         { write_exp_elt_opcode (pstate, UNOP_HIGH); }
  209.         ;

  210. exp         :        MIN_FUNC '(' type ')'
  211.                         { write_exp_elt_opcode (pstate, UNOP_MIN);
  212.                           write_exp_elt_type (pstate, $3);
  213.                           write_exp_elt_opcode (pstate, UNOP_MIN); }
  214.         ;

  215. exp        :         MAX_FUNC '(' type ')'
  216.                         { write_exp_elt_opcode (pstate, UNOP_MAX);
  217.                           write_exp_elt_type (pstate, $3);
  218.                           write_exp_elt_opcode (pstate, UNOP_MAX); }
  219.         ;

  220. exp        :        FLOAT_FUNC '(' exp ')'
  221.                         { write_exp_elt_opcode (pstate, UNOP_FLOAT); }
  222.         ;

  223. exp        :        VAL '(' type ',' exp ')'
  224.                         { write_exp_elt_opcode (pstate, BINOP_VAL);
  225.                           write_exp_elt_type (pstate, $3);
  226.                           write_exp_elt_opcode (pstate, BINOP_VAL); }
  227.         ;

  228. exp        :        CHR '(' exp ')'
  229.                         { write_exp_elt_opcode (pstate, UNOP_CHR); }
  230.         ;

  231. exp        :        ODD '(' exp ')'
  232.                         { write_exp_elt_opcode (pstate, UNOP_ODD); }
  233.         ;

  234. exp        :        TRUNC '(' exp ')'
  235.                         { write_exp_elt_opcode (pstate, UNOP_TRUNC); }
  236.         ;

  237. exp        :        TSIZE '(' exp ')'
  238.                         { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
  239.         ;

  240. exp        :        SIZE exp       %prec UNARY
  241.                         { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
  242.         ;


  243. exp        :        INC '(' exp ')'
  244.                         { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
  245.         ;

  246. exp        :        INC '(' exp ',' exp ')'
  247.                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
  248.                           write_exp_elt_opcode (pstate, BINOP_ADD);
  249.                           write_exp_elt_opcode (pstate,
  250.                                                 BINOP_ASSIGN_MODIFY); }
  251.         ;

  252. exp        :        DEC '(' exp ')'
  253.                         { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT);}
  254.         ;

  255. exp        :        DEC '(' exp ',' exp ')'
  256.                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
  257.                           write_exp_elt_opcode (pstate, BINOP_SUB);
  258.                           write_exp_elt_opcode (pstate,
  259.                                                 BINOP_ASSIGN_MODIFY); }
  260.         ;

  261. exp        :        exp DOT NAME
  262.                         { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
  263.                           write_exp_string (pstate, $3);
  264.                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
  265.         ;

  266. exp        :        set
  267.         ;

  268. exp        :        exp IN set
  269.                         { error (_("Sets are not implemented."));}
  270.         ;

  271. exp        :        INCL '(' exp ',' exp ')'
  272.                         { error (_("Sets are not implemented."));}
  273.         ;

  274. exp        :        EXCL '(' exp ',' exp ')'
  275.                         { error (_("Sets are not implemented."));}
  276.         ;

  277. set        :        '{' arglist '}'
  278.                         { error (_("Sets are not implemented."));}
  279.         |        type '{' arglist '}'
  280.                         { error (_("Sets are not implemented."));}
  281.         ;


  282. /* Modula-2 array subscript notation [a,b,c...] */
  283. exp     :       exp '['
  284.                         /* This function just saves the number of arguments
  285.                            that follow in the list.  It is *not* specific to
  286.                            function types */
  287.                         { start_arglist(); }
  288.                 non_empty_arglist ']'  %prec DOT
  289.                         { write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
  290.                           write_exp_elt_longcst (pstate,
  291.                                                  (LONGEST) end_arglist());
  292.                           write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT); }
  293.         ;

  294. exp        :        exp '[' exp ']'
  295.                         { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
  296.         ;

  297. exp        :        exp '('
  298.                         /* This is to save the value of arglist_len
  299.                            being accumulated by an outer function call.  */
  300.                         { start_arglist (); }
  301.                 arglist ')'        %prec DOT
  302.                         { write_exp_elt_opcode (pstate, OP_FUNCALL);
  303.                           write_exp_elt_longcst (pstate,
  304.                                                  (LONGEST) end_arglist ());
  305.                           write_exp_elt_opcode (pstate, OP_FUNCALL); }
  306.         ;

  307. arglist        :
  308.         ;

  309. arglist        :        exp
  310.                         { arglist_len = 1; }
  311.         ;

  312. arglist        :        arglist ',' exp   %prec ABOVE_COMMA
  313.                         { arglist_len++; }
  314.         ;

  315. non_empty_arglist
  316.         :       exp
  317.                         { arglist_len = 1; }
  318.         ;

  319. non_empty_arglist
  320.         :       non_empty_arglist ',' exp %prec ABOVE_COMMA
  321.                                         { arglist_len++; }
  322.              ;

  323. /* GDB construct */
  324. exp        :        '{' type '}' exp  %prec UNARY
  325.                         { write_exp_elt_opcode (pstate, UNOP_MEMVAL);
  326.                           write_exp_elt_type (pstate, $2);
  327.                           write_exp_elt_opcode (pstate, UNOP_MEMVAL); }
  328.         ;

  329. exp     :       type '(' exp ')' %prec UNARY
  330.                         { write_exp_elt_opcode (pstate, UNOP_CAST);
  331.                           write_exp_elt_type (pstate, $1);
  332.                           write_exp_elt_opcode (pstate, UNOP_CAST); }
  333.         ;

  334. exp        :        '(' exp ')'
  335.                         { }
  336.         ;

  337. /* Binary operators in order of decreasing precedence.  Note that some
  338.    of these operators are overloaded!  (ie. sets) */

  339. /* GDB construct */
  340. exp        :        exp '@' exp
  341.                         { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
  342.         ;

  343. exp        :        exp '*' exp
  344.                         { write_exp_elt_opcode (pstate, BINOP_MUL); }
  345.         ;

  346. exp        :        exp '/' exp
  347.                         { write_exp_elt_opcode (pstate, BINOP_DIV); }
  348.         ;

  349. exp     :       exp DIV exp
  350.                         { write_exp_elt_opcode (pstate, BINOP_INTDIV); }
  351.         ;

  352. exp        :        exp MOD exp
  353.                         { write_exp_elt_opcode (pstate, BINOP_REM); }
  354.         ;

  355. exp        :        exp '+' exp
  356.                         { write_exp_elt_opcode (pstate, BINOP_ADD); }
  357.         ;

  358. exp        :        exp '-' exp
  359.                         { write_exp_elt_opcode (pstate, BINOP_SUB); }
  360.         ;

  361. exp        :        exp '=' exp
  362.                         { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
  363.         ;

  364. exp        :        exp NOTEQUAL exp
  365.                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
  366.         |       exp '#' exp
  367.                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
  368.         ;

  369. exp        :        exp LEQ exp
  370.                         { write_exp_elt_opcode (pstate, BINOP_LEQ); }
  371.         ;

  372. exp        :        exp GEQ exp
  373.                         { write_exp_elt_opcode (pstate, BINOP_GEQ); }
  374.         ;

  375. exp        :        exp '<' exp
  376.                         { write_exp_elt_opcode (pstate, BINOP_LESS); }
  377.         ;

  378. exp        :        exp '>' exp
  379.                         { write_exp_elt_opcode (pstate, BINOP_GTR); }
  380.         ;

  381. exp        :        exp LOGICAL_AND exp
  382.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
  383.         ;

  384. exp        :        exp OROR exp
  385.                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
  386.         ;

  387. exp        :        exp ASSIGN exp
  388.                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
  389.         ;


  390. /* Constants */

  391. exp        :        M2_TRUE
  392.                         { write_exp_elt_opcode (pstate, OP_BOOL);
  393.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  394.                           write_exp_elt_opcode (pstate, OP_BOOL); }
  395.         ;

  396. exp        :        M2_FALSE
  397.                         { write_exp_elt_opcode (pstate, OP_BOOL);
  398.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  399.                           write_exp_elt_opcode (pstate, OP_BOOL); }
  400.         ;

  401. exp        :        INT
  402.                         { write_exp_elt_opcode (pstate, OP_LONG);
  403.                           write_exp_elt_type (pstate,
  404.                                         parse_m2_type (pstate)->builtin_int);
  405.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  406.                           write_exp_elt_opcode (pstate, OP_LONG); }
  407.         ;

  408. exp        :        UINT
  409.                         {
  410.                           write_exp_elt_opcode (pstate, OP_LONG);
  411.                           write_exp_elt_type (pstate,
  412.                                               parse_m2_type (pstate)
  413.                                               ->builtin_card);
  414.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  415.                           write_exp_elt_opcode (pstate, OP_LONG);
  416.                         }
  417.         ;

  418. exp        :        CHAR
  419.                         { write_exp_elt_opcode (pstate, OP_LONG);
  420.                           write_exp_elt_type (pstate,
  421.                                               parse_m2_type (pstate)
  422.                                               ->builtin_char);
  423.                           write_exp_elt_longcst (pstate, (LONGEST) $1);
  424.                           write_exp_elt_opcode (pstate, OP_LONG); }
  425.         ;


  426. exp        :        FLOAT
  427.                         { write_exp_elt_opcode (pstate, OP_DOUBLE);
  428.                           write_exp_elt_type (pstate,
  429.                                               parse_m2_type (pstate)
  430.                                               ->builtin_real);
  431.                           write_exp_elt_dblcst (pstate, $1);
  432.                           write_exp_elt_opcode (pstate, OP_DOUBLE); }
  433.         ;

  434. exp        :        variable
  435.         ;

  436. exp        :        SIZE '(' type ')'        %prec UNARY
  437.                         { write_exp_elt_opcode (pstate, OP_LONG);
  438.                           write_exp_elt_type (pstate,
  439.                                             parse_type (pstate)->builtin_int);
  440.                           write_exp_elt_longcst (pstate,
  441.                                                  (LONGEST) TYPE_LENGTH ($3));
  442.                           write_exp_elt_opcode (pstate, OP_LONG); }
  443.         ;

  444. exp        :        STRING
  445.                         { write_exp_elt_opcode (pstate, OP_M2_STRING);
  446.                           write_exp_string (pstate, $1);
  447.                           write_exp_elt_opcode (pstate, OP_M2_STRING); }
  448.         ;

  449. /* This will be used for extensions later.  Like adding modules.  */
  450. block        :        fblock
  451.                         { $$ = SYMBOL_BLOCK_VALUE($1); }
  452.         ;

  453. fblock        :        BLOCKNAME
  454.                         { struct symbol *sym
  455.                             = lookup_symbol (copy_name ($1),
  456.                                              expression_context_block,
  457.                                              VAR_DOMAIN, 0);
  458.                           $$ = sym;}
  459.         ;


  460. /* GDB scope operator */
  461. fblock        :        block COLONCOLON BLOCKNAME
  462.                         { struct symbol *tem
  463.                             = lookup_symbol (copy_name ($3), $1,
  464.                                              VAR_DOMAIN, 0);
  465.                           if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  466.                             error (_("No function \"%s\" in specified context."),
  467.                                    copy_name ($3));
  468.                           $$ = tem;
  469.                         }
  470.         ;

  471. /* Useful for assigning to PROCEDURE variables */
  472. variable:        fblock
  473.                         { write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  474.                           write_exp_elt_block (pstate, NULL);
  475.                           write_exp_elt_sym (pstate, $1);
  476.                           write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
  477.         ;

  478. /* GDB internal ($foo) variable */
  479. variable:        INTERNAL_VAR
  480.         ;

  481. /* GDB scope operator */
  482. variable:        block COLONCOLON NAME
  483.                         { struct symbol *sym;
  484.                           sym = lookup_symbol (copy_name ($3), $1,
  485.                                                VAR_DOMAIN, 0);
  486.                           if (sym == 0)
  487.                             error (_("No symbol \"%s\" in specified context."),
  488.                                    copy_name ($3));
  489.                           if (symbol_read_needs_frame (sym))
  490.                             {
  491.                               if (innermost_block == 0
  492.                                   || contained_in (block_found,
  493.                                                    innermost_block))
  494.                                 innermost_block = block_found;
  495.                             }

  496.                           write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  497.                           /* block_found is set by lookup_symbol.  */
  498.                           write_exp_elt_block (pstate, block_found);
  499.                           write_exp_elt_sym (pstate, sym);
  500.                           write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
  501.         ;

  502. /* Base case for variables.  */
  503. variable:        NAME
  504.                         { struct symbol *sym;
  505.                           struct field_of_this_result is_a_field_of_this;

  506.                            sym = lookup_symbol (copy_name ($1),
  507.                                                expression_context_block,
  508.                                                VAR_DOMAIN,
  509.                                                &is_a_field_of_this);
  510.                           if (sym)
  511.                             {
  512.                               if (symbol_read_needs_frame (sym))
  513.                                 {
  514.                                   if (innermost_block == 0 ||
  515.                                       contained_in (block_found,
  516.                                                     innermost_block))
  517.                                     innermost_block = block_found;
  518.                                 }

  519.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  520.                               /* We want to use the selected frame, not
  521.                                  another more inner frame which happens to
  522.                                  be in the same block.  */
  523.                               write_exp_elt_block (pstate, NULL);
  524.                               write_exp_elt_sym (pstate, sym);
  525.                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
  526.                             }
  527.                           else
  528.                             {
  529.                               struct bound_minimal_symbol msymbol;
  530.                               char *arg = copy_name ($1);

  531.                               msymbol =
  532.                                 lookup_bound_minimal_symbol (arg);
  533.                               if (msymbol.minsym != NULL)
  534.                                 write_exp_msymbol (pstate, msymbol);
  535.                               else if (!have_full_symbols () && !have_partial_symbols ())
  536.                                 error (_("No symbol table is loaded.  Use the \"symbol-file\" command."));
  537.                               else
  538.                                 error (_("No symbol \"%s\" in current context."),
  539.                                        copy_name ($1));
  540.                             }
  541.                         }
  542.         ;

  543. type
  544.         :        TYPENAME
  545.                         { $$ = lookup_typename (parse_language (pstate),
  546.                                                 parse_gdbarch (pstate),
  547.                                                 copy_name ($1),
  548.                                                 expression_context_block, 0); }

  549.         ;

  550. %%

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

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

  555. static int
  556. parse_number (int olen)
  557. {
  558.   const char *p = lexptr;
  559.   LONGEST n = 0;
  560.   LONGEST prevn = 0;
  561.   int c,i,ischar=0;
  562.   int base = input_radix;
  563.   int len = olen;
  564.   int unsigned_p = number_sign == 1 ? 1 : 0;

  565.   if(p[len-1] == 'H')
  566.   {
  567.      base = 16;
  568.      len--;
  569.   }
  570.   else if(p[len-1] == 'C' || p[len-1] == 'B')
  571.   {
  572.      base = 8;
  573.      ischar = p[len-1] == 'C';
  574.      len--;
  575.   }

  576.   /* Scan the number */
  577.   for (c = 0; c < len; c++)
  578.   {
  579.     if (p[c] == '.' && base == 10)
  580.       {
  581.         /* It's a float since it contains a point.  */
  582.         yylval.dval = atof (p);
  583.         lexptr += len;
  584.         return FLOAT;
  585.       }
  586.     if (p[c] == '.' && base != 10)
  587.        error (_("Floating point numbers must be base 10."));
  588.     if (base == 10 && (p[c] < '0' || p[c] > '9'))
  589.        error (_("Invalid digit \'%c\' in number."),p[c]);
  590. }

  591.   while (len-- > 0)
  592.     {
  593.       c = *p++;
  594.       n *= base;
  595.       if( base == 8 && (c == '8' || c == '9'))
  596.          error (_("Invalid digit \'%c\' in octal number."),c);
  597.       if (c >= '0' && c <= '9')
  598.         i = c - '0';
  599.       else
  600.         {
  601.           if (base == 16 && c >= 'A' && c <= 'F')
  602.             i = c - 'A' + 10;
  603.           else
  604.              return ERROR;
  605.         }
  606.       n+=i;
  607.       if(i >= base)
  608.          return ERROR;
  609.       if(!unsigned_p && number_sign == 1 && (prevn >= n))
  610.          unsigned_p=1;                /* Try something unsigned */
  611.       /* Don't do the range check if n==i and i==0, since that special
  612.          case will give an overflow error.  */
  613.       if(RANGE_CHECK && n!=i && i)
  614.       {
  615.          if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
  616.             ((!unsigned_p && number_sign==-1) && -prevn <= -n))
  617.             range_error (_("Overflow on numeric constant."));
  618.       }
  619.          prevn=n;
  620.     }

  621.   lexptr = p;
  622.   if(*p == 'B' || *p == 'C' || *p == 'H')
  623.      lexptr++;                        /* Advance past B,C or H */

  624.   if (ischar)
  625.   {
  626.      yylval.ulval = n;
  627.      return CHAR;
  628.   }
  629.   else if ( unsigned_p && number_sign == 1)
  630.   {
  631.      yylval.ulval = n;
  632.      return UINT;
  633.   }
  634.   else if((unsigned_p && (n<0))) {
  635.      range_error (_("Overflow on numeric constant -- number too large."));
  636.      /* But, this can return if range_check == range_warn.  */
  637.   }
  638.   yylval.lval = n;
  639.   return INT;
  640. }


  641. /* Some tokens */

  642. static struct
  643. {
  644.    char name[2];
  645.    int token;
  646. } tokentab2[] =
  647. {
  648.     { {'<', '>'},    NOTEQUAL         },
  649.     { {':', '='},    ASSIGN        },
  650.     { {'<', '='},    LEQ        },
  651.     { {'>', '='},    GEQ        },
  652.     { {':', ':'},    COLONCOLON },

  653. };

  654. /* Some specific keywords */

  655. struct keyword {
  656.    char keyw[10];
  657.    int token;
  658. };

  659. static struct keyword keytab[] =
  660. {
  661.     {"OR" ,   OROR         },
  662.     {"IN",    IN         },/* Note space after IN */
  663.     {"AND",   LOGICAL_AND},
  664.     {"ABS",   ABS         },
  665.     {"CHR",   CHR         },
  666.     {"DEC",   DEC         },
  667.     {"NOT",   NOT         },
  668.     {"DIV",   DIV             },
  669.     {"INC",   INC         },
  670.     {"MAX",   MAX_FUNC         },
  671.     {"MIN",   MIN_FUNC         },
  672.     {"MOD",   MOD         },
  673.     {"ODD",   ODD         },
  674.     {"CAP",   CAP         },
  675.     {"ORD",   ORD         },
  676.     {"VAL",   VAL         },
  677.     {"EXCL",  EXCL         },
  678.     {"HIGH",  HIGH       },
  679.     {"INCL",  INCL         },
  680.     {"SIZE",  SIZE       },
  681.     {"FLOAT", FLOAT_FUNC },
  682.     {"TRUNC", TRUNC         },
  683.     {"TSIZE", SIZE       },
  684. };


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

  686. /* This is where we will check to make sure that the language and the
  687.    operators used are compatible  */

  688. static int
  689. yylex (void)
  690. {
  691.   int c;
  692.   int namelen;
  693.   int i;
  694.   const char *tokstart;
  695.   char quote;

  696. retry:

  697.   prev_lexptr = lexptr;

  698.   tokstart = lexptr;


  699.   /* See if it is a special token of length 2 */
  700.   for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
  701.      if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
  702.      {
  703.         lexptr += 2;
  704.         return tokentab2[i].token;
  705.      }

  706.   switch (c = *tokstart)
  707.     {
  708.     case 0:
  709.       return 0;

  710.     case ' ':
  711.     case '\t':
  712.     case '\n':
  713.       lexptr++;
  714.       goto retry;

  715.     case '(':
  716.       paren_depth++;
  717.       lexptr++;
  718.       return c;

  719.     case ')':
  720.       if (paren_depth == 0)
  721.         return 0;
  722.       paren_depth--;
  723.       lexptr++;
  724.       return c;

  725.     case ',':
  726.       if (comma_terminates && paren_depth == 0)
  727.         return 0;
  728.       lexptr++;
  729.       return c;

  730.     case '.':
  731.       /* Might be a floating point number.  */
  732.       if (lexptr[1] >= '0' && lexptr[1] <= '9')
  733.         break;                        /* Falls into number code.  */
  734.       else
  735.       {
  736.          lexptr++;
  737.          return DOT;
  738.       }

  739. /* These are character tokens that appear as-is in the YACC grammar */
  740.     case '+':
  741.     case '-':
  742.     case '*':
  743.     case '/':
  744.     case '^':
  745.     case '<':
  746.     case '>':
  747.     case '[':
  748.     case ']':
  749.     case '=':
  750.     case '{':
  751.     case '}':
  752.     case '#':
  753.     case '@':
  754.     case '~':
  755.     case '&':
  756.       lexptr++;
  757.       return c;

  758.     case '\'' :
  759.     case '"':
  760.       quote = c;
  761.       for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
  762.         if (c == '\\')
  763.           {
  764.             c = tokstart[++namelen];
  765.             if (c >= '0' && c <= '9')
  766.               {
  767.                 c = tokstart[++namelen];
  768.                 if (c >= '0' && c <= '9')
  769.                   c = tokstart[++namelen];
  770.               }
  771.           }
  772.       if(c != quote)
  773.          error (_("Unterminated string or character constant."));
  774.       yylval.sval.ptr = tokstart + 1;
  775.       yylval.sval.length = namelen - 1;
  776.       lexptr += namelen + 1;

  777.       if(namelen == 2)          /* Single character */
  778.       {
  779.            yylval.ulval = tokstart[1];
  780.            return CHAR;
  781.       }
  782.       else
  783.          return STRING;
  784.     }

  785.   /* Is it a number?  */
  786.   /* Note:  We have already dealt with the case of the token '.'.
  787.      See case '.' above.  */
  788.   if ((c >= '0' && c <= '9'))
  789.     {
  790.       /* It's a number.  */
  791.       int got_dot = 0, got_e = 0;
  792.       const char *p = tokstart;
  793.       int toktype;

  794.       for (++p ;; ++p)
  795.         {
  796.           if (!got_e && (*p == 'e' || *p == 'E'))
  797.             got_dot = got_e = 1;
  798.           else if (!got_dot && *p == '.')
  799.             got_dot = 1;
  800.           else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  801.                    && (*p == '-' || *p == '+'))
  802.             /* This is the sign of the exponent, not the end of the
  803.                number.  */
  804.             continue;
  805.           else if ((*p < '0' || *p > '9') &&
  806.                    (*p < 'A' || *p > 'F') &&
  807.                    (*p != 'H'))  /* Modula-2 hexadecimal number */
  808.             break;
  809.         }
  810.         toktype = parse_number (p - tokstart);
  811.         if (toktype == ERROR)
  812.           {
  813.             char *err_copy = (char *) alloca (p - tokstart + 1);

  814.             memcpy (err_copy, tokstart, p - tokstart);
  815.             err_copy[p - tokstart] = 0;
  816.             error (_("Invalid number \"%s\"."), err_copy);
  817.           }
  818.         lexptr = p;
  819.         return toktype;
  820.     }

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

  825.   /* It's a name.  See how long it is.  */
  826.   namelen = 0;
  827.   for (c = tokstart[namelen];
  828.        (c == '_' || c == '$' || (c >= '0' && c <= '9')
  829.         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  830.        c = tokstart[++namelen])
  831.     ;

  832.   /* The token "if" terminates the expression and is NOT
  833.      removed from the input stream.  */
  834.   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  835.     {
  836.       return 0;
  837.     }

  838.   lexptr += namelen;

  839.   /*  Lookup special keywords */
  840.   for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
  841.      if (namelen == strlen (keytab[i].keyw)
  842.          && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
  843.            return keytab[i].token;

  844.   yylval.sval.ptr = tokstart;
  845.   yylval.sval.length = namelen;

  846.   if (*tokstart == '$')
  847.     {
  848.       write_dollar_variable (pstate, yylval.sval);
  849.       return INTERNAL_VAR;
  850.     }

  851.   /* Use token-type BLOCKNAME for symbols that happen to be defined as
  852.      functions.  If this is not so, then ...
  853.      Use token-type TYPENAME for symbols that happen to be defined
  854.      currently as names of types; NAME for other symbols.
  855.      The caller is not constrained to care about the distinction.  */
  856. {


  857.     char *tmp = copy_name (yylval.sval);
  858.     struct symbol *sym;

  859.     if (lookup_symtab (tmp))
  860.       return BLOCKNAME;
  861.     sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 0);
  862.     if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  863.       return BLOCKNAME;
  864.     if (lookup_typename (parse_language (pstate), parse_gdbarch (pstate),
  865.                          copy_name (yylval.sval),
  866.                          expression_context_block, 1))
  867.       return TYPENAME;

  868.     if(sym)
  869.     {
  870.       switch(SYMBOL_CLASS (sym))
  871.        {
  872.        case LOC_STATIC:
  873.        case LOC_REGISTER:
  874.        case LOC_ARG:
  875.        case LOC_REF_ARG:
  876.        case LOC_REGPARM_ADDR:
  877.        case LOC_LOCAL:
  878.        case LOC_CONST:
  879.        case LOC_CONST_BYTES:
  880.        case LOC_OPTIMIZED_OUT:
  881.        case LOC_COMPUTED:
  882.           return NAME;

  883.        case LOC_TYPEDEF:
  884.           return TYPENAME;

  885.        case LOC_BLOCK:
  886.           return BLOCKNAME;

  887.        case LOC_UNDEF:
  888.           error (_("internal:  Undefined class in m2lex()"));

  889.        case LOC_LABEL:
  890.        case LOC_UNRESOLVED:
  891.           error (_("internal:  Unforseen case in m2lex()"));

  892.        default:
  893.           error (_("unhandled token in m2lex()"));
  894.           break;
  895.        }
  896.     }
  897.     else
  898.     {
  899.        /* Built-in BOOLEAN type.  This is sort of a hack.  */
  900.        if (strncmp (tokstart, "TRUE", 4) == 0)
  901.        {
  902.           yylval.ulval = 1;
  903.           return M2_TRUE;
  904.        }
  905.        else if (strncmp (tokstart, "FALSE", 5) == 0)
  906.        {
  907.           yylval.ulval = 0;
  908.           return M2_FALSE;
  909.        }
  910.     }

  911.     /* Must be another type of name...  */
  912.     return NAME;
  913. }
  914. }

  915. int
  916. m2_parse (struct parser_state *par_state)
  917. {
  918.   int result;
  919.   struct cleanup *c = make_cleanup_clear_parser_state (&pstate);

  920.   /* Setting up the parser state.  */
  921.   gdb_assert (par_state != NULL);
  922.   pstate = par_state;

  923.   result = yyparse ();
  924.   do_cleanups (c);

  925.   return result;
  926. }

  927. void
  928. yyerror (char *msg)
  929. {
  930.   if (prev_lexptr)
  931.     lexptr = prev_lexptr;

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