1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Parse a Chill expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator.
37 Also note that the language accepted by this parser is more liberal
38 than the one accepted by an actual Chill compiler. For example, the
39 language rule that a simple name string can not be one of the reserved
40 simple name strings is not enforced (e.g "case" is not treated as a
41 reserved name). Another example is that Chill is a strongly typed
42 language, and certain expressions that violate the type constraints
43 may still be evaluated if gdb can do so in a meaningful manner, while
44 such expressions would be rejected by the compiler. The reason for
45 this more liberal behavior is the philosophy that the debugger
46 is intended to be a tool that is used by the programmer when things
47 go wrong, and as such, it should provide as few artificial barriers
48 to it's use as possible. If it can do something meaningful, even
49 something that violates language contraints that are enforced by the
50 compiler, it should do so without complaint.
59 #include "expression.h"
62 #include "parser-defs.h"
64 #include "bfd.h" /* Required by objfiles.h. */
65 #include "symfile.h" /* Required by objfiles.h. */
66 #include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
68 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
69 as well as gratuitiously global symbol names, so we can have multiple
70 yacc generated parsers in gdb. Note that these are only the variables
71 produced by yacc. If other parser generators (bison, byacc, etc) produce
72 additional global names that conflict at link time, then those parser
73 generators need to be fixed instead of adding those names to this list. */
75 #define yymaxdepth chill_maxdepth
76 #define yyparse chill_parse
77 #define yylex chill_lex
78 #define yyerror chill_error
79 #define yylval chill_lval
80 #define yychar chill_char
81 #define yydebug chill_debug
82 #define yypact chill_pact
85 #define yydef chill_def
86 #define yychk chill_chk
87 #define yypgo chill_pgo
88 #define yyact chill_act
89 #define yyexca chill_exca
90 #define yyerrflag chill_errflag
91 #define yynerrs chill_nerrs
95 #define yy_yys chill_yys
96 #define yystate chill_state
97 #define yytmp chill_tmp
99 #define yy_yyv chill_yyv
100 #define yyval chill_val
101 #define yylloc chill_lloc
102 #define yyreds chill_reds /* With YYDEBUG defined */
103 #define yytoks chill_toks /* With YYDEBUG defined */
104 #define yylhs chill_yylhs
105 #define yylen chill_yylen
106 #define yydefred chill_yydefred
107 #define yydgoto chill_yydgoto
108 #define yysindex chill_yysindex
109 #define yyrindex chill_yyrindex
110 #define yygindex chill_yygindex
111 #define yytable chill_yytable
112 #define yycheck chill_yycheck
115 #define YYDEBUG 0 /* Default to no yydebug support */
119 write_lower_upper_value PARAMS ((enum exp_opcode, struct type *type));
122 yyparse PARAMS ((void));
125 yylex PARAMS ((void));
128 yyerror PARAMS ((char *));
132 /* Although the yacc "value" of an expression is not used,
133 since the result is stored in the structure being created,
134 other node types do have values. */
139 unsigned LONGEST ulval;
149 struct symtoken ssym;
152 enum exp_opcode opcode;
153 struct internalvar *ivar;
159 %token <typed_val> INTEGER_LITERAL
160 %token <ulval> BOOLEAN_LITERAL
161 %token <typed_val> CHARACTER_LITERAL
162 %token <dval> FLOAT_LITERAL
163 %token <ssym> GENERAL_PROCEDURE_NAME
164 %token <ssym> LOCATION_NAME
165 %token <voidval> EMPTINESS_LITERAL
166 %token <sval> CHARACTER_STRING_LITERAL
167 %token <sval> BIT_STRING_LITERAL
168 %token <tsym> TYPENAME
169 %token <sval> FIELD_NAME
174 %token <voidval> CASE
176 %token <voidval> ESAC
177 %token <voidval> LOGIOR
178 %token <voidval> ORIF
179 %token <voidval> LOGXOR
180 %token <voidval> LOGAND
181 %token <voidval> ANDIF
183 %token <voidval> NOTEQUAL
193 %token <voidval> SLASH_SLASH
197 %token <voidval> POINTER
198 %token <voidval> RECEIVE
205 %token <voidval> THEN
206 %token <voidval> ELSE
208 %token <voidval> ELSIF
209 %token <voidval> ILLEGAL_TOKEN
211 %token <voidval> PRED
212 %token <voidval> SUCC
214 %token <voidval> CARD
215 %token <voidval> MAX_TOKEN
216 %token <voidval> MIN_TOKEN
217 %token <voidval> ADDR_TOKEN
218 %token <voidval> SIZE
219 %token <voidval> UPPER
220 %token <voidval> LOWER
221 %token <voidval> LENGTH
222 %token <voidval> ARRAY
224 /* Tokens which are not Chill tokens used in expressions, but rather GDB
225 specific things that we recognize in the same context as Chill tokens
226 (register names for example). */
228 %token <lval> GDB_REGNAME /* Machine register name */
229 %token <lval> GDB_LAST /* Value history */
230 %token <ivar> GDB_VARIABLE /* Convenience variable */
231 %token <voidval> GDB_ASSIGNMENT /* Assign value to somewhere */
233 %type <voidval> access_name
234 %type <voidval> primitive_value
235 %type <voidval> value_name
236 %type <voidval> literal
237 %type <voidval> tuple
238 %type <voidval> slice
239 %type <voidval> expression_conversion
240 %type <voidval> value_built_in_routine_call
241 %type <voidval> parenthesised_expression
242 %type <voidval> value
243 %type <voidval> expression
244 %type <voidval> conditional_expression
245 %type <voidval> then_alternative
246 %type <voidval> else_alternative
247 %type <voidval> operand_0
248 %type <voidval> operand_1
249 %type <voidval> operand_2
250 %type <voidval> operand_3
251 %type <voidval> operand_4
252 %type <voidval> operand_5
253 %type <voidval> operand_6
254 %type <voidval> expression_list
255 %type <tval> mode_argument
256 %type <voidval> single_assignment_action
257 %type <tsym> mode_name
261 %type <voidval> undefined_value
262 %type <voidval> array_mode_name
263 %type <voidval> string_mode_name
264 %type <voidval> variant_structure_mode_name
273 { write_exp_elt_opcode(OP_TYPE);
274 write_exp_elt_type($1.type);
275 write_exp_elt_opcode(OP_TYPE);}
287 access_name : LOCATION_NAME
289 write_exp_elt_opcode (OP_VAR_VALUE);
290 write_exp_elt_block (NULL);
291 write_exp_elt_sym ($1.sym);
292 write_exp_elt_opcode (OP_VAR_VALUE);
294 | GDB_LAST /* gdb specific */
296 write_exp_elt_opcode (OP_LAST);
297 write_exp_elt_longcst ($1);
298 write_exp_elt_opcode (OP_LAST);
300 | GDB_REGNAME /* gdb specific */
302 write_exp_elt_opcode (OP_REGISTER);
303 write_exp_elt_longcst ($1);
304 write_exp_elt_opcode (OP_REGISTER);
306 | GDB_VARIABLE /* gdb specific */
308 write_exp_elt_opcode (OP_INTERNALVAR);
309 write_exp_elt_intern ($1);
310 write_exp_elt_opcode (OP_INTERNALVAR);
316 expression_list : expression
320 | expression_list ',' expression
326 maybe_expression_list: /* EMPTY */
336 primitive_value_lparen: primitive_value '('
337 /* This is to save the value of arglist_len
338 being accumulated for each dimension. */
339 { start_arglist (); }
343 { $$ = end_arglist (); }
348 | primitive_value_lparen maybe_expression_list rparen
350 write_exp_elt_opcode (MULTI_SUBSCRIPT);
351 write_exp_elt_longcst ($3);
352 write_exp_elt_opcode (MULTI_SUBSCRIPT);
354 | primitive_value FIELD_NAME
355 { write_exp_elt_opcode (STRUCTOP_STRUCT);
356 write_exp_string ($2);
357 write_exp_elt_opcode (STRUCTOP_STRUCT);
359 | primitive_value POINTER
361 write_exp_elt_opcode (UNOP_IND);
363 | primitive_value POINTER mode_name
365 write_exp_elt_opcode (UNOP_CAST);
366 write_exp_elt_type (lookup_pointer_type ($3.type));
367 write_exp_elt_opcode (UNOP_CAST);
368 write_exp_elt_opcode (UNOP_IND);
374 | expression_conversion
375 | value_built_in_routine_call
382 | parenthesised_expression
387 value_name : GENERAL_PROCEDURE_NAME
389 write_exp_elt_opcode (OP_VAR_VALUE);
390 write_exp_elt_block (NULL);
391 write_exp_elt_sym ($1.sym);
392 write_exp_elt_opcode (OP_VAR_VALUE);
398 literal : INTEGER_LITERAL
400 write_exp_elt_opcode (OP_LONG);
401 write_exp_elt_type ($1.type);
402 write_exp_elt_longcst ((LONGEST) ($1.val));
403 write_exp_elt_opcode (OP_LONG);
407 write_exp_elt_opcode (OP_BOOL);
408 write_exp_elt_longcst ((LONGEST) $1);
409 write_exp_elt_opcode (OP_BOOL);
413 write_exp_elt_opcode (OP_LONG);
414 write_exp_elt_type ($1.type);
415 write_exp_elt_longcst ((LONGEST) ($1.val));
416 write_exp_elt_opcode (OP_LONG);
420 write_exp_elt_opcode (OP_DOUBLE);
421 write_exp_elt_type (builtin_type_double);
422 write_exp_elt_dblcst ($1);
423 write_exp_elt_opcode (OP_DOUBLE);
427 struct type *void_ptr_type
428 = lookup_pointer_type (builtin_type_void);
429 write_exp_elt_opcode (OP_LONG);
430 write_exp_elt_type (void_ptr_type);
431 write_exp_elt_longcst (0);
432 write_exp_elt_opcode (OP_LONG);
434 | CHARACTER_STRING_LITERAL
436 write_exp_elt_opcode (OP_STRING);
437 write_exp_string ($1);
438 write_exp_elt_opcode (OP_STRING);
442 write_exp_elt_opcode (OP_BITSTRING);
443 write_exp_bitstring ($1);
444 write_exp_elt_opcode (OP_BITSTRING);
450 tuple_element : expression
451 | named_record_element
454 named_record_element: FIELD_NAME ',' named_record_element
455 { write_exp_elt_opcode (OP_LABELED);
456 write_exp_string ($1);
457 write_exp_elt_opcode (OP_LABELED);
459 | FIELD_NAME ':' expression
460 { write_exp_elt_opcode (OP_LABELED);
461 write_exp_string ($1);
462 write_exp_elt_opcode (OP_LABELED);
466 tuple_elements : tuple_element
470 | tuple_elements ',' tuple_element
476 maybe_tuple_elements : tuple_elements
481 { start_arglist (); }
482 maybe_tuple_elements ']'
484 write_exp_elt_opcode (OP_ARRAY);
485 write_exp_elt_longcst ((LONGEST) 0);
486 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
487 write_exp_elt_opcode (OP_ARRAY);
491 { start_arglist (); }
492 maybe_tuple_elements ']'
494 write_exp_elt_opcode (OP_ARRAY);
495 write_exp_elt_longcst ((LONGEST) 0);
496 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
497 write_exp_elt_opcode (OP_ARRAY);
499 write_exp_elt_opcode (UNOP_CAST);
500 write_exp_elt_type ($1.type);
501 write_exp_elt_opcode (UNOP_CAST);
509 slice: primitive_value_lparen expression ':' expression rparen
511 write_exp_elt_opcode (TERNOP_SLICE);
513 | primitive_value_lparen expression UP expression rparen
515 write_exp_elt_opcode (TERNOP_SLICE_COUNT);
521 expression_conversion: mode_name parenthesised_expression
523 write_exp_elt_opcode (UNOP_CAST);
524 write_exp_elt_type ($1.type);
525 write_exp_elt_opcode (UNOP_CAST);
527 | ARRAY '(' ')' mode_name parenthesised_expression
528 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
529 which casts to an artificial array. */
531 struct type *range_type
532 = create_range_type ((struct type *) NULL,
533 builtin_type_int, 0, 0);
534 struct type *array_type
535 = create_array_type ((struct type *) NULL,
536 $4.type, range_type);
537 TYPE_ARRAY_UPPER_BOUND_TYPE(array_type)
538 = BOUND_CANNOT_BE_DETERMINED;
539 write_exp_elt_opcode (UNOP_CAST);
540 write_exp_elt_type (array_type);
541 write_exp_elt_opcode (UNOP_CAST);
547 parenthesised_expression: '(' expression ')'
552 expression : operand_0
553 | single_assignment_action
554 | conditional_expression
557 conditional_expression : IF expression then_alternative else_alternative FI
558 { write_exp_elt_opcode (TERNOP_COND); }
560 | CASE case_selector_list OF value_case_alternative ELSE expression ESAC
561 { error ("not implemented: CASE expression" }
565 then_alternative: THEN expression
568 else_alternative: ELSE expression
569 | ELSIF expression then_alternative else_alternative
570 { write_exp_elt_opcode (TERNOP_COND); }
575 operand_0 : operand_1
576 | operand_0 LOGIOR operand_1
578 write_exp_elt_opcode (BINOP_BITWISE_IOR);
580 | operand_0 ORIF operand_1
582 write_exp_elt_opcode (BINOP_LOGICAL_OR);
584 | operand_0 LOGXOR operand_1
586 write_exp_elt_opcode (BINOP_BITWISE_XOR);
592 operand_1 : operand_2
593 | operand_1 LOGAND operand_2
595 write_exp_elt_opcode (BINOP_BITWISE_AND);
597 | operand_1 ANDIF operand_2
599 write_exp_elt_opcode (BINOP_LOGICAL_AND);
605 operand_2 : operand_3
606 | operand_2 '=' operand_3
608 write_exp_elt_opcode (BINOP_EQUAL);
610 | operand_2 NOTEQUAL operand_3
612 write_exp_elt_opcode (BINOP_NOTEQUAL);
614 | operand_2 '>' operand_3
616 write_exp_elt_opcode (BINOP_GTR);
618 | operand_2 GTR operand_3
620 write_exp_elt_opcode (BINOP_GEQ);
622 | operand_2 '<' operand_3
624 write_exp_elt_opcode (BINOP_LESS);
626 | operand_2 LEQ operand_3
628 write_exp_elt_opcode (BINOP_LEQ);
630 | operand_2 IN operand_3
632 write_exp_elt_opcode (BINOP_IN);
639 operand_3 : operand_4
640 | operand_3 '+' operand_4
642 write_exp_elt_opcode (BINOP_ADD);
644 | operand_3 '-' operand_4
646 write_exp_elt_opcode (BINOP_SUB);
648 | operand_3 SLASH_SLASH operand_4
650 write_exp_elt_opcode (BINOP_CONCAT);
656 operand_4 : operand_5
657 | operand_4 '*' operand_5
659 write_exp_elt_opcode (BINOP_MUL);
661 | operand_4 '/' operand_5
663 write_exp_elt_opcode (BINOP_DIV);
665 | operand_4 MOD operand_5
667 write_exp_elt_opcode (BINOP_MOD);
669 | operand_4 REM operand_5
671 write_exp_elt_opcode (BINOP_REM);
677 operand_5 : operand_6
680 write_exp_elt_opcode (UNOP_NEG);
684 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
686 | parenthesised_expression literal
687 /* We require the string operand to be a literal, to avoid some
688 nasty parsing ambiguities. */
690 write_exp_elt_opcode (BINOP_CONCAT);
696 operand_6 : POINTER primitive_value
698 write_exp_elt_opcode (UNOP_ADDR);
701 { error ("not implemented: RECEIVE expression"); }
708 single_assignment_action :
709 primitive_value GDB_ASSIGNMENT value
711 write_exp_elt_opcode (BINOP_ASSIGN);
717 value_built_in_routine_call :
718 NUM '(' expression ')'
720 write_exp_elt_opcode (UNOP_CAST);
721 write_exp_elt_type (builtin_type_int);
722 write_exp_elt_opcode (UNOP_CAST);
724 | PRED '(' expression ')'
725 { error ("not implemented: PRED builtin function"); }
726 | SUCC '(' expression ')'
727 { error ("not implemented: SUCC builtin function"); }
728 | ADDR_TOKEN '(' expression ')'
729 { write_exp_elt_opcode (UNOP_ADDR); }
730 | ABS '(' expression ')'
731 { error ("not implemented: ABS builtin function"); }
732 | CARD '(' expression ')'
733 { error ("not implemented: CARD builtin function"); }
734 | MAX_TOKEN '(' expression ')'
735 { error ("not implemented: MAX builtin function"); }
736 | MIN_TOKEN '(' expression ')'
737 { error ("not implemented: MIN builtin function"); }
738 | SIZE '(' expression ')'
739 { write_exp_elt_opcode (UNOP_SIZEOF); }
740 | SIZE '(' mode_argument ')'
741 { write_exp_elt_opcode (OP_LONG);
742 write_exp_elt_type (builtin_type_int);
743 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
744 write_exp_elt_opcode (OP_LONG); }
745 | LOWER '(' mode_argument ')'
746 { write_lower_upper_value (UNOP_LOWER, $3); }
747 | UPPER '(' mode_argument ')'
748 { write_lower_upper_value (UNOP_UPPER, $3); }
749 | LOWER '(' expression ')'
750 { write_exp_elt_opcode (UNOP_LOWER); }
751 | UPPER '(' expression ')'
752 { write_exp_elt_opcode (UNOP_UPPER); }
753 | LENGTH '(' expression ')'
754 { write_exp_elt_opcode (UNOP_LENGTH); }
757 mode_argument : mode_name
762 | array_mode_name '(' expression ')'
764 | string_mode_name '(' expression ')'
766 | variant_structure_mode_name '(' expression_list ')'
776 /* Implementation of a dynamically expandable buffer for processing input
777 characters acquired through lexptr and building a value to return in
780 static char *tempbuf; /* Current buffer contents */
781 static int tempbufsize; /* Size of allocated buffer */
782 static int tempbufindex; /* Current index into buffer */
784 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
786 #define CHECKBUF(size) \
788 if (tempbufindex + (size) >= tempbufsize) \
790 growbuf_by_size (size); \
794 /* Grow the static temp buffer if necessary, including allocating the first one
798 growbuf_by_size (count)
803 growby = max (count, GROWBY_MIN_SIZE);
804 tempbufsize += growby;
807 tempbuf = (char *) malloc (tempbufsize);
811 tempbuf = (char *) realloc (tempbuf, tempbufsize);
815 /* Try to consume a simple name string token. If successful, returns
816 a pointer to a nullbyte terminated copy of the name that can be used
817 in symbol table lookups. If not successful, returns NULL. */
820 match_simple_name_string ()
822 char *tokptr = lexptr;
824 if (isalpha (*tokptr) || *tokptr == '_')
829 } while (isalnum (*tokptr) || (*tokptr == '_'));
830 yylval.sval.ptr = lexptr;
831 yylval.sval.length = tokptr - lexptr;
833 result = copy_name (yylval.sval);
839 /* Start looking for a value composed of valid digits as set by the base
840 in use. Note that '_' characters are valid anywhere, in any quantity,
841 and are simply ignored. Since we must find at least one valid digit,
842 or reject this token as an integer literal, we keep track of how many
843 digits we have encountered. */
846 decode_integer_value (base, tokptrptr, ivalptr)
851 char *tokptr = *tokptrptr;
855 while (*tokptr != '\0')
859 temp = tolower (temp);
865 case '0': case '1': case '2': case '3': case '4':
866 case '5': case '6': case '7': case '8': case '9':
869 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
885 /* Found something not in domain for current base. */
886 tokptr--; /* Unconsume what gave us indigestion. */
891 /* If we didn't find any digits, then we don't have a valid integer
892 value, so reject the entire token. Otherwise, update the lexical
893 scan pointer, and return non-zero for success. */
907 decode_integer_literal (valptr, tokptrptr)
911 char *tokptr = *tokptrptr;
914 int explicit_base = 0;
916 /* Look for an explicit base specifier, which is optional. */
949 /* If we found an explicit base ensure that the character after the
950 explicit base is a single quote. */
952 if (explicit_base && (*tokptr++ != '\''))
957 /* Attempt to decode whatever follows as an integer value in the
958 indicated base, updating the token pointer in the process and
959 computing the value into ival. Also, if we have an explicit
960 base, then the next character must not be a single quote, or we
961 have a bitstring literal, so reject the entire token in this case.
962 Otherwise, update the lexical scan pointer, and return non-zero
965 if (!decode_integer_value (base, &tokptr, &ival))
969 else if (explicit_base && (*tokptr == '\''))
981 /* If it wasn't for the fact that floating point values can contain '_'
982 characters, we could just let strtod do all the hard work by letting it
983 try to consume as much of the current token buffer as possible and
984 find a legal conversion. Unfortunately we need to filter out the '_'
985 characters before calling strtod, which we do by copying the other
986 legal chars to a local buffer to be converted. However since we also
987 need to keep track of where the last unconsumed character in the input
988 buffer is, we have transfer only as many characters as may compose a
989 legal floating point value. */
992 match_float_literal ()
994 char *tokptr = lexptr;
998 extern double strtod ();
1000 /* Make local buffer in which to build the string to convert. This is
1001 required because underscores are valid in chill floating point numbers
1002 but not in the string passed to strtod to convert. The string will be
1003 no longer than our input string. */
1005 copy = buf = (char *) alloca (strlen (tokptr) + 1);
1007 /* Transfer all leading digits to the conversion buffer, discarding any
1010 while (isdigit (*tokptr) || *tokptr == '_')
1019 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1020 of whether we found any leading digits, and we simply accept it and
1021 continue on to look for the fractional part and/or exponent. One of
1022 [eEdD] is legal only if we have seen digits, and means that there
1023 is no fractional part. If we find neither of these, then this is
1024 not a floating point number, so return failure. */
1029 /* Accept and then look for fractional part and/or exponent. */
1042 goto collect_exponent;
1050 /* We found a '.', copy any fractional digits to the conversion buffer, up
1051 to the first nondigit, non-underscore character. */
1053 while (isdigit (*tokptr) || *tokptr == '_')
1062 /* Look for an exponent, which must start with one of [eEdD]. If none
1063 is found, jump directly to trying to convert what we have collected
1080 /* Accept an optional '-' or '+' following one of [eEdD]. */
1083 if (*tokptr == '+' || *tokptr == '-')
1085 *copy++ = *tokptr++;
1088 /* Now copy an exponent into the conversion buffer. Note that at the
1089 moment underscores are *not* allowed in exponents. */
1091 while (isdigit (*tokptr))
1093 *copy++ = *tokptr++;
1096 /* If we transfered any chars to the conversion buffer, try to interpret its
1097 contents as a floating point value. If any characters remain, then we
1098 must not have a valid floating point string. */
1104 dval = strtod (buf, ©);
1109 return (FLOAT_LITERAL);
1115 /* Recognize a string literal. A string literal is a sequence
1116 of characters enclosed in matching single or double quotes, except that
1117 a single character inside single quotes is a character literal, which
1118 we reject as a string literal. To embed the terminator character inside
1119 a string, it is simply doubled (I.E. "this""is""one""string") */
1122 match_string_literal ()
1124 char *tokptr = lexptr;
1126 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1129 if (*tokptr == *lexptr)
1131 if (*(tokptr + 1) == *lexptr)
1140 tempbuf[tempbufindex++] = *tokptr;
1142 if (*tokptr == '\0' /* no terminator */
1143 || (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
1149 tempbuf[tempbufindex] = '\0';
1150 yylval.sval.ptr = tempbuf;
1151 yylval.sval.length = tempbufindex;
1153 return (CHARACTER_STRING_LITERAL);
1157 /* Recognize a character literal. A character literal is single character
1158 or a control sequence, enclosed in single quotes. A control sequence
1159 is a comma separated list of one or more integer literals, enclosed
1160 in parenthesis and introduced with a circumflex character.
1162 EX: 'a' '^(7)' '^(7,8)'
1164 As a GNU chill extension, the syntax C'xx' is also recognized as a
1165 character literal, where xx is a hex value for the character.
1167 Note that more than a single character, enclosed in single quotes, is
1170 Also note that the control sequence form is not in GNU Chill since it
1171 is ambiguous with the string literal form using single quotes. I.E.
1172 is '^(7)' a character literal or a string literal. In theory it it
1173 possible to tell by context, but GNU Chill doesn't accept the control
1174 sequence form, so neither do we (for now the code is disabled).
1176 Returns CHARACTER_LITERAL if a match is found.
1180 match_character_literal ()
1182 char *tokptr = lexptr;
1185 if ((*tokptr == 'c' || *tokptr == 'C') && (*(tokptr + 1) == '\''))
1187 /* We have a GNU chill extension form, so skip the leading "C'",
1188 decode the hex value, and then ensure that we have a trailing
1189 single quote character. */
1191 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1197 else if (*tokptr == '\'')
1201 /* Determine which form we have, either a control sequence or the
1202 single character form. */
1204 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1206 #if 0 /* Disable, see note above. -fnf */
1207 /* Match and decode a control sequence. Return zero if we don't
1208 find a valid integer literal, or if the next unconsumed character
1209 after the integer literal is not the trailing ')'.
1210 FIXME: We currently don't handle the multiple integer literal
1213 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1226 /* The trailing quote has not yet been consumed. If we don't find
1227 it, then we have no match. */
1229 if (*tokptr++ != '\'')
1236 /* Not a character literal. */
1239 yylval.typed_val.val = ival;
1240 yylval.typed_val.type = builtin_type_chill_char;
1242 return (CHARACTER_LITERAL);
1245 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1246 Note that according to 5.2.4.2, a single "_" is also a valid integer
1247 literal, however GNU-chill requires there to be at least one "digit"
1248 in any integer literal. */
1251 match_integer_literal ()
1253 char *tokptr = lexptr;
1256 if (!decode_integer_literal (&ival, &tokptr))
1262 yylval.typed_val.val = ival;
1263 yylval.typed_val.type = builtin_type_int;
1265 return (INTEGER_LITERAL);
1269 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1270 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1271 literal, however GNU-chill requires there to be at least one "digit"
1272 in any bit-string literal. */
1275 match_bitstring_literal ()
1277 register char *tokptr = lexptr;
1287 /* Look for the required explicit base specifier. */
1308 /* Ensure that the character after the explicit base is a single quote. */
1310 if (*tokptr++ != '\'')
1315 while (*tokptr != '\0' && *tokptr != '\'')
1318 if (isupper (digit))
1319 digit = tolower (digit);
1325 case '0': case '1': case '2': case '3': case '4':
1326 case '5': case '6': case '7': case '8': case '9':
1329 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1337 if (digit >= 1 << bits_per_char)
1339 /* Found something not in domain for current base. */
1344 /* Extract bits from digit, packing them into the bitstring byte. */
1345 int k = TARGET_BYTE_ORDER == BIG_ENDIAN ? bits_per_char - 1 : 0;
1346 for (; TARGET_BYTE_ORDER == BIG_ENDIAN ? k >= 0 : k < bits_per_char;
1347 TARGET_BYTE_ORDER == BIG_ENDIAN ? k-- : k++)
1350 if (digit & (1 << k))
1352 tempbuf[tempbufindex] |=
1353 (TARGET_BYTE_ORDER == BIG_ENDIAN)
1354 ? (1 << (HOST_CHAR_BIT - 1 - bitoffset))
1358 if (bitoffset == HOST_CHAR_BIT)
1363 tempbuf[tempbufindex] = 0;
1369 /* Verify that we consumed everything up to the trailing single quote,
1370 and that we found some bits (IE not just underbars). */
1372 if (*tokptr++ != '\'')
1378 yylval.sval.ptr = tempbuf;
1379 yylval.sval.length = bitcount;
1381 return (BIT_STRING_LITERAL);
1385 /* Recognize tokens that start with '$'. These include:
1387 $regname A native register name or a "standard
1389 Return token GDB_REGNAME.
1391 $variable A convenience variable with a name chosen
1393 Return token GDB_VARIABLE.
1395 $digits Value history with index <digits>, starting
1396 from the first value which has index 1.
1399 $$digits Value history with index <digits> relative
1400 to the last value. I.E. $$0 is the last
1401 value, $$1 is the one previous to that, $$2
1402 is the one previous to $$1, etc.
1403 Return token GDB_LAST.
1405 $ | $0 | $$0 The last value in the value history.
1406 Return token GDB_LAST.
1408 $$ An abbreviation for the second to the last
1409 value in the value history, I.E. $$1
1410 Return token GDB_LAST.
1412 Note that we currently assume that register names and convenience
1413 variables follow the convention of starting with a letter or '_'.
1418 match_dollar_tokens ()
1426 /* We will always have a successful match, even if it is just for
1427 a single '$', the abbreviation for $$0. So advance lexptr. */
1431 if (*tokptr == '_' || isalpha (*tokptr))
1433 /* Look for a match with a native register name, usually something
1434 like "r0" for example. */
1436 for (regno = 0; regno < NUM_REGS; regno++)
1438 namelength = strlen (reg_names[regno]);
1439 if (STREQN (tokptr, reg_names[regno], namelength)
1440 && !isalnum (tokptr[namelength]))
1442 yylval.lval = regno;
1443 lexptr += namelength;
1444 return (GDB_REGNAME);
1448 /* Look for a match with a standard register name, usually something
1449 like "pc", which gdb always recognizes as the program counter
1450 regardless of what the native register name is. */
1452 for (regno = 0; regno < num_std_regs; regno++)
1454 namelength = strlen (std_regs[regno].name);
1455 if (STREQN (tokptr, std_regs[regno].name, namelength)
1456 && !isalnum (tokptr[namelength]))
1458 yylval.lval = std_regs[regno].regnum;
1459 lexptr += namelength;
1460 return (GDB_REGNAME);
1464 /* Attempt to match against a convenience variable. Note that
1465 this will always succeed, because if no variable of that name
1466 already exists, the lookup_internalvar will create one for us.
1467 Also note that both lexptr and tokptr currently point to the
1468 start of the input string we are trying to match, and that we
1469 have already tested the first character for non-numeric, so we
1470 don't have to treat it specially. */
1472 while (*tokptr == '_' || isalnum (*tokptr))
1476 yylval.sval.ptr = lexptr;
1477 yylval.sval.length = tokptr - lexptr;
1478 yylval.ivar = lookup_internalvar (copy_name (yylval.sval));
1480 return (GDB_VARIABLE);
1483 /* Since we didn't match against a register name or convenience
1484 variable, our only choice left is a history value. */
1498 /* Attempt to decode more characters as an integer value giving
1499 the index in the history list. If successful, the value will
1500 overwrite ival (currently 0 or 1), and if not, ival will be
1501 left alone, which is good since it is currently correct for
1502 the '$' or '$$' case. */
1504 decode_integer_literal (&ival, &tokptr);
1505 yylval.lval = negate ? -ival : ival;
1516 static const struct token idtokentab[] =
1519 { "length", LENGTH },
1530 { "max", MAX_TOKEN },
1531 { "min", MIN_TOKEN },
1540 { "addr", ADDR_TOKEN },
1541 { "null", EMPTINESS_LITERAL }
1544 static const struct token tokentab2[] =
1546 { ":=", GDB_ASSIGNMENT },
1547 { "//", SLASH_SLASH },
1554 /* Read one token, getting characters through lexptr. */
1555 /* This is where we will check to make sure that the language and the
1556 operators used are compatible. */
1566 /* Skip over any leading whitespace. */
1567 while (isspace (*lexptr))
1571 /* Look for special single character cases which can't be the first
1572 character of some other multicharacter token. */
1589 /* Look for characters which start a particular kind of multicharacter
1590 token, such as a character literal, register name, convenience
1591 variable name, string literal, etc. */
1596 /* First try to match a string literal, which is any
1597 sequence of characters enclosed in matching single or double
1598 quotes, except that a single character inside single quotes
1599 is a character literal, so we have to catch that case also. */
1600 token = match_string_literal ();
1605 if (*lexptr == '\'')
1607 token = match_character_literal ();
1616 token = match_character_literal ();
1623 token = match_dollar_tokens ();
1630 /* See if it is a special token of length 2. */
1631 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1633 if (STREQN (lexptr, tokentab2[i].operator, 2))
1636 return (tokentab2[i].token);
1639 /* Look for single character cases which which could be the first
1640 character of some other multicharacter token, but aren't, or we
1641 would already have found it. */
1651 /* Look for a float literal before looking for an integer literal, so
1652 we match as much of the input stream as possible. */
1653 token = match_float_literal ();
1658 token = match_bitstring_literal ();
1663 token = match_integer_literal ();
1669 /* Try to match a simple name string, and if a match is found, then
1670 further classify what sort of name it is and return an appropriate
1671 token. Note that attempting to match a simple name string consumes
1672 the token from lexptr, so we can't back out if we later find that
1673 we can't classify what sort of name it is. */
1675 inputname = match_simple_name_string ();
1677 if (inputname != NULL)
1679 char *simplename = (char*) alloca (strlen (inputname) + 1);
1681 char *dptr = simplename, *sptr = inputname;
1682 for (; *sptr; sptr++)
1683 *dptr++ = isupper (*sptr) ? tolower(*sptr) : *sptr;
1686 /* See if it is a reserved identifier. */
1687 for (i = 0; i < sizeof (idtokentab) / sizeof (idtokentab[0]); i++)
1689 if (STREQ (simplename, idtokentab[i].operator))
1691 return (idtokentab[i].token);
1695 /* Look for other special tokens. */
1696 if (STREQ (simplename, "true"))
1699 return (BOOLEAN_LITERAL);
1701 if (STREQ (simplename, "false"))
1704 return (BOOLEAN_LITERAL);
1707 sym = lookup_symbol (inputname, expression_context_block,
1708 VAR_NAMESPACE, (int *) NULL,
1709 (struct symtab **) NULL);
1710 if (sym == NULL && strcmp (inputname, simplename) != 0)
1712 sym = lookup_symbol (simplename, expression_context_block,
1713 VAR_NAMESPACE, (int *) NULL,
1714 (struct symtab **) NULL);
1718 yylval.ssym.stoken.ptr = NULL;
1719 yylval.ssym.stoken.length = 0;
1720 yylval.ssym.sym = sym;
1721 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1722 switch (SYMBOL_CLASS (sym))
1725 /* Found a procedure name. */
1726 return (GENERAL_PROCEDURE_NAME);
1728 /* Found a global or local static variable. */
1729 return (LOCATION_NAME);
1734 case LOC_REGPARM_ADDR:
1738 case LOC_BASEREG_ARG:
1739 if (innermost_block == NULL
1740 || contained_in (block_found, innermost_block))
1742 innermost_block = block_found;
1744 return (LOCATION_NAME);
1748 return (LOCATION_NAME);
1751 yylval.tsym.type = SYMBOL_TYPE (sym);
1754 case LOC_CONST_BYTES:
1755 case LOC_OPTIMIZED_OUT:
1756 error ("Symbol \"%s\" names no location.", inputname);
1760 else if (!have_full_symbols () && !have_partial_symbols ())
1762 error ("No symbol table is loaded. Use the \"file\" command.");
1766 error ("No symbol \"%s\" in current context.", inputname);
1770 /* Catch single character tokens which are not part of some
1775 case '.': /* Not float for example. */
1777 while (isspace (*lexptr)) lexptr++;
1778 inputname = match_simple_name_string ();
1784 return (ILLEGAL_TOKEN);
1788 write_lower_upper_value (opcode, type)
1789 enum exp_opcode opcode; /* Either UNOP_LOWER or UNOP_UPPER */
1792 extern LONGEST type_lower_upper ();
1793 struct type *result_type;
1794 LONGEST val = type_lower_upper (opcode, type, &result_type);
1795 write_exp_elt_opcode (OP_LONG);
1796 write_exp_elt_type (result_type);
1797 write_exp_elt_longcst (val);
1798 write_exp_elt_opcode (OP_LONG);
1805 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);