1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986, 1989-1996, 1999-2000, 2007-2012 Free Software
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 *);
116 static int parse_number (int);
118 /* The sign of the number being parsed. */
119 static int number_sign = 1;
123 /* Although the yacc "value" of an expression is not used,
124 since the result is stored in the structure being created,
125 other node types do have values. */
137 enum exp_opcode opcode;
138 struct internalvar *ivar;
144 %type <voidval> exp type_exp start set
145 %type <voidval> variable
150 %token <lval> INT HEX ERROR
151 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
154 /* Both NAME and TYPENAME tokens represent symbols in the input,
155 and both convey their data as strings.
156 But a TYPENAME is a string that happens to be defined as a typedef
157 or builtin type name (such as int or char)
158 and a NAME is any other symbol.
160 Contexts where this distinction is not important can use the
161 nonterminal "name", which matches either NAME or TYPENAME. */
164 %token <sval> NAME BLOCKNAME IDENT VARNAME
165 %token <sval> TYPENAME
167 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
169 %token INC DEC INCL EXCL
171 /* The GDB scope operator */
174 %token <voidval> INTERNAL_VAR
180 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
182 %left LOGICAL_AND '&'
185 %left '*' '/' DIV MOD
187 %right '^' DOT '[' '('
190 /* This is not an actual token ; it is used for precedence.
202 { write_exp_elt_opcode(OP_TYPE);
203 write_exp_elt_type($1);
204 write_exp_elt_opcode(OP_TYPE);
210 exp : exp '^' %prec UNARY
211 { write_exp_elt_opcode (UNOP_IND); }
215 { number_sign = -1; }
218 write_exp_elt_opcode (UNOP_NEG); }
221 exp : '+' exp %prec UNARY
222 { write_exp_elt_opcode(UNOP_PLUS); }
225 exp : not_exp exp %prec UNARY
226 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
233 exp : CAP '(' exp ')'
234 { write_exp_elt_opcode (UNOP_CAP); }
237 exp : ORD '(' exp ')'
238 { write_exp_elt_opcode (UNOP_ORD); }
241 exp : ABS '(' exp ')'
242 { write_exp_elt_opcode (UNOP_ABS); }
245 exp : HIGH '(' exp ')'
246 { write_exp_elt_opcode (UNOP_HIGH); }
249 exp : MIN_FUNC '(' type ')'
250 { write_exp_elt_opcode (UNOP_MIN);
251 write_exp_elt_type ($3);
252 write_exp_elt_opcode (UNOP_MIN); }
255 exp : MAX_FUNC '(' type ')'
256 { write_exp_elt_opcode (UNOP_MAX);
257 write_exp_elt_type ($3);
258 write_exp_elt_opcode (UNOP_MAX); }
261 exp : FLOAT_FUNC '(' exp ')'
262 { write_exp_elt_opcode (UNOP_FLOAT); }
265 exp : VAL '(' type ',' exp ')'
266 { write_exp_elt_opcode (BINOP_VAL);
267 write_exp_elt_type ($3);
268 write_exp_elt_opcode (BINOP_VAL); }
271 exp : CHR '(' exp ')'
272 { write_exp_elt_opcode (UNOP_CHR); }
275 exp : ODD '(' exp ')'
276 { write_exp_elt_opcode (UNOP_ODD); }
279 exp : TRUNC '(' exp ')'
280 { write_exp_elt_opcode (UNOP_TRUNC); }
283 exp : TSIZE '(' exp ')'
284 { write_exp_elt_opcode (UNOP_SIZEOF); }
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."));}
333 set : '{' arglist '}'
334 { error (_("Sets are not implemented."));}
335 | type '{' arglist '}'
336 { error (_("Sets are not implemented."));}
340 /* Modula-2 array subscript notation [a,b,c...] */
342 /* This function just saves the number of arguments
343 that follow in the list. It is *not* specific to
346 non_empty_arglist ']' %prec DOT
347 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
348 write_exp_elt_longcst ((LONGEST) end_arglist());
349 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
352 exp : exp '[' exp ']'
353 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
357 /* This is to save the value of arglist_len
358 being accumulated by an outer function call. */
359 { start_arglist (); }
360 arglist ')' %prec DOT
361 { write_exp_elt_opcode (OP_FUNCALL);
362 write_exp_elt_longcst ((LONGEST) end_arglist ());
363 write_exp_elt_opcode (OP_FUNCALL); }
373 arglist : arglist ',' exp %prec ABOVE_COMMA
383 : non_empty_arglist ',' exp %prec ABOVE_COMMA
388 exp : '{' type '}' exp %prec UNARY
389 { write_exp_elt_opcode (UNOP_MEMVAL);
390 write_exp_elt_type ($2);
391 write_exp_elt_opcode (UNOP_MEMVAL); }
394 exp : type '(' exp ')' %prec UNARY
395 { write_exp_elt_opcode (UNOP_CAST);
396 write_exp_elt_type ($1);
397 write_exp_elt_opcode (UNOP_CAST); }
404 /* Binary operators in order of decreasing precedence. Note that some
405 of these operators are overloaded! (ie. sets) */
409 { write_exp_elt_opcode (BINOP_REPEAT); }
413 { write_exp_elt_opcode (BINOP_MUL); }
417 { write_exp_elt_opcode (BINOP_DIV); }
421 { write_exp_elt_opcode (BINOP_INTDIV); }
425 { write_exp_elt_opcode (BINOP_REM); }
429 { write_exp_elt_opcode (BINOP_ADD); }
433 { write_exp_elt_opcode (BINOP_SUB); }
437 { write_exp_elt_opcode (BINOP_EQUAL); }
440 exp : exp NOTEQUAL exp
441 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
443 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
447 { write_exp_elt_opcode (BINOP_LEQ); }
451 { write_exp_elt_opcode (BINOP_GEQ); }
455 { write_exp_elt_opcode (BINOP_LESS); }
459 { write_exp_elt_opcode (BINOP_GTR); }
462 exp : exp LOGICAL_AND exp
463 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
467 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
471 { write_exp_elt_opcode (BINOP_ASSIGN); }
478 { write_exp_elt_opcode (OP_BOOL);
479 write_exp_elt_longcst ((LONGEST) $1);
480 write_exp_elt_opcode (OP_BOOL); }
484 { write_exp_elt_opcode (OP_BOOL);
485 write_exp_elt_longcst ((LONGEST) $1);
486 write_exp_elt_opcode (OP_BOOL); }
490 { write_exp_elt_opcode (OP_LONG);
491 write_exp_elt_type (parse_m2_type->builtin_int);
492 write_exp_elt_longcst ((LONGEST) $1);
493 write_exp_elt_opcode (OP_LONG); }
498 write_exp_elt_opcode (OP_LONG);
499 write_exp_elt_type (parse_m2_type->builtin_card);
500 write_exp_elt_longcst ((LONGEST) $1);
501 write_exp_elt_opcode (OP_LONG);
506 { write_exp_elt_opcode (OP_LONG);
507 write_exp_elt_type (parse_m2_type->builtin_char);
508 write_exp_elt_longcst ((LONGEST) $1);
509 write_exp_elt_opcode (OP_LONG); }
514 { write_exp_elt_opcode (OP_DOUBLE);
515 write_exp_elt_type (parse_m2_type->builtin_real);
516 write_exp_elt_dblcst ($1);
517 write_exp_elt_opcode (OP_DOUBLE); }
523 exp : SIZE '(' type ')' %prec UNARY
524 { write_exp_elt_opcode (OP_LONG);
525 write_exp_elt_type (parse_type->builtin_int);
526 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
527 write_exp_elt_opcode (OP_LONG); }
531 { write_exp_elt_opcode (OP_M2_STRING);
532 write_exp_string ($1);
533 write_exp_elt_opcode (OP_M2_STRING); }
536 /* This will be used for extensions later. Like adding modules. */
538 { $$ = SYMBOL_BLOCK_VALUE($1); }
543 = lookup_symbol (copy_name ($1), expression_context_block,
549 /* GDB scope operator */
550 fblock : block COLONCOLON BLOCKNAME
552 = lookup_symbol (copy_name ($3), $1,
554 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
555 error (_("No function \"%s\" in specified context."),
561 /* Useful for assigning to PROCEDURE variables */
563 { write_exp_elt_opcode(OP_VAR_VALUE);
564 write_exp_elt_block (NULL);
565 write_exp_elt_sym ($1);
566 write_exp_elt_opcode (OP_VAR_VALUE); }
569 /* GDB internal ($foo) variable */
570 variable: INTERNAL_VAR
573 /* GDB scope operator */
574 variable: block COLONCOLON NAME
575 { struct symbol *sym;
576 sym = lookup_symbol (copy_name ($3), $1,
579 error (_("No symbol \"%s\" in specified context."),
581 if (symbol_read_needs_frame (sym))
583 if (innermost_block == 0
584 || contained_in (block_found,
586 innermost_block = block_found;
589 write_exp_elt_opcode (OP_VAR_VALUE);
590 /* block_found is set by lookup_symbol. */
591 write_exp_elt_block (block_found);
592 write_exp_elt_sym (sym);
593 write_exp_elt_opcode (OP_VAR_VALUE); }
596 /* Base case for variables. */
598 { struct symbol *sym;
599 int is_a_field_of_this;
601 sym = lookup_symbol (copy_name ($1),
602 expression_context_block,
604 &is_a_field_of_this);
607 if (symbol_read_needs_frame (sym))
609 if (innermost_block == 0 ||
610 contained_in (block_found,
612 innermost_block = block_found;
615 write_exp_elt_opcode (OP_VAR_VALUE);
616 /* We want to use the selected frame, not
617 another more inner frame which happens to
618 be in the same block. */
619 write_exp_elt_block (NULL);
620 write_exp_elt_sym (sym);
621 write_exp_elt_opcode (OP_VAR_VALUE);
625 struct minimal_symbol *msymbol;
626 char *arg = copy_name ($1);
629 lookup_minimal_symbol (arg, NULL, NULL);
631 write_exp_msymbol (msymbol);
632 else if (!have_full_symbols () && !have_partial_symbols ())
633 error (_("No symbol table is loaded. Use the \"symbol-file\" command."));
635 error (_("No symbol \"%s\" in current context."),
643 { $$ = lookup_typename (parse_language, parse_gdbarch,
645 expression_context_block, 0); }
651 /* Take care of parsing a number (anything that starts with a digit).
652 Set yylval and return the token type; update lexptr.
653 LEN is the number of characters in it. */
655 /*** Needs some error checking for the float case ***/
658 parse_number (int olen)
664 int base = input_radix;
666 int unsigned_p = number_sign == 1 ? 1 : 0;
673 else if(p[len-1] == 'C' || p[len-1] == 'B')
676 ischar = p[len-1] == 'C';
680 /* Scan the number */
681 for (c = 0; c < len; c++)
683 if (p[c] == '.' && base == 10)
685 /* It's a float since it contains a point. */
686 yylval.dval = atof (p);
690 if (p[c] == '.' && base != 10)
691 error (_("Floating point numbers must be base 10."));
692 if (base == 10 && (p[c] < '0' || p[c] > '9'))
693 error (_("Invalid digit \'%c\' in number."),p[c]);
700 if( base == 8 && (c == '8' || c == '9'))
701 error (_("Invalid digit \'%c\' in octal number."),c);
702 if (c >= '0' && c <= '9')
706 if (base == 16 && c >= 'A' && c <= 'F')
714 if(!unsigned_p && number_sign == 1 && (prevn >= n))
715 unsigned_p=1; /* Try something unsigned */
716 /* Don't do the range check if n==i and i==0, since that special
717 case will give an overflow error. */
718 if(RANGE_CHECK && n!=i && i)
720 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
721 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
722 range_error (_("Overflow on numeric constant."));
728 if(*p == 'B' || *p == 'C' || *p == 'H')
729 lexptr++; /* Advance past B,C or H */
736 else if ( unsigned_p && number_sign == 1)
741 else if((unsigned_p && (n<0))) {
742 range_error (_("Overflow on numeric constant -- number too large."));
743 /* But, this can return if range_check == range_warn. */
758 { {'<', '>'}, NOTEQUAL },
759 { {':', '='}, ASSIGN },
762 { {':', ':'}, COLONCOLON },
766 /* Some specific keywords */
773 static struct keyword keytab[] =
776 {"IN", IN },/* Note space after IN */
777 {"AND", LOGICAL_AND},
795 {"FLOAT", FLOAT_FUNC },
801 /* Read one token, getting characters through lexptr. */
803 /* This is where we will check to make sure that the language and the operators used are
817 prev_lexptr = lexptr;
822 /* See if it is a special token of length 2 */
823 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
824 if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
827 return tokentab2[i].token;
830 switch (c = *tokstart)
847 if (paren_depth == 0)
854 if (comma_terminates && paren_depth == 0)
860 /* Might be a floating point number. */
861 if (lexptr[1] >= '0' && lexptr[1] <= '9')
862 break; /* Falls into number code. */
869 /* These are character tokens that appear as-is in the YACC grammar */
892 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
895 c = tokstart[++namelen];
896 if (c >= '0' && c <= '9')
898 c = tokstart[++namelen];
899 if (c >= '0' && c <= '9')
900 c = tokstart[++namelen];
904 error (_("Unterminated string or character constant."));
905 yylval.sval.ptr = tokstart + 1;
906 yylval.sval.length = namelen - 1;
907 lexptr += namelen + 1;
909 if(namelen == 2) /* Single character */
911 yylval.ulval = tokstart[1];
918 /* Is it a number? */
919 /* Note: We have already dealt with the case of the token '.'.
920 See case '.' above. */
921 if ((c >= '0' && c <= '9'))
924 int got_dot = 0, got_e = 0;
930 if (!got_e && (*p == 'e' || *p == 'E'))
932 else if (!got_dot && *p == '.')
934 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
935 && (*p == '-' || *p == '+'))
936 /* This is the sign of the exponent, not the end of the
939 else if ((*p < '0' || *p > '9') &&
940 (*p < 'A' || *p > 'F') &&
941 (*p != 'H')) /* Modula-2 hexadecimal number */
944 toktype = parse_number (p - tokstart);
945 if (toktype == ERROR)
947 char *err_copy = (char *) alloca (p - tokstart + 1);
949 memcpy (err_copy, tokstart, p - tokstart);
950 err_copy[p - tokstart] = 0;
951 error (_("Invalid number \"%s\"."), err_copy);
957 if (!(c == '_' || c == '$'
958 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
959 /* We must have come across a bad character (e.g. ';'). */
960 error (_("Invalid character '%c' in expression."), c);
962 /* It's a name. See how long it is. */
964 for (c = tokstart[namelen];
965 (c == '_' || c == '$' || (c >= '0' && c <= '9')
966 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
967 c = tokstart[++namelen])
970 /* The token "if" terminates the expression and is NOT
971 removed from the input stream. */
972 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
979 /* Lookup special keywords */
980 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
981 if (namelen == strlen (keytab[i].keyw)
982 && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
983 return keytab[i].token;
985 yylval.sval.ptr = tokstart;
986 yylval.sval.length = namelen;
988 if (*tokstart == '$')
990 write_dollar_variable (yylval.sval);
994 /* Use token-type BLOCKNAME for symbols that happen to be defined as
995 functions. If this is not so, then ...
996 Use token-type TYPENAME for symbols that happen to be defined
997 currently as names of types; NAME for other symbols.
998 The caller is not constrained to care about the distinction. */
1002 char *tmp = copy_name (yylval.sval);
1005 if (lookup_symtab (tmp))
1007 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 0);
1008 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1010 if (lookup_typename (parse_language, parse_gdbarch,
1011 copy_name (yylval.sval), expression_context_block, 1))
1016 switch(SYMBOL_CLASS (sym))
1022 case LOC_REGPARM_ADDR:
1025 case LOC_CONST_BYTES:
1026 case LOC_OPTIMIZED_OUT:
1037 error (_("internal: Undefined class in m2lex()"));
1040 case LOC_UNRESOLVED:
1041 error (_("internal: Unforseen case in m2lex()"));
1044 error (_("unhandled token in m2lex()"));
1050 /* Built-in BOOLEAN type. This is sort of a hack. */
1051 if (strncmp (tokstart, "TRUE", 4) == 0)
1056 else if (strncmp (tokstart, "FALSE", 5) == 0)
1063 /* Must be another type of name... */
1072 lexptr = prev_lexptr;
1074 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);