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 1 /* Default to yydebug support */
104 #define YYFPRINTF parser_fprintf
108 static int yylex (void);
110 void yyerror (char *);
113 static char *make_qualname (char *, char *);
116 static int parse_number (int);
118 /* The sign of the number being parsed. */
119 static int number_sign = 1;
121 /* The block that the module specified by the qualifer on an identifer is
124 static struct block *modblock=0;
129 /* Although the yacc "value" of an expression is not used,
130 since the result is stored in the structure being created,
131 other node types do have values. */
143 enum exp_opcode opcode;
144 struct internalvar *ivar;
150 %type <voidval> exp type_exp start set
151 %type <voidval> variable
156 %token <lval> INT HEX ERROR
157 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
160 /* Both NAME and TYPENAME tokens represent symbols in the input,
161 and both convey their data as strings.
162 But a TYPENAME is a string that happens to be defined as a typedef
163 or builtin type name (such as int or char)
164 and a NAME is any other symbol.
166 Contexts where this distinction is not important can use the
167 nonterminal "name", which matches either NAME or TYPENAME. */
170 %token <sval> NAME BLOCKNAME IDENT VARNAME
171 %token <sval> TYPENAME
173 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
174 %token INC DEC INCL EXCL
176 /* The GDB scope operator */
179 %token <voidval> INTERNAL_VAR
185 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
187 %left LOGICAL_AND '&'
190 %left '*' '/' DIV MOD
192 %right '^' DOT '[' '('
195 /* This is not an actual token ; it is used for precedence.
207 { write_exp_elt_opcode(OP_TYPE);
208 write_exp_elt_type($1);
209 write_exp_elt_opcode(OP_TYPE);
215 exp : exp '^' %prec UNARY
216 { write_exp_elt_opcode (UNOP_IND); }
219 { number_sign = -1; }
222 write_exp_elt_opcode (UNOP_NEG); }
225 exp : '+' exp %prec UNARY
226 { write_exp_elt_opcode(UNOP_PLUS); }
229 exp : not_exp exp %prec UNARY
230 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
237 exp : CAP '(' exp ')'
238 { write_exp_elt_opcode (UNOP_CAP); }
241 exp : ORD '(' exp ')'
242 { write_exp_elt_opcode (UNOP_ORD); }
245 exp : ABS '(' exp ')'
246 { write_exp_elt_opcode (UNOP_ABS); }
249 exp : HIGH '(' exp ')'
250 { write_exp_elt_opcode (UNOP_HIGH); }
253 exp : MIN_FUNC '(' type ')'
254 { write_exp_elt_opcode (UNOP_MIN);
255 write_exp_elt_type ($3);
256 write_exp_elt_opcode (UNOP_MIN); }
259 exp : MAX_FUNC '(' type ')'
260 { write_exp_elt_opcode (UNOP_MAX);
261 write_exp_elt_type ($3);
262 write_exp_elt_opcode (UNOP_MIN); }
265 exp : FLOAT_FUNC '(' exp ')'
266 { write_exp_elt_opcode (UNOP_FLOAT); }
269 exp : VAL '(' type ',' exp ')'
270 { write_exp_elt_opcode (BINOP_VAL);
271 write_exp_elt_type ($3);
272 write_exp_elt_opcode (BINOP_VAL); }
275 exp : CHR '(' exp ')'
276 { write_exp_elt_opcode (UNOP_CHR); }
279 exp : ODD '(' exp ')'
280 { write_exp_elt_opcode (UNOP_ODD); }
283 exp : TRUNC '(' exp ')'
284 { write_exp_elt_opcode (UNOP_TRUNC); }
287 exp : SIZE exp %prec UNARY
288 { write_exp_elt_opcode (UNOP_SIZEOF); }
292 exp : INC '(' exp ')'
293 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
296 exp : INC '(' exp ',' exp ')'
297 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
298 write_exp_elt_opcode(BINOP_ADD);
299 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
302 exp : DEC '(' exp ')'
303 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
306 exp : DEC '(' exp ',' exp ')'
307 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
308 write_exp_elt_opcode(BINOP_SUB);
309 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
313 { write_exp_elt_opcode (STRUCTOP_STRUCT);
314 write_exp_string ($3);
315 write_exp_elt_opcode (STRUCTOP_STRUCT); }
322 { error("Sets are not implemented.");}
325 exp : INCL '(' exp ',' exp ')'
326 { error("Sets are not implemented.");}
329 exp : EXCL '(' exp ',' exp ')'
330 { error("Sets are not implemented.");}
332 set : '{' arglist '}'
333 { error("Sets are not implemented.");}
334 | type '{' arglist '}'
335 { error("Sets are not implemented.");}
339 /* Modula-2 array subscript notation [a,b,c...] */
341 /* This function just saves the number of arguments
342 that follow in the list. It is *not* specific to
345 non_empty_arglist ']' %prec DOT
346 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
347 write_exp_elt_longcst ((LONGEST) end_arglist());
348 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
352 /* This is to save the value of arglist_len
353 being accumulated by an outer function call. */
354 { start_arglist (); }
355 arglist ')' %prec DOT
356 { write_exp_elt_opcode (OP_FUNCALL);
357 write_exp_elt_longcst ((LONGEST) end_arglist ());
358 write_exp_elt_opcode (OP_FUNCALL); }
368 arglist : arglist ',' exp %prec ABOVE_COMMA
378 : non_empty_arglist ',' exp %prec ABOVE_COMMA
383 exp : '{' type '}' exp %prec UNARY
384 { write_exp_elt_opcode (UNOP_MEMVAL);
385 write_exp_elt_type ($2);
386 write_exp_elt_opcode (UNOP_MEMVAL); }
389 exp : type '(' exp ')' %prec UNARY
390 { write_exp_elt_opcode (UNOP_CAST);
391 write_exp_elt_type ($1);
392 write_exp_elt_opcode (UNOP_CAST); }
399 /* Binary operators in order of decreasing precedence. Note that some
400 of these operators are overloaded! (ie. sets) */
404 { write_exp_elt_opcode (BINOP_REPEAT); }
408 { write_exp_elt_opcode (BINOP_MUL); }
412 { write_exp_elt_opcode (BINOP_DIV); }
416 { write_exp_elt_opcode (BINOP_INTDIV); }
420 { write_exp_elt_opcode (BINOP_REM); }
424 { write_exp_elt_opcode (BINOP_ADD); }
428 { write_exp_elt_opcode (BINOP_SUB); }
432 { write_exp_elt_opcode (BINOP_EQUAL); }
435 exp : exp NOTEQUAL exp
436 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
438 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
442 { write_exp_elt_opcode (BINOP_LEQ); }
446 { write_exp_elt_opcode (BINOP_GEQ); }
450 { write_exp_elt_opcode (BINOP_LESS); }
454 { write_exp_elt_opcode (BINOP_GTR); }
457 exp : exp LOGICAL_AND exp
458 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
462 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
466 { write_exp_elt_opcode (BINOP_ASSIGN); }
473 { write_exp_elt_opcode (OP_BOOL);
474 write_exp_elt_longcst ((LONGEST) $1);
475 write_exp_elt_opcode (OP_BOOL); }
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_LONG);
486 write_exp_elt_type (builtin_type_m2_int);
487 write_exp_elt_longcst ((LONGEST) $1);
488 write_exp_elt_opcode (OP_LONG); }
493 write_exp_elt_opcode (OP_LONG);
494 write_exp_elt_type (builtin_type_m2_card);
495 write_exp_elt_longcst ((LONGEST) $1);
496 write_exp_elt_opcode (OP_LONG);
501 { write_exp_elt_opcode (OP_LONG);
502 write_exp_elt_type (builtin_type_m2_char);
503 write_exp_elt_longcst ((LONGEST) $1);
504 write_exp_elt_opcode (OP_LONG); }
509 { write_exp_elt_opcode (OP_DOUBLE);
510 write_exp_elt_type (builtin_type_m2_real);
511 write_exp_elt_dblcst ($1);
512 write_exp_elt_opcode (OP_DOUBLE); }
518 exp : SIZE '(' type ')' %prec UNARY
519 { write_exp_elt_opcode (OP_LONG);
520 write_exp_elt_type (builtin_type_int);
521 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
522 write_exp_elt_opcode (OP_LONG); }
526 { write_exp_elt_opcode (OP_M2_STRING);
527 write_exp_string ($1);
528 write_exp_elt_opcode (OP_M2_STRING); }
531 /* This will be used for extensions later. Like adding modules. */
533 { $$ = SYMBOL_BLOCK_VALUE($1); }
538 = lookup_symbol (copy_name ($1), expression_context_block,
539 VAR_NAMESPACE, 0, NULL);
544 /* GDB scope operator */
545 fblock : block COLONCOLON BLOCKNAME
547 = lookup_symbol (copy_name ($3), $1,
548 VAR_NAMESPACE, 0, NULL);
549 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
550 error ("No function \"%s\" in specified context.",
556 /* Useful for assigning to PROCEDURE variables */
558 { write_exp_elt_opcode(OP_VAR_VALUE);
559 write_exp_elt_block (NULL);
560 write_exp_elt_sym ($1);
561 write_exp_elt_opcode (OP_VAR_VALUE); }
564 /* GDB internal ($foo) variable */
565 variable: INTERNAL_VAR
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);
574 error ("No symbol \"%s\" in specified context.",
577 write_exp_elt_opcode (OP_VAR_VALUE);
578 /* block_found is set by lookup_symbol. */
579 write_exp_elt_block (block_found);
580 write_exp_elt_sym (sym);
581 write_exp_elt_opcode (OP_VAR_VALUE); }
584 /* Base case for variables. */
586 { struct symbol *sym;
587 int is_a_field_of_this;
589 sym = lookup_symbol (copy_name ($1),
590 expression_context_block,
596 if (symbol_read_needs_frame (sym))
598 if (innermost_block == 0 ||
599 contained_in (block_found,
601 innermost_block = block_found;
604 write_exp_elt_opcode (OP_VAR_VALUE);
605 /* We want to use the selected frame, not
606 another more inner frame which happens to
607 be in the same block. */
608 write_exp_elt_block (NULL);
609 write_exp_elt_sym (sym);
610 write_exp_elt_opcode (OP_VAR_VALUE);
614 struct minimal_symbol *msymbol;
615 register char *arg = copy_name ($1);
618 lookup_minimal_symbol (arg, NULL, NULL);
623 lookup_function_type (builtin_type_int),
626 else if (!have_full_symbols () && !have_partial_symbols ())
627 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
629 error ("No symbol \"%s\" in current context.",
637 { $$ = lookup_typename (copy_name ($1),
638 expression_context_block, 0); }
649 return (MAX_OF_TYPE(builtin_type_m2_int) - b) < a;
656 return (MAX_OF_TYPE(builtin_type_m2_card) - b) < a;
660 /* Take care of parsing a number (anything that starts with a digit).
661 Set yylval and return the token type; update lexptr.
662 LEN is the number of characters in it. */
664 /*** Needs some error checking for the float case ***/
670 register char *p = lexptr;
671 register LONGEST n = 0;
672 register LONGEST prevn = 0;
673 register int c,i,ischar=0;
674 register int base = input_radix;
675 register int len = olen;
676 int unsigned_p = number_sign == 1 ? 1 : 0;
683 else if(p[len-1] == 'C' || p[len-1] == 'B')
686 ischar = p[len-1] == 'C';
690 /* Scan the number */
691 for (c = 0; c < len; c++)
693 if (p[c] == '.' && base == 10)
695 /* It's a float since it contains a point. */
696 yylval.dval = atof (p);
700 if (p[c] == '.' && base != 10)
701 error("Floating point numbers must be base 10.");
702 if (base == 10 && (p[c] < '0' || p[c] > '9'))
703 error("Invalid digit \'%c\' in number.",p[c]);
710 if( base == 8 && (c == '8' || c == '9'))
711 error("Invalid digit \'%c\' in octal number.",c);
712 if (c >= '0' && c <= '9')
716 if (base == 16 && c >= 'A' && c <= 'F')
724 if(!unsigned_p && number_sign == 1 && (prevn >= n))
725 unsigned_p=1; /* Try something unsigned */
726 /* Don't do the range check if n==i and i==0, since that special
727 case will give an overflow error. */
728 if(RANGE_CHECK && n!=i && i)
730 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
731 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
732 range_error("Overflow on numeric constant.");
738 if(*p == 'B' || *p == 'C' || *p == 'H')
739 lexptr++; /* Advance past B,C or H */
746 else if ( unsigned_p && number_sign == 1)
751 else if((unsigned_p && (n<0))) {
752 range_error("Overflow on numeric constant -- number too large.");
753 /* But, this can return if range_check == range_warn. */
768 { {'<', '>'}, NOTEQUAL },
769 { {':', '='}, ASSIGN },
772 { {':', ':'}, COLONCOLON },
776 /* Some specific keywords */
783 static struct keyword keytab[] =
786 {"IN", IN },/* Note space after IN */
787 {"AND", LOGICAL_AND},
805 {"FLOAT", FLOAT_FUNC },
810 /* Read one token, getting characters through lexptr. */
812 /* This is where we will check to make sure that the language and the operators used are
819 register int namelen;
821 register char *tokstart;
826 prev_lexptr = lexptr;
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);
1098 lexptr = prev_lexptr;
1100 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);