1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3 Generated from expread.y (now c-exp.y) and contributed by the Department
4 of Computer Science at the State University of New York at Buffalo, 1991.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Parse a Modula-2 expression from text in a string,
22 and return the result as a struct expression pointer.
23 That structure contains arithmetic operations in reverse polish,
24 with constants represented by operations that are followed by special data.
25 See expression.h for the details of the format.
26 What is important here is that it can be built up sequentially
27 during the process of parsing; the lower levels of the tree always
28 come first in the result.
30 Note that malloc's and realloc's in this file are transformed to
31 xmalloc and xrealloc respectively by the same sed command in the
32 makefile that remaps any other malloc/realloc inserted by the parser
33 generator. Doing this with #defines and trying to control the interaction
34 with include files (<malloc.h> and <stdlib.h> for example) just became
35 too messy, particularly when such includes can be inserted at random
36 times by the parser generator. */
41 #include "expression.h"
44 #include "parser-defs.h"
46 #include "bfd.h" /* Required by objfiles.h. */
47 #include "symfile.h" /* Required by objfiles.h. */
48 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
51 #define parse_type(ps) builtin_type (ps->gdbarch ())
52 #define parse_m2_type(ps) builtin_m2_type (ps->gdbarch ())
54 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
56 #define GDB_YY_REMAP_PREFIX m2_
59 /* The state of the parser, used internally when we are parsing the
62 static struct parser_state *pstate = NULL;
66 static int yylex (void);
68 static void yyerror (const char *);
70 static int parse_number (int);
72 /* The sign of the number being parsed. */
73 static int number_sign = 1;
77 /* Although the yacc "value" of an expression is not used,
78 since the result is stored in the structure being created,
79 other node types do have values. */
90 const struct block *bval;
91 enum exp_opcode opcode;
92 struct internalvar *ivar;
98 %type <voidval> exp type_exp start set
99 %type <voidval> variable
104 %token <lval> INT HEX ERROR
105 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
108 /* Both NAME and TYPENAME tokens represent symbols in the input,
109 and both convey their data as strings.
110 But a TYPENAME is a string that happens to be defined as a typedef
111 or builtin type name (such as int or char)
112 and a NAME is any other symbol.
114 Contexts where this distinction is not important can use the
115 nonterminal "name", which matches either NAME or TYPENAME. */
118 %token <sval> NAME BLOCKNAME IDENT VARNAME
119 %token <sval> TYPENAME
121 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
123 %token INC DEC INCL EXCL
125 /* The GDB scope operator */
128 %token <voidval> DOLLAR_VARIABLE
134 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
136 %left LOGICAL_AND '&'
139 %left '*' '/' DIV MOD
141 %right '^' DOT '[' '('
144 /* This is not an actual token ; it is used for precedence.
156 { write_exp_elt_opcode (pstate, OP_TYPE);
157 write_exp_elt_type (pstate, $1);
158 write_exp_elt_opcode (pstate, OP_TYPE);
164 exp : exp '^' %prec UNARY
165 { write_exp_elt_opcode (pstate, UNOP_IND); }
169 { number_sign = -1; }
172 write_exp_elt_opcode (pstate, UNOP_NEG); }
175 exp : '+' exp %prec UNARY
176 { write_exp_elt_opcode (pstate, UNOP_PLUS); }
179 exp : not_exp exp %prec UNARY
180 { write_exp_elt_opcode (pstate, UNOP_LOGICAL_NOT); }
187 exp : CAP '(' exp ')'
188 { write_exp_elt_opcode (pstate, UNOP_CAP); }
191 exp : ORD '(' exp ')'
192 { write_exp_elt_opcode (pstate, UNOP_ORD); }
195 exp : ABS '(' exp ')'
196 { write_exp_elt_opcode (pstate, UNOP_ABS); }
199 exp : HIGH '(' exp ')'
200 { write_exp_elt_opcode (pstate, UNOP_HIGH); }
203 exp : MIN_FUNC '(' type ')'
204 { write_exp_elt_opcode (pstate, UNOP_MIN);
205 write_exp_elt_type (pstate, $3);
206 write_exp_elt_opcode (pstate, UNOP_MIN); }
209 exp : MAX_FUNC '(' type ')'
210 { write_exp_elt_opcode (pstate, UNOP_MAX);
211 write_exp_elt_type (pstate, $3);
212 write_exp_elt_opcode (pstate, UNOP_MAX); }
215 exp : FLOAT_FUNC '(' exp ')'
216 { write_exp_elt_opcode (pstate, UNOP_FLOAT); }
219 exp : VAL '(' type ',' exp ')'
220 { write_exp_elt_opcode (pstate, BINOP_VAL);
221 write_exp_elt_type (pstate, $3);
222 write_exp_elt_opcode (pstate, BINOP_VAL); }
225 exp : CHR '(' exp ')'
226 { write_exp_elt_opcode (pstate, UNOP_CHR); }
229 exp : ODD '(' exp ')'
230 { write_exp_elt_opcode (pstate, UNOP_ODD); }
233 exp : TRUNC '(' exp ')'
234 { write_exp_elt_opcode (pstate, UNOP_TRUNC); }
237 exp : TSIZE '(' exp ')'
238 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
241 exp : SIZE exp %prec UNARY
242 { write_exp_elt_opcode (pstate, UNOP_SIZEOF); }
246 exp : INC '(' exp ')'
247 { write_exp_elt_opcode (pstate, UNOP_PREINCREMENT); }
250 exp : INC '(' exp ',' exp ')'
251 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
252 write_exp_elt_opcode (pstate, BINOP_ADD);
253 write_exp_elt_opcode (pstate,
254 BINOP_ASSIGN_MODIFY); }
257 exp : DEC '(' exp ')'
258 { write_exp_elt_opcode (pstate, UNOP_PREDECREMENT);}
261 exp : DEC '(' exp ',' exp ')'
262 { write_exp_elt_opcode (pstate, BINOP_ASSIGN_MODIFY);
263 write_exp_elt_opcode (pstate, BINOP_SUB);
264 write_exp_elt_opcode (pstate,
265 BINOP_ASSIGN_MODIFY); }
269 { write_exp_elt_opcode (pstate, STRUCTOP_STRUCT);
270 write_exp_string (pstate, $3);
271 write_exp_elt_opcode (pstate, STRUCTOP_STRUCT); }
278 { error (_("Sets are not implemented."));}
281 exp : INCL '(' exp ',' exp ')'
282 { error (_("Sets are not implemented."));}
285 exp : EXCL '(' exp ',' exp ')'
286 { error (_("Sets are not implemented."));}
289 set : '{' arglist '}'
290 { error (_("Sets are not implemented."));}
291 | type '{' arglist '}'
292 { error (_("Sets are not implemented."));}
296 /* Modula-2 array subscript notation [a,b,c...]. */
298 /* This function just saves the number of arguments
299 that follow in the list. It is *not* specific to
301 { pstate->start_arglist(); }
302 non_empty_arglist ']' %prec DOT
304 gdb_assert (pstate->arglist_len > 0);
305 write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
306 write_exp_elt_longcst (pstate,
307 pstate->end_arglist());
308 write_exp_elt_opcode (pstate, MULTI_SUBSCRIPT);
313 /* This is to save the value of arglist_len
314 being accumulated by an outer function call. */
315 { pstate->start_arglist (); }
316 arglist ')' %prec DOT
317 { write_exp_elt_opcode (pstate, OP_FUNCALL);
318 write_exp_elt_longcst (pstate,
319 pstate->end_arglist ());
320 write_exp_elt_opcode (pstate, OP_FUNCALL); }
327 { pstate->arglist_len = 1; }
330 arglist : arglist ',' exp %prec ABOVE_COMMA
331 { pstate->arglist_len++; }
336 { pstate->arglist_len = 1; }
340 : non_empty_arglist ',' exp %prec ABOVE_COMMA
341 { pstate->arglist_len++; }
345 exp : '{' type '}' exp %prec UNARY
346 { write_exp_elt_opcode (pstate, UNOP_MEMVAL);
347 write_exp_elt_type (pstate, $2);
348 write_exp_elt_opcode (pstate, UNOP_MEMVAL); }
351 exp : type '(' exp ')' %prec UNARY
352 { write_exp_elt_opcode (pstate, UNOP_CAST);
353 write_exp_elt_type (pstate, $1);
354 write_exp_elt_opcode (pstate, UNOP_CAST); }
361 /* Binary operators in order of decreasing precedence. Note that some
362 of these operators are overloaded! (ie. sets) */
366 { write_exp_elt_opcode (pstate, BINOP_REPEAT); }
370 { write_exp_elt_opcode (pstate, BINOP_MUL); }
374 { write_exp_elt_opcode (pstate, BINOP_DIV); }
378 { write_exp_elt_opcode (pstate, BINOP_INTDIV); }
382 { write_exp_elt_opcode (pstate, BINOP_REM); }
386 { write_exp_elt_opcode (pstate, BINOP_ADD); }
390 { write_exp_elt_opcode (pstate, BINOP_SUB); }
394 { write_exp_elt_opcode (pstate, BINOP_EQUAL); }
397 exp : exp NOTEQUAL exp
398 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
400 { write_exp_elt_opcode (pstate, BINOP_NOTEQUAL); }
404 { write_exp_elt_opcode (pstate, BINOP_LEQ); }
408 { write_exp_elt_opcode (pstate, BINOP_GEQ); }
412 { write_exp_elt_opcode (pstate, BINOP_LESS); }
416 { write_exp_elt_opcode (pstate, BINOP_GTR); }
419 exp : exp LOGICAL_AND exp
420 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_AND); }
424 { write_exp_elt_opcode (pstate, BINOP_LOGICAL_OR); }
428 { write_exp_elt_opcode (pstate, BINOP_ASSIGN); }
435 { write_exp_elt_opcode (pstate, OP_BOOL);
436 write_exp_elt_longcst (pstate, (LONGEST) $1);
437 write_exp_elt_opcode (pstate, OP_BOOL); }
441 { write_exp_elt_opcode (pstate, OP_BOOL);
442 write_exp_elt_longcst (pstate, (LONGEST) $1);
443 write_exp_elt_opcode (pstate, OP_BOOL); }
447 { write_exp_elt_opcode (pstate, OP_LONG);
448 write_exp_elt_type (pstate,
449 parse_m2_type (pstate)->builtin_int);
450 write_exp_elt_longcst (pstate, (LONGEST) $1);
451 write_exp_elt_opcode (pstate, OP_LONG); }
456 write_exp_elt_opcode (pstate, OP_LONG);
457 write_exp_elt_type (pstate,
458 parse_m2_type (pstate)
460 write_exp_elt_longcst (pstate, (LONGEST) $1);
461 write_exp_elt_opcode (pstate, OP_LONG);
466 { write_exp_elt_opcode (pstate, OP_LONG);
467 write_exp_elt_type (pstate,
468 parse_m2_type (pstate)
470 write_exp_elt_longcst (pstate, (LONGEST) $1);
471 write_exp_elt_opcode (pstate, OP_LONG); }
476 { write_exp_elt_opcode (pstate, OP_FLOAT);
477 write_exp_elt_type (pstate,
478 parse_m2_type (pstate)
480 write_exp_elt_floatcst (pstate, $1);
481 write_exp_elt_opcode (pstate, OP_FLOAT); }
487 exp : SIZE '(' type ')' %prec UNARY
488 { write_exp_elt_opcode (pstate, OP_LONG);
489 write_exp_elt_type (pstate,
490 parse_type (pstate)->builtin_int);
491 write_exp_elt_longcst (pstate,
492 (LONGEST) TYPE_LENGTH ($3));
493 write_exp_elt_opcode (pstate, OP_LONG); }
497 { write_exp_elt_opcode (pstate, OP_M2_STRING);
498 write_exp_string (pstate, $1);
499 write_exp_elt_opcode (pstate, OP_M2_STRING); }
502 /* This will be used for extensions later. Like adding modules. */
504 { $$ = SYMBOL_BLOCK_VALUE($1); }
509 = lookup_symbol (copy_name ($1).c_str (),
510 pstate->expression_context_block,
511 VAR_DOMAIN, 0).symbol;
516 /* GDB scope operator */
517 fblock : block COLONCOLON BLOCKNAME
519 = lookup_symbol (copy_name ($3).c_str (), $1,
520 VAR_DOMAIN, 0).symbol;
521 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
522 error (_("No function \"%s\" in specified context."),
523 copy_name ($3).c_str ());
528 /* Useful for assigning to PROCEDURE variables */
530 { write_exp_elt_opcode (pstate, OP_VAR_VALUE);
531 write_exp_elt_block (pstate, NULL);
532 write_exp_elt_sym (pstate, $1);
533 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
536 /* GDB internal ($foo) variable */
537 variable: DOLLAR_VARIABLE
540 /* GDB scope operator */
541 variable: block COLONCOLON NAME
542 { struct block_symbol sym
543 = lookup_symbol (copy_name ($3).c_str (), $1,
547 error (_("No symbol \"%s\" in specified context."),
548 copy_name ($3).c_str ());
549 if (symbol_read_needs_frame (sym.symbol))
550 pstate->block_tracker->update (sym);
552 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
553 write_exp_elt_block (pstate, sym.block);
554 write_exp_elt_sym (pstate, sym.symbol);
555 write_exp_elt_opcode (pstate, OP_VAR_VALUE); }
558 /* Base case for variables. */
560 { struct block_symbol sym;
561 struct field_of_this_result is_a_field_of_this;
564 = lookup_symbol (copy_name ($1).c_str (),
565 pstate->expression_context_block,
567 &is_a_field_of_this);
571 if (symbol_read_needs_frame (sym.symbol))
572 pstate->block_tracker->update (sym);
574 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
575 write_exp_elt_block (pstate, sym.block);
576 write_exp_elt_sym (pstate, sym.symbol);
577 write_exp_elt_opcode (pstate, OP_VAR_VALUE);
581 struct bound_minimal_symbol msymbol;
582 std::string arg = copy_name ($1);
585 lookup_bound_minimal_symbol (arg.c_str ());
586 if (msymbol.minsym != NULL)
587 write_exp_msymbol (pstate, msymbol);
588 else if (!have_full_symbols () && !have_partial_symbols ())
589 error (_("No symbol table is loaded. Use the \"symbol-file\" command."));
591 error (_("No symbol \"%s\" in current context."),
600 = lookup_typename (pstate->language (),
601 copy_name ($1).c_str (),
602 pstate->expression_context_block,
610 /* Take care of parsing a number (anything that starts with a digit).
611 Set yylval and return the token type; update lexptr.
612 LEN is the number of characters in it. */
614 /*** Needs some error checking for the float case ***/
617 parse_number (int olen)
619 const char *p = pstate->lexptr;
623 int base = input_radix;
625 int unsigned_p = number_sign == 1 ? 1 : 0;
632 else if(p[len-1] == 'C' || p[len-1] == 'B')
635 ischar = p[len-1] == 'C';
639 /* Scan the number */
640 for (c = 0; c < len; c++)
642 if (p[c] == '.' && base == 10)
644 /* It's a float since it contains a point. */
645 if (!parse_float (p, len,
646 parse_m2_type (pstate)->builtin_real,
650 pstate->lexptr += len;
653 if (p[c] == '.' && base != 10)
654 error (_("Floating point numbers must be base 10."));
655 if (base == 10 && (p[c] < '0' || p[c] > '9'))
656 error (_("Invalid digit \'%c\' in number."),p[c]);
663 if( base == 8 && (c == '8' || c == '9'))
664 error (_("Invalid digit \'%c\' in octal number."),c);
665 if (c >= '0' && c <= '9')
669 if (base == 16 && c >= 'A' && c <= 'F')
677 if(!unsigned_p && number_sign == 1 && (prevn >= n))
678 unsigned_p=1; /* Try something unsigned */
679 /* Don't do the range check if n==i and i==0, since that special
680 case will give an overflow error. */
681 if(RANGE_CHECK && n!=i && i)
683 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
684 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
685 range_error (_("Overflow on numeric constant."));
691 if(*p == 'B' || *p == 'C' || *p == 'H')
692 pstate->lexptr++; /* Advance past B,C or H */
699 else if ( unsigned_p && number_sign == 1)
704 else if((unsigned_p && (n<0))) {
705 range_error (_("Overflow on numeric constant -- number too large."));
706 /* But, this can return if range_check == range_warn. */
721 { {'<', '>'}, NOTEQUAL },
722 { {':', '='}, ASSIGN },
725 { {':', ':'}, COLONCOLON },
729 /* Some specific keywords */
736 static struct keyword keytab[] =
739 {"IN", IN },/* Note space after IN */
740 {"AND", LOGICAL_AND},
758 {"FLOAT", FLOAT_FUNC },
764 /* Depth of parentheses. */
765 static int paren_depth;
767 /* Read one token, getting characters through lexptr. */
769 /* This is where we will check to make sure that the language and the
770 operators used are compatible */
778 const char *tokstart;
783 pstate->prev_lexptr = pstate->lexptr;
785 tokstart = pstate->lexptr;
788 /* See if it is a special token of length 2 */
789 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
790 if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
793 return tokentab2[i].token;
796 switch (c = *tokstart)
813 if (paren_depth == 0)
820 if (pstate->comma_terminates && paren_depth == 0)
826 /* Might be a floating point number. */
827 if (pstate->lexptr[1] >= '0' && pstate->lexptr[1] <= '9')
828 break; /* Falls into number code. */
835 /* These are character tokens that appear as-is in the YACC grammar */
858 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
861 c = tokstart[++namelen];
862 if (c >= '0' && c <= '9')
864 c = tokstart[++namelen];
865 if (c >= '0' && c <= '9')
866 c = tokstart[++namelen];
870 error (_("Unterminated string or character constant."));
871 yylval.sval.ptr = tokstart + 1;
872 yylval.sval.length = namelen - 1;
873 pstate->lexptr += namelen + 1;
875 if(namelen == 2) /* Single character */
877 yylval.ulval = tokstart[1];
884 /* Is it a number? */
885 /* Note: We have already dealt with the case of the token '.'.
886 See case '.' above. */
887 if ((c >= '0' && c <= '9'))
890 int got_dot = 0, got_e = 0;
891 const char *p = tokstart;
896 if (!got_e && (*p == 'e' || *p == 'E'))
898 else if (!got_dot && *p == '.')
900 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
901 && (*p == '-' || *p == '+'))
902 /* This is the sign of the exponent, not the end of the
905 else if ((*p < '0' || *p > '9') &&
906 (*p < 'A' || *p > 'F') &&
907 (*p != 'H')) /* Modula-2 hexadecimal number */
910 toktype = parse_number (p - tokstart);
911 if (toktype == ERROR)
913 char *err_copy = (char *) alloca (p - tokstart + 1);
915 memcpy (err_copy, tokstart, p - tokstart);
916 err_copy[p - tokstart] = 0;
917 error (_("Invalid number \"%s\"."), err_copy);
923 if (!(c == '_' || c == '$'
924 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
925 /* We must have come across a bad character (e.g. ';'). */
926 error (_("Invalid character '%c' in expression."), c);
928 /* It's a name. See how long it is. */
930 for (c = tokstart[namelen];
931 (c == '_' || c == '$' || (c >= '0' && c <= '9')
932 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
933 c = tokstart[++namelen])
936 /* The token "if" terminates the expression and is NOT
937 removed from the input stream. */
938 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
943 pstate->lexptr += namelen;
945 /* Lookup special keywords */
946 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
947 if (namelen == strlen (keytab[i].keyw)
948 && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
949 return keytab[i].token;
951 yylval.sval.ptr = tokstart;
952 yylval.sval.length = namelen;
954 if (*tokstart == '$')
956 write_dollar_variable (pstate, yylval.sval);
957 return DOLLAR_VARIABLE;
960 /* Use token-type BLOCKNAME for symbols that happen to be defined as
961 functions. If this is not so, then ...
962 Use token-type TYPENAME for symbols that happen to be defined
963 currently as names of types; NAME for other symbols.
964 The caller is not constrained to care about the distinction. */
966 std::string tmp = copy_name (yylval.sval);
969 if (lookup_symtab (tmp.c_str ()))
971 sym = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
972 VAR_DOMAIN, 0).symbol;
973 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
975 if (lookup_typename (pstate->language (),
976 tmp.c_str (), pstate->expression_context_block, 1))
981 switch(SYMBOL_CLASS (sym))
987 case LOC_REGPARM_ADDR:
990 case LOC_CONST_BYTES:
991 case LOC_OPTIMIZED_OUT:
1002 error (_("internal: Undefined class in m2lex()"));
1005 case LOC_UNRESOLVED:
1006 error (_("internal: Unforseen case in m2lex()"));
1009 error (_("unhandled token in m2lex()"));
1015 /* Built-in BOOLEAN type. This is sort of a hack. */
1016 if (strncmp (tokstart, "TRUE", 4) == 0)
1021 else if (strncmp (tokstart, "FALSE", 5) == 0)
1028 /* Must be another type of name... */
1034 m2_language::parser (struct parser_state *par_state) const
1036 /* Setting up the parser state. */
1037 scoped_restore pstate_restore = make_scoped_restore (&pstate);
1038 gdb_assert (par_state != NULL);
1046 yyerror (const char *msg)
1048 if (pstate->prev_lexptr)
1049 pstate->lexptr = pstate->prev_lexptr;
1051 error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);