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 yyname m2_name /* With YYDEBUG defined */
91 #define yyrule m2_rule /* With YYDEBUG defined */
92 #define yylhs m2_yylhs
93 #define yylen m2_yylen
94 #define yydefred m2_yydefred
95 #define yydgoto m2_yydgoto
96 #define yysindex m2_yysindex
97 #define yyrindex m2_yyrindex
98 #define yygindex m2_yygindex
99 #define yytable m2_yytable
100 #define yycheck m2_yycheck
103 #define YYDEBUG 1 /* Default to yydebug support */
106 #define YYFPRINTF parser_fprintf
110 static int yylex (void);
112 void yyerror (char *);
115 static char *make_qualname (char *, char *);
118 static int parse_number (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); }
222 { number_sign = -1; }
225 write_exp_elt_opcode (UNOP_NEG); }
228 exp : '+' exp %prec UNARY
229 { write_exp_elt_opcode(UNOP_PLUS); }
232 exp : not_exp exp %prec UNARY
233 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
240 exp : CAP '(' exp ')'
241 { write_exp_elt_opcode (UNOP_CAP); }
244 exp : ORD '(' exp ')'
245 { write_exp_elt_opcode (UNOP_ORD); }
248 exp : ABS '(' exp ')'
249 { write_exp_elt_opcode (UNOP_ABS); }
252 exp : HIGH '(' exp ')'
253 { write_exp_elt_opcode (UNOP_HIGH); }
256 exp : MIN_FUNC '(' type ')'
257 { write_exp_elt_opcode (UNOP_MIN);
258 write_exp_elt_type ($3);
259 write_exp_elt_opcode (UNOP_MIN); }
262 exp : MAX_FUNC '(' type ')'
263 { write_exp_elt_opcode (UNOP_MAX);
264 write_exp_elt_type ($3);
265 write_exp_elt_opcode (UNOP_MIN); }
268 exp : FLOAT_FUNC '(' exp ')'
269 { write_exp_elt_opcode (UNOP_FLOAT); }
272 exp : VAL '(' type ',' exp ')'
273 { write_exp_elt_opcode (BINOP_VAL);
274 write_exp_elt_type ($3);
275 write_exp_elt_opcode (BINOP_VAL); }
278 exp : CHR '(' exp ')'
279 { write_exp_elt_opcode (UNOP_CHR); }
282 exp : ODD '(' exp ')'
283 { write_exp_elt_opcode (UNOP_ODD); }
286 exp : TRUNC '(' exp ')'
287 { write_exp_elt_opcode (UNOP_TRUNC); }
290 exp : SIZE exp %prec UNARY
291 { write_exp_elt_opcode (UNOP_SIZEOF); }
295 exp : INC '(' exp ')'
296 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
299 exp : INC '(' exp ',' exp ')'
300 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
301 write_exp_elt_opcode(BINOP_ADD);
302 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
305 exp : DEC '(' exp ')'
306 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
309 exp : DEC '(' exp ',' exp ')'
310 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
311 write_exp_elt_opcode(BINOP_SUB);
312 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
316 { write_exp_elt_opcode (STRUCTOP_STRUCT);
317 write_exp_string ($3);
318 write_exp_elt_opcode (STRUCTOP_STRUCT); }
325 { error("Sets are not implemented.");}
328 exp : INCL '(' exp ',' exp ')'
329 { error("Sets are not implemented.");}
332 exp : EXCL '(' exp ',' exp ')'
333 { error("Sets are not implemented.");}
336 set : '{' arglist '}'
337 { error("Sets are not implemented.");}
338 | type '{' arglist '}'
339 { error("Sets are not implemented.");}
343 /* Modula-2 array subscript notation [a,b,c...] */
345 /* This function just saves the number of arguments
346 that follow in the list. It is *not* specific to
349 non_empty_arglist ']' %prec DOT
350 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
351 write_exp_elt_longcst ((LONGEST) end_arglist());
352 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
356 /* This is to save the value of arglist_len
357 being accumulated by an outer function call. */
358 { start_arglist (); }
359 arglist ')' %prec DOT
360 { write_exp_elt_opcode (OP_FUNCALL);
361 write_exp_elt_longcst ((LONGEST) end_arglist ());
362 write_exp_elt_opcode (OP_FUNCALL); }
372 arglist : arglist ',' exp %prec ABOVE_COMMA
382 : non_empty_arglist ',' exp %prec ABOVE_COMMA
387 exp : '{' type '}' exp %prec UNARY
388 { write_exp_elt_opcode (UNOP_MEMVAL);
389 write_exp_elt_type ($2);
390 write_exp_elt_opcode (UNOP_MEMVAL); }
393 exp : type '(' exp ')' %prec UNARY
394 { write_exp_elt_opcode (UNOP_CAST);
395 write_exp_elt_type ($1);
396 write_exp_elt_opcode (UNOP_CAST); }
403 /* Binary operators in order of decreasing precedence. Note that some
404 of these operators are overloaded! (ie. sets) */
408 { write_exp_elt_opcode (BINOP_REPEAT); }
412 { write_exp_elt_opcode (BINOP_MUL); }
416 { write_exp_elt_opcode (BINOP_DIV); }
420 { write_exp_elt_opcode (BINOP_INTDIV); }
424 { write_exp_elt_opcode (BINOP_REM); }
428 { write_exp_elt_opcode (BINOP_ADD); }
432 { write_exp_elt_opcode (BINOP_SUB); }
436 { write_exp_elt_opcode (BINOP_EQUAL); }
439 exp : exp NOTEQUAL exp
440 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
442 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
446 { write_exp_elt_opcode (BINOP_LEQ); }
450 { write_exp_elt_opcode (BINOP_GEQ); }
454 { write_exp_elt_opcode (BINOP_LESS); }
458 { write_exp_elt_opcode (BINOP_GTR); }
461 exp : exp LOGICAL_AND exp
462 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
466 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
470 { write_exp_elt_opcode (BINOP_ASSIGN); }
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_BOOL);
484 write_exp_elt_longcst ((LONGEST) $1);
485 write_exp_elt_opcode (OP_BOOL); }
489 { write_exp_elt_opcode (OP_LONG);
490 write_exp_elt_type (builtin_type_m2_int);
491 write_exp_elt_longcst ((LONGEST) $1);
492 write_exp_elt_opcode (OP_LONG); }
497 write_exp_elt_opcode (OP_LONG);
498 write_exp_elt_type (builtin_type_m2_card);
499 write_exp_elt_longcst ((LONGEST) $1);
500 write_exp_elt_opcode (OP_LONG);
505 { write_exp_elt_opcode (OP_LONG);
506 write_exp_elt_type (builtin_type_m2_char);
507 write_exp_elt_longcst ((LONGEST) $1);
508 write_exp_elt_opcode (OP_LONG); }
513 { write_exp_elt_opcode (OP_DOUBLE);
514 write_exp_elt_type (builtin_type_m2_real);
515 write_exp_elt_dblcst ($1);
516 write_exp_elt_opcode (OP_DOUBLE); }
522 exp : SIZE '(' type ')' %prec UNARY
523 { write_exp_elt_opcode (OP_LONG);
524 write_exp_elt_type (builtin_type_int);
525 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
526 write_exp_elt_opcode (OP_LONG); }
530 { write_exp_elt_opcode (OP_M2_STRING);
531 write_exp_string ($1);
532 write_exp_elt_opcode (OP_M2_STRING); }
535 /* This will be used for extensions later. Like adding modules. */
537 { $$ = SYMBOL_BLOCK_VALUE($1); }
542 = lookup_symbol (copy_name ($1), expression_context_block,
543 VAR_NAMESPACE, 0, NULL);
548 /* GDB scope operator */
549 fblock : block COLONCOLON BLOCKNAME
551 = lookup_symbol (copy_name ($3), $1,
552 VAR_NAMESPACE, 0, NULL);
553 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
554 error ("No function \"%s\" in specified context.",
560 /* Useful for assigning to PROCEDURE variables */
562 { write_exp_elt_opcode(OP_VAR_VALUE);
563 write_exp_elt_block (NULL);
564 write_exp_elt_sym ($1);
565 write_exp_elt_opcode (OP_VAR_VALUE); }
568 /* GDB internal ($foo) variable */
569 variable: INTERNAL_VAR
572 /* GDB scope operator */
573 variable: block COLONCOLON NAME
574 { struct symbol *sym;
575 sym = lookup_symbol (copy_name ($3), $1,
576 VAR_NAMESPACE, 0, NULL);
578 error ("No symbol \"%s\" in specified context.",
581 write_exp_elt_opcode (OP_VAR_VALUE);
582 /* block_found is set by lookup_symbol. */
583 write_exp_elt_block (block_found);
584 write_exp_elt_sym (sym);
585 write_exp_elt_opcode (OP_VAR_VALUE); }
588 /* Base case for variables. */
590 { struct symbol *sym;
591 int is_a_field_of_this;
593 sym = lookup_symbol (copy_name ($1),
594 expression_context_block,
600 if (symbol_read_needs_frame (sym))
602 if (innermost_block == 0 ||
603 contained_in (block_found,
605 innermost_block = block_found;
608 write_exp_elt_opcode (OP_VAR_VALUE);
609 /* We want to use the selected frame, not
610 another more inner frame which happens to
611 be in the same block. */
612 write_exp_elt_block (NULL);
613 write_exp_elt_sym (sym);
614 write_exp_elt_opcode (OP_VAR_VALUE);
618 struct minimal_symbol *msymbol;
619 register char *arg = copy_name ($1);
622 lookup_minimal_symbol (arg, NULL, NULL);
627 lookup_function_type (builtin_type_int),
630 else if (!have_full_symbols () && !have_partial_symbols ())
631 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
633 error ("No symbol \"%s\" in current context.",
641 { $$ = lookup_typename (copy_name ($1),
642 expression_context_block, 0); }
653 return (MAX_OF_TYPE(builtin_type_m2_int) - b) < a;
660 return (MAX_OF_TYPE(builtin_type_m2_card) - b) < a;
664 /* Take care of parsing a number (anything that starts with a digit).
665 Set yylval and return the token type; update lexptr.
666 LEN is the number of characters in it. */
668 /*** Needs some error checking for the float case ***/
674 register char *p = lexptr;
675 register LONGEST n = 0;
676 register LONGEST prevn = 0;
677 register int c,i,ischar=0;
678 register int base = input_radix;
679 register int len = olen;
680 int unsigned_p = number_sign == 1 ? 1 : 0;
687 else if(p[len-1] == 'C' || p[len-1] == 'B')
690 ischar = p[len-1] == 'C';
694 /* Scan the number */
695 for (c = 0; c < len; c++)
697 if (p[c] == '.' && base == 10)
699 /* It's a float since it contains a point. */
700 yylval.dval = atof (p);
704 if (p[c] == '.' && base != 10)
705 error("Floating point numbers must be base 10.");
706 if (base == 10 && (p[c] < '0' || p[c] > '9'))
707 error("Invalid digit \'%c\' in number.",p[c]);
714 if( base == 8 && (c == '8' || c == '9'))
715 error("Invalid digit \'%c\' in octal number.",c);
716 if (c >= '0' && c <= '9')
720 if (base == 16 && c >= 'A' && c <= 'F')
728 if(!unsigned_p && number_sign == 1 && (prevn >= n))
729 unsigned_p=1; /* Try something unsigned */
730 /* Don't do the range check if n==i and i==0, since that special
731 case will give an overflow error. */
732 if(RANGE_CHECK && n!=i && i)
734 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
735 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
736 range_error("Overflow on numeric constant.");
742 if(*p == 'B' || *p == 'C' || *p == 'H')
743 lexptr++; /* Advance past B,C or H */
750 else if ( unsigned_p && number_sign == 1)
755 else if((unsigned_p && (n<0))) {
756 range_error("Overflow on numeric constant -- number too large.");
757 /* But, this can return if range_check == range_warn. */
772 { {'<', '>'}, NOTEQUAL },
773 { {':', '='}, ASSIGN },
776 { {':', ':'}, COLONCOLON },
780 /* Some specific keywords */
787 static struct keyword keytab[] =
790 {"IN", IN },/* Note space after IN */
791 {"AND", LOGICAL_AND},
809 {"FLOAT", FLOAT_FUNC },
814 /* Read one token, getting characters through lexptr. */
816 /* This is where we will check to make sure that the language and the operators used are
823 register int namelen;
825 register char *tokstart;
830 prev_lexptr = lexptr;
835 /* See if it is a special token of length 2 */
836 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
837 if(STREQN(tokentab2[i].name, tokstart, 2))
840 return tokentab2[i].token;
843 switch (c = *tokstart)
860 if (paren_depth == 0)
867 if (comma_terminates && paren_depth == 0)
873 /* Might be a floating point number. */
874 if (lexptr[1] >= '0' && lexptr[1] <= '9')
875 break; /* Falls into number code. */
882 /* These are character tokens that appear as-is in the YACC grammar */
905 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
908 c = tokstart[++namelen];
909 if (c >= '0' && c <= '9')
911 c = tokstart[++namelen];
912 if (c >= '0' && c <= '9')
913 c = tokstart[++namelen];
917 error("Unterminated string or character constant.");
918 yylval.sval.ptr = tokstart + 1;
919 yylval.sval.length = namelen - 1;
920 lexptr += namelen + 1;
922 if(namelen == 2) /* Single character */
924 yylval.ulval = tokstart[1];
931 /* Is it a number? */
932 /* Note: We have already dealt with the case of the token '.'.
933 See case '.' above. */
934 if ((c >= '0' && c <= '9'))
937 int got_dot = 0, got_e = 0;
938 register char *p = tokstart;
943 if (!got_e && (*p == 'e' || *p == 'E'))
945 else if (!got_dot && *p == '.')
947 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
948 && (*p == '-' || *p == '+'))
949 /* This is the sign of the exponent, not the end of the
952 else if ((*p < '0' || *p > '9') &&
953 (*p < 'A' || *p > 'F') &&
954 (*p != 'H')) /* Modula-2 hexadecimal number */
957 toktype = parse_number (p - tokstart);
958 if (toktype == ERROR)
960 char *err_copy = (char *) alloca (p - tokstart + 1);
962 memcpy (err_copy, tokstart, p - tokstart);
963 err_copy[p - tokstart] = 0;
964 error ("Invalid number \"%s\".", err_copy);
970 if (!(c == '_' || c == '$'
971 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
972 /* We must have come across a bad character (e.g. ';'). */
973 error ("Invalid character '%c' in expression.", c);
975 /* It's a name. See how long it is. */
977 for (c = tokstart[namelen];
978 (c == '_' || c == '$' || (c >= '0' && c <= '9')
979 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
980 c = tokstart[++namelen])
983 /* The token "if" terminates the expression and is NOT
984 removed from the input stream. */
985 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
992 /* Lookup special keywords */
993 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
994 if(namelen == strlen(keytab[i].keyw) && STREQN(tokstart,keytab[i].keyw,namelen))
995 return keytab[i].token;
997 yylval.sval.ptr = tokstart;
998 yylval.sval.length = namelen;
1000 if (*tokstart == '$')
1002 write_dollar_variable (yylval.sval);
1003 return INTERNAL_VAR;
1006 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1007 functions. If this is not so, then ...
1008 Use token-type TYPENAME for symbols that happen to be defined
1009 currently as names of types; NAME for other symbols.
1010 The caller is not constrained to care about the distinction. */
1014 char *tmp = copy_name (yylval.sval);
1017 if (lookup_partial_symtab (tmp))
1019 sym = lookup_symbol (tmp, expression_context_block,
1020 VAR_NAMESPACE, 0, NULL);
1021 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1023 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1035 case LOC_REGPARM_ADDR:
1039 case LOC_BASEREG_ARG:
1041 case LOC_CONST_BYTES:
1042 case LOC_OPTIMIZED_OUT:
1052 error("internal: Undefined class in m2lex()");
1055 case LOC_UNRESOLVED:
1056 error("internal: Unforseen case in m2lex()");
1059 error ("unhandled token in m2lex()");
1065 /* Built-in BOOLEAN type. This is sort of a hack. */
1066 if(STREQN(tokstart,"TRUE",4))
1071 else if(STREQN(tokstart,"FALSE",5))
1078 /* Must be another type of name... */
1085 make_qualname(mod,ident)
1088 char *new = malloc(strlen(mod)+strlen(ident)+2);
1102 lexptr = prev_lexptr;
1104 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);