1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright (C) 1992 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., 675 Mass Ave, Cambridge, MA 02139, 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.
57 #include "expression.h"
60 #include "parser-defs.h"
63 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
64 as well as gratuitiously global symbol names, so we can have multiple
65 yacc generated parsers in gdb. Note that these are only the variables
66 produced by yacc. If other parser generators (bison, byacc, etc) produce
67 additional global names that conflict at link time, then those parser
68 generators need to be fixed instead of adding those names to this list. */
70 #define yymaxdepth chill_maxdepth
71 #define yyparse chill_parse
72 #define yylex chill_lex
73 #define yyerror chill_error
74 #define yylval chill_lval
75 #define yychar chill_char
76 #define yydebug chill_debug
77 #define yypact chill_pact
80 #define yydef chill_def
81 #define yychk chill_chk
82 #define yypgo chill_pgo
83 #define yyact chill_act
84 #define yyexca chill_exca
85 #define yyerrflag chill_errflag
86 #define yynerrs chill_nerrs
90 #define yy_yys chill_yys
91 #define yystate chill_state
92 #define yytmp chill_tmp
94 #define yy_yyv chill_yyv
95 #define yyval chill_val
96 #define yylloc chill_lloc
97 #define yyreds chill_reds /* With YYDEBUG defined */
98 #define yytoks chill_toks /* With YYDEBUG defined */
101 #define YYDEBUG 0 /* Default to no yydebug support */
105 yyparse PARAMS ((void));
108 yylex PARAMS ((void));
111 yyerror PARAMS ((char *));
115 /* Although the yacc "value" of an expression is not used,
116 since the result is stored in the structure being created,
117 other node types do have values. */
122 unsigned LONGEST ulval;
132 struct symtoken ssym;
135 enum exp_opcode opcode;
136 struct internalvar *ivar;
142 %token <voidval> FIXME
144 %token <typed_val> INTEGER_LITERAL
145 %token <ulval> BOOLEAN_LITERAL
146 %token <typed_val> CHARACTER_LITERAL
147 %token <dval> FLOAT_LITERAL
148 %token <ssym> GENERAL_PROCEDURE_NAME
149 %token <ssym> LOCATION_NAME
150 %token <voidval> SET_LITERAL
151 %token <voidval> EMPTINESS_LITERAL
152 %token <voidval> CHARACTER_STRING_LITERAL
153 %token <sval> BIT_STRING_LITERAL
155 %token <voidval> STRING
156 %token <voidval> CONSTANT
160 %token <voidval> CASE
162 %token <voidval> ESAC
163 %token <voidval> LOGIOR
164 %token <voidval> ORIF
165 %token <voidval> LOGXOR
166 %token <voidval> LOGAND
167 %token <voidval> ANDIF
169 %token <voidval> NOTEQUAL
179 %token <voidval> SLASH_SLASH
183 %token <voidval> POINTER
184 %token <voidval> RECEIVE
192 %token <voidval> THEN
193 %token <voidval> ELSE
195 %token <voidval> ELSIF
196 %token <voidval> ILLEGAL_TOKEN
198 %token <voidval> PRED
199 %token <voidval> SUCC
201 %token <voidval> CARD
204 %token <voidval> SIZE
205 %token <voidval> UPPER
206 %token <voidval> LOWER
207 %token <voidval> LENGTH
209 /* Tokens which are not Chill tokens used in expressions, but rather GDB
210 specific things that we recognize in the same context as Chill tokens
211 (register names for example). */
213 %token <lval> GDB_REGNAME /* Machine register name */
214 %token <lval> GDB_LAST /* Value history */
215 %token <ivar> GDB_VARIABLE /* Convenience variable */
216 %token <voidval> GDB_ASSIGNMENT /* Assign value to somewhere */
218 %type <voidval> location
219 %type <voidval> access_name
220 %type <voidval> primitive_value
221 %type <voidval> location_contents
222 %type <voidval> value_name
223 %type <voidval> literal
224 %type <voidval> tuple
225 %type <voidval> value_string_element
226 %type <voidval> value_string_slice
227 %type <voidval> value_array_element
228 %type <voidval> value_array_slice
229 %type <voidval> value_structure_field
230 %type <voidval> expression_conversion
231 %type <voidval> value_procedure_call
232 %type <voidval> value_built_in_routine_call
233 %type <voidval> chill_value_built_in_routine_call
234 %type <voidval> start_expression
235 %type <voidval> zero_adic_operator
236 %type <voidval> parenthesised_expression
237 %type <voidval> value
238 %type <voidval> undefined_value
239 %type <voidval> expression
240 %type <voidval> conditional_expression
241 %type <voidval> then_alternative
242 %type <voidval> else_alternative
243 %type <voidval> sub_expression
244 %type <voidval> value_case_alternative
245 %type <voidval> operand_0
246 %type <voidval> operand_1
247 %type <voidval> operand_2
248 %type <voidval> operand_3
249 %type <voidval> operand_4
250 %type <voidval> operand_5
251 %type <voidval> operand_6
252 %type <voidval> integer_literal_expression
253 %type <voidval> synonym_name
254 %type <voidval> value_enumeration_name
255 %type <voidval> value_do_with_name
256 %type <voidval> value_receive_name
257 %type <voidval> string_primitive_value
258 %type <voidval> start_element
259 %type <voidval> left_element
260 %type <voidval> right_element
261 %type <voidval> slice_size
262 %type <voidval> array_primitive_value
263 %type <voidval> expression_list
264 %type <voidval> lower_element
265 %type <voidval> upper_element
266 %type <voidval> first_element
267 %type <voidval> structure_primitive_value
268 %type <voidval> field_name
269 %type <voidval> mode_argument
270 %type <voidval> upper_lower_argument
271 %type <voidval> length_argument
272 %type <voidval> mode_name
273 %type <voidval> array_mode_name
274 %type <voidval> string_mode_name
275 %type <voidval> variant_structure_mode_name
276 %type <voidval> boolean_expression
277 %type <voidval> case_selector_list
278 %type <voidval> subexpression
279 %type <voidval> case_label_specification
280 %type <voidval> buffer_location
282 %type <voidval> single_assignment_action
298 undefined_value : FIXME
306 location : access_name
318 access_name : LOCATION_NAME
320 write_exp_elt_opcode (OP_VAR_VALUE);
321 write_exp_elt_sym ($1.sym);
322 write_exp_elt_opcode (OP_VAR_VALUE);
324 | GDB_LAST /* gdb specific */
326 write_exp_elt_opcode (OP_LAST);
327 write_exp_elt_longcst ($1);
328 write_exp_elt_opcode (OP_LAST);
330 | GDB_REGNAME /* gdb specific */
332 write_exp_elt_opcode (OP_REGISTER);
333 write_exp_elt_longcst ($1);
334 write_exp_elt_opcode (OP_REGISTER);
336 | GDB_VARIABLE /* gdb specific */
338 write_exp_elt_opcode (OP_INTERNALVAR);
339 write_exp_elt_intern ($1);
340 write_exp_elt_opcode (OP_INTERNALVAR);
350 expression_list : expression
354 | expression_list ',' expression
361 primitive_value : location_contents
377 | value_string_element
385 | value_array_element
393 | value_structure_field
397 | expression_conversion
401 | value_procedure_call
405 | value_built_in_routine_call
417 | parenthesised_expression
425 location_contents: location
433 value_name : synonym_name
437 | value_enumeration_name
449 | GENERAL_PROCEDURE_NAME
451 write_exp_elt_opcode (OP_VAR_VALUE);
452 write_exp_elt_sym ($1.sym);
453 write_exp_elt_opcode (OP_VAR_VALUE);
459 literal : INTEGER_LITERAL
461 write_exp_elt_opcode (OP_LONG);
462 write_exp_elt_type ($1.type);
463 write_exp_elt_longcst ((LONGEST) ($1.val));
464 write_exp_elt_opcode (OP_LONG);
468 write_exp_elt_opcode (OP_BOOL);
469 write_exp_elt_longcst ((LONGEST) $1);
470 write_exp_elt_opcode (OP_BOOL);
474 write_exp_elt_opcode (OP_LONG);
475 write_exp_elt_type ($1.type);
476 write_exp_elt_longcst ((LONGEST) ($1.val));
477 write_exp_elt_opcode (OP_LONG);
481 write_exp_elt_opcode (OP_DOUBLE);
482 write_exp_elt_type (builtin_type_double);
483 write_exp_elt_dblcst ($1);
484 write_exp_elt_opcode (OP_DOUBLE);
494 | CHARACTER_STRING_LITERAL
500 write_exp_elt_opcode (OP_BITSTRING);
501 write_exp_bitstring ($1);
502 write_exp_elt_opcode (OP_BITSTRING);
517 value_string_element: string_primitive_value '(' start_element ')'
525 value_string_slice: string_primitive_value '(' left_element ':' right_element ')'
529 | string_primitive_value '(' start_element UP slice_size ')'
537 value_array_element: array_primitive_value '('
538 /* This is to save the value of arglist_len
539 being accumulated for each dimension. */
540 { start_arglist (); }
543 write_exp_elt_opcode (MULTI_SUBSCRIPT);
544 write_exp_elt_longcst ((LONGEST) end_arglist ());
545 write_exp_elt_opcode (MULTI_SUBSCRIPT);
551 value_array_slice: array_primitive_value '(' lower_element ':' upper_element ')'
555 | array_primitive_value '(' first_element UP slice_size ')'
563 value_structure_field: structure_primitive_value '.' field_name
571 expression_conversion: mode_name '(' expression ')'
579 value_procedure_call: FIXME
587 value_built_in_routine_call: chill_value_built_in_routine_call
595 start_expression: FIXME
598 } /* Not in GNU-Chill */
603 zero_adic_operator: FIXME
611 parenthesised_expression: '(' expression ')'
619 expression : operand_0
623 | conditional_expression
629 conditional_expression : IF boolean_expression then_alternative else_alternative FI
633 | CASE case_selector_list OF value_case_alternative '[' ELSE sub_expression ']' ESAC
639 then_alternative: THEN subexpression
645 else_alternative: ELSE subexpression
649 | ELSIF boolean_expression then_alternative else_alternative
655 sub_expression : expression
661 value_case_alternative: case_label_specification ':' sub_expression ';'
669 operand_0 : operand_1
673 | operand_0 LOGIOR operand_1
675 write_exp_elt_opcode (BINOP_BITWISE_IOR);
677 | operand_0 ORIF operand_1
681 | operand_0 LOGXOR operand_1
683 write_exp_elt_opcode (BINOP_BITWISE_XOR);
685 | single_assignment_action
693 operand_1 : operand_2
697 | operand_1 LOGAND operand_2
699 write_exp_elt_opcode (BINOP_BITWISE_AND);
701 | operand_1 ANDIF operand_2
709 operand_2 : operand_3
713 | operand_2 '=' operand_3
715 write_exp_elt_opcode (BINOP_EQUAL);
717 | operand_2 NOTEQUAL operand_3
719 write_exp_elt_opcode (BINOP_NOTEQUAL);
721 | operand_2 '>' operand_3
723 write_exp_elt_opcode (BINOP_GTR);
725 | operand_2 GTR operand_3
727 write_exp_elt_opcode (BINOP_GEQ);
729 | operand_2 '<' operand_3
731 write_exp_elt_opcode (BINOP_LESS);
733 | operand_2 LEQ operand_3
735 write_exp_elt_opcode (BINOP_LEQ);
737 | operand_2 IN operand_3
746 operand_3 : operand_4
750 | operand_3 '+' operand_4
752 write_exp_elt_opcode (BINOP_ADD);
754 | operand_3 '-' operand_4
756 write_exp_elt_opcode (BINOP_SUB);
758 | operand_3 SLASH_SLASH operand_4
766 operand_4 : operand_5
770 | operand_4 '*' operand_5
772 write_exp_elt_opcode (BINOP_MUL);
774 | operand_4 '/' operand_5
776 write_exp_elt_opcode (BINOP_DIV);
778 | operand_4 MOD operand_5
780 write_exp_elt_opcode (BINOP_MOD);
782 | operand_4 REM operand_5
784 write_exp_elt_opcode (BINOP_REM);
790 operand_5 : operand_6
796 write_exp_elt_opcode (UNOP_NEG);
800 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
802 | '(' integer_literal_expression ')' operand_6
810 operand_6 : POINTER location
814 | RECEIVE buffer_location
827 single_assignment_action :
828 location GDB_ASSIGNMENT value
830 write_exp_elt_opcode (BINOP_ASSIGN);
836 chill_value_built_in_routine_call :
837 NUM '(' expression ')'
841 | PRED '(' expression ')'
845 | SUCC '(' expression ')'
849 | ABS '(' expression ')'
853 | CARD '(' expression ')'
857 | MAX '(' expression ')'
861 | MIN '(' expression ')'
865 | SIZE '(' location ')'
869 | SIZE '(' mode_argument ')'
873 | UPPER '(' upper_lower_argument ')'
877 | LOWER '(' upper_lower_argument ')'
881 | LENGTH '(' length_argument ')'
887 mode_argument : mode_name
891 | array_mode_name '(' expression ')'
895 | string_mode_name '(' expression ')'
899 | variant_structure_mode_name '(' expression_list ')'
905 upper_lower_argument : location
919 length_argument : location
930 /* FIXME: For now we just accept only a single integer literal. */
932 integer_literal_expression:
941 array_primitive_value : primitive_value
948 /* Things which still need productions... */
950 array_mode_name : FIXME { $$ = 0; }
951 string_mode_name : FIXME { $$ = 0; }
952 variant_structure_mode_name: FIXME { $$ = 0; }
953 synonym_name : FIXME { $$ = 0; }
954 value_enumeration_name : FIXME { $$ = 0; }
955 value_do_with_name : FIXME { $$ = 0; }
956 value_receive_name : FIXME { $$ = 0; }
957 string_primitive_value : FIXME { $$ = 0; }
958 start_element : FIXME { $$ = 0; }
959 left_element : FIXME { $$ = 0; }
960 right_element : FIXME { $$ = 0; }
961 slice_size : FIXME { $$ = 0; }
962 lower_element : FIXME { $$ = 0; }
963 upper_element : FIXME { $$ = 0; }
964 first_element : FIXME { $$ = 0; }
965 structure_primitive_value: FIXME { $$ = 0; }
966 field_name : FIXME { $$ = 0; }
967 mode_name : FIXME { $$ = 0; }
968 boolean_expression : FIXME { $$ = 0; }
969 case_selector_list : FIXME { $$ = 0; }
970 subexpression : FIXME { $$ = 0; }
971 case_label_specification: FIXME { $$ = 0; }
972 buffer_location : FIXME { $$ = 0; }
976 /* Try to consume a simple name string token. If successful, returns
977 a pointer to a nullbyte terminated copy of the name that can be used
978 in symbol table lookups. If not successful, returns NULL. */
981 match_simple_name_string ()
983 char *tokptr = lexptr;
985 if (isalpha (*tokptr))
989 } while (isalpha (*tokptr) || isdigit (*tokptr) || (*tokptr == '_'));
990 yylval.sval.ptr = lexptr;
991 yylval.sval.length = tokptr - lexptr;
993 return (copy_name (yylval.sval));
998 /* Start looking for a value composed of valid digits as set by the base
999 in use. Note that '_' characters are valid anywhere, in any quantity,
1000 and are simply ignored. Since we must find at least one valid digit,
1001 or reject this token as an integer literal, we keep track of how many
1002 digits we have encountered. */
1005 decode_integer_value (base, tokptrptr, ivalptr)
1010 char *tokptr = *tokptrptr;
1014 while (*tokptr != '\0')
1016 temp = tolower (*tokptr);
1022 case '0': case '1': case '2': case '3': case '4':
1023 case '5': case '6': case '7': case '8': case '9':
1026 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1042 /* Found something not in domain for current base. */
1043 tokptr--; /* Unconsume what gave us indigestion. */
1048 /* If we didn't find any digits, then we don't have a valid integer
1049 value, so reject the entire token. Otherwise, update the lexical
1050 scan pointer, and return non-zero for success. */
1058 *tokptrptr = tokptr;
1064 decode_integer_literal (valptr, tokptrptr)
1068 char *tokptr = *tokptrptr;
1071 int explicit_base = 0;
1073 /* Look for an explicit base specifier, which is optional. */
1106 /* If we found an explicit base ensure that the character after the
1107 explicit base is a single quote. */
1109 if (explicit_base && (*tokptr++ != '\''))
1114 /* Attempt to decode whatever follows as an integer value in the
1115 indicated base, updating the token pointer in the process and
1116 computing the value into ival. Also, if we have an explicit
1117 base, then the next character must not be a single quote, or we
1118 have a bitstring literal, so reject the entire token in this case.
1119 Otherwise, update the lexical scan pointer, and return non-zero
1122 if (!decode_integer_value (base, &tokptr, &ival))
1126 else if (explicit_base && (*tokptr == '\''))
1133 *tokptrptr = tokptr;
1138 /* If it wasn't for the fact that floating point values can contain '_'
1139 characters, we could just let strtod do all the hard work by letting it
1140 try to consume as much of the current token buffer as possible and
1141 find a legal conversion. Unfortunately we need to filter out the '_'
1142 characters before calling strtod, which we do by copying the other
1143 legal chars to a local buffer to be converted. However since we also
1144 need to keep track of where the last unconsumed character in the input
1145 buffer is, we have transfer only as many characters as may compose a
1146 legal floating point value. */
1149 match_float_literal ()
1151 char *tokptr = lexptr;
1156 extern double strtod ();
1158 /* Make local buffer in which to build the string to convert. This is
1159 required because underscores are valid in chill floating point numbers
1160 but not in the string passed to strtod to convert. The string will be
1161 no longer than our input string. */
1163 copy = buf = (char *) alloca (strlen (tokptr) + 1);
1165 /* Transfer all leading digits to the conversion buffer, discarding any
1168 while (isdigit (*tokptr) || *tokptr == '_')
1177 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1178 of whether we found any leading digits, and we simply accept it and
1179 continue on to look for the fractional part and/or exponent. One of
1180 [eEdD] is legal only if we have seen digits, and means that there
1181 is no fractional part. If we find neither of these, then this is
1182 not a floating point number, so return failure. */
1187 /* Accept and then look for fractional part and/or exponent. */
1200 goto collect_exponent;
1208 /* We found a '.', copy any fractional digits to the conversion buffer, up
1209 to the first nondigit, non-underscore character. */
1211 while (isdigit (*tokptr) || *tokptr == '_')
1220 /* Look for an exponent, which must start with one of [eEdD]. If none
1221 is found, jump directly to trying to convert what we have collected
1238 /* Accept an optional '-' or '+' following one of [eEdD]. */
1241 if (*tokptr == '+' || *tokptr == '-')
1243 *copy++ = *tokptr++;
1246 /* Now copy an exponent into the conversion buffer. Note that at the
1247 moment underscores are *not* allowed in exponents. */
1249 while (isdigit (*tokptr))
1251 *copy++ = *tokptr++;
1254 /* If we transfered any chars to the conversion buffer, try to interpret its
1255 contents as a floating point value. If any characters remain, then we
1256 must not have a valid floating point string. */
1262 dval = strtod (buf, ©);
1267 return (FLOAT_LITERAL);
1273 /* Recognize a character literal. A character literal is single character
1274 or a control sequence, enclosed in single quotes. A control sequence
1275 is a comma separated list of one or more integer literals, enclosed
1276 in parenthesis and introduced with a circumflex character.
1278 EX: 'a' '^(7)' '^(7,8)'
1280 As a GNU chill extension, the syntax C'xx' is also recognized as a
1281 character literal, where xx is a hex value for the character.
1283 Returns CHARACTER_LITERAL if a match is found.
1287 match_character_literal ()
1289 char *tokptr = lexptr;
1292 if ((tolower (*tokptr) == 'c') && (*(tokptr + 1) == '\''))
1294 /* We have a GNU chill extension form, so skip the leading "C'",
1295 decode the hex value, and then ensure that we have a trailing
1296 single quote character. */
1298 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1304 else if (*tokptr == '\'')
1308 /* Determine which form we have, either a control sequence or the
1309 single character form. */
1311 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1313 /* Match and decode a control sequence. Return zero if we don't
1314 find a valid integer literal, or if the next unconsumed character
1315 after the integer literal is not the trailing ')'.
1316 FIXME: We currently don't handle the multiple integer literal
1319 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1329 /* The trailing quote has not yet been consumed. If we don't find
1330 it, then we have no match. */
1332 if (*tokptr++ != '\'')
1339 /* Not a character literal. */
1342 yylval.typed_val.val = ival;
1343 yylval.typed_val.type = builtin_type_chill_char;
1345 return (CHARACTER_LITERAL);
1348 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1349 Note that according to 5.2.4.2, a single "_" is also a valid integer
1350 literal, however GNU-chill requires there to be at least one "digit"
1351 in any integer literal. */
1354 match_integer_literal ()
1356 char *tokptr = lexptr;
1359 if (!decode_integer_literal (&ival, &tokptr))
1365 yylval.typed_val.val = ival;
1366 yylval.typed_val.type = builtin_type_int;
1368 return (INTEGER_LITERAL);
1372 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1373 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1374 literal, however GNU-chill requires there to be at least one "digit"
1375 in any bit-string literal. */
1378 match_bitstring_literal ()
1380 char *tokptr = lexptr;
1386 static char *tempbuf;
1387 static int tempbufsize;
1388 static int tempbufindex;
1390 /* Look for the required explicit base specifier. */
1411 /* Ensure that the character after the explicit base is a single quote. */
1413 if (*tokptr++ != '\'')
1418 while (*tokptr != '\0' && *tokptr != '\'')
1420 digit = tolower (*tokptr);
1426 case '0': case '1': case '2': case '3': case '4':
1427 case '5': case '6': case '7': case '8': case '9':
1430 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1440 /* Found something not in domain for current base. */
1445 /* Extract bits from digit, starting with the msbit appropriate for
1446 the current base, and packing them into the bitstring byte,
1447 starting at the lsbit. */
1448 for (mask = (base >> 1); mask > 0; mask >>= 1)
1451 /* Grow the static temp buffer if necessary, including allocating
1452 the first one on demand. */
1453 if (tempbufindex >= tempbufsize)
1456 if (tempbuf == NULL)
1458 tempbuf = (char *) malloc (tempbufsize);
1462 tempbuf = (char *) realloc (tempbuf, tempbufsize);
1467 tempbuf[tempbufindex] |= (1 << bitoffset);
1470 if (bitoffset == HOST_CHAR_BIT)
1479 /* Verify that we consumed everything up to the trailing single quote,
1480 and that we found some bits (IE not just underbars). */
1482 if (*tokptr++ != '\'')
1488 yylval.sval.ptr = tempbuf;
1489 yylval.sval.length = bitcount;
1491 return (BIT_STRING_LITERAL);
1495 /* Recognize tokens that start with '$'. These include:
1497 $regname A native register name or a "standard
1499 Return token GDB_REGNAME.
1501 $variable A convenience variable with a name chosen
1503 Return token GDB_VARIABLE.
1505 $digits Value history with index <digits>, starting
1506 from the first value which has index 1.
1509 $$digits Value history with index <digits> relative
1510 to the last value. I.E. $$0 is the last
1511 value, $$1 is the one previous to that, $$2
1512 is the one previous to $$1, etc.
1513 Return token GDB_LAST.
1515 $ | $0 | $$0 The last value in the value history.
1516 Return token GDB_LAST.
1518 $$ An abbreviation for the second to the last
1519 value in the value history, I.E. $$1
1520 Return token GDB_LAST.
1522 Note that we currently assume that register names and convenience
1523 variables follow the convention of starting with a letter or '_'.
1528 match_dollar_tokens ()
1536 /* We will always have a successful match, even if it is just for
1537 a single '$', the abbreviation for $$0. So advance lexptr. */
1541 if (*tokptr == '_' || isalpha (*tokptr))
1543 /* Look for a match with a native register name, usually something
1544 like "r0" for example. */
1546 for (regno = 0; regno < NUM_REGS; regno++)
1548 namelength = strlen (reg_names[regno]);
1549 if (STREQN (tokptr, reg_names[regno], namelength)
1550 && !isalnum (tokptr[namelength]))
1552 yylval.lval = regno;
1553 lexptr += namelength + 1;
1554 return (GDB_REGNAME);
1558 /* Look for a match with a standard register name, usually something
1559 like "pc", which gdb always recognizes as the program counter
1560 regardless of what the native register name is. */
1562 for (regno = 0; regno < num_std_regs; regno++)
1564 namelength = strlen (std_regs[regno].name);
1565 if (STREQN (tokptr, std_regs[regno].name, namelength)
1566 && !isalnum (tokptr[namelength]))
1568 yylval.lval = std_regs[regno].regnum;
1569 lexptr += namelength;
1570 return (GDB_REGNAME);
1574 /* Attempt to match against a convenience variable. Note that
1575 this will always succeed, because if no variable of that name
1576 already exists, the lookup_internalvar will create one for us.
1577 Also note that both lexptr and tokptr currently point to the
1578 start of the input string we are trying to match, and that we
1579 have already tested the first character for non-numeric, so we
1580 don't have to treat it specially. */
1582 while (*tokptr == '_' || isalnum (*tokptr))
1586 yylval.sval.ptr = lexptr;
1587 yylval.sval.length = tokptr - lexptr;
1588 yylval.ivar = lookup_internalvar (copy_name (yylval.sval));
1590 return (GDB_VARIABLE);
1593 /* Since we didn't match against a register name or convenience
1594 variable, our only choice left is a history value. */
1608 /* Attempt to decode more characters as an integer value giving
1609 the index in the history list. If successful, the value will
1610 overwrite ival (currently 0 or 1), and if not, ival will be
1611 left alone, which is good since it is currently correct for
1612 the '$' or '$$' case. */
1614 decode_integer_literal (&ival, &tokptr);
1615 yylval.lval = negate ? -ival : ival;
1626 static const struct token tokentab6[] =
1628 { "LENGTH", LENGTH }
1631 static const struct token tokentab5[] =
1638 static const struct token tokentab4[] =
1647 static const struct token tokentab3[] =
1660 static const struct token tokentab2[] =
1662 { ":=", GDB_ASSIGNMENT },
1663 { "//", SLASH_SLASH },
1671 /* Read one token, getting characters through lexptr. */
1672 /* This is where we will check to make sure that the language and the
1673 operators used are compatible. */
1683 /* Skip over any leading whitespace. */
1684 while (isspace (*lexptr))
1688 /* Look for special single character cases which can't be the first
1689 character of some other multicharacter token. */
1708 /* Look for characters which start a particular kind of multicharacter
1709 token, such as a character literal, register name, convenience
1710 variable name, etc. */
1716 token = match_character_literal ();
1723 token = match_dollar_tokens ();
1730 /* See if it is a special token of length 6. */
1731 for (i = 0; i < sizeof (tokentab6) / sizeof (tokentab6[0]); i++)
1733 if (STREQN (lexptr, tokentab6[i].operator, 6))
1736 return (tokentab6[i].token);
1739 /* See if it is a special token of length 5. */
1740 for (i = 0; i < sizeof (tokentab5) / sizeof (tokentab5[0]); i++)
1742 if (STREQN (lexptr, tokentab5[i].operator, 5))
1745 return (tokentab5[i].token);
1748 /* See if it is a special token of length 4. */
1749 for (i = 0; i < sizeof (tokentab4) / sizeof (tokentab4[0]); i++)
1751 if (STREQN (lexptr, tokentab4[i].operator, 4))
1754 return (tokentab4[i].token);
1757 /* See if it is a special token of length 3. */
1758 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1760 if (STREQN (lexptr, tokentab3[i].operator, 3))
1763 return (tokentab3[i].token);
1766 /* See if it is a special token of length 2. */
1767 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1769 if (STREQN (lexptr, tokentab2[i].operator, 2))
1772 return (tokentab2[i].token);
1775 /* Look for single character cases which which could be the first
1776 character of some other multicharacter token, but aren't, or we
1777 would already have found it. */
1786 /* Look for other special tokens. */
1787 if (STREQN (lexptr, "TRUE", 4)) /* FIXME: What about lowercase? */
1791 return (BOOLEAN_LITERAL);
1793 if (STREQN (lexptr, "FALSE", 5)) /* FIXME: What about lowercase? */
1797 return (BOOLEAN_LITERAL);
1799 /* Look for a float literal before looking for an integer literal, so
1800 we match as much of the input stream as possible. */
1801 token = match_float_literal ();
1806 token = match_bitstring_literal ();
1811 token = match_integer_literal ();
1817 /* Try to match a simple name string, and if a match is found, then
1818 further classify what sort of name it is and return an appropriate
1819 token. Note that attempting to match a simple name string consumes
1820 the token from lexptr, so we can't back out if we later find that
1821 we can't classify what sort of name it is. */
1823 simplename = match_simple_name_string ();
1824 if (simplename != NULL)
1826 sym = lookup_symbol (simplename, expression_context_block,
1827 VAR_NAMESPACE, (int *) NULL,
1828 (struct symtab **) NULL);
1831 yylval.ssym.stoken.ptr = NULL;
1832 yylval.ssym.stoken.length = 0;
1833 yylval.ssym.sym = sym;
1834 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1835 switch (SYMBOL_CLASS (sym))
1838 /* Found a procedure name. */
1839 return (GENERAL_PROCEDURE_NAME);
1841 /* Found a global or local static variable. */
1842 return (LOCATION_NAME);
1849 if (innermost_block == NULL
1850 || contained_in (block_found, innermost_block))
1852 innermost_block = block_found;
1854 return (LOCATION_NAME);
1858 return (LOCATION_NAME);
1862 case LOC_CONST_BYTES:
1863 error ("Symbol \"%s\" names no location.", simplename);
1867 else if (!have_full_symbols () && !have_partial_symbols ())
1869 error ("No symbol table is loaded. Use the \"file\" command.");
1873 error ("No symbol \"%s\" in current context.", simplename);
1877 /* Catch single character tokens which are not part of some
1882 case '.': /* Not float for example. */
1886 return (ILLEGAL_TOKEN);
1891 char *msg; /* unused */
1893 printf ("Parsing: %s\n", lexptr);
1896 error ("Invalid syntax in expression near character '%c'.", yychar);
1900 error ("Invalid syntax in expression");