1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* Parse a Modula-2 expression from text in a string,
25 and return the result as a struct expression pointer.
26 That structure contains arithmetic operations in reverse polish,
27 with constants represented by operations that are followed by special data.
28 See expression.h for the details of the format.
29 What is important here is that it can be built up sequentially
30 during the process of parsing; the lower levels of the tree always
31 come first in the result.
33 Note that malloc's and realloc's in this file are transformed to
34 xmalloc and xrealloc respectively by the same sed command in the
35 makefile that remaps any other malloc/realloc inserted by the parser
36 generator. Doing this with #defines and trying to control the interaction
37 with include files (<malloc.h> and <stdlib.h> for example) just became
38 too messy, particularly when such includes can be inserted at random
39 times by the parser generator. */
44 #include "gdb_string.h"
45 #include "expression.h"
48 #include "parser-defs.h"
50 #include "bfd.h" /* Required by objfiles.h. */
51 #include "symfile.h" /* Required by objfiles.h. */
52 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
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. */
61 #define yymaxdepth m2_maxdepth
62 #define yyparse m2_parse
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
75 #define yyexca m2_exca
76 #define yyerrflag m2_errflag
77 #define yynerrs m2_nerrs
82 #define yystate m2_state
87 #define yylloc m2_lloc
88 #define yyreds m2_reds /* With YYDEBUG defined */
89 #define yytoks m2_toks /* With YYDEBUG defined */
90 #define yylhs m2_yylhs
91 #define yylen m2_yylen
92 #define yydefred m2_yydefred
93 #define yydgoto m2_yydgoto
94 #define yysindex m2_yysindex
95 #define yyrindex m2_yyrindex
96 #define yygindex m2_yygindex
97 #define yytable m2_yytable
98 #define yycheck m2_yycheck
101 #define YYDEBUG 0 /* Default to no yydebug support */
106 static int yylex (void);
108 void yyerror (char *);
111 static char *make_qualname (char *, char *);
114 static int parse_number (int);
116 /* The sign of the number being parsed. */
117 static int number_sign = 1;
119 /* The block that the module specified by the qualifer on an identifer is
122 static struct block *modblock=0;
127 /* Although the yacc "value" of an expression is not used,
128 since the result is stored in the structure being created,
129 other node types do have values. */
141 enum exp_opcode opcode;
142 struct internalvar *ivar;
148 %type <voidval> exp type_exp start set
149 %type <voidval> variable
154 %token <lval> INT HEX ERROR
155 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
158 /* Both NAME and TYPENAME tokens represent symbols in the input,
159 and both convey their data as strings.
160 But a TYPENAME is a string that happens to be defined as a typedef
161 or builtin type name (such as int or char)
162 and a NAME is any other symbol.
164 Contexts where this distinction is not important can use the
165 nonterminal "name", which matches either NAME or TYPENAME. */
168 %token <sval> NAME BLOCKNAME IDENT VARNAME
169 %token <sval> TYPENAME
171 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
172 %token INC DEC INCL EXCL
174 /* The GDB scope operator */
177 %token <voidval> INTERNAL_VAR
183 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
185 %left LOGICAL_AND '&'
188 %left '*' '/' DIV MOD
190 %right '^' DOT '[' '('
193 /* This is not an actual token ; it is used for precedence.
205 { write_exp_elt_opcode(OP_TYPE);
206 write_exp_elt_type($1);
207 write_exp_elt_opcode(OP_TYPE);
213 exp : exp '^' %prec UNARY
214 { write_exp_elt_opcode (UNOP_IND); }
217 { number_sign = -1; }
220 write_exp_elt_opcode (UNOP_NEG); }
223 exp : '+' exp %prec UNARY
224 { write_exp_elt_opcode(UNOP_PLUS); }
227 exp : not_exp exp %prec UNARY
228 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
235 exp : CAP '(' exp ')'
236 { write_exp_elt_opcode (UNOP_CAP); }
239 exp : ORD '(' exp ')'
240 { write_exp_elt_opcode (UNOP_ORD); }
243 exp : ABS '(' exp ')'
244 { write_exp_elt_opcode (UNOP_ABS); }
247 exp : HIGH '(' exp ')'
248 { write_exp_elt_opcode (UNOP_HIGH); }
251 exp : MIN_FUNC '(' type ')'
252 { write_exp_elt_opcode (UNOP_MIN);
253 write_exp_elt_type ($3);
254 write_exp_elt_opcode (UNOP_MIN); }
257 exp : MAX_FUNC '(' type ')'
258 { write_exp_elt_opcode (UNOP_MAX);
259 write_exp_elt_type ($3);
260 write_exp_elt_opcode (UNOP_MIN); }
263 exp : FLOAT_FUNC '(' exp ')'
264 { write_exp_elt_opcode (UNOP_FLOAT); }
267 exp : VAL '(' type ',' exp ')'
268 { write_exp_elt_opcode (BINOP_VAL);
269 write_exp_elt_type ($3);
270 write_exp_elt_opcode (BINOP_VAL); }
273 exp : CHR '(' exp ')'
274 { write_exp_elt_opcode (UNOP_CHR); }
277 exp : ODD '(' exp ')'
278 { write_exp_elt_opcode (UNOP_ODD); }
281 exp : TRUNC '(' exp ')'
282 { write_exp_elt_opcode (UNOP_TRUNC); }
285 exp : SIZE exp %prec UNARY
286 { write_exp_elt_opcode (UNOP_SIZEOF); }
290 exp : INC '(' exp ')'
291 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
294 exp : INC '(' exp ',' exp ')'
295 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
296 write_exp_elt_opcode(BINOP_ADD);
297 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
300 exp : DEC '(' exp ')'
301 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
304 exp : DEC '(' exp ',' exp ')'
305 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
306 write_exp_elt_opcode(BINOP_SUB);
307 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
311 { write_exp_elt_opcode (STRUCTOP_STRUCT);
312 write_exp_string ($3);
313 write_exp_elt_opcode (STRUCTOP_STRUCT); }
320 { error("Sets are not implemented.");}
323 exp : INCL '(' exp ',' exp ')'
324 { error("Sets are not implemented.");}
327 exp : EXCL '(' exp ',' exp ')'
328 { error("Sets are not implemented.");}
330 set : '{' arglist '}'
331 { error("Sets are not implemented.");}
332 | type '{' arglist '}'
333 { error("Sets are not implemented.");}
337 /* Modula-2 array subscript notation [a,b,c...] */
339 /* This function just saves the number of arguments
340 that follow in the list. It is *not* specific to
343 non_empty_arglist ']' %prec DOT
344 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
345 write_exp_elt_longcst ((LONGEST) end_arglist());
346 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
350 /* This is to save the value of arglist_len
351 being accumulated by an outer function call. */
352 { start_arglist (); }
353 arglist ')' %prec DOT
354 { write_exp_elt_opcode (OP_FUNCALL);
355 write_exp_elt_longcst ((LONGEST) end_arglist ());
356 write_exp_elt_opcode (OP_FUNCALL); }
366 arglist : arglist ',' exp %prec ABOVE_COMMA
376 : non_empty_arglist ',' exp %prec ABOVE_COMMA
381 exp : '{' type '}' exp %prec UNARY
382 { write_exp_elt_opcode (UNOP_MEMVAL);
383 write_exp_elt_type ($2);
384 write_exp_elt_opcode (UNOP_MEMVAL); }
387 exp : type '(' exp ')' %prec UNARY
388 { write_exp_elt_opcode (UNOP_CAST);
389 write_exp_elt_type ($1);
390 write_exp_elt_opcode (UNOP_CAST); }
397 /* Binary operators in order of decreasing precedence. Note that some
398 of these operators are overloaded! (ie. sets) */
402 { write_exp_elt_opcode (BINOP_REPEAT); }
406 { write_exp_elt_opcode (BINOP_MUL); }
410 { write_exp_elt_opcode (BINOP_DIV); }
414 { write_exp_elt_opcode (BINOP_INTDIV); }
418 { write_exp_elt_opcode (BINOP_REM); }
422 { write_exp_elt_opcode (BINOP_ADD); }
426 { write_exp_elt_opcode (BINOP_SUB); }
430 { write_exp_elt_opcode (BINOP_EQUAL); }
433 exp : exp NOTEQUAL exp
434 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
436 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
440 { write_exp_elt_opcode (BINOP_LEQ); }
444 { write_exp_elt_opcode (BINOP_GEQ); }
448 { write_exp_elt_opcode (BINOP_LESS); }
452 { write_exp_elt_opcode (BINOP_GTR); }
455 exp : exp LOGICAL_AND exp
456 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
460 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
464 { write_exp_elt_opcode (BINOP_ASSIGN); }
471 { write_exp_elt_opcode (OP_BOOL);
472 write_exp_elt_longcst ((LONGEST) $1);
473 write_exp_elt_opcode (OP_BOOL); }
477 { write_exp_elt_opcode (OP_BOOL);
478 write_exp_elt_longcst ((LONGEST) $1);
479 write_exp_elt_opcode (OP_BOOL); }
483 { write_exp_elt_opcode (OP_LONG);
484 write_exp_elt_type (builtin_type_m2_int);
485 write_exp_elt_longcst ((LONGEST) $1);
486 write_exp_elt_opcode (OP_LONG); }
491 write_exp_elt_opcode (OP_LONG);
492 write_exp_elt_type (builtin_type_m2_card);
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_char);
501 write_exp_elt_longcst ((LONGEST) $1);
502 write_exp_elt_opcode (OP_LONG); }
507 { write_exp_elt_opcode (OP_DOUBLE);
508 write_exp_elt_type (builtin_type_m2_real);
509 write_exp_elt_dblcst ($1);
510 write_exp_elt_opcode (OP_DOUBLE); }
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); }
524 { write_exp_elt_opcode (OP_M2_STRING);
525 write_exp_string ($1);
526 write_exp_elt_opcode (OP_M2_STRING); }
529 /* This will be used for extensions later. Like adding modules. */
531 { $$ = SYMBOL_BLOCK_VALUE($1); }
536 = lookup_symbol (copy_name ($1), expression_context_block,
537 VAR_NAMESPACE, 0, NULL);
542 /* GDB scope operator */
543 fblock : block COLONCOLON BLOCKNAME
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.",
554 /* Useful for assigning to PROCEDURE variables */
556 { write_exp_elt_opcode(OP_VAR_VALUE);
557 write_exp_elt_block (NULL);
558 write_exp_elt_sym ($1);
559 write_exp_elt_opcode (OP_VAR_VALUE); }
562 /* GDB internal ($foo) variable */
563 variable: INTERNAL_VAR
566 /* GDB scope operator */
567 variable: block COLONCOLON NAME
568 { struct symbol *sym;
569 sym = lookup_symbol (copy_name ($3), $1,
570 VAR_NAMESPACE, 0, NULL);
572 error ("No symbol \"%s\" in specified context.",
575 write_exp_elt_opcode (OP_VAR_VALUE);
576 /* block_found is set by lookup_symbol. */
577 write_exp_elt_block (block_found);
578 write_exp_elt_sym (sym);
579 write_exp_elt_opcode (OP_VAR_VALUE); }
582 /* Base case for variables. */
584 { struct symbol *sym;
585 int is_a_field_of_this;
587 sym = lookup_symbol (copy_name ($1),
588 expression_context_block,
594 if (symbol_read_needs_frame (sym))
596 if (innermost_block == 0 ||
597 contained_in (block_found,
599 innermost_block = block_found;
602 write_exp_elt_opcode (OP_VAR_VALUE);
603 /* We want to use the selected frame, not
604 another more inner frame which happens to
605 be in the same block. */
606 write_exp_elt_block (NULL);
607 write_exp_elt_sym (sym);
608 write_exp_elt_opcode (OP_VAR_VALUE);
612 struct minimal_symbol *msymbol;
613 register char *arg = copy_name ($1);
616 lookup_minimal_symbol (arg, NULL, NULL);
621 lookup_function_type (builtin_type_int),
624 else if (!have_full_symbols () && !have_partial_symbols ())
625 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
627 error ("No symbol \"%s\" in current context.",
635 { $$ = lookup_typename (copy_name ($1),
636 expression_context_block, 0); }
647 return (MAX_OF_TYPE(builtin_type_m2_int) - b) < a;
654 return (MAX_OF_TYPE(builtin_type_m2_card) - b) < a;
658 /* Take care of parsing a number (anything that starts with a digit).
659 Set yylval and return the token type; update lexptr.
660 LEN is the number of characters in it. */
662 /*** Needs some error checking for the float case ***/
668 register char *p = lexptr;
669 register LONGEST n = 0;
670 register LONGEST prevn = 0;
671 register int c,i,ischar=0;
672 register int base = input_radix;
673 register int len = olen;
674 int unsigned_p = number_sign == 1 ? 1 : 0;
681 else if(p[len-1] == 'C' || p[len-1] == 'B')
684 ischar = p[len-1] == 'C';
688 /* Scan the number */
689 for (c = 0; c < len; c++)
691 if (p[c] == '.' && base == 10)
693 /* It's a float since it contains a point. */
694 yylval.dval = atof (p);
698 if (p[c] == '.' && base != 10)
699 error("Floating point numbers must be base 10.");
700 if (base == 10 && (p[c] < '0' || p[c] > '9'))
701 error("Invalid digit \'%c\' in number.",p[c]);
708 if( base == 8 && (c == '8' || c == '9'))
709 error("Invalid digit \'%c\' in octal number.",c);
710 if (c >= '0' && c <= '9')
714 if (base == 16 && c >= 'A' && c <= 'F')
722 if(!unsigned_p && number_sign == 1 && (prevn >= n))
723 unsigned_p=1; /* Try something unsigned */
724 /* Don't do the range check if n==i and i==0, since that special
725 case will give an overflow error. */
726 if(RANGE_CHECK && n!=i && i)
728 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
729 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
730 range_error("Overflow on numeric constant.");
736 if(*p == 'B' || *p == 'C' || *p == 'H')
737 lexptr++; /* Advance past B,C or H */
744 else if ( unsigned_p && number_sign == 1)
749 else if((unsigned_p && (n<0))) {
750 range_error("Overflow on numeric constant -- number too large.");
751 /* But, this can return if range_check == range_warn. */
766 { {'<', '>'}, NOTEQUAL },
767 { {':', '='}, ASSIGN },
770 { {':', ':'}, COLONCOLON },
774 /* Some specific keywords */
781 static struct keyword keytab[] =
784 {"IN", IN },/* Note space after IN */
785 {"AND", LOGICAL_AND},
803 {"FLOAT", FLOAT_FUNC },
808 /* Read one token, getting characters through lexptr. */
810 /* This is where we will check to make sure that the language and the operators used are
817 register int namelen;
819 register char *tokstart;
827 /* See if it is a special token of length 2 */
828 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
829 if(STREQN(tokentab2[i].name, tokstart, 2))
832 return tokentab2[i].token;
835 switch (c = *tokstart)
852 if (paren_depth == 0)
859 if (comma_terminates && paren_depth == 0)
865 /* Might be a floating point number. */
866 if (lexptr[1] >= '0' && lexptr[1] <= '9')
867 break; /* Falls into number code. */
874 /* These are character tokens that appear as-is in the YACC grammar */
897 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
900 c = tokstart[++namelen];
901 if (c >= '0' && c <= '9')
903 c = tokstart[++namelen];
904 if (c >= '0' && c <= '9')
905 c = tokstart[++namelen];
909 error("Unterminated string or character constant.");
910 yylval.sval.ptr = tokstart + 1;
911 yylval.sval.length = namelen - 1;
912 lexptr += namelen + 1;
914 if(namelen == 2) /* Single character */
916 yylval.ulval = tokstart[1];
923 /* Is it a number? */
924 /* Note: We have already dealt with the case of the token '.'.
925 See case '.' above. */
926 if ((c >= '0' && c <= '9'))
929 int got_dot = 0, got_e = 0;
930 register char *p = tokstart;
935 if (!got_e && (*p == 'e' || *p == 'E'))
937 else if (!got_dot && *p == '.')
939 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
940 && (*p == '-' || *p == '+'))
941 /* This is the sign of the exponent, not the end of the
944 else if ((*p < '0' || *p > '9') &&
945 (*p < 'A' || *p > 'F') &&
946 (*p != 'H')) /* Modula-2 hexadecimal number */
949 toktype = parse_number (p - tokstart);
950 if (toktype == ERROR)
952 char *err_copy = (char *) alloca (p - tokstart + 1);
954 memcpy (err_copy, tokstart, p - tokstart);
955 err_copy[p - tokstart] = 0;
956 error ("Invalid number \"%s\".", err_copy);
962 if (!(c == '_' || c == '$'
963 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
964 /* We must have come across a bad character (e.g. ';'). */
965 error ("Invalid character '%c' in expression.", c);
967 /* It's a name. See how long it is. */
969 for (c = tokstart[namelen];
970 (c == '_' || c == '$' || (c >= '0' && c <= '9')
971 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
972 c = tokstart[++namelen])
975 /* The token "if" terminates the expression and is NOT
976 removed from the input stream. */
977 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
984 /* Lookup special keywords */
985 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
986 if(namelen == strlen(keytab[i].keyw) && STREQN(tokstart,keytab[i].keyw,namelen))
987 return keytab[i].token;
989 yylval.sval.ptr = tokstart;
990 yylval.sval.length = namelen;
992 if (*tokstart == '$')
994 write_dollar_variable (yylval.sval);
998 /* Use token-type BLOCKNAME for symbols that happen to be defined as
999 functions. If this is not so, then ...
1000 Use token-type TYPENAME for symbols that happen to be defined
1001 currently as names of types; NAME for other symbols.
1002 The caller is not constrained to care about the distinction. */
1006 char *tmp = copy_name (yylval.sval);
1009 if (lookup_partial_symtab (tmp))
1011 sym = lookup_symbol (tmp, expression_context_block,
1012 VAR_NAMESPACE, 0, NULL);
1013 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1015 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1027 case LOC_REGPARM_ADDR:
1031 case LOC_BASEREG_ARG:
1033 case LOC_CONST_BYTES:
1034 case LOC_OPTIMIZED_OUT:
1044 error("internal: Undefined class in m2lex()");
1047 case LOC_UNRESOLVED:
1048 error("internal: Unforseen case in m2lex()");
1051 error ("unhandled token in m2lex()");
1057 /* Built-in BOOLEAN type. This is sort of a hack. */
1058 if(STREQN(tokstart,"TRUE",4))
1063 else if(STREQN(tokstart,"FALSE",5))
1070 /* Must be another type of name... */
1077 make_qualname(mod,ident)
1080 char *new = malloc(strlen(mod)+strlen(ident)+2);
1093 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);