1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999,
4 Free Software Foundation, Inc.
5 Generated from expread.y (now c-exp.y) and contributed by the Department
6 of Computer Science at the State University of New York at Buffalo, 1991.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
25 /* Parse a Modula-2 expression from text in a string,
26 and return the result as a struct expression pointer.
27 That structure contains arithmetic operations in reverse polish,
28 with constants represented by operations that are followed by special data.
29 See expression.h for the details of the format.
30 What is important here is that it can be built up sequentially
31 during the process of parsing; the lower levels of the tree always
32 come first in the result.
34 Note that malloc's and realloc's in this file are transformed to
35 xmalloc and xrealloc respectively by the same sed command in the
36 makefile that remaps any other malloc/realloc inserted by the parser
37 generator. Doing this with #defines and trying to control the interaction
38 with include files (<malloc.h> and <stdlib.h> for example) just became
39 too messy, particularly when such includes can be inserted at random
40 times by the parser generator. */
45 #include "gdb_string.h"
46 #include "expression.h"
49 #include "parser-defs.h"
51 #include "bfd.h" /* Required by objfiles.h. */
52 #include "symfile.h" /* Required by objfiles.h. */
53 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
56 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
57 as well as gratuitiously global symbol names, so we can have multiple
58 yacc generated parsers in gdb. Note that these are only the variables
59 produced by yacc. If other parser generators (bison, byacc, etc) produce
60 additional global names that conflict at link time, then those parser
61 generators need to be fixed instead of adding those names to this list. */
63 #define yymaxdepth m2_maxdepth
64 #define yyparse m2_parse
66 #define yyerror m2_error
67 #define yylval m2_lval
68 #define yychar m2_char
69 #define yydebug m2_debug
70 #define yypact m2_pact
77 #define yyexca m2_exca
78 #define yyerrflag m2_errflag
79 #define yynerrs m2_nerrs
84 #define yystate m2_state
89 #define yylloc m2_lloc
90 #define yyreds m2_reds /* With YYDEBUG defined */
91 #define yytoks m2_toks /* With YYDEBUG defined */
92 #define yyname m2_name /* With YYDEBUG defined */
93 #define yyrule m2_rule /* With YYDEBUG defined */
94 #define yylhs m2_yylhs
95 #define yylen m2_yylen
96 #define yydefred m2_yydefred
97 #define yydgoto m2_yydgoto
98 #define yysindex m2_yysindex
99 #define yyrindex m2_yyrindex
100 #define yygindex m2_yygindex
101 #define yytable m2_yytable
102 #define yycheck m2_yycheck
105 #define YYDEBUG 1 /* Default to yydebug support */
108 #define YYFPRINTF parser_fprintf
112 static int yylex (void);
114 void yyerror (char *);
117 static char *make_qualname (char *, char *);
120 static int parse_number (int);
122 /* The sign of the number being parsed. */
123 static int number_sign = 1;
125 /* The block that the module specified by the qualifer on an identifer is
128 static struct block *modblock=0;
133 /* Although the yacc "value" of an expression is not used,
134 since the result is stored in the structure being created,
135 other node types do have values. */
147 enum exp_opcode opcode;
148 struct internalvar *ivar;
154 %type <voidval> exp type_exp start set
155 %type <voidval> variable
160 %token <lval> INT HEX ERROR
161 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
164 /* Both NAME and TYPENAME tokens represent symbols in the input,
165 and both convey their data as strings.
166 But a TYPENAME is a string that happens to be defined as a typedef
167 or builtin type name (such as int or char)
168 and a NAME is any other symbol.
170 Contexts where this distinction is not important can use the
171 nonterminal "name", which matches either NAME or TYPENAME. */
174 %token <sval> NAME BLOCKNAME IDENT VARNAME
175 %token <sval> TYPENAME
177 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
178 %token INC DEC INCL EXCL
180 /* The GDB scope operator */
183 %token <voidval> INTERNAL_VAR
189 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
191 %left LOGICAL_AND '&'
194 %left '*' '/' DIV MOD
196 %right '^' DOT '[' '('
199 /* This is not an actual token ; it is used for precedence.
211 { write_exp_elt_opcode(OP_TYPE);
212 write_exp_elt_type($1);
213 write_exp_elt_opcode(OP_TYPE);
219 exp : exp '^' %prec UNARY
220 { write_exp_elt_opcode (UNOP_IND); }
224 { number_sign = -1; }
227 write_exp_elt_opcode (UNOP_NEG); }
230 exp : '+' exp %prec UNARY
231 { write_exp_elt_opcode(UNOP_PLUS); }
234 exp : not_exp exp %prec UNARY
235 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
242 exp : CAP '(' exp ')'
243 { write_exp_elt_opcode (UNOP_CAP); }
246 exp : ORD '(' exp ')'
247 { write_exp_elt_opcode (UNOP_ORD); }
250 exp : ABS '(' exp ')'
251 { write_exp_elt_opcode (UNOP_ABS); }
254 exp : HIGH '(' exp ')'
255 { write_exp_elt_opcode (UNOP_HIGH); }
258 exp : MIN_FUNC '(' type ')'
259 { write_exp_elt_opcode (UNOP_MIN);
260 write_exp_elt_type ($3);
261 write_exp_elt_opcode (UNOP_MIN); }
264 exp : MAX_FUNC '(' type ')'
265 { write_exp_elt_opcode (UNOP_MAX);
266 write_exp_elt_type ($3);
267 write_exp_elt_opcode (UNOP_MIN); }
270 exp : FLOAT_FUNC '(' exp ')'
271 { write_exp_elt_opcode (UNOP_FLOAT); }
274 exp : VAL '(' type ',' exp ')'
275 { write_exp_elt_opcode (BINOP_VAL);
276 write_exp_elt_type ($3);
277 write_exp_elt_opcode (BINOP_VAL); }
280 exp : CHR '(' exp ')'
281 { write_exp_elt_opcode (UNOP_CHR); }
284 exp : ODD '(' exp ')'
285 { write_exp_elt_opcode (UNOP_ODD); }
288 exp : TRUNC '(' exp ')'
289 { write_exp_elt_opcode (UNOP_TRUNC); }
292 exp : SIZE exp %prec UNARY
293 { write_exp_elt_opcode (UNOP_SIZEOF); }
297 exp : INC '(' exp ')'
298 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
301 exp : INC '(' exp ',' exp ')'
302 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
303 write_exp_elt_opcode(BINOP_ADD);
304 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
307 exp : DEC '(' exp ')'
308 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
311 exp : DEC '(' exp ',' exp ')'
312 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
313 write_exp_elt_opcode(BINOP_SUB);
314 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
318 { write_exp_elt_opcode (STRUCTOP_STRUCT);
319 write_exp_string ($3);
320 write_exp_elt_opcode (STRUCTOP_STRUCT); }
327 { error("Sets are not implemented.");}
330 exp : INCL '(' exp ',' exp ')'
331 { error("Sets are not implemented.");}
334 exp : EXCL '(' exp ',' exp ')'
335 { error("Sets are not implemented.");}
338 set : '{' arglist '}'
339 { error("Sets are not implemented.");}
340 | type '{' arglist '}'
341 { error("Sets are not implemented.");}
345 /* Modula-2 array subscript notation [a,b,c...] */
347 /* This function just saves the number of arguments
348 that follow in the list. It is *not* specific to
351 non_empty_arglist ']' %prec DOT
352 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
353 write_exp_elt_longcst ((LONGEST) end_arglist());
354 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
358 /* This is to save the value of arglist_len
359 being accumulated by an outer function call. */
360 { start_arglist (); }
361 arglist ')' %prec DOT
362 { write_exp_elt_opcode (OP_FUNCALL);
363 write_exp_elt_longcst ((LONGEST) end_arglist ());
364 write_exp_elt_opcode (OP_FUNCALL); }
374 arglist : arglist ',' exp %prec ABOVE_COMMA
384 : non_empty_arglist ',' exp %prec ABOVE_COMMA
389 exp : '{' type '}' exp %prec UNARY
390 { write_exp_elt_opcode (UNOP_MEMVAL);
391 write_exp_elt_type ($2);
392 write_exp_elt_opcode (UNOP_MEMVAL); }
395 exp : type '(' exp ')' %prec UNARY
396 { write_exp_elt_opcode (UNOP_CAST);
397 write_exp_elt_type ($1);
398 write_exp_elt_opcode (UNOP_CAST); }
405 /* Binary operators in order of decreasing precedence. Note that some
406 of these operators are overloaded! (ie. sets) */
410 { write_exp_elt_opcode (BINOP_REPEAT); }
414 { write_exp_elt_opcode (BINOP_MUL); }
418 { write_exp_elt_opcode (BINOP_DIV); }
422 { write_exp_elt_opcode (BINOP_INTDIV); }
426 { write_exp_elt_opcode (BINOP_REM); }
430 { write_exp_elt_opcode (BINOP_ADD); }
434 { write_exp_elt_opcode (BINOP_SUB); }
438 { write_exp_elt_opcode (BINOP_EQUAL); }
441 exp : exp NOTEQUAL exp
442 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
444 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
448 { write_exp_elt_opcode (BINOP_LEQ); }
452 { write_exp_elt_opcode (BINOP_GEQ); }
456 { write_exp_elt_opcode (BINOP_LESS); }
460 { write_exp_elt_opcode (BINOP_GTR); }
463 exp : exp LOGICAL_AND exp
464 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
468 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
472 { write_exp_elt_opcode (BINOP_ASSIGN); }
479 { write_exp_elt_opcode (OP_BOOL);
480 write_exp_elt_longcst ((LONGEST) $1);
481 write_exp_elt_opcode (OP_BOOL); }
485 { write_exp_elt_opcode (OP_BOOL);
486 write_exp_elt_longcst ((LONGEST) $1);
487 write_exp_elt_opcode (OP_BOOL); }
491 { write_exp_elt_opcode (OP_LONG);
492 write_exp_elt_type (builtin_type_m2_int);
493 write_exp_elt_longcst ((LONGEST) $1);
494 write_exp_elt_opcode (OP_LONG); }
499 write_exp_elt_opcode (OP_LONG);
500 write_exp_elt_type (builtin_type_m2_card);
501 write_exp_elt_longcst ((LONGEST) $1);
502 write_exp_elt_opcode (OP_LONG);
507 { write_exp_elt_opcode (OP_LONG);
508 write_exp_elt_type (builtin_type_m2_char);
509 write_exp_elt_longcst ((LONGEST) $1);
510 write_exp_elt_opcode (OP_LONG); }
515 { write_exp_elt_opcode (OP_DOUBLE);
516 write_exp_elt_type (builtin_type_m2_real);
517 write_exp_elt_dblcst ($1);
518 write_exp_elt_opcode (OP_DOUBLE); }
524 exp : SIZE '(' type ')' %prec UNARY
525 { write_exp_elt_opcode (OP_LONG);
526 write_exp_elt_type (builtin_type_int);
527 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
528 write_exp_elt_opcode (OP_LONG); }
532 { write_exp_elt_opcode (OP_M2_STRING);
533 write_exp_string ($1);
534 write_exp_elt_opcode (OP_M2_STRING); }
537 /* This will be used for extensions later. Like adding modules. */
539 { $$ = SYMBOL_BLOCK_VALUE($1); }
544 = lookup_symbol (copy_name ($1), expression_context_block,
545 VAR_DOMAIN, 0, NULL);
550 /* GDB scope operator */
551 fblock : block COLONCOLON BLOCKNAME
553 = lookup_symbol (copy_name ($3), $1,
554 VAR_DOMAIN, 0, NULL);
555 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
556 error ("No function \"%s\" in specified context.",
562 /* Useful for assigning to PROCEDURE variables */
564 { write_exp_elt_opcode(OP_VAR_VALUE);
565 write_exp_elt_block (NULL);
566 write_exp_elt_sym ($1);
567 write_exp_elt_opcode (OP_VAR_VALUE); }
570 /* GDB internal ($foo) variable */
571 variable: INTERNAL_VAR
574 /* GDB scope operator */
575 variable: block COLONCOLON NAME
576 { struct symbol *sym;
577 sym = lookup_symbol (copy_name ($3), $1,
578 VAR_DOMAIN, 0, NULL);
580 error ("No symbol \"%s\" in specified context.",
583 write_exp_elt_opcode (OP_VAR_VALUE);
584 /* block_found is set by lookup_symbol. */
585 write_exp_elt_block (block_found);
586 write_exp_elt_sym (sym);
587 write_exp_elt_opcode (OP_VAR_VALUE); }
590 /* Base case for variables. */
592 { struct symbol *sym;
593 int is_a_field_of_this;
595 sym = lookup_symbol (copy_name ($1),
596 expression_context_block,
602 if (symbol_read_needs_frame (sym))
604 if (innermost_block == 0 ||
605 contained_in (block_found,
607 innermost_block = block_found;
610 write_exp_elt_opcode (OP_VAR_VALUE);
611 /* We want to use the selected frame, not
612 another more inner frame which happens to
613 be in the same block. */
614 write_exp_elt_block (NULL);
615 write_exp_elt_sym (sym);
616 write_exp_elt_opcode (OP_VAR_VALUE);
620 struct minimal_symbol *msymbol;
621 char *arg = copy_name ($1);
624 lookup_minimal_symbol (arg, NULL, NULL);
629 lookup_function_type (builtin_type_int),
632 else if (!have_full_symbols () && !have_partial_symbols ())
633 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
635 error ("No symbol \"%s\" in current context.",
643 { $$ = lookup_typename (copy_name ($1),
644 expression_context_block, 0); }
655 return (MAX_OF_TYPE(builtin_type_m2_int) - b) < a;
662 return (MAX_OF_TYPE(builtin_type_m2_card) - b) < a;
666 /* Take care of parsing a number (anything that starts with a digit).
667 Set yylval and return the token type; update lexptr.
668 LEN is the number of characters in it. */
670 /*** Needs some error checking for the float case ***/
680 int base = input_radix;
682 int unsigned_p = number_sign == 1 ? 1 : 0;
689 else if(p[len-1] == 'C' || p[len-1] == 'B')
692 ischar = p[len-1] == 'C';
696 /* Scan the number */
697 for (c = 0; c < len; c++)
699 if (p[c] == '.' && base == 10)
701 /* It's a float since it contains a point. */
702 yylval.dval = atof (p);
706 if (p[c] == '.' && base != 10)
707 error("Floating point numbers must be base 10.");
708 if (base == 10 && (p[c] < '0' || p[c] > '9'))
709 error("Invalid digit \'%c\' in number.",p[c]);
716 if( base == 8 && (c == '8' || c == '9'))
717 error("Invalid digit \'%c\' in octal number.",c);
718 if (c >= '0' && c <= '9')
722 if (base == 16 && c >= 'A' && c <= 'F')
730 if(!unsigned_p && number_sign == 1 && (prevn >= n))
731 unsigned_p=1; /* Try something unsigned */
732 /* Don't do the range check if n==i and i==0, since that special
733 case will give an overflow error. */
734 if(RANGE_CHECK && n!=i && i)
736 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
737 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
738 range_error("Overflow on numeric constant.");
744 if(*p == 'B' || *p == 'C' || *p == 'H')
745 lexptr++; /* Advance past B,C or H */
752 else if ( unsigned_p && number_sign == 1)
757 else if((unsigned_p && (n<0))) {
758 range_error("Overflow on numeric constant -- number too large.");
759 /* But, this can return if range_check == range_warn. */
774 { {'<', '>'}, NOTEQUAL },
775 { {':', '='}, ASSIGN },
778 { {':', ':'}, COLONCOLON },
782 /* Some specific keywords */
789 static struct keyword keytab[] =
792 {"IN", IN },/* Note space after IN */
793 {"AND", LOGICAL_AND},
811 {"FLOAT", FLOAT_FUNC },
816 /* Read one token, getting characters through lexptr. */
818 /* This is where we will check to make sure that the language and the operators used are
832 prev_lexptr = lexptr;
837 /* See if it is a special token of length 2 */
838 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
839 if(DEPRECATED_STREQN(tokentab2[i].name, tokstart, 2))
842 return tokentab2[i].token;
845 switch (c = *tokstart)
862 if (paren_depth == 0)
869 if (comma_terminates && paren_depth == 0)
875 /* Might be a floating point number. */
876 if (lexptr[1] >= '0' && lexptr[1] <= '9')
877 break; /* Falls into number code. */
884 /* These are character tokens that appear as-is in the YACC grammar */
907 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
910 c = tokstart[++namelen];
911 if (c >= '0' && c <= '9')
913 c = tokstart[++namelen];
914 if (c >= '0' && c <= '9')
915 c = tokstart[++namelen];
919 error("Unterminated string or character constant.");
920 yylval.sval.ptr = tokstart + 1;
921 yylval.sval.length = namelen - 1;
922 lexptr += namelen + 1;
924 if(namelen == 2) /* Single character */
926 yylval.ulval = tokstart[1];
933 /* Is it a number? */
934 /* Note: We have already dealt with the case of the token '.'.
935 See case '.' above. */
936 if ((c >= '0' && c <= '9'))
939 int got_dot = 0, got_e = 0;
945 if (!got_e && (*p == 'e' || *p == 'E'))
947 else if (!got_dot && *p == '.')
949 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
950 && (*p == '-' || *p == '+'))
951 /* This is the sign of the exponent, not the end of the
954 else if ((*p < '0' || *p > '9') &&
955 (*p < 'A' || *p > 'F') &&
956 (*p != 'H')) /* Modula-2 hexadecimal number */
959 toktype = parse_number (p - tokstart);
960 if (toktype == ERROR)
962 char *err_copy = (char *) alloca (p - tokstart + 1);
964 memcpy (err_copy, tokstart, p - tokstart);
965 err_copy[p - tokstart] = 0;
966 error ("Invalid number \"%s\".", err_copy);
972 if (!(c == '_' || c == '$'
973 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
974 /* We must have come across a bad character (e.g. ';'). */
975 error ("Invalid character '%c' in expression.", c);
977 /* It's a name. See how long it is. */
979 for (c = tokstart[namelen];
980 (c == '_' || c == '$' || (c >= '0' && c <= '9')
981 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
982 c = tokstart[++namelen])
985 /* The token "if" terminates the expression and is NOT
986 removed from the input stream. */
987 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
994 /* Lookup special keywords */
995 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
996 if(namelen == strlen(keytab[i].keyw) && DEPRECATED_STREQN(tokstart,keytab[i].keyw,namelen))
997 return keytab[i].token;
999 yylval.sval.ptr = tokstart;
1000 yylval.sval.length = namelen;
1002 if (*tokstart == '$')
1004 write_dollar_variable (yylval.sval);
1005 return INTERNAL_VAR;
1008 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1009 functions. If this is not so, then ...
1010 Use token-type TYPENAME for symbols that happen to be defined
1011 currently as names of types; NAME for other symbols.
1012 The caller is not constrained to care about the distinction. */
1016 char *tmp = copy_name (yylval.sval);
1019 if (lookup_partial_symtab (tmp))
1021 sym = lookup_symbol (tmp, expression_context_block,
1022 VAR_DOMAIN, 0, NULL);
1023 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1025 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1037 case LOC_REGPARM_ADDR:
1041 case LOC_BASEREG_ARG:
1043 case LOC_CONST_BYTES:
1044 case LOC_OPTIMIZED_OUT:
1046 case LOC_COMPUTED_ARG:
1056 error("internal: Undefined class in m2lex()");
1059 case LOC_UNRESOLVED:
1060 error("internal: Unforseen case in m2lex()");
1063 error ("unhandled token in m2lex()");
1069 /* Built-in BOOLEAN type. This is sort of a hack. */
1070 if(DEPRECATED_STREQN(tokstart,"TRUE",4))
1075 else if(DEPRECATED_STREQN(tokstart,"FALSE",5))
1082 /* Must be another type of name... */
1089 make_qualname(mod,ident)
1092 char *new = malloc(strlen(mod)+strlen(ident)+2);
1106 lexptr = prev_lexptr;
1108 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);