Make aarch64_notify_debug_reg_change the same on GDB and GDBserver
[external/binutils.git] / gdb / m2-exp.y
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
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /* Parse a Modula-2 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 "expression.h"
42 #include "language.h"
43 #include "value.h"
44 #include "parser-defs.h"
45 #include "m2-lang.h"
46 #include "bfd.h" /* Required by objfiles.h.  */
47 #include "symfile.h" /* Required by objfiles.h.  */
48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
49 #include "block.h"
50
51 #define parse_type(ps) builtin_type (parse_gdbarch (ps))
52 #define parse_m2_type(ps) builtin_m2_type (parse_gdbarch (ps))
53
54 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
55    as well as gratuitiously global symbol names, so we can have multiple
56    yacc generated parsers in gdb.  Note that these are only the variables
57    produced by yacc.  If other parser generators (bison, byacc, etc) produce
58    additional global names that conflict at link time, then those parser
59    generators need to be fixed instead of adding those names to this list.  */
60
61 #define yymaxdepth m2_maxdepth
62 #define yyparse m2_parse_internal
63 #define yylex   m2_lex
64 #define yyerror m2_error
65 #define yylval  m2_lval
66 #define yychar  m2_char
67 #define yydebug m2_debug
68 #define yypact  m2_pact
69 #define yyr1    m2_r1
70 #define yyr2    m2_r2
71 #define yydef   m2_def
72 #define yychk   m2_chk
73 #define yypgo   m2_pgo
74 #define yyact   m2_act
75 #define yyexca  m2_exca
76 #define yyerrflag m2_errflag
77 #define yynerrs m2_nerrs
78 #define yyps    m2_ps
79 #define yypv    m2_pv
80 #define yys     m2_s
81 #define yy_yys  m2_yys
82 #define yystate m2_state
83 #define yytmp   m2_tmp
84 #define yyv     m2_v
85 #define yy_yyv  m2_yyv
86 #define yyval   m2_val
87 #define yylloc  m2_lloc
88 #define yyreds  m2_reds         /* With YYDEBUG defined */
89 #define yytoks  m2_toks         /* With YYDEBUG defined */
90 #define yyname  m2_name         /* With YYDEBUG defined */
91 #define yyrule  m2_rule         /* With YYDEBUG defined */
92 #define yylhs   m2_yylhs
93 #define yylen   m2_yylen
94 #define yydefred m2_yydefred
95 #define yydgoto m2_yydgoto
96 #define yysindex m2_yysindex
97 #define yyrindex m2_yyrindex
98 #define yygindex m2_yygindex
99 #define yytable  m2_yytable
100 #define yycheck  m2_yycheck
101 #define yyss    m2_yyss
102 #define yysslim m2_yysslim
103 #define yyssp   m2_yyssp
104 #define yystacksize m2_yystacksize
105 #define yyvs    m2_yyvs
106 #define yyvsp   m2_yyvsp
107
108 #ifndef YYDEBUG
109 #define YYDEBUG 1               /* Default to yydebug support */
110 #endif
111
112 #define YYFPRINTF parser_fprintf
113
114 /* The state of the parser, used internally when we are parsing the
115    expression.  */
116
117 static struct parser_state *pstate = NULL;
118
119 int yyparse (void);
120
121 static int yylex (void);
122
123 void yyerror (char *);
124
125 static int parse_number (int);
126
127 /* The sign of the number being parsed.  */
128 static int number_sign = 1;
129
130 %}
131
132 /* Although the yacc "value" of an expression is not used,
133    since the result is stored in the structure being created,
134    other node types do have values.  */
135
136 %union
137   {
138     LONGEST lval;
139     ULONGEST ulval;
140     DOUBLEST dval;
141     struct symbol *sym;
142     struct type *tval;
143     struct stoken sval;
144     int voidval;
145     const struct block *bval;
146     enum exp_opcode opcode;
147     struct internalvar *ivar;
148
149     struct type **tvec;
150     int *ivec;
151   }
152
153 %type <voidval> exp type_exp start set
154 %type <voidval> variable
155 %type <tval> type
156 %type <bval> block 
157 %type <sym> fblock 
158
159 %token <lval> INT HEX ERROR
160 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
161 %token <dval> FLOAT
162
163 /* Both NAME and TYPENAME tokens represent symbols in the input,
164    and both convey their data as strings.
165    But a TYPENAME is a string that happens to be defined as a typedef
166    or builtin type name (such as int or char)
167    and a NAME is any other symbol.
168
169    Contexts where this distinction is not important can use the
170    nonterminal "name", which matches either NAME or TYPENAME.  */
171
172 %token <sval> STRING
173 %token <sval> NAME BLOCKNAME IDENT VARNAME
174 %token <sval> TYPENAME
175
176 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
177 %token TSIZE
178 %token INC DEC INCL EXCL
179
180 /* The GDB scope operator */
181 %token COLONCOLON
182
183 %token <voidval> INTERNAL_VAR
184
185 /* M2 tokens */
186 %left ','
187 %left ABOVE_COMMA
188 %nonassoc ASSIGN
189 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
190 %left OROR
191 %left LOGICAL_AND '&'
192 %left '@'
193 %left '+' '-'
194 %left '*' '/' DIV MOD
195 %right UNARY
196 %right '^' DOT '[' '('
197 %right NOT '~'
198 %left COLONCOLON QID
199 /* This is not an actual token ; it is used for precedence. 
200 %right QID
201 */
202
203 \f
204 %%
205
206 start   :       exp
207         |       type_exp
208         ;
209
210 type_exp:       type
211                 { write_exp_elt_opcode (pstate, OP_TYPE);
212                   write_exp_elt_type (pstate, $1);
213                   write_exp_elt_opcode (pstate, OP_TYPE);
214                 }
215         ;
216
217 /* Expressions */
218
219 exp     :       exp '^'   %prec UNARY
220                         { write_exp_elt_opcode (pstate, UNOP_IND); }
221         ;
222
223 exp     :       '-'
224                         { number_sign = -1; }
225                 exp    %prec UNARY
226                         { number_sign = 1;
227                           write_exp_elt_opcode (pstate, UNOP_NEG); }
228         ;
229
230 exp     :       '+' exp    %prec UNARY
231                 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
232         ;
233
234 exp     :       not_exp exp %prec UNARY
235                         { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
236         ;
237
238 not_exp :       NOT
239         |       '~'
240         ;
241
242 exp     :       CAP '(' exp ')'
243                         { write_exp_elt_opcode (pstate, UNOP_CAP); }
244         ;
245
246 exp     :       ORD '(' exp ')'
247                         { write_exp_elt_opcode (pstate, UNOP_ORD); }
248         ;
249
250 exp     :       ABS '(' exp ')'
251                         { write_exp_elt_opcode (pstate, UNOP_ABS); }
252         ;
253
254 exp     :       HIGH '(' exp ')'
255                         { write_exp_elt_opcode (pstate, UNOP_HIGH); }
256         ;
257
258 exp     :       MIN_FUNC '(' type ')'
259                         { write_exp_elt_opcode (pstate, UNOP_MIN);
260                           write_exp_elt_type (pstate, $3);
261                           write_exp_elt_opcode (pstate, UNOP_MIN); }
262         ;
263
264 exp     :       MAX_FUNC '(' type ')'
265                         { write_exp_elt_opcode (pstate, UNOP_MAX);
266                           write_exp_elt_type (pstate, $3);
267                           write_exp_elt_opcode (pstate, UNOP_MAX); }
268         ;
269
270 exp     :       FLOAT_FUNC '(' exp ')'
271                         { write_exp_elt_opcode (pstate, UNOP_FLOAT); }
272         ;
273
274 exp     :       VAL '(' type ',' exp ')'
275                         { write_exp_elt_opcode (pstate, BINOP_VAL);
276                           write_exp_elt_type (pstate, $3);
277                           write_exp_elt_opcode (pstate, BINOP_VAL); }
278         ;
279
280 exp     :       CHR '(' exp ')'
281                         { write_exp_elt_opcode (pstate, UNOP_CHR); }
282         ;
283
284 exp     :       ODD '(' exp ')'
285                         { write_exp_elt_opcode (pstate, UNOP_ODD); }
286         ;
287
288 exp     :       TRUNC '(' exp ')'
289                         { write_exp_elt_opcode (pstate, UNOP_TRUNC); }
290         ;
291
292 exp     :       TSIZE '(' exp ')'
293                         { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
294         ;
295
296 exp     :       SIZE exp       %prec UNARY
297                         { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
298         ;
299
300
301 exp     :       INC '(' exp ')'
302                         { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
303         ;
304
305 exp     :       INC '(' exp ',' exp ')'
306                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
307                           write_exp_elt_opcode (pstate, BINOP_ADD);
308                           write_exp_elt_opcode (pstate,
309                                                 BINOP_ASSIGN_MODIFY); }
310         ;
311
312 exp     :       DEC '(' exp ')'
313                         { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT);}
314         ;
315
316 exp     :       DEC '(' exp ',' exp ')'
317                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
318                           write_exp_elt_opcode (pstate, BINOP_SUB);
319                           write_exp_elt_opcode (pstate,
320                                                 BINOP_ASSIGN_MODIFY); }
321         ;
322
323 exp     :       exp DOT NAME
324                         { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
325                           write_exp_string (pstate, $3);
326                           write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
327         ;
328
329 exp     :       set
330         ;
331
332 exp     :       exp IN set
333                         { error (_("Sets are not implemented."));}
334         ;
335
336 exp     :       INCL '(' exp ',' exp ')'
337                         { error (_("Sets are not implemented."));}
338         ;
339
340 exp     :       EXCL '(' exp ',' exp ')'
341                         { error (_("Sets are not implemented."));}
342         ;
343
344 set     :       '{' arglist '}'
345                         { error (_("Sets are not implemented."));}
346         |       type '{' arglist '}'
347                         { error (_("Sets are not implemented."));}
348         ;
349
350
351 /* Modula-2 array subscript notation [a,b,c...] */
352 exp     :       exp '['
353                         /* This function just saves the number of arguments
354                            that follow in the list.  It is *not* specific to
355                            function types */
356                         { start_arglist(); }
357                 non_empty_arglist ']'  %prec DOT
358                         { write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
359                           write_exp_elt_longcst (pstate,
360                                                  (LONGEST) end_arglist());
361                           write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT); }
362         ;
363
364 exp     :       exp '[' exp ']'
365                         { write_exp_elt_opcode (pstate, BINOP_SUBSCRIPT); }
366         ;
367
368 exp     :       exp '('
369                         /* This is to save the value of arglist_len
370                            being accumulated by an outer function call.  */
371                         { start_arglist (); }
372                 arglist ')'     %prec DOT
373                         { write_exp_elt_opcode (pstate, OP_FUNCALL);
374                           write_exp_elt_longcst (pstate,
375                                                  (LONGEST) end_arglist ());
376                           write_exp_elt_opcode (pstate, OP_FUNCALL); }
377         ;
378
379 arglist :
380         ;
381
382 arglist :       exp
383                         { arglist_len = 1; }
384         ;
385
386 arglist :       arglist ',' exp   %prec ABOVE_COMMA
387                         { arglist_len++; }
388         ;
389
390 non_empty_arglist
391         :       exp
392                         { arglist_len = 1; }
393         ;
394
395 non_empty_arglist
396         :       non_empty_arglist ',' exp %prec ABOVE_COMMA
397                         { arglist_len++; }
398         ;
399
400 /* GDB construct */
401 exp     :       '{' type '}' exp  %prec UNARY
402                         { write_exp_elt_opcode (pstate, UNOP_MEMVAL);
403                           write_exp_elt_type (pstate, $2);
404                           write_exp_elt_opcode (pstate, UNOP_MEMVAL); }
405         ;
406
407 exp     :       type '(' exp ')' %prec UNARY
408                         { write_exp_elt_opcode (pstate, UNOP_CAST);
409                           write_exp_elt_type (pstate, $1);
410                           write_exp_elt_opcode (pstate, UNOP_CAST); }
411         ;
412
413 exp     :       '(' exp ')'
414                         { }
415         ;
416
417 /* Binary operators in order of decreasing precedence.  Note that some
418    of these operators are overloaded!  (ie. sets) */
419
420 /* GDB construct */
421 exp     :       exp '@' exp
422                         { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
423         ;
424
425 exp     :       exp '*' exp
426                         { write_exp_elt_opcode (pstate, BINOP_MUL); }
427         ;
428
429 exp     :       exp '/' exp
430                         { write_exp_elt_opcode (pstate, BINOP_DIV); }
431         ;
432
433 exp     :       exp DIV exp
434                         { write_exp_elt_opcode (pstate, BINOP_INTDIV); }
435         ;
436
437 exp     :       exp MOD exp
438                         { write_exp_elt_opcode (pstate, BINOP_REM); }
439         ;
440
441 exp     :       exp '+' exp
442                         { write_exp_elt_opcode (pstate, BINOP_ADD); }
443         ;
444
445 exp     :       exp '-' exp
446                         { write_exp_elt_opcode (pstate, BINOP_SUB); }
447         ;
448
449 exp     :       exp '=' exp
450                         { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
451         ;
452
453 exp     :       exp NOTEQUAL exp
454                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
455         |       exp '#' exp
456                         { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
457         ;
458
459 exp     :       exp LEQ exp
460                         { write_exp_elt_opcode (pstate, BINOP_LEQ); }
461         ;
462
463 exp     :       exp GEQ exp
464                         { write_exp_elt_opcode (pstate, BINOP_GEQ); }
465         ;
466
467 exp     :       exp '<' exp
468                         { write_exp_elt_opcode (pstate, BINOP_LESS); }
469         ;
470
471 exp     :       exp '>' exp
472                         { write_exp_elt_opcode (pstate, BINOP_GTR); }
473         ;
474
475 exp     :       exp LOGICAL_AND exp
476                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
477         ;
478
479 exp     :       exp OROR exp
480                         { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
481         ;
482
483 exp     :       exp ASSIGN exp
484                         { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
485         ;
486
487
488 /* Constants */
489
490 exp     :       M2_TRUE
491                         { write_exp_elt_opcode (pstate, OP_BOOL);
492                           write_exp_elt_longcst (pstate, (LONGEST) $1);
493                           write_exp_elt_opcode (pstate, OP_BOOL); }
494         ;
495
496 exp     :       M2_FALSE
497                         { write_exp_elt_opcode (pstate, OP_BOOL);
498                           write_exp_elt_longcst (pstate, (LONGEST) $1);
499                           write_exp_elt_opcode (pstate, OP_BOOL); }
500         ;
501
502 exp     :       INT
503                         { write_exp_elt_opcode (pstate, OP_LONG);
504                           write_exp_elt_type (pstate,
505                                         parse_m2_type (pstate)->builtin_int);
506                           write_exp_elt_longcst (pstate, (LONGEST) $1);
507                           write_exp_elt_opcode (pstate, OP_LONG); }
508         ;
509
510 exp     :       UINT
511                         {
512                           write_exp_elt_opcode (pstate, OP_LONG);
513                           write_exp_elt_type (pstate,
514                                               parse_m2_type (pstate)
515                                               ->builtin_card);
516                           write_exp_elt_longcst (pstate, (LONGEST) $1);
517                           write_exp_elt_opcode (pstate, OP_LONG);
518                         }
519         ;
520
521 exp     :       CHAR
522                         { write_exp_elt_opcode (pstate, OP_LONG);
523                           write_exp_elt_type (pstate,
524                                               parse_m2_type (pstate)
525                                               ->builtin_char);
526                           write_exp_elt_longcst (pstate, (LONGEST) $1);
527                           write_exp_elt_opcode (pstate, OP_LONG); }
528         ;
529
530
531 exp     :       FLOAT
532                         { write_exp_elt_opcode (pstate, OP_DOUBLE);
533                           write_exp_elt_type (pstate,
534                                               parse_m2_type (pstate)
535                                               ->builtin_real);
536                           write_exp_elt_dblcst (pstate, $1);
537                           write_exp_elt_opcode (pstate, OP_DOUBLE); }
538         ;
539
540 exp     :       variable
541         ;
542
543 exp     :       SIZE '(' type ')'       %prec UNARY
544                         { write_exp_elt_opcode (pstate, OP_LONG);
545                           write_exp_elt_type (pstate,
546                                             parse_type (pstate)->builtin_int);
547                           write_exp_elt_longcst (pstate,
548                                                  (LONGEST) TYPE_LENGTH ($3));
549                           write_exp_elt_opcode (pstate, OP_LONG); }
550         ;
551
552 exp     :       STRING
553                         { write_exp_elt_opcode (pstate, OP_M2_STRING);
554                           write_exp_string (pstate, $1);
555                           write_exp_elt_opcode (pstate, OP_M2_STRING); }
556         ;
557
558 /* This will be used for extensions later.  Like adding modules.  */
559 block   :       fblock  
560                         { $$ = SYMBOL_BLOCK_VALUE($1); }
561         ;
562
563 fblock  :       BLOCKNAME
564                         { struct symbol *sym
565                             = lookup_symbol (copy_name ($1),
566                                              expression_context_block,
567                                              VAR_DOMAIN, 0).symbol;
568                           $$ = sym;}
569         ;
570                              
571
572 /* GDB scope operator */
573 fblock  :       block COLONCOLON BLOCKNAME
574                         { struct symbol *tem
575                             = lookup_symbol (copy_name ($3), $1,
576                                              VAR_DOMAIN, 0).symbol;
577                           if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
578                             error (_("No function \"%s\" in specified context."),
579                                    copy_name ($3));
580                           $$ = tem;
581                         }
582         ;
583
584 /* Useful for assigning to PROCEDURE variables */
585 variable:       fblock
586                         { write_exp_elt_opcode (pstate, OP_VAR_VALUE);
587                           write_exp_elt_block (pstate, NULL);
588                           write_exp_elt_sym (pstate, $1);
589                           write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
590         ;
591
592 /* GDB internal ($foo) variable */
593 variable:       INTERNAL_VAR
594         ;
595
596 /* GDB scope operator */
597 variable:       block COLONCOLON NAME
598                         { struct block_symbol sym
599                             = lookup_symbol (copy_name ($3), $1,
600                                              VAR_DOMAIN, 0);
601
602                           if (sym.symbol == 0)
603                             error (_("No symbol \"%s\" in specified context."),
604                                    copy_name ($3));
605                           if (symbol_read_needs_frame (sym.symbol))
606                             {
607                               if (innermost_block == 0
608                                   || contained_in (sym.block,
609                                                    innermost_block))
610                                 innermost_block = sym.block;
611                             }
612
613                           write_exp_elt_opcode (pstate, OP_VAR_VALUE);
614                           write_exp_elt_block (pstate, sym.block);
615                           write_exp_elt_sym (pstate, sym.symbol);
616                           write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
617         ;
618
619 /* Base case for variables.  */
620 variable:       NAME
621                         { struct block_symbol sym;
622                           struct field_of_this_result is_a_field_of_this;
623
624                           sym = lookup_symbol (copy_name ($1),
625                                                expression_context_block,
626                                                VAR_DOMAIN,
627                                                &is_a_field_of_this);
628
629                           if (sym.symbol)
630                             {
631                               if (symbol_read_needs_frame (sym.symbol))
632                                 {
633                                   if (innermost_block == 0 ||
634                                       contained_in (sym.block,
635                                                     innermost_block))
636                                     innermost_block = sym.block;
637                                 }
638
639                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
640                               /* We want to use the selected frame, not
641                                  another more inner frame which happens to
642                                  be in the same block.  */
643                               write_exp_elt_block (pstate, NULL);
644                               write_exp_elt_sym (pstate, sym.symbol);
645                               write_exp_elt_opcode (pstate, OP_VAR_VALUE);
646                             }
647                           else
648                             {
649                               struct bound_minimal_symbol msymbol;
650                               char *arg = copy_name ($1);
651
652                               msymbol =
653                                 lookup_bound_minimal_symbol (arg);
654                               if (msymbol.minsym != NULL)
655                                 write_exp_msymbol (pstate, msymbol);
656                               else if (!have_full_symbols () && !have_partial_symbols ())
657                                 error (_("No symbol table is loaded.  Use the \"symbol-file\" command."));
658                               else
659                                 error (_("No symbol \"%s\" in current context."),
660                                        copy_name ($1));
661                             }
662                         }
663         ;
664
665 type
666         :       TYPENAME
667                         { $$ = lookup_typename (parse_language (pstate),
668                                                 parse_gdbarch (pstate),
669                                                 copy_name ($1),
670                                                 expression_context_block, 0); }
671
672         ;
673
674 %%
675
676 /* Take care of parsing a number (anything that starts with a digit).
677    Set yylval and return the token type; update lexptr.
678    LEN is the number of characters in it.  */
679
680 /*** Needs some error checking for the float case ***/
681
682 static int
683 parse_number (int olen)
684 {
685   const char *p = lexptr;
686   LONGEST n = 0;
687   LONGEST prevn = 0;
688   int c,i,ischar=0;
689   int base = input_radix;
690   int len = olen;
691   int unsigned_p = number_sign == 1 ? 1 : 0;
692
693   if(p[len-1] == 'H')
694   {
695      base = 16;
696      len--;
697   }
698   else if(p[len-1] == 'C' || p[len-1] == 'B')
699   {
700      base = 8;
701      ischar = p[len-1] == 'C';
702      len--;
703   }
704
705   /* Scan the number */
706   for (c = 0; c < len; c++)
707   {
708     if (p[c] == '.' && base == 10)
709       {
710         /* It's a float since it contains a point.  */
711         yylval.dval = atof (p);
712         lexptr += len;
713         return FLOAT;
714       }
715     if (p[c] == '.' && base != 10)
716        error (_("Floating point numbers must be base 10."));
717     if (base == 10 && (p[c] < '0' || p[c] > '9'))
718        error (_("Invalid digit \'%c\' in number."),p[c]);
719  }
720
721   while (len-- > 0)
722     {
723       c = *p++;
724       n *= base;
725       if( base == 8 && (c == '8' || c == '9'))
726          error (_("Invalid digit \'%c\' in octal number."),c);
727       if (c >= '0' && c <= '9')
728         i = c - '0';
729       else
730         {
731           if (base == 16 && c >= 'A' && c <= 'F')
732             i = c - 'A' + 10;
733           else
734              return ERROR;
735         }
736       n+=i;
737       if(i >= base)
738          return ERROR;
739       if(!unsigned_p && number_sign == 1 && (prevn >= n))
740          unsigned_p=1;          /* Try something unsigned */
741       /* Don't do the range check if n==i and i==0, since that special
742          case will give an overflow error.  */
743       if(RANGE_CHECK && n!=i && i)
744       {
745          if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
746             ((!unsigned_p && number_sign==-1) && -prevn <= -n))
747             range_error (_("Overflow on numeric constant."));
748       }
749          prevn=n;
750     }
751
752   lexptr = p;
753   if(*p == 'B' || *p == 'C' || *p == 'H')
754      lexptr++;                  /* Advance past B,C or H */
755
756   if (ischar)
757   {
758      yylval.ulval = n;
759      return CHAR;
760   }
761   else if ( unsigned_p && number_sign == 1)
762   {
763      yylval.ulval = n;
764      return UINT;
765   }
766   else if((unsigned_p && (n<0))) {
767      range_error (_("Overflow on numeric constant -- number too large."));
768      /* But, this can return if range_check == range_warn.  */
769   }
770   yylval.lval = n;
771   return INT;
772 }
773
774
775 /* Some tokens */
776
777 static struct
778 {
779    char name[2];
780    int token;
781 } tokentab2[] =
782 {
783     { {'<', '>'},    NOTEQUAL   },
784     { {':', '='},    ASSIGN     },
785     { {'<', '='},    LEQ        },
786     { {'>', '='},    GEQ        },
787     { {':', ':'},    COLONCOLON },
788
789 };
790
791 /* Some specific keywords */
792
793 struct keyword {
794    char keyw[10];
795    int token;
796 };
797
798 static struct keyword keytab[] =
799 {
800     {"OR" ,   OROR       },
801     {"IN",    IN         },/* Note space after IN */
802     {"AND",   LOGICAL_AND},
803     {"ABS",   ABS        },
804     {"CHR",   CHR        },
805     {"DEC",   DEC        },
806     {"NOT",   NOT        },
807     {"DIV",   DIV        },
808     {"INC",   INC        },
809     {"MAX",   MAX_FUNC   },
810     {"MIN",   MIN_FUNC   },
811     {"MOD",   MOD        },
812     {"ODD",   ODD        },
813     {"CAP",   CAP        },
814     {"ORD",   ORD        },
815     {"VAL",   VAL        },
816     {"EXCL",  EXCL       },
817     {"HIGH",  HIGH       },
818     {"INCL",  INCL       },
819     {"SIZE",  SIZE       },
820     {"FLOAT", FLOAT_FUNC },
821     {"TRUNC", TRUNC      },
822     {"TSIZE", SIZE       },
823 };
824
825
826 /* Read one token, getting characters through lexptr.  */
827
828 /* This is where we will check to make sure that the language and the
829    operators used are compatible  */
830
831 static int
832 yylex (void)
833 {
834   int c;
835   int namelen;
836   int i;
837   const char *tokstart;
838   char quote;
839
840  retry:
841
842   prev_lexptr = lexptr;
843
844   tokstart = lexptr;
845
846
847   /* See if it is a special token of length 2 */
848   for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
849      if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
850      {
851         lexptr += 2;
852         return tokentab2[i].token;
853      }
854
855   switch (c = *tokstart)
856     {
857     case 0:
858       return 0;
859
860     case ' ':
861     case '\t':
862     case '\n':
863       lexptr++;
864       goto retry;
865
866     case '(':
867       paren_depth++;
868       lexptr++;
869       return c;
870
871     case ')':
872       if (paren_depth == 0)
873         return 0;
874       paren_depth--;
875       lexptr++;
876       return c;
877
878     case ',':
879       if (comma_terminates && paren_depth == 0)
880         return 0;
881       lexptr++;
882       return c;
883
884     case '.':
885       /* Might be a floating point number.  */
886       if (lexptr[1] >= '0' && lexptr[1] <= '9')
887         break;                  /* Falls into number code.  */
888       else
889       {
890          lexptr++;
891          return DOT;
892       }
893
894 /* These are character tokens that appear as-is in the YACC grammar */
895     case '+':
896     case '-':
897     case '*':
898     case '/':
899     case '^':
900     case '<':
901     case '>':
902     case '[':
903     case ']':
904     case '=':
905     case '{':
906     case '}':
907     case '#':
908     case '@':
909     case '~':
910     case '&':
911       lexptr++;
912       return c;
913
914     case '\'' :
915     case '"':
916       quote = c;
917       for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
918         if (c == '\\')
919           {
920             c = tokstart[++namelen];
921             if (c >= '0' && c <= '9')
922               {
923                 c = tokstart[++namelen];
924                 if (c >= '0' && c <= '9')
925                   c = tokstart[++namelen];
926               }
927           }
928       if(c != quote)
929          error (_("Unterminated string or character constant."));
930       yylval.sval.ptr = tokstart + 1;
931       yylval.sval.length = namelen - 1;
932       lexptr += namelen + 1;
933
934       if(namelen == 2)          /* Single character */
935       {
936            yylval.ulval = tokstart[1];
937            return CHAR;
938       }
939       else
940          return STRING;
941     }
942
943   /* Is it a number?  */
944   /* Note:  We have already dealt with the case of the token '.'.
945      See case '.' above.  */
946   if ((c >= '0' && c <= '9'))
947     {
948       /* It's a number.  */
949       int got_dot = 0, got_e = 0;
950       const char *p = tokstart;
951       int toktype;
952
953       for (++p ;; ++p)
954         {
955           if (!got_e && (*p == 'e' || *p == 'E'))
956             got_dot = got_e = 1;
957           else if (!got_dot && *p == '.')
958             got_dot = 1;
959           else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
960                    && (*p == '-' || *p == '+'))
961             /* This is the sign of the exponent, not the end of the
962                number.  */
963             continue;
964           else if ((*p < '0' || *p > '9') &&
965                    (*p < 'A' || *p > 'F') &&
966                    (*p != 'H'))  /* Modula-2 hexadecimal number */
967             break;
968         }
969         toktype = parse_number (p - tokstart);
970         if (toktype == ERROR)
971           {
972             char *err_copy = (char *) alloca (p - tokstart + 1);
973
974             memcpy (err_copy, tokstart, p - tokstart);
975             err_copy[p - tokstart] = 0;
976             error (_("Invalid number \"%s\"."), err_copy);
977           }
978         lexptr = p;
979         return toktype;
980     }
981
982   if (!(c == '_' || c == '$'
983         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
984     /* We must have come across a bad character (e.g. ';').  */
985     error (_("Invalid character '%c' in expression."), c);
986
987   /* It's a name.  See how long it is.  */
988   namelen = 0;
989   for (c = tokstart[namelen];
990        (c == '_' || c == '$' || (c >= '0' && c <= '9')
991         || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
992        c = tokstart[++namelen])
993     ;
994
995   /* The token "if" terminates the expression and is NOT
996      removed from the input stream.  */
997   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
998     {
999       return 0;
1000     }
1001
1002   lexptr += namelen;
1003
1004   /*  Lookup special keywords */
1005   for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
1006      if (namelen == strlen (keytab[i].keyw)
1007          && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
1008            return keytab[i].token;
1009
1010   yylval.sval.ptr = tokstart;
1011   yylval.sval.length = namelen;
1012
1013   if (*tokstart == '$')
1014     {
1015       write_dollar_variable (pstate, yylval.sval);
1016       return INTERNAL_VAR;
1017     }
1018
1019   /* Use token-type BLOCKNAME for symbols that happen to be defined as
1020      functions.  If this is not so, then ...
1021      Use token-type TYPENAME for symbols that happen to be defined
1022      currently as names of types; NAME for other symbols.
1023      The caller is not constrained to care about the distinction.  */
1024  {
1025
1026
1027     char *tmp = copy_name (yylval.sval);
1028     struct symbol *sym;
1029
1030     if (lookup_symtab (tmp))
1031       return BLOCKNAME;
1032     sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 0).symbol;
1033     if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1034       return BLOCKNAME;
1035     if (lookup_typename (parse_language (pstate), parse_gdbarch (pstate),
1036                          copy_name (yylval.sval),
1037                          expression_context_block, 1))
1038       return TYPENAME;
1039
1040     if(sym)
1041     {
1042       switch(SYMBOL_CLASS (sym))
1043        {
1044        case LOC_STATIC:
1045        case LOC_REGISTER:
1046        case LOC_ARG:
1047        case LOC_REF_ARG:
1048        case LOC_REGPARM_ADDR:
1049        case LOC_LOCAL:
1050        case LOC_CONST:
1051        case LOC_CONST_BYTES:
1052        case LOC_OPTIMIZED_OUT:
1053        case LOC_COMPUTED:
1054           return NAME;
1055
1056        case LOC_TYPEDEF:
1057           return TYPENAME;
1058
1059        case LOC_BLOCK:
1060           return BLOCKNAME;
1061
1062        case LOC_UNDEF:
1063           error (_("internal:  Undefined class in m2lex()"));
1064
1065        case LOC_LABEL:
1066        case LOC_UNRESOLVED:
1067           error (_("internal:  Unforseen case in m2lex()"));
1068
1069        default:
1070           error (_("unhandled token in m2lex()"));
1071           break;
1072        }
1073     }
1074     else
1075     {
1076        /* Built-in BOOLEAN type.  This is sort of a hack.  */
1077        if (strncmp (tokstart, "TRUE", 4) == 0)
1078        {
1079           yylval.ulval = 1;
1080           return M2_TRUE;
1081        }
1082        else if (strncmp (tokstart, "FALSE", 5) == 0)
1083        {
1084           yylval.ulval = 0;
1085           return M2_FALSE;
1086        }
1087     }
1088
1089     /* Must be another type of name...  */
1090     return NAME;
1091  }
1092 }
1093
1094 int
1095 m2_parse (struct parser_state *par_state)
1096 {
1097   int result;
1098   struct cleanup *c = make_cleanup_clear_parser_state (&pstate);
1099
1100   /* Setting up the parser state.  */
1101   gdb_assert (par_state != NULL);
1102   pstate = par_state;
1103
1104   result = yyparse ();
1105   do_cleanups (c);
1106
1107   return result;
1108 }
1109
1110 void
1111 yyerror (char *msg)
1112 {
1113   if (prev_lexptr)
1114     lexptr = prev_lexptr;
1115
1116   error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);
1117 }