1 /* YACC grammar for Modula-2 expressions, for GDB.
2 Copyright (C) 1986-2023 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"
49 #define parse_type(ps) builtin_type (ps->gdbarch ())
50 #define parse_m2_type(ps) builtin_m2_type (ps->gdbarch ())
52 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
54 #define GDB_YY_REMAP_PREFIX m2_
57 /* The state of the parser, used internally when we are parsing the
60 static struct parser_state
*pstate
= NULL
;
64 static int yylex (void);
66 static void yyerror (const char *);
68 static int parse_number
(int);
70 /* The sign of the number being parsed. */
71 static int number_sign
= 1;
76 /* Although the yacc "value" of an expression is not used,
77 since the result is stored in the structure being created,
78 other node types do have values. */
89 const struct block
*bval
;
90 enum exp_opcode opcode
;
91 struct internalvar
*ivar
;
97 %type
<voidval
> exp type_exp start set
98 %type
<voidval
> variable
103 %token
<lval
> INT HEX ERROR
104 %token
<ulval
> UINT M2_TRUE M2_FALSE CHAR
107 /* Both NAME and TYPENAME tokens represent symbols in the input,
108 and both convey their data as strings.
109 But a TYPENAME is a string that happens to be defined as a typedef
110 or builtin type name (such as int or char)
111 and a NAME is any other symbol.
113 Contexts where this distinction is not important can use the
114 nonterminal "name", which matches either NAME or TYPENAME. */
117 %token
<sval
> NAME BLOCKNAME IDENT VARNAME
118 %token
<sval
> TYPENAME
120 %token SIZE CAP ORD HIGH ABS MIN_FUNC MAX_FUNC FLOAT_FUNC VAL CHR ODD TRUNC
122 %token INC DEC INCL EXCL
124 /* The GDB scope operator */
127 %token
<sval
> DOLLAR_VARIABLE
133 %left
'<' '>' LEQ GEQ
'=' NOTEQUAL
'#' IN
135 %left LOGICAL_AND
'&'
138 %left
'*' '/' DIV MOD
140 %right
'^' DOT
'[' '('
143 /* This is not an actual token ; it is used for precedence.
155 { pstate
->push_new
<type_operation
> ($1); }
160 exp
: exp
'^' %prec UNARY
161 { pstate
->wrap
<unop_ind_operation
> (); }
165 { number_sign
= -1; }
168 pstate
->wrap
<unary_neg_operation
> (); }
171 exp
: '+' exp %prec UNARY
172 { pstate
->wrap
<unary_plus_operation
> (); }
175 exp
: not_exp exp %prec UNARY
176 { pstate
->wrap
<unary_logical_not_operation
> (); }
183 exp
: CAP
'(' exp
')'
184 { error (_
("CAP function is not implemented")); }
187 exp
: ORD
'(' exp
')'
188 { error (_
("ORD function is not implemented")); }
191 exp
: ABS
'(' exp
')'
192 { error (_
("ABS function is not implemented")); }
195 exp
: HIGH
'(' exp
')'
196 { pstate
->wrap
<m2_unop_high_operation
> (); }
199 exp
: MIN_FUNC
'(' type
')'
200 { error (_
("MIN function is not implemented")); }
203 exp
: MAX_FUNC
'(' type
')'
204 { error (_
("MAX function is not implemented")); }
207 exp
: FLOAT_FUNC
'(' exp
')'
208 { error (_
("FLOAT function is not implemented")); }
211 exp
: VAL
'(' type
',' exp
')'
212 { error (_
("VAL function is not implemented")); }
215 exp
: CHR
'(' exp
')'
216 { error (_
("CHR function is not implemented")); }
219 exp
: ODD
'(' exp
')'
220 { error (_
("ODD function is not implemented")); }
223 exp
: TRUNC
'(' exp
')'
224 { error (_
("TRUNC function is not implemented")); }
227 exp
: TSIZE
'(' exp
')'
228 { pstate
->wrap
<unop_sizeof_operation
> (); }
231 exp
: SIZE exp %prec UNARY
232 { pstate
->wrap
<unop_sizeof_operation
> (); }
236 exp
: INC
'(' exp
')'
237 { pstate
->wrap
<preinc_operation
> (); }
240 exp
: INC
'(' exp
',' exp
')'
242 operation_up rhs
= pstate
->pop
();
243 operation_up lhs
= pstate
->pop
();
244 pstate
->push_new
<assign_modify_operation
>
245 (BINOP_ADD
, std
::move
(lhs
), std
::move
(rhs
));
249 exp
: DEC
'(' exp
')'
250 { pstate
->wrap
<predec_operation
> (); }
253 exp
: DEC
'(' exp
',' exp
')'
255 operation_up rhs
= pstate
->pop
();
256 operation_up lhs
= pstate
->pop
();
257 pstate
->push_new
<assign_modify_operation
>
258 (BINOP_SUB
, std
::move
(lhs
), std
::move
(rhs
));
264 pstate
->push_new
<structop_operation
>
265 (pstate
->pop
(), copy_name
($3));
273 { error (_
("Sets are not implemented."));}
276 exp
: INCL
'(' exp
',' exp
')'
277 { error (_
("Sets are not implemented."));}
280 exp
: EXCL
'(' exp
',' exp
')'
281 { error (_
("Sets are not implemented."));}
284 set
: '{' arglist
'}'
285 { error (_
("Sets are not implemented."));}
286 | type
'{' arglist
'}'
287 { error (_
("Sets are not implemented."));}
291 /* Modula-2 array subscript notation [a,b,c...]. */
293 /* This function just saves the number of arguments
294 that follow in the list. It is *not* specific to
296 { pstate
->start_arglist
(); }
297 non_empty_arglist
']' %prec DOT
299 gdb_assert
(pstate
->arglist_len
> 0);
300 std
::vector
<operation_up
> args
301 = pstate
->pop_vector
(pstate
->end_arglist
());
302 pstate
->push_new
<multi_subscript_operation
>
303 (pstate
->pop
(), std
::move
(args
));
308 /* This is to save the value of arglist_len
309 being accumulated by an outer function call. */
310 { pstate
->start_arglist
(); }
311 arglist
')' %prec DOT
313 std
::vector
<operation_up
> args
314 = pstate
->pop_vector
(pstate
->end_arglist
());
315 pstate
->push_new
<funcall_operation
>
316 (pstate
->pop
(), std
::move
(args
));
324 { pstate
->arglist_len
= 1; }
327 arglist
: arglist
',' exp %prec ABOVE_COMMA
328 { pstate
->arglist_len
++; }
333 { pstate
->arglist_len
= 1; }
337 : non_empty_arglist
',' exp %prec ABOVE_COMMA
338 { pstate
->arglist_len
++; }
342 exp
: '{' type
'}' exp %prec UNARY
344 pstate
->push_new
<unop_memval_operation
>
345 (pstate
->pop
(), $2);
349 exp
: type
'(' exp
')' %prec UNARY
351 pstate
->push_new
<unop_cast_operation
>
352 (pstate
->pop
(), $1);
360 /* Binary operators in order of decreasing precedence. Note that some
361 of these operators are overloaded! (ie. sets) */
365 { pstate
->wrap2
<repeat_operation
> (); }
369 { pstate
->wrap2
<mul_operation
> (); }
373 { pstate
->wrap2
<div_operation
> (); }
377 { pstate
->wrap2
<intdiv_operation
> (); }
381 { pstate
->wrap2
<rem_operation
> (); }
385 { pstate
->wrap2
<add_operation
> (); }
389 { pstate
->wrap2
<sub_operation
> (); }
393 { pstate
->wrap2
<equal_operation
> (); }
396 exp
: exp NOTEQUAL exp
397 { pstate
->wrap2
<notequal_operation
> (); }
399 { pstate
->wrap2
<notequal_operation
> (); }
403 { pstate
->wrap2
<leq_operation
> (); }
407 { pstate
->wrap2
<geq_operation
> (); }
411 { pstate
->wrap2
<less_operation
> (); }
415 { pstate
->wrap2
<gtr_operation
> (); }
418 exp
: exp LOGICAL_AND exp
419 { pstate
->wrap2
<logical_and_operation
> (); }
423 { pstate
->wrap2
<logical_or_operation
> (); }
427 { pstate
->wrap2
<assign_operation
> (); }
434 { pstate
->push_new
<bool_operation
> ($1); }
438 { pstate
->push_new
<bool_operation
> ($1); }
443 pstate
->push_new
<long_const_operation
>
444 (parse_m2_type
(pstate
)->builtin_int
, $1);
450 pstate
->push_new
<long_const_operation
>
451 (parse_m2_type
(pstate
)->builtin_card
, $1);
457 pstate
->push_new
<long_const_operation
>
458 (parse_m2_type
(pstate
)->builtin_char
, $1);
466 std
::copy
(std
::begin
($1), std
::end
($1),
468 pstate
->push_new
<float_const_operation
>
469 (parse_m2_type
(pstate
)->builtin_real
, data
);
476 exp
: SIZE
'(' type
')' %prec UNARY
478 pstate
->push_new
<long_const_operation
>
479 (parse_m2_type
(pstate
)->builtin_int
,
485 { error (_
("strings are not implemented")); }
488 /* This will be used for extensions later. Like adding modules. */
490 { $$
= $1->value_block
(); }
495 = lookup_symbol
(copy_name
($1).c_str
(),
496 pstate
->expression_context_block
,
497 VAR_DOMAIN
, 0).symbol
;
502 /* GDB scope operator */
503 fblock
: block COLONCOLON BLOCKNAME
505 = lookup_symbol
(copy_name
($3).c_str
(), $1,
506 VAR_DOMAIN
, 0).symbol
;
507 if
(!tem || tem
->aclass
() != LOC_BLOCK
)
508 error (_
("No function \"%s\" in specified context."),
509 copy_name
($3).c_str
());
514 /* Useful for assigning to PROCEDURE variables */
517 block_symbol sym
{ $1, nullptr
};
518 pstate
->push_new
<var_value_operation
> (sym
);
522 /* GDB internal ($foo) variable */
523 variable: DOLLAR_VARIABLE
524 { pstate
->push_dollar
($1); }
527 /* GDB scope operator */
528 variable: block COLONCOLON NAME
529 { struct block_symbol sym
530 = lookup_symbol
(copy_name
($3).c_str
(), $1,
534 error (_
("No symbol \"%s\" in specified context."),
535 copy_name
($3).c_str
());
536 if
(symbol_read_needs_frame
(sym.symbol
))
537 pstate
->block_tracker
->update
(sym
);
539 pstate
->push_new
<var_value_operation
> (sym
);
543 /* Base case for variables. */
545 { struct block_symbol sym
;
546 struct field_of_this_result is_a_field_of_this
;
548 std
::string name
= copy_name
($1);
550 = lookup_symbol
(name.c_str
(),
551 pstate
->expression_context_block
,
553 &is_a_field_of_this
);
555 pstate
->push_symbol
(name.c_str
(), sym
);
562 = lookup_typename
(pstate
->language
(),
563 copy_name
($1).c_str
(),
564 pstate
->expression_context_block
,
572 /* Take care of parsing a number (anything that starts with a digit).
573 Set yylval and return the token type; update lexptr.
574 LEN is the number of characters in it. */
576 /*** Needs some error checking for the float case ***/
579 parse_number
(int olen
)
581 const char *p
= pstate
->lexptr
;
585 int base
= input_radix
;
593 else if
(p
[len
-1] == 'C' || p
[len
-1] == 'B')
596 ischar
= p
[len
-1] == 'C';
600 /* Scan the number */
601 for
(c
= 0; c
< len
; c
++)
603 if
(p
[c
] == '.' && base
== 10)
605 /* It's a float since it contains a point. */
606 if
(!parse_float
(p
, len
,
607 parse_m2_type
(pstate
)->builtin_real
,
611 pstate
->lexptr
+= len
;
614 if
(p
[c
] == '.' && base
!= 10)
615 error (_
("Floating point numbers must be base 10."));
616 if
(base
== 10 && (p
[c
] < '0' || p
[c
] > '9'))
617 error (_
("Invalid digit \'%c\' in number."),p
[c
]);
624 if
( base
== 8 && (c
== '8' || c
== '9'))
625 error (_
("Invalid digit \'%c\' in octal number."),c
);
626 if
(c
>= '0' && c
<= '9')
630 if
(base
== 16 && c
>= 'A' && c
<= 'F')
638 if
(n
== 0 && prevn
== 0)
640 else if
(RANGE_CHECK
&& prevn
>= n
)
641 range_error
(_
("Overflow on numeric constant."));
647 if
(*p
== 'B' ||
*p
== 'C' ||
*p
== 'H')
648 pstate
->lexptr
++; /* Advance past B,C or H */
656 int int_bits
= gdbarch_int_bit
(pstate
->gdbarch
());
657 bool have_signed
= number_sign
== -1;
658 bool have_unsigned
= number_sign
== 1;
659 if
(have_signed
&& fits_in_type
(number_sign
, n
, int_bits
, true
))
664 else if
(have_unsigned
&& fits_in_type
(number_sign
, n
, int_bits
, false
))
670 error (_
("Overflow on numeric constant."));
682 { {'<', '>'}, NOTEQUAL
},
683 { {':', '='}, ASSIGN
},
686 { {':', ':'}, COLONCOLON
},
690 /* Some specific keywords */
697 static struct keyword keytab
[] =
700 {"IN", IN
},/* Note space after IN */
701 {"AND", LOGICAL_AND
},
719 {"FLOAT", FLOAT_FUNC
},
725 /* Depth of parentheses. */
726 static int paren_depth
;
728 /* Read one token, getting characters through lexptr. */
730 /* This is where we will check to make sure that the language and the
731 operators used are compatible */
739 const char *tokstart
;
744 pstate
->prev_lexptr
= pstate
->lexptr
;
746 tokstart
= pstate
->lexptr
;
749 /* See if it is a special token of length 2 */
750 for
( i
= 0 ; i
< (int) (sizeof tokentab2
/ sizeof tokentab2
[0]) ; i
++)
751 if
(strncmp
(tokentab2
[i
].name
, tokstart
, 2) == 0)
754 return tokentab2
[i
].token
;
757 switch
(c
= *tokstart
)
774 if
(paren_depth
== 0)
781 if
(pstate
->comma_terminates
&& paren_depth
== 0)
787 /* Might be a floating point number. */
788 if
(pstate
->lexptr
[1] >= '0' && pstate
->lexptr
[1] <= '9')
789 break
; /* Falls into number code. */
796 /* These are character tokens that appear as-is in the YACC grammar */
819 for
(namelen
= 1; (c
= tokstart
[namelen
]) != quote
&& c
!= '\0'; namelen
++)
822 c
= tokstart
[++namelen
];
823 if
(c
>= '0' && c
<= '9')
825 c
= tokstart
[++namelen
];
826 if
(c
>= '0' && c
<= '9')
827 c
= tokstart
[++namelen
];
831 error (_
("Unterminated string or character constant."));
832 yylval.sval.ptr
= tokstart
+ 1;
833 yylval.sval.length
= namelen
- 1;
834 pstate
->lexptr
+= namelen
+ 1;
836 if
(namelen
== 2) /* Single character */
838 yylval.ulval
= tokstart
[1];
845 /* Is it a number? */
846 /* Note: We have already dealt with the case of the token '.'.
847 See case '.' above. */
848 if
((c
>= '0' && c
<= '9'))
851 int got_dot
= 0, got_e
= 0;
852 const char *p
= tokstart
;
857 if
(!got_e
&& (*p
== 'e' ||
*p
== 'E'))
859 else if
(!got_dot
&& *p
== '.')
861 else if
(got_e
&& (p
[-1] == 'e' || p
[-1] == 'E')
862 && (*p
== '-' ||
*p
== '+'))
863 /* This is the sign of the exponent, not the end of the
866 else if
((*p
< '0' ||
*p
> '9') &&
867 (*p
< 'A' ||
*p
> 'F') &&
868 (*p
!= 'H')) /* Modula-2 hexadecimal number */
871 toktype
= parse_number
(p
- tokstart
);
872 if
(toktype
== ERROR
)
874 char *err_copy
= (char *) alloca
(p
- tokstart
+ 1);
876 memcpy
(err_copy
, tokstart
, p
- tokstart
);
877 err_copy
[p
- tokstart
] = 0;
878 error (_
("Invalid number \"%s\"."), err_copy
);
884 if
(!(c
== '_' || c
== '$'
885 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z')))
886 /* We must have come across a bad character (e.g. ';'). */
887 error (_
("Invalid character '%c' in expression."), c
);
889 /* It's a name. See how long it is. */
891 for
(c
= tokstart
[namelen
];
892 (c
== '_' || c
== '$' ||
(c
>= '0' && c
<= '9')
893 ||
(c
>= 'a' && c
<= 'z') ||
(c
>= 'A' && c
<= 'Z'));
894 c
= tokstart
[++namelen
])
897 /* The token "if" terminates the expression and is NOT
898 removed from the input stream. */
899 if
(namelen
== 2 && tokstart
[0] == 'i' && tokstart
[1] == 'f')
904 pstate
->lexptr
+= namelen
;
906 /* Lookup special keywords */
907 for
(i
= 0 ; i
< (int) (sizeof
(keytab
) / sizeof
(keytab
[0])) ; i
++)
908 if
(namelen
== strlen
(keytab
[i
].keyw
)
909 && strncmp
(tokstart
, keytab
[i
].keyw
, namelen
) == 0)
910 return keytab
[i
].token
;
912 yylval.sval.ptr
= tokstart
;
913 yylval.sval.length
= namelen
;
915 if
(*tokstart
== '$')
916 return DOLLAR_VARIABLE
;
918 /* Use token-type BLOCKNAME for symbols that happen to be defined as
919 functions. If this is not so, then ...
920 Use token-type TYPENAME for symbols that happen to be defined
921 currently as names of types; NAME for other symbols.
922 The caller is not constrained to care about the distinction. */
924 std
::string tmp
= copy_name
(yylval.sval
);
927 if
(lookup_symtab
(tmp.c_str
()))
929 sym
= lookup_symbol
(tmp.c_str
(), pstate
->expression_context_block
,
930 VAR_DOMAIN
, 0).symbol
;
931 if
(sym
&& sym
->aclass
() == LOC_BLOCK
)
933 if
(lookup_typename
(pstate
->language
(),
934 tmp.c_str
(), pstate
->expression_context_block
, 1))
939 switch
(sym
->aclass
())
945 case LOC_REGPARM_ADDR
:
948 case LOC_CONST_BYTES
:
949 case LOC_OPTIMIZED_OUT
:
960 error (_
("internal: Undefined class in m2lex()"));
964 error (_
("internal: Unforseen case in m2lex()"));
967 error (_
("unhandled token in m2lex()"));
973 /* Built-in BOOLEAN type. This is sort of a hack. */
974 if
(startswith
(tokstart
, "TRUE"))
979 else if
(startswith
(tokstart
, "FALSE"))
986 /* Must be another type of name... */
992 m2_language::parser
(struct parser_state
*par_state
) const
994 /* Setting up the parser state. */
995 scoped_restore pstate_restore
= make_scoped_restore
(&pstate
);
996 gdb_assert
(par_state
!= NULL
);
1000 int result
= yyparse ();
1002 pstate
->set_operation
(pstate
->pop
());
1007 yyerror (const char *msg
)
1009 pstate
->parse_error
(msg
);