1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994
3 Free Software Foundation, Inc.
4 Generated from expread.y (now c-exp.y) and contributed by the Department
5 of Computer Science at the State University of New York at Buffalo, 1991.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* Parse a Modula-2 expression from text in a string,
24 and return the result as a struct expression pointer.
25 That structure contains arithmetic operations in reverse polish,
26 with constants represented by operations that are followed by special data.
27 See expression.h for the details of the format.
28 What is important here is that it can be built up sequentially
29 during the process of parsing; the lower levels of the tree always
30 come first in the result.
32 Note that malloc's and realloc's in this file are transformed to
33 xmalloc and xrealloc respectively by the same sed command in the
34 makefile that remaps any other malloc/realloc inserted by the parser
35 generator. Doing this with #defines and trying to control the interaction
36 with include files (<malloc.h> and <stdlib.h> for example) just became
37 too messy, particularly when such includes can be inserted at random
38 times by the parser generator. */
44 #include "expression.h"
47 #include "parser-defs.h"
49 #include "bfd.h" /* Required by objfiles.h. */
50 #include "symfile.h" /* Required by objfiles.h. */
51 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
53 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
54 as well as gratuitiously global symbol names, so we can have multiple
55 yacc generated parsers in gdb. Note that these are only the variables
56 produced by yacc. If other parser generators (bison, byacc, etc) produce
57 additional global names that conflict at link time, then those parser
58 generators need to be fixed instead of adding those names to this list. */
60 #define yymaxdepth m2_maxdepth
61 #define yyparse m2_parse
63 #define yyerror m2_error
64 #define yylval m2_lval
65 #define yychar m2_char
66 #define yydebug m2_debug
67 #define yypact m2_pact
74 #define yyexca m2_exca
75 #define yyerrflag m2_errflag
76 #define yynerrs m2_nerrs
81 #define yystate m2_state
86 #define yylloc m2_lloc
87 #define yyreds m2_reds /* With YYDEBUG defined */
88 #define yytoks m2_toks /* With YYDEBUG defined */
91 #define YYDEBUG 0 /* Default to no yydebug support */
95 yyparse PARAMS ((void));
98 yylex PARAMS ((void));
101 yyerror PARAMS ((char *));
105 make_qualname PARAMS ((char *, char *));
109 parse_number PARAMS ((int));
111 /* The sign of the number being parsed. */
112 static int number_sign = 1;
114 /* The block that the module specified by the qualifer on an identifer is
117 static struct block *modblock=0;
122 /* Although the yacc "value" of an expression is not used,
123 since the result is stored in the structure being created,
124 other node types do have values. */
129 unsigned LONGEST ulval;
136 enum exp_opcode opcode;
137 struct internalvar *ivar;
143 %type <voidval> exp type_exp start set
144 %type <voidval> variable
149 %token <lval> INT HEX ERROR
150 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
153 /* Both NAME and TYPENAME tokens represent symbols in the input,
154 and both convey their data as strings.
155 But a TYPENAME is a string that happens to be defined as a typedef
156 or builtin type name (such as int or char)
157 and a NAME is any other symbol.
159 Contexts where this distinction is not important can use the
160 nonterminal "name", which matches either NAME or TYPENAME. */
163 %token <sval> NAME BLOCKNAME IDENT VARNAME
164 %token <sval> TYPENAME
166 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
167 %token INC DEC INCL EXCL
169 /* The GDB scope operator */
172 %token <lval> LAST REGNAME
174 %token <ivar> INTERNAL_VAR
180 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
182 %left LOGICAL_AND '&'
185 %left '*' '/' DIV MOD
187 %right '^' DOT '[' '('
190 /* This is not an actual token ; it is used for precedence.
202 { write_exp_elt_opcode(OP_TYPE);
203 write_exp_elt_type($1);
204 write_exp_elt_opcode(OP_TYPE);
210 exp : exp '^' %prec UNARY
211 { write_exp_elt_opcode (UNOP_IND); }
214 { number_sign = -1; }
217 write_exp_elt_opcode (UNOP_NEG); }
220 exp : '+' exp %prec UNARY
221 { write_exp_elt_opcode(UNOP_PLUS); }
224 exp : not_exp exp %prec UNARY
225 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
232 exp : CAP '(' exp ')'
233 { write_exp_elt_opcode (UNOP_CAP); }
236 exp : ORD '(' exp ')'
237 { write_exp_elt_opcode (UNOP_ORD); }
240 exp : ABS '(' exp ')'
241 { write_exp_elt_opcode (UNOP_ABS); }
244 exp : HIGH '(' exp ')'
245 { write_exp_elt_opcode (UNOP_HIGH); }
248 exp : MIN_FUNC '(' type ')'
249 { write_exp_elt_opcode (UNOP_MIN);
250 write_exp_elt_type ($3);
251 write_exp_elt_opcode (UNOP_MIN); }
254 exp : MAX_FUNC '(' type ')'
255 { write_exp_elt_opcode (UNOP_MAX);
256 write_exp_elt_type ($3);
257 write_exp_elt_opcode (UNOP_MIN); }
260 exp : FLOAT_FUNC '(' exp ')'
261 { write_exp_elt_opcode (UNOP_FLOAT); }
264 exp : VAL '(' type ',' exp ')'
265 { write_exp_elt_opcode (BINOP_VAL);
266 write_exp_elt_type ($3);
267 write_exp_elt_opcode (BINOP_VAL); }
270 exp : CHR '(' exp ')'
271 { write_exp_elt_opcode (UNOP_CHR); }
274 exp : ODD '(' exp ')'
275 { write_exp_elt_opcode (UNOP_ODD); }
278 exp : TRUNC '(' exp ')'
279 { write_exp_elt_opcode (UNOP_TRUNC); }
282 exp : SIZE exp %prec UNARY
283 { write_exp_elt_opcode (UNOP_SIZEOF); }
287 exp : INC '(' exp ')'
288 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
291 exp : INC '(' exp ',' exp ')'
292 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
293 write_exp_elt_opcode(BINOP_ADD);
294 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
297 exp : DEC '(' exp ')'
298 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
301 exp : DEC '(' exp ',' exp ')'
302 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
303 write_exp_elt_opcode(BINOP_SUB);
304 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
308 { write_exp_elt_opcode (STRUCTOP_STRUCT);
309 write_exp_string ($3);
310 write_exp_elt_opcode (STRUCTOP_STRUCT); }
317 { error("Sets are not implemented.");}
320 exp : INCL '(' exp ',' exp ')'
321 { error("Sets are not implemented.");}
324 exp : EXCL '(' exp ',' exp ')'
325 { error("Sets are not implemented.");}
327 set : '{' arglist '}'
328 { error("Sets are not implemented.");}
329 | type '{' arglist '}'
330 { error("Sets are not implemented.");}
334 /* Modula-2 array subscript notation [a,b,c...] */
336 /* This function just saves the number of arguments
337 that follow in the list. It is *not* specific to
340 non_empty_arglist ']' %prec DOT
341 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
342 write_exp_elt_longcst ((LONGEST) end_arglist());
343 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
347 /* This is to save the value of arglist_len
348 being accumulated by an outer function call. */
349 { start_arglist (); }
350 arglist ')' %prec DOT
351 { write_exp_elt_opcode (OP_FUNCALL);
352 write_exp_elt_longcst ((LONGEST) end_arglist ());
353 write_exp_elt_opcode (OP_FUNCALL); }
363 arglist : arglist ',' exp %prec ABOVE_COMMA
373 : non_empty_arglist ',' exp %prec ABOVE_COMMA
378 exp : '{' type '}' exp %prec UNARY
379 { write_exp_elt_opcode (UNOP_MEMVAL);
380 write_exp_elt_type ($2);
381 write_exp_elt_opcode (UNOP_MEMVAL); }
384 exp : type '(' exp ')' %prec UNARY
385 { write_exp_elt_opcode (UNOP_CAST);
386 write_exp_elt_type ($1);
387 write_exp_elt_opcode (UNOP_CAST); }
394 /* Binary operators in order of decreasing precedence. Note that some
395 of these operators are overloaded! (ie. sets) */
399 { write_exp_elt_opcode (BINOP_REPEAT); }
403 { write_exp_elt_opcode (BINOP_MUL); }
407 { write_exp_elt_opcode (BINOP_DIV); }
411 { write_exp_elt_opcode (BINOP_INTDIV); }
415 { write_exp_elt_opcode (BINOP_REM); }
419 { write_exp_elt_opcode (BINOP_ADD); }
423 { write_exp_elt_opcode (BINOP_SUB); }
427 { write_exp_elt_opcode (BINOP_EQUAL); }
430 exp : exp NOTEQUAL exp
431 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
433 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
437 { write_exp_elt_opcode (BINOP_LEQ); }
441 { write_exp_elt_opcode (BINOP_GEQ); }
445 { write_exp_elt_opcode (BINOP_LESS); }
449 { write_exp_elt_opcode (BINOP_GTR); }
452 exp : exp LOGICAL_AND exp
453 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
457 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
461 { write_exp_elt_opcode (BINOP_ASSIGN); }
468 { write_exp_elt_opcode (OP_BOOL);
469 write_exp_elt_longcst ((LONGEST) $1);
470 write_exp_elt_opcode (OP_BOOL); }
474 { write_exp_elt_opcode (OP_BOOL);
475 write_exp_elt_longcst ((LONGEST) $1);
476 write_exp_elt_opcode (OP_BOOL); }
480 { write_exp_elt_opcode (OP_LONG);
481 write_exp_elt_type (builtin_type_m2_int);
482 write_exp_elt_longcst ((LONGEST) $1);
483 write_exp_elt_opcode (OP_LONG); }
488 write_exp_elt_opcode (OP_LONG);
489 write_exp_elt_type (builtin_type_m2_card);
490 write_exp_elt_longcst ((LONGEST) $1);
491 write_exp_elt_opcode (OP_LONG);
496 { write_exp_elt_opcode (OP_LONG);
497 write_exp_elt_type (builtin_type_m2_char);
498 write_exp_elt_longcst ((LONGEST) $1);
499 write_exp_elt_opcode (OP_LONG); }
504 { write_exp_elt_opcode (OP_DOUBLE);
505 write_exp_elt_type (builtin_type_m2_real);
506 write_exp_elt_dblcst ($1);
507 write_exp_elt_opcode (OP_DOUBLE); }
513 /* The GDB internal variable $$, et al. */
515 { write_exp_elt_opcode (OP_LAST);
516 write_exp_elt_longcst ((LONGEST) $1);
517 write_exp_elt_opcode (OP_LAST); }
521 { write_exp_elt_opcode (OP_REGISTER);
522 write_exp_elt_longcst ((LONGEST) $1);
523 write_exp_elt_opcode (OP_REGISTER); }
526 exp : SIZE '(' type ')' %prec UNARY
527 { write_exp_elt_opcode (OP_LONG);
528 write_exp_elt_type (builtin_type_int);
529 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
530 write_exp_elt_opcode (OP_LONG); }
534 { write_exp_elt_opcode (OP_M2_STRING);
535 write_exp_string ($1);
536 write_exp_elt_opcode (OP_M2_STRING); }
539 /* This will be used for extensions later. Like adding modules. */
541 { $$ = SYMBOL_BLOCK_VALUE($1); }
546 = lookup_symbol (copy_name ($1), expression_context_block,
547 VAR_NAMESPACE, 0, NULL);
552 /* GDB scope operator */
553 fblock : block COLONCOLON BLOCKNAME
555 = lookup_symbol (copy_name ($3), $1,
556 VAR_NAMESPACE, 0, NULL);
557 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
558 error ("No function \"%s\" in specified context.",
564 /* Useful for assigning to PROCEDURE variables */
566 { write_exp_elt_opcode(OP_VAR_VALUE);
567 write_exp_elt_block (NULL);
568 write_exp_elt_sym ($1);
569 write_exp_elt_opcode (OP_VAR_VALUE); }
572 /* GDB internal ($foo) variable */
573 variable: INTERNAL_VAR
574 { write_exp_elt_opcode (OP_INTERNALVAR);
575 write_exp_elt_intern ($1);
576 write_exp_elt_opcode (OP_INTERNALVAR); }
579 /* GDB scope operator */
580 variable: block COLONCOLON NAME
581 { struct symbol *sym;
582 sym = lookup_symbol (copy_name ($3), $1,
583 VAR_NAMESPACE, 0, NULL);
585 error ("No symbol \"%s\" in specified context.",
588 write_exp_elt_opcode (OP_VAR_VALUE);
589 /* block_found is set by lookup_symbol. */
590 write_exp_elt_block (block_found);
591 write_exp_elt_sym (sym);
592 write_exp_elt_opcode (OP_VAR_VALUE); }
595 /* Base case for variables. */
597 { struct symbol *sym;
598 int is_a_field_of_this;
600 sym = lookup_symbol (copy_name ($1),
601 expression_context_block,
607 if (symbol_read_needs_frame (sym))
609 if (innermost_block == 0 ||
610 contained_in (block_found,
612 innermost_block = block_found;
615 write_exp_elt_opcode (OP_VAR_VALUE);
616 /* We want to use the selected frame, not
617 another more inner frame which happens to
618 be in the same block. */
619 write_exp_elt_block (NULL);
620 write_exp_elt_sym (sym);
621 write_exp_elt_opcode (OP_VAR_VALUE);
625 struct minimal_symbol *msymbol;
626 register char *arg = copy_name ($1);
628 msymbol = lookup_minimal_symbol (arg, NULL);
633 lookup_function_type (builtin_type_int),
636 else if (!have_full_symbols () && !have_partial_symbols ())
637 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
639 error ("No symbol \"%s\" in current context.",
647 { $$ = lookup_typename (copy_name ($1),
648 expression_context_block, 0); }
659 return (MAX_OF_TYPE(builtin_type_m2_int) - b) < a;
666 return (MAX_OF_TYPE(builtin_type_m2_card) - b) < a;
670 /* Take care of parsing a number (anything that starts with a digit).
671 Set yylval and return the token type; update lexptr.
672 LEN is the number of characters in it. */
674 /*** Needs some error checking for the float case ***/
680 register char *p = lexptr;
681 register LONGEST n = 0;
682 register LONGEST prevn = 0;
683 register int c,i,ischar=0;
684 register int base = input_radix;
685 register int len = olen;
686 int unsigned_p = number_sign == 1 ? 1 : 0;
693 else if(p[len-1] == 'C' || p[len-1] == 'B')
696 ischar = p[len-1] == 'C';
700 /* Scan the number */
701 for (c = 0; c < len; c++)
703 if (p[c] == '.' && base == 10)
705 /* It's a float since it contains a point. */
706 yylval.dval = atof (p);
710 if (p[c] == '.' && base != 10)
711 error("Floating point numbers must be base 10.");
712 if (base == 10 && (p[c] < '0' || p[c] > '9'))
713 error("Invalid digit \'%c\' in number.",p[c]);
720 if( base == 8 && (c == '8' || c == '9'))
721 error("Invalid digit \'%c\' in octal number.",c);
722 if (c >= '0' && c <= '9')
726 if (base == 16 && c >= 'A' && c <= 'F')
734 if(!unsigned_p && number_sign == 1 && (prevn >= n))
735 unsigned_p=1; /* Try something unsigned */
736 /* Don't do the range check if n==i and i==0, since that special
737 case will give an overflow error. */
738 if(RANGE_CHECK && n!=i && i)
740 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
741 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
742 range_error("Overflow on numeric constant.");
748 if(*p == 'B' || *p == 'C' || *p == 'H')
749 lexptr++; /* Advance past B,C or H */
756 else if ( unsigned_p && number_sign == 1)
761 else if((unsigned_p && (n<0))) {
762 range_error("Overflow on numeric constant -- number too large.");
763 /* But, this can return if range_check == range_warn. */
778 { {'<', '>'}, NOTEQUAL },
779 { {':', '='}, ASSIGN },
782 { {':', ':'}, COLONCOLON },
786 /* Some specific keywords */
793 static struct keyword keytab[] =
796 {"IN", IN },/* Note space after IN */
797 {"AND", LOGICAL_AND},
815 {"FLOAT", FLOAT_FUNC },
820 /* Read one token, getting characters through lexptr. */
822 /* This is where we will check to make sure that the language and the operators used are
829 register int namelen;
831 register char *tokstart;
839 /* See if it is a special token of length 2 */
840 for( i = 0 ; i < sizeof tokentab2 / sizeof tokentab2[0] ; i++)
841 if(STREQN(tokentab2[i].name, tokstart, 2))
844 return tokentab2[i].token;
847 switch (c = *tokstart)
864 if (paren_depth == 0)
871 if (comma_terminates && paren_depth == 0)
877 /* Might be a floating point number. */
878 if (lexptr[1] >= '0' && lexptr[1] <= '9')
879 break; /* Falls into number code. */
886 /* These are character tokens that appear as-is in the YACC grammar */
909 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
912 c = tokstart[++namelen];
913 if (c >= '0' && c <= '9')
915 c = tokstart[++namelen];
916 if (c >= '0' && c <= '9')
917 c = tokstart[++namelen];
921 error("Unterminated string or character constant.");
922 yylval.sval.ptr = tokstart + 1;
923 yylval.sval.length = namelen - 1;
924 lexptr += namelen + 1;
926 if(namelen == 2) /* Single character */
928 yylval.ulval = tokstart[1];
935 /* Is it a number? */
936 /* Note: We have already dealt with the case of the token '.'.
937 See case '.' above. */
938 if ((c >= '0' && c <= '9'))
941 int got_dot = 0, got_e = 0;
942 register char *p = tokstart;
947 if (!got_e && (*p == 'e' || *p == 'E'))
949 else if (!got_dot && *p == '.')
951 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
952 && (*p == '-' || *p == '+'))
953 /* This is the sign of the exponent, not the end of the
956 else if ((*p < '0' || *p > '9') &&
957 (*p < 'A' || *p > 'F') &&
958 (*p != 'H')) /* Modula-2 hexadecimal number */
961 toktype = parse_number (p - tokstart);
962 if (toktype == ERROR)
964 char *err_copy = (char *) alloca (p - tokstart + 1);
966 memcpy (err_copy, tokstart, p - tokstart);
967 err_copy[p - tokstart] = 0;
968 error ("Invalid number \"%s\".", err_copy);
974 if (!(c == '_' || c == '$'
975 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
976 /* We must have come across a bad character (e.g. ';'). */
977 error ("Invalid character '%c' in expression.", c);
979 /* It's a name. See how long it is. */
981 for (c = tokstart[namelen];
982 (c == '_' || c == '$' || (c >= '0' && c <= '9')
983 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
984 c = tokstart[++namelen])
987 /* The token "if" terminates the expression and is NOT
988 removed from the input stream. */
989 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
996 /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
997 and $$digits (equivalent to $<-digits> if you could type that).
998 Make token type LAST, and put the number (the digits) in yylval. */
1000 if (*tokstart == '$')
1002 register int negate = 0;
1004 /* Double dollar means negate the number and add -1 as well.
1005 Thus $$ alone means -1. */
1006 if (namelen >= 2 && tokstart[1] == '$')
1013 /* Just dollars (one or two) */
1014 yylval.lval = - negate;
1017 /* Is the rest of the token digits? */
1018 for (; c < namelen; c++)
1019 if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
1023 yylval.lval = atoi (tokstart + 1 + negate);
1025 yylval.lval = - yylval.lval;
1030 /* Handle tokens that refer to machine registers:
1031 $ followed by a register name. */
1033 if (*tokstart == '$') {
1034 for (c = 0; c < NUM_REGS; c++)
1035 if (namelen - 1 == strlen (reg_names[c])
1036 && STREQN (tokstart + 1, reg_names[c], namelen - 1))
1041 for (c = 0; c < num_std_regs; c++)
1042 if (namelen - 1 == strlen (std_regs[c].name)
1043 && STREQN (tokstart + 1, std_regs[c].name, namelen - 1))
1045 yylval.lval = std_regs[c].regnum;
1051 /* Lookup special keywords */
1052 for(i = 0 ; i < sizeof(keytab) / sizeof(keytab[0]) ; i++)
1053 if(namelen == strlen(keytab[i].keyw) && STREQN(tokstart,keytab[i].keyw,namelen))
1054 return keytab[i].token;
1056 yylval.sval.ptr = tokstart;
1057 yylval.sval.length = namelen;
1059 /* Any other names starting in $ are debugger internal variables. */
1061 if (*tokstart == '$')
1063 yylval.ivar = (struct internalvar *) lookup_internalvar (copy_name (yylval.sval) + 1);
1064 return INTERNAL_VAR;
1068 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1069 functions. If this is not so, then ...
1070 Use token-type TYPENAME for symbols that happen to be defined
1071 currently as names of types; NAME for other symbols.
1072 The caller is not constrained to care about the distinction. */
1076 char *tmp = copy_name (yylval.sval);
1079 if (lookup_partial_symtab (tmp))
1081 sym = lookup_symbol (tmp, expression_context_block,
1082 VAR_NAMESPACE, 0, NULL);
1083 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1085 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1097 case LOC_REGPARM_ADDR:
1101 case LOC_BASEREG_ARG:
1103 case LOC_CONST_BYTES:
1104 case LOC_OPTIMIZED_OUT:
1114 error("internal: Undefined class in m2lex()");
1117 error("internal: Unforseen case in m2lex()");
1122 /* Built-in BOOLEAN type. This is sort of a hack. */
1123 if(STREQN(tokstart,"TRUE",4))
1128 else if(STREQN(tokstart,"FALSE",5))
1135 /* Must be another type of name... */
1142 make_qualname(mod,ident)
1145 char *new = malloc(strlen(mod)+strlen(ident)+2);
1158 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);