* c-exp.y, m2-exp.y: Migrate code that has nothing to do with
[deliverable/binutils-gdb.git] / gdb / m2-exp.y
1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991 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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 /* Parse a Modula-2 expression from text in a string,
23 and return the result as a struct expression pointer.
24 That structure contains arithmetic operations in reverse polish,
25 with constants represented by operations that are followed by special data.
26 See expression.h for the details of the format.
27 What is important here is that it can be built up sequentially
28 during the process of parsing; the lower levels of the tree always
29 come first in the result.
30
31 Note that malloc's and realloc's in this file are transformed to
32 xmalloc and xrealloc respectively by the same sed command in the
33 makefile that remaps any other malloc/realloc inserted by the parser
34 generator. Doing this with #defines and trying to control the interaction
35 with include files (<malloc.h> and <stdlib.h> for example) just became
36 too messy, particularly when such includes can be inserted at random
37 times by the parser generator. */
38
39 %{
40
41 #include "defs.h"
42 #include "expression.h"
43 #include "language.h"
44 #include "value.h"
45 #include "parser-defs.h"
46 #include "m2-lang.h"
47
48 /* These MUST be included in any grammar file!!!! Please choose unique names!
49 Note that this are a combined list of variables that can be produced
50 by any one of bison, byacc, or yacc. */
51 #define yymaxdepth m2_maxdepth
52 #define yyparse m2_parse
53 #define yylex m2_lex
54 #define yyerror m2_error
55 #define yylval m2_lval
56 #define yychar m2_char
57 #define yydebug m2_debug
58 #define yypact m2_pact
59 #define yyr1 m2_r1
60 #define yyr2 m2_r2
61 #define yydef m2_def
62 #define yychk m2_chk
63 #define yypgo m2_pgo
64 #define yyact m2_act
65 #define yyexca m2_exca
66 #define yyerrflag m2_errflag
67 #define yynerrs m2_nerrs
68 #define yyps m2_ps
69 #define yypv m2_pv
70 #define yys m2_s
71 #define yy_yys m2_yys
72 #define yystate m2_state
73 #define yytmp m2_tmp
74 #define yyv m2_v
75 #define yy_yyv m2_yyv
76 #define yyval m2_val
77 #define yylloc m2_lloc
78 #define yyss m2_yyss /* byacc */
79 #define yyssp m2_yysp /* byacc */
80 #define yyvs m2_yyvs /* byacc */
81 #define yyvsp m2_yyvsp /* byacc */
82
83 #if 0
84 static char *
85 make_qualname PARAMS ((char *, char *));
86 #endif
87
88 static int
89 parse_number PARAMS ((int));
90
91 static int
92 yylex PARAMS ((void));
93
94 void
95 yyerror PARAMS ((char *));
96
97 int
98 yyparse PARAMS ((void));
99
100 /* The sign of the number being parsed. */
101 static int number_sign = 1;
102
103 /* The block that the module specified by the qualifer on an identifer is
104 contained in, */
105 #if 0
106 static struct block *modblock=0;
107 #endif
108
109 /* #define YYDEBUG 1 */
110 %}
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
116 %union
117 {
118 LONGEST lval;
119 unsigned LONGEST ulval;
120 double dval;
121 struct symbol *sym;
122 struct type *tval;
123 struct stoken sval;
124 int voidval;
125 struct block *bval;
126 enum exp_opcode opcode;
127 struct internalvar *ivar;
128
129 struct type **tvec;
130 int *ivec;
131 }
132
133 %type <voidval> exp type_exp start set
134 %type <voidval> variable
135 %type <tval> type
136 %type <bval> block
137 %type <sym> fblock
138
139 %token <lval> INT HEX ERROR
140 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
141 %token <dval> FLOAT
142
143 /* Both NAME and TYPENAME tokens represent symbols in the input,
144 and both convey their data as strings.
145 But a TYPENAME is a string that happens to be defined as a typedef
146 or builtin type name (such as int or char)
147 and a NAME is any other symbol.
148
149 Contexts where this distinction is not important can use the
150 nonterminal "name", which matches either NAME or TYPENAME. */
151
152 %token <sval> STRING
153 %token <sval> NAME BLOCKNAME IDENT VARNAME
154 %token <sval> TYPENAME
155
156 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
157 %token INC DEC INCL EXCL
158
159 /* The GDB scope operator */
160 %token COLONCOLON
161
162 %token <lval> LAST REGNAME
163
164 %token <ivar> INTERNAL_VAR
165
166 /* M2 tokens */
167 %left ','
168 %left ABOVE_COMMA
169 %nonassoc ASSIGN
170 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
171 %left OROR
172 %left LOGICAL_AND '&'
173 %left '@'
174 %left '+' '-'
175 %left '*' '/' DIV MOD
176 %right UNARY
177 %right '^' DOT '[' '('
178 %right NOT '~'
179 %left COLONCOLON QID
180 /* This is not an actual token ; it is used for precedence.
181 %right QID
182 */
183
184 \f
185 %%
186
187 start : exp
188 | type_exp
189 ;
190
191 type_exp: type
192 { write_exp_elt_opcode(OP_TYPE);
193 write_exp_elt_type($1);
194 write_exp_elt_opcode(OP_TYPE);
195 }
196 ;
197
198 /* Expressions */
199
200 exp : exp '^' %prec UNARY
201 { write_exp_elt_opcode (UNOP_IND); }
202
203 exp : '-'
204 { number_sign = -1; }
205 exp %prec UNARY
206 { number_sign = 1;
207 write_exp_elt_opcode (UNOP_NEG); }
208 ;
209
210 exp : '+' exp %prec UNARY
211 { write_exp_elt_opcode(UNOP_PLUS); }
212 ;
213
214 exp : not_exp exp %prec UNARY
215 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
216 ;
217
218 not_exp : NOT
219 | '~'
220 ;
221
222 exp : CAP '(' exp ')'
223 { write_exp_elt_opcode (UNOP_CAP); }
224 ;
225
226 exp : ORD '(' exp ')'
227 { write_exp_elt_opcode (UNOP_ORD); }
228 ;
229
230 exp : ABS '(' exp ')'
231 { write_exp_elt_opcode (UNOP_ABS); }
232 ;
233
234 exp : HIGH '(' exp ')'
235 { write_exp_elt_opcode (UNOP_HIGH); }
236 ;
237
238 exp : MIN_FUNC '(' type ')'
239 { write_exp_elt_opcode (UNOP_MIN);
240 write_exp_elt_type ($3);
241 write_exp_elt_opcode (UNOP_MIN); }
242 ;
243
244 exp : MAX_FUNC '(' type ')'
245 { write_exp_elt_opcode (UNOP_MAX);
246 write_exp_elt_type ($3);
247 write_exp_elt_opcode (UNOP_MIN); }
248 ;
249
250 exp : FLOAT_FUNC '(' exp ')'
251 { write_exp_elt_opcode (UNOP_FLOAT); }
252 ;
253
254 exp : VAL '(' type ',' exp ')'
255 { write_exp_elt_opcode (BINOP_VAL);
256 write_exp_elt_type ($3);
257 write_exp_elt_opcode (BINOP_VAL); }
258 ;
259
260 exp : CHR '(' exp ')'
261 { write_exp_elt_opcode (UNOP_CHR); }
262 ;
263
264 exp : ODD '(' exp ')'
265 { write_exp_elt_opcode (UNOP_ODD); }
266 ;
267
268 exp : TRUNC '(' exp ')'
269 { write_exp_elt_opcode (UNOP_TRUNC); }
270 ;
271
272 exp : SIZE exp %prec UNARY
273 { write_exp_elt_opcode (UNOP_SIZEOF); }
274 ;
275
276
277 exp : INC '(' exp ')'
278 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
279 ;
280
281 exp : INC '(' exp ',' exp ')'
282 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
283 write_exp_elt_opcode(BINOP_ADD);
284 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
285 ;
286
287 exp : DEC '(' exp ')'
288 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
289 ;
290
291 exp : DEC '(' exp ',' exp ')'
292 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
293 write_exp_elt_opcode(BINOP_SUB);
294 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
295 ;
296
297 exp : exp DOT NAME
298 { write_exp_elt_opcode (STRUCTOP_STRUCT);
299 write_exp_string ($3);
300 write_exp_elt_opcode (STRUCTOP_STRUCT); }
301 ;
302
303 exp : set
304 ;
305
306 exp : exp IN set
307 { error("Sets are not implemented.");}
308 ;
309
310 exp : INCL '(' exp ',' exp ')'
311 { error("Sets are not implemented.");}
312 ;
313
314 exp : EXCL '(' exp ',' exp ')'
315 { error("Sets are not implemented.");}
316
317 set : '{' arglist '}'
318 { error("Sets are not implemented.");}
319 | type '{' arglist '}'
320 { error("Sets are not implemented.");}
321 ;
322
323
324 /* Modula-2 array subscript notation [a,b,c...] */
325 exp : exp '['
326 /* This function just saves the number of arguments
327 that follow in the list. It is *not* specific to
328 function types */
329 { start_arglist(); }
330 non_empty_arglist ']' %prec DOT
331 { write_exp_elt_opcode (BINOP_MULTI_SUBSCRIPT);
332 write_exp_elt_longcst ((LONGEST) end_arglist());
333 write_exp_elt_opcode (BINOP_MULTI_SUBSCRIPT); }
334 ;
335
336 exp : exp '('
337 /* This is to save the value of arglist_len
338 being accumulated by an outer function call. */
339 { start_arglist (); }
340 arglist ')' %prec DOT
341 { write_exp_elt_opcode (OP_FUNCALL);
342 write_exp_elt_longcst ((LONGEST) end_arglist ());
343 write_exp_elt_opcode (OP_FUNCALL); }
344 ;
345
346 arglist :
347 ;
348
349 arglist : exp
350 { arglist_len = 1; }
351 ;
352
353 arglist : arglist ',' exp %prec ABOVE_COMMA
354 { arglist_len++; }
355 ;
356
357 non_empty_arglist
358 : exp
359 { arglist_len = 1; }
360 ;
361
362 non_empty_arglist
363 : non_empty_arglist ',' exp %prec ABOVE_COMMA
364 { arglist_len++; }
365 ;
366
367 /* GDB construct */
368 exp : '{' type '}' exp %prec UNARY
369 { write_exp_elt_opcode (UNOP_MEMVAL);
370 write_exp_elt_type ($2);
371 write_exp_elt_opcode (UNOP_MEMVAL); }
372 ;
373
374 exp : type '(' exp ')' %prec UNARY
375 { write_exp_elt_opcode (UNOP_CAST);
376 write_exp_elt_type ($1);
377 write_exp_elt_opcode (UNOP_CAST); }
378 ;
379
380 exp : '(' exp ')'
381 { }
382 ;
383
384 /* Binary operators in order of decreasing precedence. Note that some
385 of these operators are overloaded! (ie. sets) */
386
387 /* GDB construct */
388 exp : exp '@' exp
389 { write_exp_elt_opcode (BINOP_REPEAT); }
390 ;
391
392 exp : exp '*' exp
393 { write_exp_elt_opcode (BINOP_MUL); }
394 ;
395
396 exp : exp '/' exp
397 { write_exp_elt_opcode (BINOP_DIV); }
398 ;
399
400 exp : exp DIV exp
401 { write_exp_elt_opcode (BINOP_INTDIV); }
402 ;
403
404 exp : exp MOD exp
405 { write_exp_elt_opcode (BINOP_REM); }
406 ;
407
408 exp : exp '+' exp
409 { write_exp_elt_opcode (BINOP_ADD); }
410 ;
411
412 exp : exp '-' exp
413 { write_exp_elt_opcode (BINOP_SUB); }
414 ;
415
416 exp : exp '=' exp
417 { write_exp_elt_opcode (BINOP_EQUAL); }
418 ;
419
420 exp : exp NOTEQUAL exp
421 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
422 | exp '#' exp
423 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
424 ;
425
426 exp : exp LEQ exp
427 { write_exp_elt_opcode (BINOP_LEQ); }
428 ;
429
430 exp : exp GEQ exp
431 { write_exp_elt_opcode (BINOP_GEQ); }
432 ;
433
434 exp : exp '<' exp
435 { write_exp_elt_opcode (BINOP_LESS); }
436 ;
437
438 exp : exp '>' exp
439 { write_exp_elt_opcode (BINOP_GTR); }
440 ;
441
442 exp : exp LOGICAL_AND exp
443 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
444 ;
445
446 exp : exp OROR exp
447 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
448 ;
449
450 exp : exp ASSIGN exp
451 { write_exp_elt_opcode (BINOP_ASSIGN); }
452 ;
453
454
455 /* Constants */
456
457 exp : M2_TRUE
458 { write_exp_elt_opcode (OP_BOOL);
459 write_exp_elt_longcst ((LONGEST) $1);
460 write_exp_elt_opcode (OP_BOOL); }
461 ;
462
463 exp : M2_FALSE
464 { write_exp_elt_opcode (OP_BOOL);
465 write_exp_elt_longcst ((LONGEST) $1);
466 write_exp_elt_opcode (OP_BOOL); }
467 ;
468
469 exp : INT
470 { write_exp_elt_opcode (OP_LONG);
471 write_exp_elt_type (builtin_type_m2_int);
472 write_exp_elt_longcst ((LONGEST) $1);
473 write_exp_elt_opcode (OP_LONG); }
474 ;
475
476 exp : UINT
477 {
478 write_exp_elt_opcode (OP_LONG);
479 write_exp_elt_type (builtin_type_m2_card);
480 write_exp_elt_longcst ((LONGEST) $1);
481 write_exp_elt_opcode (OP_LONG);
482 }
483 ;
484
485 exp : CHAR
486 { write_exp_elt_opcode (OP_LONG);
487 write_exp_elt_type (builtin_type_m2_char);
488 write_exp_elt_longcst ((LONGEST) $1);
489 write_exp_elt_opcode (OP_LONG); }
490 ;
491
492
493 exp : FLOAT
494 { write_exp_elt_opcode (OP_DOUBLE);
495 write_exp_elt_type (builtin_type_m2_real);
496 write_exp_elt_dblcst ($1);
497 write_exp_elt_opcode (OP_DOUBLE); }
498 ;
499
500 exp : variable
501 ;
502
503 /* The GDB internal variable $$, et al. */
504 exp : LAST
505 { write_exp_elt_opcode (OP_LAST);
506 write_exp_elt_longcst ((LONGEST) $1);
507 write_exp_elt_opcode (OP_LAST); }
508 ;
509
510 exp : REGNAME
511 { write_exp_elt_opcode (OP_REGISTER);
512 write_exp_elt_longcst ((LONGEST) $1);
513 write_exp_elt_opcode (OP_REGISTER); }
514 ;
515
516 exp : SIZE '(' type ')' %prec UNARY
517 { write_exp_elt_opcode (OP_LONG);
518 write_exp_elt_type (builtin_type_int);
519 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
520 write_exp_elt_opcode (OP_LONG); }
521 ;
522
523 exp : STRING
524 { write_exp_elt_opcode (OP_M2_STRING);
525 write_exp_string ($1);
526 write_exp_elt_opcode (OP_M2_STRING); }
527 ;
528
529 /* This will be used for extensions later. Like adding modules. */
530 block : fblock
531 { $$ = SYMBOL_BLOCK_VALUE($1); }
532 ;
533
534 fblock : BLOCKNAME
535 { struct symbol *sym
536 = lookup_symbol (copy_name ($1), expression_context_block,
537 VAR_NAMESPACE, 0, NULL);
538 $$ = sym;}
539 ;
540
541
542 /* GDB scope operator */
543 fblock : block COLONCOLON BLOCKNAME
544 { struct symbol *tem
545 = lookup_symbol (copy_name ($3), $1,
546 VAR_NAMESPACE, 0, NULL);
547 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
548 error ("No function \"%s\" in specified context.",
549 copy_name ($3));
550 $$ = tem;
551 }
552 ;
553
554 /* Useful for assigning to PROCEDURE variables */
555 variable: fblock
556 { write_exp_elt_opcode(OP_VAR_VALUE);
557 write_exp_elt_sym ($1);
558 write_exp_elt_opcode (OP_VAR_VALUE); }
559 ;
560
561 /* GDB internal ($foo) variable */
562 variable: INTERNAL_VAR
563 { write_exp_elt_opcode (OP_INTERNALVAR);
564 write_exp_elt_intern ($1);
565 write_exp_elt_opcode (OP_INTERNALVAR); }
566 ;
567
568 /* GDB scope operator */
569 variable: block COLONCOLON NAME
570 { struct symbol *sym;
571 sym = lookup_symbol (copy_name ($3), $1,
572 VAR_NAMESPACE, 0, NULL);
573 if (sym == 0)
574 error ("No symbol \"%s\" in specified context.",
575 copy_name ($3));
576
577 write_exp_elt_opcode (OP_VAR_VALUE);
578 write_exp_elt_sym (sym);
579 write_exp_elt_opcode (OP_VAR_VALUE); }
580 ;
581
582 /* Base case for variables. */
583 variable: NAME
584 { struct symbol *sym;
585 int is_a_field_of_this;
586
587 sym = lookup_symbol (copy_name ($1),
588 expression_context_block,
589 VAR_NAMESPACE,
590 &is_a_field_of_this,
591 NULL);
592 if (sym)
593 {
594 switch (sym->class)
595 {
596 case LOC_REGISTER:
597 case LOC_ARG:
598 case LOC_LOCAL:
599 case LOC_REF_ARG:
600 case LOC_REGPARM:
601 case LOC_LOCAL_ARG:
602 if (innermost_block == 0 ||
603 contained_in (block_found,
604 innermost_block))
605 innermost_block = block_found;
606 break;
607
608 case LOC_UNDEF:
609 case LOC_CONST:
610 case LOC_STATIC:
611 case LOC_TYPEDEF:
612 case LOC_LABEL: /* maybe should go above? */
613 case LOC_BLOCK:
614 case LOC_CONST_BYTES:
615 /* These are listed so gcc -Wall will reveal
616 un-handled cases. */
617 break;
618 }
619 write_exp_elt_opcode (OP_VAR_VALUE);
620 write_exp_elt_sym (sym);
621 write_exp_elt_opcode (OP_VAR_VALUE);
622 }
623 else
624 {
625 struct minimal_symbol *msymbol;
626 register char *arg = copy_name ($1);
627
628 msymbol = lookup_minimal_symbol (arg,
629 (struct objfile *) NULL);
630 if (msymbol != NULL)
631 {
632 write_exp_elt_opcode (OP_LONG);
633 write_exp_elt_type (builtin_type_int);
634 write_exp_elt_longcst ((LONGEST) msymbol -> address);
635 write_exp_elt_opcode (OP_LONG);
636 write_exp_elt_opcode (UNOP_MEMVAL);
637 if (msymbol -> type == mst_data ||
638 msymbol -> type == mst_bss)
639 write_exp_elt_type (builtin_type_int);
640 else if (msymbol -> type == mst_text)
641 write_exp_elt_type (lookup_function_type (builtin_type_int));
642 else
643 write_exp_elt_type (builtin_type_char);
644 write_exp_elt_opcode (UNOP_MEMVAL);
645 }
646 else if (!have_full_symbols () && !have_partial_symbols ())
647 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
648 else
649 error ("No symbol \"%s\" in current context.",
650 copy_name ($1));
651 }
652 }
653 ;
654
655 type
656 : TYPENAME
657 { $$ = lookup_typename (copy_name ($1),
658 expression_context_block, 0); }
659
660 ;
661
662 %%
663
664 #if 0 /* FIXME! */
665 int
666 overflow(a,b)
667 long a,b;
668 {
669 return (MAX_OF_TYPE(builtin_type_m2_int) - b) < a;
670 }
671
672 int
673 uoverflow(a,b)
674 unsigned long a,b;
675 {
676 return (MAX_OF_TYPE(builtin_type_m2_card) - b) < a;
677 }
678 #endif /* FIXME */
679
680 /* Take care of parsing a number (anything that starts with a digit).
681 Set yylval and return the token type; update lexptr.
682 LEN is the number of characters in it. */
683
684 /*** Needs some error checking for the float case ***/
685
686 static int
687 parse_number (olen)
688 int olen;
689 {
690 register char *p = lexptr;
691 register LONGEST n = 0;
692 register LONGEST prevn = 0;
693 register int c,i,ischar=0;
694 register int base = input_radix;
695 register int len = olen;
696 int unsigned_p = number_sign == 1 ? 1 : 0;
697
698 if(p[len-1] == 'H')
699 {
700 base = 16;
701 len--;
702 }
703 else if(p[len-1] == 'C' || p[len-1] == 'B')
704 {
705 base = 8;
706 ischar = p[len-1] == 'C';
707 len--;
708 }
709
710 /* Scan the number */
711 for (c = 0; c < len; c++)
712 {
713 if (p[c] == '.' && base == 10)
714 {
715 /* It's a float since it contains a point. */
716 yylval.dval = atof (p);
717 lexptr += len;
718 return FLOAT;
719 }
720 if (p[c] == '.' && base != 10)
721 error("Floating point numbers must be base 10.");
722 if (base == 10 && (p[c] < '0' || p[c] > '9'))
723 error("Invalid digit \'%c\' in number.",p[c]);
724 }
725
726 while (len-- > 0)
727 {
728 c = *p++;
729 n *= base;
730 if( base == 8 && (c == '8' || c == '9'))
731 error("Invalid digit \'%c\' in octal number.",c);
732 if (c >= '0' && c <= '9')
733 i = c - '0';
734 else
735 {
736 if (base == 16 && c >= 'A' && c <= 'F')
737 i = c - 'A' + 10;
738 else
739 return ERROR;
740 }
741 n+=i;
742 if(i >= base)
743 return ERROR;
744 if(!unsigned_p && number_sign == 1 && (prevn >= n))
745 unsigned_p=1; /* Try something unsigned */
746 /* Don't do the range check if n==i and i==0, since that special
747 case will give an overflow error. */
748 if(RANGE_CHECK && n!=i && i)
749 {
750 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
751 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
752 range_error("Overflow on numeric constant.");
753 }
754 prevn=n;
755 }
756
757 lexptr = p;
758 if(*p == 'B' || *p == 'C' || *p == 'H')
759 lexptr++; /* Advance past B,C or H */
760
761 if (ischar)
762 {
763 yylval.ulval = n;
764 return CHAR;
765 }
766 else if ( unsigned_p && number_sign == 1)
767 {
768 yylval.ulval = n;
769 return UINT;
770 }
771 else if((unsigned_p && (n<0))) {
772 range_error("Overflow on numeric constant -- number too large.");
773 /* But, this can return if range_check == range_warn. */
774 }
775 yylval.lval = n;
776 return INT;
777 }
778
779
780 /* Some tokens */
781
782 static struct
783 {
784 char name[2];
785 int token;
786 } tokentab2[] =
787 {
788 { {'<', '>'}, NOTEQUAL },
789 { {':', '='}, ASSIGN },
790 { {'<', '='}, LEQ },
791 { {'>', '='}, GEQ },
792 { {':', ':'}, COLONCOLON },
793
794 };
795
796 /* Some specific keywords */
797
798 struct keyword {
799 char keyw[10];
800 int token;
801 };
802
803 static struct keyword keytab[] =
804 {
805 {"OR" , OROR },
806 {"IN", IN },/* Note space after IN */
807 {"AND", LOGICAL_AND},
808 {"ABS", ABS },
809 {"CHR", CHR },
810 {"DEC", DEC },
811 {"NOT", NOT },
812 {"DIV", DIV },
813 {"INC", INC },
814 {"MAX", MAX_FUNC },
815 {"MIN", MIN_FUNC },
816 {"MOD", MOD },
817 {"ODD", ODD },
818 {"CAP", CAP },
819 {"ORD", ORD },
820 {"VAL", VAL },
821 {"EXCL", EXCL },
822 {"HIGH", HIGH },
823 {"INCL", INCL },
824 {"SIZE", SIZE },
825 {"FLOAT", FLOAT_FUNC },
826 {"TRUNC", TRUNC },
827 };
828
829
830 /* Read one token, getting characters through lexptr. */
831
832 /* This is where we will check to make sure that the language and the operators used are
833 compatible */
834
835 static int
836 yylex ()
837 {
838 register int c;
839 register int namelen;
840 register int i;
841 register char *tokstart;
842 register char quote;
843
844 retry:
845
846 tokstart = lexptr;
847
848
849 /* See if it is a special token of length 2 */
850 for( i = 0 ; i < sizeof tokentab2 / sizeof tokentab2[0] ; i++)
851 if(!strncmp(tokentab2[i].name, tokstart, 2))
852 {
853 lexptr += 2;
854 return tokentab2[i].token;
855 }
856
857 switch (c = *tokstart)
858 {
859 case 0:
860 return 0;
861
862 case ' ':
863 case '\t':
864 case '\n':
865 lexptr++;
866 goto retry;
867
868 case '(':
869 paren_depth++;
870 lexptr++;
871 return c;
872
873 case ')':
874 if (paren_depth == 0)
875 return 0;
876 paren_depth--;
877 lexptr++;
878 return c;
879
880 case ',':
881 if (comma_terminates && paren_depth == 0)
882 return 0;
883 lexptr++;
884 return c;
885
886 case '.':
887 /* Might be a floating point number. */
888 if (lexptr[1] >= '0' && lexptr[1] <= '9')
889 break; /* Falls into number code. */
890 else
891 {
892 lexptr++;
893 return DOT;
894 }
895
896 /* These are character tokens that appear as-is in the YACC grammar */
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 case '~':
912 case '&':
913 lexptr++;
914 return c;
915
916 case '\'' :
917 case '"':
918 quote = c;
919 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
920 if (c == '\\')
921 {
922 c = tokstart[++namelen];
923 if (c >= '0' && c <= '9')
924 {
925 c = tokstart[++namelen];
926 if (c >= '0' && c <= '9')
927 c = tokstart[++namelen];
928 }
929 }
930 if(c != quote)
931 error("Unterminated string or character constant.");
932 yylval.sval.ptr = tokstart + 1;
933 yylval.sval.length = namelen - 1;
934 lexptr += namelen + 1;
935
936 if(namelen == 2) /* Single character */
937 {
938 yylval.ulval = tokstart[1];
939 return CHAR;
940 }
941 else
942 return STRING;
943 }
944
945 /* Is it a number? */
946 /* Note: We have already dealt with the case of the token '.'.
947 See case '.' above. */
948 if ((c >= '0' && c <= '9'))
949 {
950 /* It's a number. */
951 int got_dot = 0, got_e = 0;
952 register char *p = tokstart;
953 int toktype;
954
955 for (++p ;; ++p)
956 {
957 if (!got_e && (*p == 'e' || *p == 'E'))
958 got_dot = got_e = 1;
959 else if (!got_dot && *p == '.')
960 got_dot = 1;
961 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
962 && (*p == '-' || *p == '+'))
963 /* This is the sign of the exponent, not the end of the
964 number. */
965 continue;
966 else if ((*p < '0' || *p > '9') &&
967 (*p < 'A' || *p > 'F') &&
968 (*p != 'H')) /* Modula-2 hexadecimal number */
969 break;
970 }
971 toktype = parse_number (p - tokstart);
972 if (toktype == ERROR)
973 {
974 char *err_copy = (char *) alloca (p - tokstart + 1);
975
976 memcpy (err_copy, tokstart, p - tokstart);
977 err_copy[p - tokstart] = 0;
978 error ("Invalid number \"%s\".", err_copy);
979 }
980 lexptr = p;
981 return toktype;
982 }
983
984 if (!(c == '_' || c == '$'
985 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
986 /* We must have come across a bad character (e.g. ';'). */
987 error ("Invalid character '%c' in expression.", c);
988
989 /* It's a name. See how long it is. */
990 namelen = 0;
991 for (c = tokstart[namelen];
992 (c == '_' || c == '$' || (c >= '0' && c <= '9')
993 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
994 c = tokstart[++namelen])
995 ;
996
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 {
1001 return 0;
1002 }
1003
1004 lexptr += namelen;
1005
1006 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
1007 and $$digits (equivalent to $<-digits> if you could type that).
1008 Make token type LAST, and put the number (the digits) in yylval. */
1009
1010 if (*tokstart == '$')
1011 {
1012 register int negate = 0;
1013 c = 1;
1014 /* Double dollar means negate the number and add -1 as well.
1015 Thus $$ alone means -1. */
1016 if (namelen >= 2 && tokstart[1] == '$')
1017 {
1018 negate = 1;
1019 c = 2;
1020 }
1021 if (c == namelen)
1022 {
1023 /* Just dollars (one or two) */
1024 yylval.lval = - negate;
1025 return LAST;
1026 }
1027 /* Is the rest of the token digits? */
1028 for (; c < namelen; c++)
1029 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1030 break;
1031 if (c == namelen)
1032 {
1033 yylval.lval = atoi (tokstart + 1 + negate);
1034 if (negate)
1035 yylval.lval = - yylval.lval;
1036 return LAST;
1037 }
1038 }
1039
1040 /* Handle tokens that refer to machine registers:
1041 $ followed by a register name. */
1042
1043 if (*tokstart == '$') {
1044 for (c = 0; c < NUM_REGS; c++)
1045 if (namelen - 1 == strlen (reg_names[c])
1046 && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
1047 {
1048 yylval.lval = c;
1049 return REGNAME;
1050 }
1051 for (c = 0; c < num_std_regs; c++)
1052 if (namelen - 1 == strlen (std_regs[c].name)
1053 && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
1054 {
1055 yylval.lval = std_regs[c].regnum;
1056 return REGNAME;
1057 }
1058 }
1059
1060
1061 /* Lookup special keywords */
1062 for(i = 0 ; i < sizeof(keytab) / sizeof(keytab[0]) ; i++)
1063 if(namelen == strlen(keytab[i].keyw) && !strncmp(tokstart,keytab[i].keyw,namelen))
1064 return keytab[i].token;
1065
1066 yylval.sval.ptr = tokstart;
1067 yylval.sval.length = namelen;
1068
1069 /* Any other names starting in $ are debugger internal variables. */
1070
1071 if (*tokstart == '$')
1072 {
1073 yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
1074 return INTERNAL_VAR;
1075 }
1076
1077
1078 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1079 functions. If this is not so, then ...
1080 Use token-type TYPENAME for symbols that happen to be defined
1081 currently as names of types; NAME for other symbols.
1082 The caller is not constrained to care about the distinction. */
1083 {
1084
1085
1086 char *tmp = copy_name (yylval.sval);
1087 struct symbol *sym;
1088
1089 if (lookup_partial_symtab (tmp))
1090 return BLOCKNAME;
1091 sym = lookup_symbol (tmp, expression_context_block,
1092 VAR_NAMESPACE, 0, NULL);
1093 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1094 return BLOCKNAME;
1095 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1096 return TYPENAME;
1097
1098 if(sym)
1099 {
1100 switch(sym->class)
1101 {
1102 case LOC_STATIC:
1103 case LOC_REGISTER:
1104 case LOC_ARG:
1105 case LOC_REF_ARG:
1106 case LOC_REGPARM:
1107 case LOC_LOCAL:
1108 case LOC_LOCAL_ARG:
1109 case LOC_CONST:
1110 case LOC_CONST_BYTES:
1111 return NAME;
1112
1113 case LOC_TYPEDEF:
1114 return TYPENAME;
1115
1116 case LOC_BLOCK:
1117 return BLOCKNAME;
1118
1119 case LOC_UNDEF:
1120 error("internal: Undefined class in m2lex()");
1121
1122 case LOC_LABEL:
1123 error("internal: Unforseen case in m2lex()");
1124 }
1125 }
1126 else
1127 {
1128 /* Built-in BOOLEAN type. This is sort of a hack. */
1129 if(!strncmp(tokstart,"TRUE",4))
1130 {
1131 yylval.ulval = 1;
1132 return M2_TRUE;
1133 }
1134 else if(!strncmp(tokstart,"FALSE",5))
1135 {
1136 yylval.ulval = 0;
1137 return M2_FALSE;
1138 }
1139 }
1140
1141 /* Must be another type of name... */
1142 return NAME;
1143 }
1144 }
1145
1146 #if 0 /* Unused */
1147 static char *
1148 make_qualname(mod,ident)
1149 char *mod, *ident;
1150 {
1151 char *new = malloc(strlen(mod)+strlen(ident)+2);
1152
1153 strcpy(new,mod);
1154 strcat(new,".");
1155 strcat(new,ident);
1156 return new;
1157 }
1158 #endif /* 0 */
1159
1160 void
1161 yyerror(msg)
1162 char *msg; /* unused */
1163 {
1164 printf("Parsing: %s\n",lexptr);
1165 if (yychar < 256)
1166 error("Invalid syntax in expression near character '%c'.",yychar);
1167 else
1168 error("Invalid syntax in expression");
1169 }
1170
This page took 0.077742 seconds and 5 git commands to generate.