1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999,
3 2000, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Generated from expread.y (now c-exp.y) and contributed by the Department
5 of Computer Science at the State University of New York at Buffalo, 1991.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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 */
55 #define parse_type builtin_type (parse_gdbarch)
56 #define parse_m2_type builtin_m2_type (parse_gdbarch)
58 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
59 as well as gratuitiously global symbol names, so we can have multiple
60 yacc generated parsers in gdb. Note that these are only the variables
61 produced by yacc. If other parser generators (bison, byacc, etc) produce
62 additional global names that conflict at link time, then those parser
63 generators need to be fixed instead of adding those names to this list. */
65 #define yymaxdepth m2_maxdepth
66 #define yyparse m2_parse
68 #define yyerror m2_error
69 #define yylval m2_lval
70 #define yychar m2_char
71 #define yydebug m2_debug
72 #define yypact m2_pact
79 #define yyexca m2_exca
80 #define yyerrflag m2_errflag
81 #define yynerrs m2_nerrs
86 #define yystate m2_state
91 #define yylloc m2_lloc
92 #define yyreds m2_reds /* With YYDEBUG defined */
93 #define yytoks m2_toks /* With YYDEBUG defined */
94 #define yyname m2_name /* With YYDEBUG defined */
95 #define yyrule m2_rule /* With YYDEBUG defined */
96 #define yylhs m2_yylhs
97 #define yylen m2_yylen
98 #define yydefred m2_yydefred
99 #define yydgoto m2_yydgoto
100 #define yysindex m2_yysindex
101 #define yyrindex m2_yyrindex
102 #define yygindex m2_yygindex
103 #define yytable m2_yytable
104 #define yycheck m2_yycheck
107 #define YYDEBUG 1 /* Default to yydebug support */
110 #define YYFPRINTF parser_fprintf
114 static int yylex (void);
116 void yyerror (char *);
119 static char *make_qualname (char *, char *);
122 static int parse_number (int);
124 /* The sign of the number being parsed. */
125 static int number_sign = 1;
127 /* The block that the module specified by the qualifer on an identifer is
130 static struct block *modblock=0;
135 /* Although the yacc "value" of an expression is not used,
136 since the result is stored in the structure being created,
137 other node types do have values. */
149 enum exp_opcode opcode;
150 struct internalvar *ivar;
156 %type <voidval> exp type_exp start set
157 %type <voidval> variable
162 %token <lval> INT HEX ERROR
163 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
166 /* Both NAME and TYPENAME tokens represent symbols in the input,
167 and both convey their data as strings.
168 But a TYPENAME is a string that happens to be defined as a typedef
169 or builtin type name (such as int or char)
170 and a NAME is any other symbol.
172 Contexts where this distinction is not important can use the
173 nonterminal "name", which matches either NAME or TYPENAME. */
176 %token <sval> NAME BLOCKNAME IDENT VARNAME
177 %token <sval> TYPENAME
179 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
181 %token INC DEC INCL EXCL
183 /* The GDB scope operator */
186 %token <voidval> INTERNAL_VAR
192 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
194 %left LOGICAL_AND '&'
197 %left '*' '/' DIV MOD
199 %right '^' DOT '[' '('
202 /* This is not an actual token ; it is used for precedence.
214 { write_exp_elt_opcode(OP_TYPE);
215 write_exp_elt_type($1);
216 write_exp_elt_opcode(OP_TYPE);
222 exp : exp '^' %prec UNARY
223 { write_exp_elt_opcode (UNOP_IND); }
227 { number_sign = -1; }
230 write_exp_elt_opcode (UNOP_NEG); }
233 exp : '+' exp %prec UNARY
234 { write_exp_elt_opcode(UNOP_PLUS); }
237 exp : not_exp exp %prec UNARY
238 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
245 exp : CAP '(' exp ')'
246 { write_exp_elt_opcode (UNOP_CAP); }
249 exp : ORD '(' exp ')'
250 { write_exp_elt_opcode (UNOP_ORD); }
253 exp : ABS '(' exp ')'
254 { write_exp_elt_opcode (UNOP_ABS); }
257 exp : HIGH '(' exp ')'
258 { write_exp_elt_opcode (UNOP_HIGH); }
261 exp : MIN_FUNC '(' type ')'
262 { write_exp_elt_opcode (UNOP_MIN);
263 write_exp_elt_type ($3);
264 write_exp_elt_opcode (UNOP_MIN); }
267 exp : MAX_FUNC '(' type ')'
268 { write_exp_elt_opcode (UNOP_MAX);
269 write_exp_elt_type ($3);
270 write_exp_elt_opcode (UNOP_MIN); }
273 exp : FLOAT_FUNC '(' exp ')'
274 { write_exp_elt_opcode (UNOP_FLOAT); }
277 exp : VAL '(' type ',' exp ')'
278 { write_exp_elt_opcode (BINOP_VAL);
279 write_exp_elt_type ($3);
280 write_exp_elt_opcode (BINOP_VAL); }
283 exp : CHR '(' exp ')'
284 { write_exp_elt_opcode (UNOP_CHR); }
287 exp : ODD '(' exp ')'
288 { write_exp_elt_opcode (UNOP_ODD); }
291 exp : TRUNC '(' exp ')'
292 { write_exp_elt_opcode (UNOP_TRUNC); }
295 exp : TSIZE '(' exp ')'
296 { write_exp_elt_opcode (UNOP_SIZEOF); }
299 exp : SIZE exp %prec UNARY
300 { write_exp_elt_opcode (UNOP_SIZEOF); }
304 exp : INC '(' exp ')'
305 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
308 exp : INC '(' exp ',' exp ')'
309 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
310 write_exp_elt_opcode(BINOP_ADD);
311 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
314 exp : DEC '(' exp ')'
315 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
318 exp : DEC '(' exp ',' exp ')'
319 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
320 write_exp_elt_opcode(BINOP_SUB);
321 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
325 { write_exp_elt_opcode (STRUCTOP_STRUCT);
326 write_exp_string ($3);
327 write_exp_elt_opcode (STRUCTOP_STRUCT); }
334 { error("Sets are not implemented.");}
337 exp : INCL '(' exp ',' exp ')'
338 { error("Sets are not implemented.");}
341 exp : EXCL '(' exp ',' exp ')'
342 { error("Sets are not implemented.");}
345 set : '{' arglist '}'
346 { error("Sets are not implemented.");}
347 | type '{' arglist '}'
348 { error("Sets are not implemented.");}
352 /* Modula-2 array subscript notation [a,b,c...] */
354 /* This function just saves the number of arguments
355 that follow in the list. It is *not* specific to
358 non_empty_arglist ']' %prec DOT
359 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
360 write_exp_elt_longcst ((LONGEST) end_arglist());
361 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
364 exp : exp '[' exp ']'
365 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
369 /* This is to save the value of arglist_len
370 being accumulated by an outer function call. */
371 { start_arglist (); }
372 arglist ')' %prec DOT
373 { write_exp_elt_opcode (OP_FUNCALL);
374 write_exp_elt_longcst ((LONGEST) end_arglist ());
375 write_exp_elt_opcode (OP_FUNCALL); }
385 arglist : arglist ',' exp %prec ABOVE_COMMA
395 : non_empty_arglist ',' exp %prec ABOVE_COMMA
400 exp : '{' type '}' exp %prec UNARY
401 { write_exp_elt_opcode (UNOP_MEMVAL);
402 write_exp_elt_type ($2);
403 write_exp_elt_opcode (UNOP_MEMVAL); }
406 exp : type '(' exp ')' %prec UNARY
407 { write_exp_elt_opcode (UNOP_CAST);
408 write_exp_elt_type ($1);
409 write_exp_elt_opcode (UNOP_CAST); }
416 /* Binary operators in order of decreasing precedence. Note that some
417 of these operators are overloaded! (ie. sets) */
421 { write_exp_elt_opcode (BINOP_REPEAT); }
425 { write_exp_elt_opcode (BINOP_MUL); }
429 { write_exp_elt_opcode (BINOP_DIV); }
433 { write_exp_elt_opcode (BINOP_INTDIV); }
437 { write_exp_elt_opcode (BINOP_REM); }
441 { write_exp_elt_opcode (BINOP_ADD); }
445 { write_exp_elt_opcode (BINOP_SUB); }
449 { write_exp_elt_opcode (BINOP_EQUAL); }
452 exp : exp NOTEQUAL exp
453 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
455 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
459 { write_exp_elt_opcode (BINOP_LEQ); }
463 { write_exp_elt_opcode (BINOP_GEQ); }
467 { write_exp_elt_opcode (BINOP_LESS); }
471 { write_exp_elt_opcode (BINOP_GTR); }
474 exp : exp LOGICAL_AND exp
475 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
479 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
483 { write_exp_elt_opcode (BINOP_ASSIGN); }
490 { write_exp_elt_opcode (OP_BOOL);
491 write_exp_elt_longcst ((LONGEST) $1);
492 write_exp_elt_opcode (OP_BOOL); }
496 { write_exp_elt_opcode (OP_BOOL);
497 write_exp_elt_longcst ((LONGEST) $1);
498 write_exp_elt_opcode (OP_BOOL); }
502 { write_exp_elt_opcode (OP_LONG);
503 write_exp_elt_type (parse_m2_type->builtin_int);
504 write_exp_elt_longcst ((LONGEST) $1);
505 write_exp_elt_opcode (OP_LONG); }
510 write_exp_elt_opcode (OP_LONG);
511 write_exp_elt_type (parse_m2_type->builtin_card);
512 write_exp_elt_longcst ((LONGEST) $1);
513 write_exp_elt_opcode (OP_LONG);
518 { write_exp_elt_opcode (OP_LONG);
519 write_exp_elt_type (parse_m2_type->builtin_char);
520 write_exp_elt_longcst ((LONGEST) $1);
521 write_exp_elt_opcode (OP_LONG); }
526 { write_exp_elt_opcode (OP_DOUBLE);
527 write_exp_elt_type (parse_m2_type->builtin_real);
528 write_exp_elt_dblcst ($1);
529 write_exp_elt_opcode (OP_DOUBLE); }
535 exp : SIZE '(' type ')' %prec UNARY
536 { write_exp_elt_opcode (OP_LONG);
537 write_exp_elt_type (parse_type->builtin_int);
538 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
539 write_exp_elt_opcode (OP_LONG); }
543 { write_exp_elt_opcode (OP_M2_STRING);
544 write_exp_string ($1);
545 write_exp_elt_opcode (OP_M2_STRING); }
548 /* This will be used for extensions later. Like adding modules. */
550 { $$ = SYMBOL_BLOCK_VALUE($1); }
555 = lookup_symbol (copy_name ($1), expression_context_block,
561 /* GDB scope operator */
562 fblock : block COLONCOLON BLOCKNAME
564 = lookup_symbol (copy_name ($3), $1,
566 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
567 error ("No function \"%s\" in specified context.",
573 /* Useful for assigning to PROCEDURE variables */
575 { write_exp_elt_opcode(OP_VAR_VALUE);
576 write_exp_elt_block (NULL);
577 write_exp_elt_sym ($1);
578 write_exp_elt_opcode (OP_VAR_VALUE); }
581 /* GDB internal ($foo) variable */
582 variable: INTERNAL_VAR
585 /* GDB scope operator */
586 variable: block COLONCOLON NAME
587 { struct symbol *sym;
588 sym = lookup_symbol (copy_name ($3), $1,
591 error ("No symbol \"%s\" in specified context.",
594 write_exp_elt_opcode (OP_VAR_VALUE);
595 /* block_found is set by lookup_symbol. */
596 write_exp_elt_block (block_found);
597 write_exp_elt_sym (sym);
598 write_exp_elt_opcode (OP_VAR_VALUE); }
601 /* Base case for variables. */
603 { struct symbol *sym;
604 int is_a_field_of_this;
606 sym = lookup_symbol (copy_name ($1),
607 expression_context_block,
609 &is_a_field_of_this);
612 if (symbol_read_needs_frame (sym))
614 if (innermost_block == 0 ||
615 contained_in (block_found,
617 innermost_block = block_found;
620 write_exp_elt_opcode (OP_VAR_VALUE);
621 /* We want to use the selected frame, not
622 another more inner frame which happens to
623 be in the same block. */
624 write_exp_elt_block (NULL);
625 write_exp_elt_sym (sym);
626 write_exp_elt_opcode (OP_VAR_VALUE);
630 struct minimal_symbol *msymbol;
631 char *arg = copy_name ($1);
634 lookup_minimal_symbol (arg, NULL, NULL);
636 write_exp_msymbol (msymbol);
637 else if (!have_full_symbols () && !have_partial_symbols ())
638 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
640 error ("No symbol \"%s\" in current context.",
648 { $$ = lookup_typename (copy_name ($1),
649 expression_context_block, 0); }
660 return (MAX_OF_TYPE(parse_m2_type->builtin_int) - b) < a;
667 return (MAX_OF_TYPE(parse_m2_type->builtin_card) - b) < a;
671 /* Take care of parsing a number (anything that starts with a digit).
672 Set yylval and return the token type; update lexptr.
673 LEN is the number of characters in it. */
675 /*** Needs some error checking for the float case ***/
685 int base = input_radix;
687 int unsigned_p = number_sign == 1 ? 1 : 0;
694 else if(p[len-1] == 'C' || p[len-1] == 'B')
697 ischar = p[len-1] == 'C';
701 /* Scan the number */
702 for (c = 0; c < len; c++)
704 if (p[c] == '.' && base == 10)
706 /* It's a float since it contains a point. */
707 yylval.dval = atof (p);
711 if (p[c] == '.' && base != 10)
712 error("Floating point numbers must be base 10.");
713 if (base == 10 && (p[c] < '0' || p[c] > '9'))
714 error("Invalid digit \'%c\' in number.",p[c]);
721 if( base == 8 && (c == '8' || c == '9'))
722 error("Invalid digit \'%c\' in octal number.",c);
723 if (c >= '0' && c <= '9')
727 if (base == 16 && c >= 'A' && c <= 'F')
735 if(!unsigned_p && number_sign == 1 && (prevn >= n))
736 unsigned_p=1; /* Try something unsigned */
737 /* Don't do the range check if n==i and i==0, since that special
738 case will give an overflow error. */
739 if(RANGE_CHECK && n!=i && i)
741 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
742 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
743 range_error("Overflow on numeric constant.");
749 if(*p == 'B' || *p == 'C' || *p == 'H')
750 lexptr++; /* Advance past B,C or H */
757 else if ( unsigned_p && number_sign == 1)
762 else if((unsigned_p && (n<0))) {
763 range_error("Overflow on numeric constant -- number too large.");
764 /* But, this can return if range_check == range_warn. */
779 { {'<', '>'}, NOTEQUAL },
780 { {':', '='}, ASSIGN },
783 { {':', ':'}, COLONCOLON },
787 /* Some specific keywords */
794 static struct keyword keytab[] =
797 {"IN", IN },/* Note space after IN */
798 {"AND", LOGICAL_AND},
816 {"FLOAT", FLOAT_FUNC },
822 /* Read one token, getting characters through lexptr. */
824 /* This is where we will check to make sure that the language and the operators used are
838 prev_lexptr = lexptr;
843 /* See if it is a special token of length 2 */
844 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
845 if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
848 return tokentab2[i].token;
851 switch (c = *tokstart)
868 if (paren_depth == 0)
875 if (comma_terminates && paren_depth == 0)
881 /* Might be a floating point number. */
882 if (lexptr[1] >= '0' && lexptr[1] <= '9')
883 break; /* Falls into number code. */
890 /* These are character tokens that appear as-is in the YACC grammar */
913 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
916 c = tokstart[++namelen];
917 if (c >= '0' && c <= '9')
919 c = tokstart[++namelen];
920 if (c >= '0' && c <= '9')
921 c = tokstart[++namelen];
925 error("Unterminated string or character constant.");
926 yylval.sval.ptr = tokstart + 1;
927 yylval.sval.length = namelen - 1;
928 lexptr += namelen + 1;
930 if(namelen == 2) /* Single character */
932 yylval.ulval = tokstart[1];
939 /* Is it a number? */
940 /* Note: We have already dealt with the case of the token '.'.
941 See case '.' above. */
942 if ((c >= '0' && c <= '9'))
945 int got_dot = 0, got_e = 0;
951 if (!got_e && (*p == 'e' || *p == 'E'))
953 else if (!got_dot && *p == '.')
955 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
956 && (*p == '-' || *p == '+'))
957 /* This is the sign of the exponent, not the end of the
960 else if ((*p < '0' || *p > '9') &&
961 (*p < 'A' || *p > 'F') &&
962 (*p != 'H')) /* Modula-2 hexadecimal number */
965 toktype = parse_number (p - tokstart);
966 if (toktype == ERROR)
968 char *err_copy = (char *) alloca (p - tokstart + 1);
970 memcpy (err_copy, tokstart, p - tokstart);
971 err_copy[p - tokstart] = 0;
972 error ("Invalid number \"%s\".", err_copy);
978 if (!(c == '_' || c == '$'
979 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
980 /* We must have come across a bad character (e.g. ';'). */
981 error ("Invalid character '%c' in expression.", c);
983 /* It's a name. See how long it is. */
985 for (c = tokstart[namelen];
986 (c == '_' || c == '$' || (c >= '0' && c <= '9')
987 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
988 c = tokstart[++namelen])
991 /* The token "if" terminates the expression and is NOT
992 removed from the input stream. */
993 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
1000 /* Lookup special keywords */
1001 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
1002 if (namelen == strlen (keytab[i].keyw)
1003 && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
1004 return keytab[i].token;
1006 yylval.sval.ptr = tokstart;
1007 yylval.sval.length = namelen;
1009 if (*tokstart == '$')
1011 write_dollar_variable (yylval.sval);
1012 return INTERNAL_VAR;
1015 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1016 functions. If this is not so, then ...
1017 Use token-type TYPENAME for symbols that happen to be defined
1018 currently as names of types; NAME for other symbols.
1019 The caller is not constrained to care about the distinction. */
1023 char *tmp = copy_name (yylval.sval);
1026 if (lookup_partial_symtab (tmp))
1028 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 0);
1029 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1031 if (lookup_typename (copy_name (yylval.sval), expression_context_block, 1))
1036 switch(SYMBOL_CLASS (sym))
1042 case LOC_REGPARM_ADDR:
1045 case LOC_CONST_BYTES:
1046 case LOC_OPTIMIZED_OUT:
1057 error("internal: Undefined class in m2lex()");
1060 case LOC_UNRESOLVED:
1061 error("internal: Unforseen case in m2lex()");
1064 error ("unhandled token in m2lex()");
1070 /* Built-in BOOLEAN type. This is sort of a hack. */
1071 if (strncmp (tokstart, "TRUE", 4) == 0)
1076 else if (strncmp (tokstart, "FALSE", 5) == 0)
1083 /* Must be another type of name... */
1090 make_qualname(mod,ident)
1093 char *new = malloc(strlen(mod)+strlen(ident)+2);
1107 lexptr = prev_lexptr;
1109 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);