1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986-2014 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. */
42 #include "expression.h"
45 #include "parser-defs.h"
47 #include "bfd.h" /* Required by objfiles.h. */
48 #include "symfile.h" /* Required by objfiles.h. */
49 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
52 #define parse_type builtin_type (parse_gdbarch)
53 #define parse_m2_type builtin_m2_type (parse_gdbarch)
55 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
56 as well as gratuitiously global symbol names, so we can have multiple
57 yacc generated parsers in gdb. Note that these are only the variables
58 produced by yacc. If other parser generators (bison, byacc, etc) produce
59 additional global names that conflict at link time, then those parser
60 generators need to be fixed instead of adding those names to this list. */
62 #define yymaxdepth m2_maxdepth
63 #define yyparse m2_parse
65 #define yyerror m2_error
66 #define yylval m2_lval
67 #define yychar m2_char
68 #define yydebug m2_debug
69 #define yypact m2_pact
76 #define yyexca m2_exca
77 #define yyerrflag m2_errflag
78 #define yynerrs m2_nerrs
83 #define yystate m2_state
88 #define yylloc m2_lloc
89 #define yyreds m2_reds /* With YYDEBUG defined */
90 #define yytoks m2_toks /* With YYDEBUG defined */
91 #define yyname m2_name /* With YYDEBUG defined */
92 #define yyrule m2_rule /* With YYDEBUG defined */
93 #define yylhs m2_yylhs
94 #define yylen m2_yylen
95 #define yydefred m2_yydefred
96 #define yydgoto m2_yydgoto
97 #define yysindex m2_yysindex
98 #define yyrindex m2_yyrindex
99 #define yygindex m2_yygindex
100 #define yytable m2_yytable
101 #define yycheck m2_yycheck
103 #define yysslim m2_yysslim
104 #define yyssp m2_yyssp
105 #define yystacksize m2_yystacksize
107 #define yyvsp m2_yyvsp
110 #define YYDEBUG 1 /* Default to yydebug support */
113 #define YYFPRINTF parser_fprintf
117 static int yylex (void);
119 void yyerror (char *);
121 static int parse_number (int);
123 /* The sign of the number being parsed. */
124 static int number_sign = 1;
128 /* Although the yacc "value" of an expression is not used,
129 since the result is stored in the structure being created,
130 other node types do have values. */
142 enum exp_opcode opcode;
143 struct internalvar *ivar;
149 %type <voidval> exp type_exp start set
150 %type <voidval> variable
155 %token <lval> INT HEX ERROR
156 %token <ulval> UINT M2_TRUE M2_FALSE CHAR
159 /* Both NAME and TYPENAME tokens represent symbols in the input,
160 and both convey their data as strings.
161 But a TYPENAME is a string that happens to be defined as a typedef
162 or builtin type name (such as int or char)
163 and a NAME is any other symbol.
165 Contexts where this distinction is not important can use the
166 nonterminal "name", which matches either NAME or TYPENAME. */
169 %token <sval> NAME BLOCKNAME IDENT VARNAME
170 %token <sval> TYPENAME
172 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
174 %token INC DEC INCL EXCL
176 /* The GDB scope operator */
179 %token <voidval> INTERNAL_VAR
185 %left '<' '>' LEQ GEQ '=' NOTEQUAL '#' IN
187 %left LOGICAL_AND '&'
190 %left '*' '/' DIV MOD
192 %right '^' DOT '[' '('
195 /* This is not an actual token ; it is used for precedence.
207 { write_exp_elt_opcode(OP_TYPE);
208 write_exp_elt_type($1);
209 write_exp_elt_opcode(OP_TYPE);
215 exp : exp '^' %prec UNARY
216 { write_exp_elt_opcode (UNOP_IND); }
220 { number_sign = -1; }
223 write_exp_elt_opcode (UNOP_NEG); }
226 exp : '+' exp %prec UNARY
227 { write_exp_elt_opcode(UNOP_PLUS); }
230 exp : not_exp exp %prec UNARY
231 { write_exp_elt_opcode (UNOP_LOGICAL_NOT); }
238 exp : CAP '(' exp ')'
239 { write_exp_elt_opcode (UNOP_CAP); }
242 exp : ORD '(' exp ')'
243 { write_exp_elt_opcode (UNOP_ORD); }
246 exp : ABS '(' exp ')'
247 { write_exp_elt_opcode (UNOP_ABS); }
250 exp : HIGH '(' exp ')'
251 { write_exp_elt_opcode (UNOP_HIGH); }
254 exp : MIN_FUNC '(' type ')'
255 { write_exp_elt_opcode (UNOP_MIN);
256 write_exp_elt_type ($3);
257 write_exp_elt_opcode (UNOP_MIN); }
260 exp : MAX_FUNC '(' type ')'
261 { write_exp_elt_opcode (UNOP_MAX);
262 write_exp_elt_type ($3);
263 write_exp_elt_opcode (UNOP_MAX); }
266 exp : FLOAT_FUNC '(' exp ')'
267 { write_exp_elt_opcode (UNOP_FLOAT); }
270 exp : VAL '(' type ',' exp ')'
271 { write_exp_elt_opcode (BINOP_VAL);
272 write_exp_elt_type ($3);
273 write_exp_elt_opcode (BINOP_VAL); }
276 exp : CHR '(' exp ')'
277 { write_exp_elt_opcode (UNOP_CHR); }
280 exp : ODD '(' exp ')'
281 { write_exp_elt_opcode (UNOP_ODD); }
284 exp : TRUNC '(' exp ')'
285 { write_exp_elt_opcode (UNOP_TRUNC); }
288 exp : TSIZE '(' exp ')'
289 { write_exp_elt_opcode (UNOP_SIZEOF); }
292 exp : SIZE exp %prec UNARY
293 { write_exp_elt_opcode (UNOP_SIZEOF); }
297 exp : INC '(' exp ')'
298 { write_exp_elt_opcode(UNOP_PREINCREMENT); }
301 exp : INC '(' exp ',' exp ')'
302 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
303 write_exp_elt_opcode(BINOP_ADD);
304 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
307 exp : DEC '(' exp ')'
308 { write_exp_elt_opcode(UNOP_PREDECREMENT);}
311 exp : DEC '(' exp ',' exp ')'
312 { write_exp_elt_opcode(BINOP_ASSIGN_MODIFY);
313 write_exp_elt_opcode(BINOP_SUB);
314 write_exp_elt_opcode(BINOP_ASSIGN_MODIFY); }
318 { write_exp_elt_opcode (STRUCTOP_STRUCT);
319 write_exp_string ($3);
320 write_exp_elt_opcode (STRUCTOP_STRUCT); }
327 { error (_("Sets are not implemented."));}
330 exp : INCL '(' exp ',' exp ')'
331 { error (_("Sets are not implemented."));}
334 exp : EXCL '(' exp ',' exp ')'
335 { error (_("Sets are not implemented."));}
338 set : '{' arglist '}'
339 { error (_("Sets are not implemented."));}
340 | type '{' arglist '}'
341 { error (_("Sets are not implemented."));}
345 /* Modula-2 array subscript notation [a,b,c...] */
347 /* This function just saves the number of arguments
348 that follow in the list. It is *not* specific to
351 non_empty_arglist ']' %prec DOT
352 { write_exp_elt_opcode (MULTI_SUBSCRIPT);
353 write_exp_elt_longcst ((LONGEST) end_arglist());
354 write_exp_elt_opcode (MULTI_SUBSCRIPT); }
357 exp : exp '[' exp ']'
358 { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
362 /* This is to save the value of arglist_len
363 being accumulated by an outer function call. */
364 { start_arglist (); }
365 arglist ')' %prec DOT
366 { write_exp_elt_opcode (OP_FUNCALL);
367 write_exp_elt_longcst ((LONGEST) end_arglist ());
368 write_exp_elt_opcode (OP_FUNCALL); }
378 arglist : arglist ',' exp %prec ABOVE_COMMA
388 : non_empty_arglist ',' exp %prec ABOVE_COMMA
393 exp : '{' type '}' exp %prec UNARY
394 { write_exp_elt_opcode (UNOP_MEMVAL);
395 write_exp_elt_type ($2);
396 write_exp_elt_opcode (UNOP_MEMVAL); }
399 exp : type '(' exp ')' %prec UNARY
400 { write_exp_elt_opcode (UNOP_CAST);
401 write_exp_elt_type ($1);
402 write_exp_elt_opcode (UNOP_CAST); }
409 /* Binary operators in order of decreasing precedence. Note that some
410 of these operators are overloaded! (ie. sets) */
414 { write_exp_elt_opcode (BINOP_REPEAT); }
418 { write_exp_elt_opcode (BINOP_MUL); }
422 { write_exp_elt_opcode (BINOP_DIV); }
426 { write_exp_elt_opcode (BINOP_INTDIV); }
430 { write_exp_elt_opcode (BINOP_REM); }
434 { write_exp_elt_opcode (BINOP_ADD); }
438 { write_exp_elt_opcode (BINOP_SUB); }
442 { write_exp_elt_opcode (BINOP_EQUAL); }
445 exp : exp NOTEQUAL exp
446 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
448 { write_exp_elt_opcode (BINOP_NOTEQUAL); }
452 { write_exp_elt_opcode (BINOP_LEQ); }
456 { write_exp_elt_opcode (BINOP_GEQ); }
460 { write_exp_elt_opcode (BINOP_LESS); }
464 { write_exp_elt_opcode (BINOP_GTR); }
467 exp : exp LOGICAL_AND exp
468 { write_exp_elt_opcode (BINOP_LOGICAL_AND); }
472 { write_exp_elt_opcode (BINOP_LOGICAL_OR); }
476 { write_exp_elt_opcode (BINOP_ASSIGN); }
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_BOOL);
490 write_exp_elt_longcst ((LONGEST) $1);
491 write_exp_elt_opcode (OP_BOOL); }
495 { write_exp_elt_opcode (OP_LONG);
496 write_exp_elt_type (parse_m2_type->builtin_int);
497 write_exp_elt_longcst ((LONGEST) $1);
498 write_exp_elt_opcode (OP_LONG); }
503 write_exp_elt_opcode (OP_LONG);
504 write_exp_elt_type (parse_m2_type->builtin_card);
505 write_exp_elt_longcst ((LONGEST) $1);
506 write_exp_elt_opcode (OP_LONG);
511 { write_exp_elt_opcode (OP_LONG);
512 write_exp_elt_type (parse_m2_type->builtin_char);
513 write_exp_elt_longcst ((LONGEST) $1);
514 write_exp_elt_opcode (OP_LONG); }
519 { write_exp_elt_opcode (OP_DOUBLE);
520 write_exp_elt_type (parse_m2_type->builtin_real);
521 write_exp_elt_dblcst ($1);
522 write_exp_elt_opcode (OP_DOUBLE); }
528 exp : SIZE '(' type ')' %prec UNARY
529 { write_exp_elt_opcode (OP_LONG);
530 write_exp_elt_type (parse_type->builtin_int);
531 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
532 write_exp_elt_opcode (OP_LONG); }
536 { write_exp_elt_opcode (OP_M2_STRING);
537 write_exp_string ($1);
538 write_exp_elt_opcode (OP_M2_STRING); }
541 /* This will be used for extensions later. Like adding modules. */
543 { $$ = SYMBOL_BLOCK_VALUE($1); }
548 = lookup_symbol (copy_name ($1), expression_context_block,
554 /* GDB scope operator */
555 fblock : block COLONCOLON BLOCKNAME
557 = lookup_symbol (copy_name ($3), $1,
559 if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
560 error (_("No function \"%s\" in specified context."),
566 /* Useful for assigning to PROCEDURE variables */
568 { write_exp_elt_opcode(OP_VAR_VALUE);
569 write_exp_elt_block (NULL);
570 write_exp_elt_sym ($1);
571 write_exp_elt_opcode (OP_VAR_VALUE); }
574 /* GDB internal ($foo) variable */
575 variable: INTERNAL_VAR
578 /* GDB scope operator */
579 variable: block COLONCOLON NAME
580 { struct symbol *sym;
581 sym = lookup_symbol (copy_name ($3), $1,
584 error (_("No symbol \"%s\" in specified context."),
586 if (symbol_read_needs_frame (sym))
588 if (innermost_block == 0
589 || contained_in (block_found,
591 innermost_block = block_found;
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 struct field_of_this_result 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 bound_minimal_symbol msymbol;
631 char *arg = copy_name ($1);
634 lookup_bound_minimal_symbol (arg);
635 if (msymbol.minsym != 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 (parse_language, parse_gdbarch,
650 expression_context_block, 0); }
656 /* Take care of parsing a number (anything that starts with a digit).
657 Set yylval and return the token type; update lexptr.
658 LEN is the number of characters in it. */
660 /*** Needs some error checking for the float case ***/
663 parse_number (int olen)
665 const char *p = lexptr;
669 int base = input_radix;
671 int unsigned_p = number_sign == 1 ? 1 : 0;
678 else if(p[len-1] == 'C' || p[len-1] == 'B')
681 ischar = p[len-1] == 'C';
685 /* Scan the number */
686 for (c = 0; c < len; c++)
688 if (p[c] == '.' && base == 10)
690 /* It's a float since it contains a point. */
691 yylval.dval = atof (p);
695 if (p[c] == '.' && base != 10)
696 error (_("Floating point numbers must be base 10."));
697 if (base == 10 && (p[c] < '0' || p[c] > '9'))
698 error (_("Invalid digit \'%c\' in number."),p[c]);
705 if( base == 8 && (c == '8' || c == '9'))
706 error (_("Invalid digit \'%c\' in octal number."),c);
707 if (c >= '0' && c <= '9')
711 if (base == 16 && c >= 'A' && c <= 'F')
719 if(!unsigned_p && number_sign == 1 && (prevn >= n))
720 unsigned_p=1; /* Try something unsigned */
721 /* Don't do the range check if n==i and i==0, since that special
722 case will give an overflow error. */
723 if(RANGE_CHECK && n!=i && i)
725 if((unsigned_p && (unsigned)prevn >= (unsigned)n) ||
726 ((!unsigned_p && number_sign==-1) && -prevn <= -n))
727 range_error (_("Overflow on numeric constant."));
733 if(*p == 'B' || *p == 'C' || *p == 'H')
734 lexptr++; /* Advance past B,C or H */
741 else if ( unsigned_p && number_sign == 1)
746 else if((unsigned_p && (n<0))) {
747 range_error (_("Overflow on numeric constant -- number too large."));
748 /* But, this can return if range_check == range_warn. */
763 { {'<', '>'}, NOTEQUAL },
764 { {':', '='}, ASSIGN },
767 { {':', ':'}, COLONCOLON },
771 /* Some specific keywords */
778 static struct keyword keytab[] =
781 {"IN", IN },/* Note space after IN */
782 {"AND", LOGICAL_AND},
800 {"FLOAT", FLOAT_FUNC },
806 /* Read one token, getting characters through lexptr. */
808 /* This is where we will check to make sure that the language and the operators used are
817 const char *tokstart;
822 prev_lexptr = lexptr;
827 /* See if it is a special token of length 2 */
828 for( i = 0 ; i < (int) (sizeof tokentab2 / sizeof tokentab2[0]) ; i++)
829 if (strncmp (tokentab2[i].name, tokstart, 2) == 0)
832 return tokentab2[i].token;
835 switch (c = *tokstart)
852 if (paren_depth == 0)
859 if (comma_terminates && paren_depth == 0)
865 /* Might be a floating point number. */
866 if (lexptr[1] >= '0' && lexptr[1] <= '9')
867 break; /* Falls into number code. */
874 /* These are character tokens that appear as-is in the YACC grammar */
897 for (namelen = 1; (c = tokstart[namelen]) != quote && c != '\0'; namelen++)
900 c = tokstart[++namelen];
901 if (c >= '0' && c <= '9')
903 c = tokstart[++namelen];
904 if (c >= '0' && c <= '9')
905 c = tokstart[++namelen];
909 error (_("Unterminated string or character constant."));
910 yylval.sval.ptr = tokstart + 1;
911 yylval.sval.length = namelen - 1;
912 lexptr += namelen + 1;
914 if(namelen == 2) /* Single character */
916 yylval.ulval = tokstart[1];
923 /* Is it a number? */
924 /* Note: We have already dealt with the case of the token '.'.
925 See case '.' above. */
926 if ((c >= '0' && c <= '9'))
929 int got_dot = 0, got_e = 0;
930 const char *p = tokstart;
935 if (!got_e && (*p == 'e' || *p == 'E'))
937 else if (!got_dot && *p == '.')
939 else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
940 && (*p == '-' || *p == '+'))
941 /* This is the sign of the exponent, not the end of the
944 else if ((*p < '0' || *p > '9') &&
945 (*p < 'A' || *p > 'F') &&
946 (*p != 'H')) /* Modula-2 hexadecimal number */
949 toktype = parse_number (p - tokstart);
950 if (toktype == ERROR)
952 char *err_copy = (char *) alloca (p - tokstart + 1);
954 memcpy (err_copy, tokstart, p - tokstart);
955 err_copy[p - tokstart] = 0;
956 error (_("Invalid number \"%s\"."), err_copy);
962 if (!(c == '_' || c == '$'
963 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
964 /* We must have come across a bad character (e.g. ';'). */
965 error (_("Invalid character '%c' in expression."), c);
967 /* It's a name. See how long it is. */
969 for (c = tokstart[namelen];
970 (c == '_' || c == '$' || (c >= '0' && c <= '9')
971 || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
972 c = tokstart[++namelen])
975 /* The token "if" terminates the expression and is NOT
976 removed from the input stream. */
977 if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
984 /* Lookup special keywords */
985 for(i = 0 ; i < (int) (sizeof(keytab) / sizeof(keytab[0])) ; i++)
986 if (namelen == strlen (keytab[i].keyw)
987 && strncmp (tokstart, keytab[i].keyw, namelen) == 0)
988 return keytab[i].token;
990 yylval.sval.ptr = tokstart;
991 yylval.sval.length = namelen;
993 if (*tokstart == '$')
995 write_dollar_variable (yylval.sval);
999 /* Use token-type BLOCKNAME for symbols that happen to be defined as
1000 functions. If this is not so, then ...
1001 Use token-type TYPENAME for symbols that happen to be defined
1002 currently as names of types; NAME for other symbols.
1003 The caller is not constrained to care about the distinction. */
1007 char *tmp = copy_name (yylval.sval);
1010 if (lookup_symtab (tmp))
1012 sym = lookup_symbol (tmp, expression_context_block, VAR_DOMAIN, 0);
1013 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1015 if (lookup_typename (parse_language, parse_gdbarch,
1016 copy_name (yylval.sval), expression_context_block, 1))
1021 switch(SYMBOL_CLASS (sym))
1027 case LOC_REGPARM_ADDR:
1030 case LOC_CONST_BYTES:
1031 case LOC_OPTIMIZED_OUT:
1042 error (_("internal: Undefined class in m2lex()"));
1045 case LOC_UNRESOLVED:
1046 error (_("internal: Unforseen case in m2lex()"));
1049 error (_("unhandled token in m2lex()"));
1055 /* Built-in BOOLEAN type. This is sort of a hack. */
1056 if (strncmp (tokstart, "TRUE", 4) == 0)
1061 else if (strncmp (tokstart, "FALSE", 5) == 0)
1068 /* Must be another type of name... */
1077 lexptr = prev_lexptr;
1079 error (_("A %s in expression, near `%s'."), (msg ? msg : "error"), lexptr);