1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1993, 1994, 1995
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. */
43 #include "gdb_string.h"
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 */
89 #define yylhs m2_yylhs
90 #define yylen m2_yylen
91 #define yydefred m2_yydefred
92 #define yydgoto m2_yydgoto
93 #define yysindex m2_yysindex
94 #define yyrindex m2_yyrindex
95 #define yygindex m2_yygindex
96 #define yytable m2_yytable
97 #define yycheck m2_yycheck
100 #define YYDEBUG 0 /* Default to no yydebug support */
104 yyparse PARAMS ((void));
107 yylex PARAMS ((void));
110 yyerror PARAMS ((char *));
114 make_qualname PARAMS ((char *, char *));
118 parse_number PARAMS ((int));
120 /* The sign of the number being parsed. */
121 static int number_sign = 1;
123 /* The block that the module specified by the qualifer on an identifer is
126 static struct block *modblock=0;
131 /* Although the yacc "value" of an expression is not used,
132 since the result is stored in the structure being created,
133 other node types do have values. */
145 enum exp_opcode opcode;
146 struct internalvar *ivar;
152 %type <voidval> exp type_exp start set
153 %type <voidval> variable
158 %token <lval> INT HEX ERROR
159 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
162 /* Both NAME and TYPENAME tokens represent symbols in the input,
163 and both convey their data as strings.
164 But a TYPENAME is a string that happens to be defined as a typedef
165 or builtin type name (such as int or char)
166 and a NAME is any other symbol.
168 Contexts where this distinction is not important can use the
169 nonterminal "name", which matches either NAME or TYPENAME. */
172 %token <sval> NAME BLOCKNAME IDENT VARNAME
173 %token <sval> TYPENAME
175 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
176 %token INC DEC INCL EXCL
178 /* The GDB scope operator */
181 %token <voidval> INTERNAL_VAR
187 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
189 %left LOGICAL_AND '&'
192 %left '*' '/' DIV MOD
194 %right '^' DOT '[' '('
197 /* This is not an actual token ; it is used for precedence.
209 { write_exp_elt_opcode(OP_TYPE);
210 write_exp_elt_type($1);
211 write_exp_elt_opcode(OP_TYPE);
217 exp : exp '^' %prec UNARY
218 { write_exp_elt_opcode (UNOP_IND); }
221 { number_sign = -1; }
224 write_exp_elt_opcode (UNOP_NEG); }
227 exp : '+' exp %prec UNARY
228 { write_exp_elt_opcode(UNOP_PLUS); }
231 exp : not_exp exp %prec UNARY
232 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
239 exp : CAP '(' exp ')'
240 { write_exp_elt_opcode (UNOP_CAP); }
243 exp : ORD '(' exp ')'
244 { write_exp_elt_opcode (UNOP_ORD); }
247 exp : ABS '(' exp ')'
248 { write_exp_elt_opcode (UNOP_ABS); }
251 exp : HIGH '(' exp ')'
252 { write_exp_elt_opcode (UNOP_HIGH); }
255 exp : MIN_FUNC '(' type ')'
256 { write_exp_elt_opcode (UNOP_MIN);
257 write_exp_elt_type ($3);
258 write_exp_elt_opcode (UNOP_MIN); }
261 exp : MAX_FUNC '(' type ')'
262 { write_exp_elt_opcode (UNOP_MAX);
263 write_exp_elt_type ($3);
264 write_exp_elt_opcode (UNOP_MIN); }
267 exp : FLOAT_FUNC '(' exp ')'
268 { write_exp_elt_opcode (UNOP_FLOAT); }
271 exp : VAL '(' type ',' exp ')'
272 { write_exp_elt_opcode (BINOP_VAL);
273 write_exp_elt_type ($3);
274 write_exp_elt_opcode (BINOP_VAL); }
277 exp : CHR '(' exp ')'
278 { write_exp_elt_opcode (UNOP_CHR); }
281 exp : ODD '(' exp ')'
282 { write_exp_elt_opcode (UNOP_ODD); }
285 exp : TRUNC '(' exp ')'
286 { write_exp_elt_opcode (UNOP_TRUNC); }
289 exp : SIZE exp %prec UNARY
290 { write_exp_elt_opcode (UNOP_SIZEOF); }
294 exp : INC '(' exp ')'
295 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
298 exp : INC '(' exp ',' exp ')'
299 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
300 write_exp_elt_opcode(BINOP_ADD);
301 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
304 exp : DEC '(' exp ')'
305 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
308 exp : DEC '(' exp ',' exp ')'
309 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
310 write_exp_elt_opcode(BINOP_SUB);
311 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
315 { write_exp_elt_opcode (STRUCTOP_STRUCT);
316 write_exp_string ($3);
317 write_exp_elt_opcode (STRUCTOP_STRUCT); }
324 { error("Sets are not implemented.");}
327 exp : INCL '(' exp ',' exp ')'
328 { error("Sets are not implemented.");}
331 exp : EXCL '(' exp ',' exp ')'
332 { error("Sets are not implemented.");}
334 set : '{' arglist '}'
335 { error("Sets are not implemented.");}
336 | type '{' arglist '}'
337 { error("Sets are not implemented.");}
341 /* Modula-2 array subscript notation [a,b,c...] */
343 /* This function just saves the number of arguments
344 that follow in the list. It is *not* specific to
347 non_empty_arglist ']' %prec DOT
348 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
349 write_exp_elt_longcst ((LONGEST) end_arglist());
350 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
354 /* This is to save the value of arglist_len
355 being accumulated by an outer function call. */
356 { start_arglist (); }
357 arglist ')' %prec DOT
358 { write_exp_elt_opcode (OP_FUNCALL);
359 write_exp_elt_longcst ((LONGEST) end_arglist ());
360 write_exp_elt_opcode (OP_FUNCALL); }
370 arglist : arglist ',' exp %prec ABOVE_COMMA
380 : non_empty_arglist ',' exp %prec ABOVE_COMMA
385 exp : '{' type '}' exp %prec UNARY
386 { write_exp_elt_opcode (UNOP_MEMVAL);
387 write_exp_elt_type ($2);
388 write_exp_elt_opcode (UNOP_MEMVAL); }
391 exp : type '(' exp ')' %prec UNARY
392 { write_exp_elt_opcode (UNOP_CAST);
393 write_exp_elt_type ($1);
394 write_exp_elt_opcode (UNOP_CAST); }
401 /* Binary operators in order of decreasing precedence. Note that some
402 of these operators are overloaded! (ie. sets) */
406 { write_exp_elt_opcode (BINOP_REPEAT); }
410 { write_exp_elt_opcode (BINOP_MUL); }
414 { write_exp_elt_opcode (BINOP_DIV); }
418 { write_exp_elt_opcode (BINOP_INTDIV); }
422 { write_exp_elt_opcode (BINOP_REM); }
426 { write_exp_elt_opcode (BINOP_ADD); }
430 { write_exp_elt_opcode (BINOP_SUB); }
434 { write_exp_elt_opcode (BINOP_EQUAL); }
437 exp : exp NOTEQUAL exp
438 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
440 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
444 { write_exp_elt_opcode (BINOP_LEQ); }
448 { write_exp_elt_opcode (BINOP_GEQ); }
452 { write_exp_elt_opcode (BINOP_LESS); }
456 { write_exp_elt_opcode (BINOP_GTR); }
459 exp : exp LOGICAL_AND exp
460 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
464 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
468 { write_exp_elt_opcode (BINOP_ASSIGN); }
475 { write_exp_elt_opcode (OP_BOOL);
476 write_exp_elt_longcst ((LONGEST) $1);
477 write_exp_elt_opcode (OP_BOOL); }
481 { write_exp_elt_opcode (OP_BOOL);
482 write_exp_elt_longcst ((LONGEST) $1);
483 write_exp_elt_opcode (OP_BOOL); }
487 { write_exp_elt_opcode (OP_LONG);
488 write_exp_elt_type (builtin_type_m2_int);
489 write_exp_elt_longcst ((LONGEST) $1);
490 write_exp_elt_opcode (OP_LONG); }
495 write_exp_elt_opcode (OP_LONG);
496 write_exp_elt_type (builtin_type_m2_card);
497 write_exp_elt_longcst ((LONGEST) $1);
498 write_exp_elt_opcode (OP_LONG);
503 { write_exp_elt_opcode (OP_LONG);
504 write_exp_elt_type (builtin_type_m2_char);
505 write_exp_elt_longcst ((LONGEST) $1);
506 write_exp_elt_opcode (OP_LONG); }
511 { write_exp_elt_opcode (OP_DOUBLE);
512 write_exp_elt_type (builtin_type_m2_real);
513 write_exp_elt_dblcst ($1);
514 write_exp_elt_opcode (OP_DOUBLE); }
520 exp : SIZE '(' type ')' %prec UNARY
521 { write_exp_elt_opcode (OP_LONG);
522 write_exp_elt_type (builtin_type_int);
523 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
524 write_exp_elt_opcode (OP_LONG); }
528 { write_exp_elt_opcode (OP_M2_STRING);
529 write_exp_string ($1);
530 write_exp_elt_opcode (OP_M2_STRING); }
533 /* This will be used for extensions later. Like adding modules. */
535 { $$ = SYMBOL_BLOCK_VALUE($1); }
540 = lookup_symbol (copy_name ($1), expression_context_block,
541 VAR_NAMESPACE, 0, NULL);
546 /* GDB scope operator */
547 fblock : block COLONCOLON BLOCKNAME
549 = lookup_symbol (copy_name ($3), $1,
550 VAR_NAMESPACE, 0, NULL);
551 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
552 error ("No function \"%s\" in specified context.",
558 /* Useful for assigning to PROCEDURE variables */
560 { write_exp_elt_opcode(OP_VAR_VALUE);
561 write_exp_elt_block (NULL);
562 write_exp_elt_sym ($1);
563 write_exp_elt_opcode (OP_VAR_VALUE); }
566 /* GDB internal ($foo) variable */
567 variable: INTERNAL_VAR
570 /* GDB scope operator */
571 variable: block COLONCOLON NAME
572 { struct symbol *sym;
573 sym = lookup_symbol (copy_name ($3), $1,
574 VAR_NAMESPACE, 0, NULL);
576 error ("No symbol \"%s\" in specified context.",
579 write_exp_elt_opcode (OP_VAR_VALUE);
580 /* block_found is set by lookup_symbol. */
581 write_exp_elt_block (block_found);
582 write_exp_elt_sym (sym);
583 write_exp_elt_opcode (OP_VAR_VALUE); }
586 /* Base case for variables. */
588 { struct symbol *sym;
589 int is_a_field_of_this;
591 sym = lookup_symbol (copy_name ($1),
592 expression_context_block,
598 if (symbol_read_needs_frame (sym))
600 if (innermost_block == 0 ||
601 contained_in (block_found,
603 innermost_block = block_found;
606 write_exp_elt_opcode (OP_VAR_VALUE);
607 /* We want to use the selected frame, not
608 another more inner frame which happens to
609 be in the same block. */
610 write_exp_elt_block (NULL);
611 write_exp_elt_sym (sym);
612 write_exp_elt_opcode (OP_VAR_VALUE);
616 struct minimal_symbol *msymbol;
617 register char *arg = copy_name ($1);
620 lookup_minimal_symbol (arg, NULL, NULL);
625 lookup_function_type (builtin_type_int),
628 else if (!have_full_symbols () && !have_partial_symbols ())
629 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
631 error ("No symbol \"%s\" in current context.",
639 { $$ = lookup_typename (copy_name ($1),
640 expression_context_block, 0); }
651 return (MAX_OF_TYPE(builtin_type_m2_int) - b) < a;
658 return (MAX_OF_TYPE(builtin_type_m2_card) - b) < a;
662 /* Take care of parsing a number (anything that starts with a digit).
663 Set yylval and return the token type; update lexptr.
664 LEN is the number of characters in it. */
666 /*** Needs some error checking for the float case ***/
672 register char *p = lexptr;
673 register LONGEST n = 0;
674 register LONGEST prevn = 0;
675 register int c,i,ischar=0;
676 register int base = input_radix;
677 register int len = olen;
678 int unsigned_p = number_sign == 1 ? 1 : 0;
685 else if(p[len-1] == 'C' || p[len-1] == 'B')
688 ischar = p[len-1] == 'C';
692 /* Scan the number */
693 for (c = 0; c < len; c++)
695 if (p[c] == '.' && base == 10)
697 /* It's a float since it contains a point. */
698 yylval.dval = atof (p);
702 if (p[c] == '.' && base != 10)
703 error("Floating point numbers must be base 10.");
704 if (base == 10 && (p[c] < '0' || p[c] > '9'))
705 error("Invalid digit \'%c\' in number.",p[c]);
712 if( base == 8 && (c == '8' || c == '9'))
713 error("Invalid digit \'%c\' in octal number.",c);
714 if (c >= '0' && c <= '9')
718 if (base == 16 && c >= 'A' && c <= 'F')
726 if(!unsigned_p && number_sign == 1 && (prevn >= n))
727 unsigned_p=1; /* Try something unsigned */
728 /* Don't do the range check if n==i and i==0, since that special
729 case will give an overflow error. */
730 if(RANGE_CHECK && n!=i && i)
732 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
733 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
734 range_error("Overflow on numeric constant.");
740 if(*p == 'B' || *p == 'C' || *p == 'H')
741 lexptr++; /* Advance past B,C or H */
748 else if ( unsigned_p && number_sign == 1)
753 else if((unsigned_p && (n<0))) {
754 range_error("Overflow on numeric constant -- number too large.");
755 /* But, this can return if range_check == range_warn. */
770 { {'<', '>'}, NOTEQUAL },
771 { {':', '='}, ASSIGN },
774 { {':', ':'}, COLONCOLON },
778 /* Some specific keywords */
785 static struct keyword keytab[] =
788 {"IN", IN },/* Note space after IN */
789 {"AND", LOGICAL_AND},
807 {"FLOAT", FLOAT_FUNC },
812 /* Read one token, getting characters through lexptr. */
814 /* This is where we will check to make sure that the language and the operators used are
821 register int namelen;
823 register char *tokstart;
831 /* See if it is a special token of length 2 */
832 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
833 if(STREQN(tokentab2[i].name, tokstart, 2))
836 return tokentab2[i].token;
839 switch (c = *tokstart)
856 if (paren_depth == 0)
863 if (comma_terminates && paren_depth == 0)
869 /* Might be a floating point number. */
870 if (lexptr[1] >= '0' && lexptr[1] <= '9')
871 break; /* Falls into number code. */
878 /* These are character tokens that appear as-is in the YACC grammar */
901 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
904 c = tokstart[++namelen];
905 if (c >= '0' && c <= '9')
907 c = tokstart[++namelen];
908 if (c >= '0' && c <= '9')
909 c = tokstart[++namelen];
913 error("Unterminated string or character constant.");
914 yylval.sval.ptr = tokstart + 1;
915 yylval.sval.length = namelen - 1;
916 lexptr += namelen + 1;
918 if(namelen == 2) /* Single character */
920 yylval.ulval = tokstart[1];
927 /* Is it a number? */
928 /* Note: We have already dealt with the case of the token '.'.
929 See case '.' above. */
930 if ((c >= '0' && c <= '9'))
933 int got_dot = 0, got_e = 0;
934 register char *p = tokstart;
939 if (!got_e && (*p == 'e' || *p == 'E'))
941 else if (!got_dot && *p == '.')
943 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
944 && (*p == '-' || *p == '+'))
945 /* This is the sign of the exponent, not the end of the
948 else if ((*p < '0' || *p > '9') &&
949 (*p < 'A' || *p > 'F') &&
950 (*p != 'H')) /* Modula-2 hexadecimal number */
953 toktype = parse_number (p - tokstart);
954 if (toktype == ERROR)
956 char *err_copy = (char *) alloca (p - tokstart + 1);
958 memcpy (err_copy, tokstart, p - tokstart);
959 err_copy[p - tokstart] = 0;
960 error ("Invalid number \"%s\".", err_copy);
966 if (!(c == '_' || c == '$'
967 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
968 /* We must have come across a bad character (e.g. ';'). */
969 error ("Invalid character '%c' in expression.", c);
971 /* It's a name. See how long it is. */
973 for (c = tokstart[namelen];
974 (c == '_' || c == '$' || (c >= '0' && c <= '9')
975 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
976 c = tokstart[++namelen])
979 /* The token "if" terminates the expression and is NOT
980 removed from the input stream. */
981 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
988 /* Lookup special keywords */
989 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
990 if(namelen == strlen(keytab[i].keyw) && STREQN(tokstart,keytab[i].keyw,namelen))
991 return keytab[i].token;
993 yylval.sval.ptr = tokstart;
994 yylval.sval.length = namelen;
996 if (*tokstart == '$')
998 write_dollar_variable (yylval.sval);
1002 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1003 functions. If this is not so, then ...
1004 Use token-type TYPENAME for symbols that happen to be defined
1005 currently as names of types; NAME for other symbols.
1006 The caller is not constrained to care about the distinction. */
1010 char *tmp = copy_name (yylval.sval);
1013 if (lookup_partial_symtab (tmp))
1015 sym = lookup_symbol (tmp, expression_context_block,
1016 VAR_NAMESPACE, 0, NULL);
1017 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1019 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1031 case LOC_REGPARM_ADDR:
1035 case LOC_BASEREG_ARG:
1037 case LOC_CONST_BYTES:
1038 case LOC_OPTIMIZED_OUT:
1048 error("internal: Undefined class in m2lex()");
1051 case LOC_UNRESOLVED:
1052 error("internal: Unforseen case in m2lex()");
1055 error ("unhandled token in m2lex()");
1061 /* Built-in BOOLEAN type. This is sort of a hack. */
1062 if(STREQN(tokstart,"TRUE",4))
1067 else if(STREQN(tokstart,"FALSE",5))
1074 /* Must be another type of name... */
1081 make_qualname(mod,ident)
1084 char *new = malloc(strlen(mod)+strlen(ident)+2);
1097 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);