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 3 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, see <http://www.gnu.org/licenses/>. */
22 /* Parse a Modula-2 expression from text in a string,
23 and return the result as a struct expression pointer.
24 That structure contains arithmetic operations in reverse polish,
25 with constants represented by operations that are followed by special data.
26 See expression.h for the details of the format.
27 What is important here is that it can be built up sequentially
28 during the process of parsing; the lower levels of the tree always
29 come first in the result.
31 Note that malloc's and realloc's in this file are transformed to
32 xmalloc and xrealloc respectively by the same sed command in the
33 makefile that remaps any other malloc/realloc inserted by the parser
34 generator. Doing this with #defines and trying to control the interaction
35 with include files (<malloc.h> and <stdlib.h> for example) just became
36 too messy, particularly when such includes can be inserted at random
37 times by the parser generator. */
42 #include "gdb_string.h"
43 #include "expression.h"
46 #include "parser-defs.h"
48 #include "bfd.h" /* Required by objfiles.h. */
49 #include "symfile.h" /* Required by objfiles.h. */
50 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
53 #define parse_type builtin_type (parse_gdbarch)
54 #define parse_m2_type builtin_m2_type (parse_gdbarch)
56 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
57 as well as gratuitiously global symbol names, so we can have multiple
58 yacc generated parsers in gdb. Note that these are only the variables
59 produced by yacc. If other parser generators (bison, byacc, etc) produce
60 additional global names that conflict at link time, then those parser
61 generators need to be fixed instead of adding those names to this list. */
63 #define yymaxdepth m2_maxdepth
64 #define yyparse m2_parse
66 #define yyerror m2_error
67 #define yylval m2_lval
68 #define yychar m2_char
69 #define yydebug m2_debug
70 #define yypact m2_pact
77 #define yyexca m2_exca
78 #define yyerrflag m2_errflag
79 #define yynerrs m2_nerrs
84 #define yystate m2_state
89 #define yylloc m2_lloc
90 #define yyreds m2_reds /* With YYDEBUG defined */
91 #define yytoks m2_toks /* With YYDEBUG defined */
92 #define yyname m2_name /* With YYDEBUG defined */
93 #define yyrule m2_rule /* With YYDEBUG defined */
94 #define yylhs m2_yylhs
95 #define yylen m2_yylen
96 #define yydefred m2_yydefred
97 #define yydgoto m2_yydgoto
98 #define yysindex m2_yysindex
99 #define yyrindex m2_yyrindex
100 #define yygindex m2_yygindex
101 #define yytable m2_yytable
102 #define yycheck m2_yycheck
105 #define YYDEBUG 1 /* Default to yydebug support */
108 #define YYFPRINTF parser_fprintf
112 static int yylex (void);
114 void yyerror (char *);
117 static char *make_qualname (char *, char *);
120 static int parse_number (int);
122 /* The sign of the number being parsed. */
123 static int number_sign = 1;
125 /* The block that the module specified by the qualifer on an identifer is
128 static struct block *modblock=0;
133 /* Although the yacc "value" of an expression is not used,
134 since the result is stored in the structure being created,
135 other node types do have values. */
147 enum exp_opcode opcode;
148 struct internalvar *ivar;
154 %type <voidval> exp type_exp start set
155 %type <voidval> variable
160 %token <lval> INT HEX ERROR
161 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
164 /* Both NAME and TYPENAME tokens represent symbols in the input,
165 and both convey their data as strings.
166 But a TYPENAME is a string that happens to be defined as a typedef
167 or builtin type name (such as int or char)
168 and a NAME is any other symbol.
170 Contexts where this distinction is not important can use the
171 nonterminal "name", which matches either NAME or TYPENAME. */
174 %token <sval> NAME BLOCKNAME IDENT VARNAME
175 %token <sval> TYPENAME
177 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
179 %token INC DEC INCL EXCL
181 /* The GDB scope operator */
184 %token <voidval> INTERNAL_VAR
190 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
192 %left LOGICAL_AND '&'
195 %left '*' '/' DIV MOD
197 %right '^' DOT '[' '('
200 /* This is not an actual token ; it is used for precedence.
212 { write_exp_elt_opcode(OP_TYPE);
213 write_exp_elt_type($1);
214 write_exp_elt_opcode(OP_TYPE);
220 exp : exp '^' %prec UNARY
221 { write_exp_elt_opcode (UNOP_IND); }
225 { number_sign = -1; }
228 write_exp_elt_opcode (UNOP_NEG); }
231 exp : '+' exp %prec UNARY
232 { write_exp_elt_opcode(UNOP_PLUS); }
235 exp : not_exp exp %prec UNARY
236 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
243 exp : CAP '(' exp ')'
244 { write_exp_elt_opcode (UNOP_CAP); }
247 exp : ORD '(' exp ')'
248 { write_exp_elt_opcode (UNOP_ORD); }
251 exp : ABS '(' exp ')'
252 { write_exp_elt_opcode (UNOP_ABS); }
255 exp : HIGH '(' exp ')'
256 { write_exp_elt_opcode (UNOP_HIGH); }
259 exp : MIN_FUNC '(' type ')'
260 { write_exp_elt_opcode (UNOP_MIN);
261 write_exp_elt_type ($3);
262 write_exp_elt_opcode (UNOP_MIN); }
265 exp : MAX_FUNC '(' type ')'
266 { write_exp_elt_opcode (UNOP_MAX);
267 write_exp_elt_type ($3);
268 write_exp_elt_opcode (UNOP_MAX); }
271 exp : FLOAT_FUNC '(' exp ')'
272 { write_exp_elt_opcode (UNOP_FLOAT); }
275 exp : VAL '(' type ',' exp ')'
276 { write_exp_elt_opcode (BINOP_VAL);
277 write_exp_elt_type ($3);
278 write_exp_elt_opcode (BINOP_VAL); }
281 exp : CHR '(' exp ')'
282 { write_exp_elt_opcode (UNOP_CHR); }
285 exp : ODD '(' exp ')'
286 { write_exp_elt_opcode (UNOP_ODD); }
289 exp : TRUNC '(' exp ')'
290 { write_exp_elt_opcode (UNOP_TRUNC); }
293 exp : TSIZE '(' exp ')'
294 { write_exp_elt_opcode (UNOP_SIZEOF); }
297 exp : SIZE exp %prec UNARY
298 { write_exp_elt_opcode (UNOP_SIZEOF); }
302 exp : INC '(' exp ')'
303 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
306 exp : INC '(' exp ',' exp ')'
307 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
308 write_exp_elt_opcode(BINOP_ADD);
309 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
312 exp : DEC '(' exp ')'
313 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
316 exp : DEC '(' exp ',' exp ')'
317 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
318 write_exp_elt_opcode(BINOP_SUB);
319 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
323 { write_exp_elt_opcode (STRUCTOP_STRUCT);
324 write_exp_string ($3);
325 write_exp_elt_opcode (STRUCTOP_STRUCT); }
332 { error("Sets are not implemented.");}
335 exp : INCL '(' exp ',' exp ')'
336 { error("Sets are not implemented.");}
339 exp : EXCL '(' exp ',' exp ')'
340 { error("Sets are not implemented.");}
343 set : '{' arglist '}'
344 { error("Sets are not implemented.");}
345 | type '{' arglist '}'
346 { error("Sets are not implemented.");}
350 /* Modula-2 array subscript notation [a,b,c...] */
352 /* This function just saves the number of arguments
353 that follow in the list. It is *not* specific to
356 non_empty_arglist ']' %prec DOT
357 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
358 write_exp_elt_longcst ((LONGEST) end_arglist());
359 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
362 exp : exp '[' exp ']'
363 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
367 /* This is to save the value of arglist_len
368 being accumulated by an outer function call. */
369 { start_arglist (); }
370 arglist ')' %prec DOT
371 { write_exp_elt_opcode (OP_FUNCALL);
372 write_exp_elt_longcst ((LONGEST) end_arglist ());
373 write_exp_elt_opcode (OP_FUNCALL); }
383 arglist : arglist ',' exp %prec ABOVE_COMMA
393 : non_empty_arglist ',' exp %prec ABOVE_COMMA
398 exp : '{' type '}' exp %prec UNARY
399 { write_exp_elt_opcode (UNOP_MEMVAL);
400 write_exp_elt_type ($2);
401 write_exp_elt_opcode (UNOP_MEMVAL); }
404 exp : type '(' exp ')' %prec UNARY
405 { write_exp_elt_opcode (UNOP_CAST);
406 write_exp_elt_type ($1);
407 write_exp_elt_opcode (UNOP_CAST); }
414 /* Binary operators in order of decreasing precedence. Note that some
415 of these operators are overloaded! (ie. sets) */
419 { write_exp_elt_opcode (BINOP_REPEAT); }
423 { write_exp_elt_opcode (BINOP_MUL); }
427 { write_exp_elt_opcode (BINOP_DIV); }
431 { write_exp_elt_opcode (BINOP_INTDIV); }
435 { write_exp_elt_opcode (BINOP_REM); }
439 { write_exp_elt_opcode (BINOP_ADD); }
443 { write_exp_elt_opcode (BINOP_SUB); }
447 { write_exp_elt_opcode (BINOP_EQUAL); }
450 exp : exp NOTEQUAL exp
451 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
453 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
457 { write_exp_elt_opcode (BINOP_LEQ); }
461 { write_exp_elt_opcode (BINOP_GEQ); }
465 { write_exp_elt_opcode (BINOP_LESS); }
469 { write_exp_elt_opcode (BINOP_GTR); }
472 exp : exp LOGICAL_AND exp
473 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
477 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
481 { write_exp_elt_opcode (BINOP_ASSIGN); }
488 { write_exp_elt_opcode (OP_BOOL);
489 write_exp_elt_longcst ((LONGEST) $1);
490 write_exp_elt_opcode (OP_BOOL); }
494 { write_exp_elt_opcode (OP_BOOL);
495 write_exp_elt_longcst ((LONGEST) $1);
496 write_exp_elt_opcode (OP_BOOL); }
500 { write_exp_elt_opcode (OP_LONG);
501 write_exp_elt_type (parse_m2_type->builtin_int);
502 write_exp_elt_longcst ((LONGEST) $1);
503 write_exp_elt_opcode (OP_LONG); }
508 write_exp_elt_opcode (OP_LONG);
509 write_exp_elt_type (parse_m2_type->builtin_card);
510 write_exp_elt_longcst ((LONGEST) $1);
511 write_exp_elt_opcode (OP_LONG);
516 { write_exp_elt_opcode (OP_LONG);
517 write_exp_elt_type (parse_m2_type->builtin_char);
518 write_exp_elt_longcst ((LONGEST) $1);
519 write_exp_elt_opcode (OP_LONG); }
524 { write_exp_elt_opcode (OP_DOUBLE);
525 write_exp_elt_type (parse_m2_type->builtin_real);
526 write_exp_elt_dblcst ($1);
527 write_exp_elt_opcode (OP_DOUBLE); }
533 exp : SIZE '(' type ')' %prec UNARY
534 { write_exp_elt_opcode (OP_LONG);
535 write_exp_elt_type (parse_type->builtin_int);
536 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
537 write_exp_elt_opcode (OP_LONG); }
541 { write_exp_elt_opcode (OP_M2_STRING);
542 write_exp_string ($1);
543 write_exp_elt_opcode (OP_M2_STRING); }
546 /* This will be used for extensions later. Like adding modules. */
548 { $$ = SYMBOL_BLOCK_VALUE($1); }
553 = lookup_symbol (copy_name ($1), expression_context_block,
559 /* GDB scope operator */
560 fblock : block COLONCOLON BLOCKNAME
562 = lookup_symbol (copy_name ($3), $1,
564 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
565 error ("No function \"%s\" in specified context.",
571 /* Useful for assigning to PROCEDURE variables */
573 { write_exp_elt_opcode(OP_VAR_VALUE);
574 write_exp_elt_block (NULL);
575 write_exp_elt_sym ($1);
576 write_exp_elt_opcode (OP_VAR_VALUE); }
579 /* GDB internal ($foo) variable */
580 variable: INTERNAL_VAR
583 /* GDB scope operator */
584 variable: block COLONCOLON NAME
585 { struct symbol *sym;
586 sym = lookup_symbol (copy_name ($3), $1,
589 error ("No symbol \"%s\" in specified context.",
592 write_exp_elt_opcode (OP_VAR_VALUE);
593 /* block_found is set by lookup_symbol. */
594 write_exp_elt_block (block_found);
595 write_exp_elt_sym (sym);
596 write_exp_elt_opcode (OP_VAR_VALUE); }
599 /* Base case for variables. */
601 { struct symbol *sym;
602 int is_a_field_of_this;
604 sym = lookup_symbol (copy_name ($1),
605 expression_context_block,
607 &is_a_field_of_this);
610 if (symbol_read_needs_frame (sym))
612 if (innermost_block == 0 ||
613 contained_in (block_found,
615 innermost_block = block_found;
618 write_exp_elt_opcode (OP_VAR_VALUE);
619 /* We want to use the selected frame, not
620 another more inner frame which happens to
621 be in the same block. */
622 write_exp_elt_block (NULL);
623 write_exp_elt_sym (sym);
624 write_exp_elt_opcode (OP_VAR_VALUE);
628 struct minimal_symbol *msymbol;
629 char *arg = copy_name ($1);
632 lookup_minimal_symbol (arg, NULL, NULL);
634 write_exp_msymbol (msymbol);
635 else if (!have_full_symbols () && !have_partial_symbols ())
636 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
638 error ("No symbol \"%s\" in current context.",
646 { $$ = lookup_typename (parse_language, parse_gdbarch,
648 expression_context_block, 0); }
659 return (MAX_OF_TYPE(parse_m2_type->builtin_int) - b) < a;
666 return (MAX_OF_TYPE(parse_m2_type->builtin_card) - b) < a;
670 /* Take care of parsing a number (anything that starts with a digit).
671 Set yylval and return the token type; update lexptr.
672 LEN is the number of characters in it. */
674 /*** Needs some error checking for the float case ***/
684 int base = input_radix;
686 int unsigned_p = number_sign == 1 ? 1 : 0;
693 else if(p[len-1] == 'C' || p[len-1] == 'B')
696 ischar = p[len-1] == 'C';
700 /* Scan the number */
701 for (c = 0; c < len; c++)
703 if (p[c] == '.' && base == 10)
705 /* It's a float since it contains a point. */
706 yylval.dval = atof (p);
710 if (p[c] == '.' && base != 10)
711 error("Floating point numbers must be base 10.");
712 if (base == 10 && (p[c] < '0' || p[c] > '9'))
713 error("Invalid digit \'%c\' in number.",p[c]);
720 if( base == 8 && (c == '8' || c == '9'))
721 error("Invalid digit \'%c\' in octal number.",c);
722 if (c >= '0' && c <= '9')
726 if (base == 16 && c >= 'A' && c <= 'F')
734 if(!unsigned_p && number_sign == 1 && (prevn >= n))
735 unsigned_p=1; /* Try something unsigned */
736 /* Don't do the range check if n==i and i==0, since that special
737 case will give an overflow error. */
738 if(RANGE_CHECK && n!=i && i)
740 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
741 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
742 range_error("Overflow on numeric constant.");
748 if(*p == 'B' || *p == 'C' || *p == 'H')
749 lexptr++; /* Advance past B,C or H */
756 else if ( unsigned_p && number_sign == 1)
761 else if((unsigned_p && (n<0))) {
762 range_error("Overflow on numeric constant -- number too large.");
763 /* But, this can return if range_check == range_warn. */
778 { {'<', '>'}, NOTEQUAL },
779 { {':', '='}, ASSIGN },
782 { {':', ':'}, COLONCOLON },
786 /* Some specific keywords */
793 static struct keyword keytab[] =
796 {"IN", IN },/* Note space after IN */
797 {"AND", LOGICAL_AND},
815 {"FLOAT", FLOAT_FUNC },
821 /* Read one token, getting characters through lexptr. */
823 /* This is where we will check to make sure that the language and the operators used are
837 prev_lexptr = lexptr;
842 /* See if it is a special token of length 2 */
843 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
844 if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
847 return tokentab2[i].token;
850 switch (c = *tokstart)
867 if (paren_depth == 0)
874 if (comma_terminates && paren_depth == 0)
880 /* Might be a floating point number. */
881 if (lexptr[1] >= '0' && lexptr[1] <= '9')
882 break; /* Falls into number code. */
889 /* These are character tokens that appear as-is in the YACC grammar */
912 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
915 c = tokstart[++namelen];
916 if (c >= '0' && c <= '9')
918 c = tokstart[++namelen];
919 if (c >= '0' && c <= '9')
920 c = tokstart[++namelen];
924 error("Unterminated string or character constant.");
925 yylval.sval.ptr = tokstart + 1;
926 yylval.sval.length = namelen - 1;
927 lexptr += namelen + 1;
929 if(namelen == 2) /* Single character */
931 yylval.ulval = tokstart[1];
938 /* Is it a number? */
939 /* Note: We have already dealt with the case of the token '.'.
940 See case '.' above. */
941 if ((c >= '0' && c <= '9'))
944 int got_dot = 0, got_e = 0;
950 if (!got_e && (*p == 'e' || *p == 'E'))
952 else if (!got_dot && *p == '.')
954 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
955 && (*p == '-' || *p == '+'))
956 /* This is the sign of the exponent, not the end of the
959 else if ((*p < '0' || *p > '9') &&
960 (*p < 'A' || *p > 'F') &&
961 (*p != 'H')) /* Modula-2 hexadecimal number */
964 toktype = parse_number (p - tokstart);
965 if (toktype == ERROR)
967 char *err_copy = (char *) alloca (p - tokstart + 1);
969 memcpy (err_copy, tokstart, p - tokstart);
970 err_copy[p - tokstart] = 0;
971 error ("Invalid number \"%s\".", err_copy);
977 if (!(c == '_' || c == '$'
978 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
979 /* We must have come across a bad character (e.g. ';'). */
980 error ("Invalid character '%c' in expression.", c);
982 /* It's a name. See how long it is. */
984 for (c = tokstart[namelen];
985 (c == '_' || c == '$' || (c >= '0' && c <= '9')
986 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
987 c = tokstart[++namelen])
990 /* The token "if" terminates the expression and is NOT
991 removed from the input stream. */
992 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
999 /* Lookup special keywords */
1000 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
1001 if (namelen == strlen (keytab[i].keyw)
1002 && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
1003 return keytab[i].token;
1005 yylval.sval.ptr = tokstart;
1006 yylval.sval.length = namelen;
1008 if (*tokstart == '$')
1010 write_dollar_variable (yylval.sval);
1011 return INTERNAL_VAR;
1014 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1015 functions. If this is not so, then ...
1016 Use token-type TYPENAME for symbols that happen to be defined
1017 currently as names of types; NAME for other symbols.
1018 The caller is not constrained to care about the distinction. */
1022 char *tmp = copy_name (yylval.sval);
1025 if (lookup_partial_symtab (tmp))
1027 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 0);
1028 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1030 if (lookup_typename (parse_language, parse_gdbarch,
1031 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);