1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright 1992, 1993 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.
58 #include "expression.h"
61 #include "parser-defs.h"
64 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
65 as well as gratuitiously global symbol names, so we can have multiple
66 yacc generated parsers in gdb. Note that these are only the variables
67 produced by yacc. If other parser generators (bison, byacc, etc) produce
68 additional global names that conflict at link time, then those parser
69 generators need to be fixed instead of adding those names to this list. */
71 #define yymaxdepth chill_maxdepth
72 #define yyparse chill_parse
73 #define yylex chill_lex
74 #define yyerror chill_error
75 #define yylval chill_lval
76 #define yychar chill_char
77 #define yydebug chill_debug
78 #define yypact chill_pact
81 #define yydef chill_def
82 #define yychk chill_chk
83 #define yypgo chill_pgo
84 #define yyact chill_act
85 #define yyexca chill_exca
86 #define yyerrflag chill_errflag
87 #define yynerrs chill_nerrs
91 #define yy_yys chill_yys
92 #define yystate chill_state
93 #define yytmp chill_tmp
95 #define yy_yyv chill_yyv
96 #define yyval chill_val
97 #define yylloc chill_lloc
98 #define yyreds chill_reds /* With YYDEBUG defined */
99 #define yytoks chill_toks /* With YYDEBUG defined */
102 #define YYDEBUG 0 /* Default to no yydebug support */
106 yyparse PARAMS ((void));
109 yylex PARAMS ((void));
112 yyerror PARAMS ((char *));
116 /* Although the yacc "value" of an expression is not used,
117 since the result is stored in the structure being created,
118 other node types do have values. */
123 unsigned LONGEST ulval;
133 struct symtoken ssym;
136 enum exp_opcode opcode;
137 struct internalvar *ivar;
143 %token <voidval> FIXME_01
144 %token <voidval> FIXME_02
145 %token <voidval> FIXME_03
146 %token <voidval> FIXME_04
147 %token <voidval> FIXME_05
148 %token <voidval> FIXME_06
149 %token <voidval> FIXME_07
150 %token <voidval> FIXME_08
151 %token <voidval> FIXME_09
152 %token <voidval> FIXME_10
153 %token <voidval> FIXME_11
154 %token <voidval> FIXME_12
155 %token <voidval> FIXME_13
156 %token <voidval> FIXME_14
157 %token <voidval> FIXME_15
158 %token <voidval> FIXME_16
159 %token <voidval> FIXME_17
160 %token <voidval> FIXME_18
161 %token <voidval> FIXME_19
162 %token <voidval> FIXME_20
163 %token <voidval> FIXME_21
164 %token <voidval> FIXME_22
165 %token <voidval> FIXME_24
166 %token <voidval> FIXME_25
167 %token <voidval> FIXME_26
168 %token <voidval> FIXME_27
169 %token <voidval> FIXME_28
170 %token <voidval> FIXME_29
171 %token <voidval> FIXME_30
173 %token <typed_val> INTEGER_LITERAL
174 %token <ulval> BOOLEAN_LITERAL
175 %token <typed_val> CHARACTER_LITERAL
176 %token <dval> FLOAT_LITERAL
177 %token <ssym> GENERAL_PROCEDURE_NAME
178 %token <ssym> LOCATION_NAME
179 %token <voidval> SET_LITERAL
180 %token <voidval> EMPTINESS_LITERAL
181 %token <sval> CHARACTER_STRING_LITERAL
182 %token <sval> BIT_STRING_LITERAL
183 %token <tsym> TYPENAME
184 %token <sval> FIELD_NAME
189 %token <voidval> CASE
191 %token <voidval> ESAC
192 %token <voidval> LOGIOR
193 %token <voidval> ORIF
194 %token <voidval> LOGXOR
195 %token <voidval> LOGAND
196 %token <voidval> ANDIF
198 %token <voidval> NOTEQUAL
208 %token <voidval> SLASH_SLASH
212 %token <voidval> POINTER
213 %token <voidval> RECEIVE
220 %token <voidval> THEN
221 %token <voidval> ELSE
223 %token <voidval> ELSIF
224 %token <voidval> ILLEGAL_TOKEN
226 %token <voidval> PRED
227 %token <voidval> SUCC
229 %token <voidval> CARD
230 %token <voidval> MAX_TOKEN
231 %token <voidval> MIN_TOKEN
232 %token <voidval> SIZE
233 %token <voidval> UPPER
234 %token <voidval> LOWER
235 %token <voidval> LENGTH
237 /* Tokens which are not Chill tokens used in expressions, but rather GDB
238 specific things that we recognize in the same context as Chill tokens
239 (register names for example). */
241 %token <lval> GDB_REGNAME /* Machine register name */
242 %token <lval> GDB_LAST /* Value history */
243 %token <ivar> GDB_VARIABLE /* Convenience variable */
244 %token <voidval> GDB_ASSIGNMENT /* Assign value to somewhere */
246 %type <voidval> location
247 %type <voidval> access_name
248 %type <voidval> primitive_value
249 %type <voidval> location_contents
250 %type <voidval> value_name
251 %type <voidval> literal
252 %type <voidval> tuple
253 %type <voidval> value_string_element
254 %type <voidval> value_string_slice
255 %type <voidval> value_array_element
256 %type <voidval> value_array_slice
257 %type <voidval> value_structure_field
258 %type <voidval> expression_conversion
259 %type <voidval> value_procedure_call
260 %type <voidval> value_built_in_routine_call
261 %type <voidval> chill_value_built_in_routine_call
262 %type <voidval> start_expression
263 %type <voidval> zero_adic_operator
264 %type <voidval> parenthesised_expression
265 %type <voidval> value
266 %type <voidval> undefined_value
267 %type <voidval> expression
268 %type <voidval> conditional_expression
269 %type <voidval> then_alternative
270 %type <voidval> else_alternative
271 %type <voidval> sub_expression
272 %type <voidval> value_case_alternative
273 %type <voidval> operand_0
274 %type <voidval> operand_1
275 %type <voidval> operand_2
276 %type <voidval> operand_3
277 %type <voidval> operand_4
278 %type <voidval> operand_5
279 %type <voidval> operand_6
280 %type <voidval> synonym_name
281 %type <voidval> value_enumeration_name
282 %type <voidval> value_do_with_name
283 %type <voidval> value_receive_name
284 %type <voidval> string_primitive_value
285 %type <voidval> start_element
286 %type <voidval> left_element
287 %type <voidval> right_element
288 %type <voidval> slice_size
289 %type <voidval> array_primitive_value
290 %type <voidval> expression_list
291 %type <voidval> lower_element
292 %type <voidval> upper_element
293 %type <voidval> first_element
294 %type <voidval> mode_argument
295 %type <voidval> upper_lower_argument
296 %type <voidval> length_argument
297 %type <voidval> array_mode_name
298 %type <voidval> string_mode_name
299 %type <voidval> variant_structure_mode_name
300 %type <voidval> boolean_expression
301 %type <voidval> case_selector_list
302 %type <voidval> subexpression
303 %type <voidval> case_label_specification
304 %type <voidval> buffer_location
305 %type <voidval> single_assignment_action
306 %type <tsym> mode_name
314 { write_exp_elt_opcode(OP_TYPE);
315 write_exp_elt_type($1.type);
316 write_exp_elt_opcode(OP_TYPE);}
329 undefined_value : FIXME_01
337 location : access_name
338 | primitive_value POINTER
340 write_exp_elt_opcode (UNOP_IND);
346 access_name : LOCATION_NAME
348 write_exp_elt_opcode (OP_VAR_VALUE);
349 write_exp_elt_block (NULL);
350 write_exp_elt_sym ($1.sym);
351 write_exp_elt_opcode (OP_VAR_VALUE);
353 | GDB_LAST /* gdb specific */
355 write_exp_elt_opcode (OP_LAST);
356 write_exp_elt_longcst ($1);
357 write_exp_elt_opcode (OP_LAST);
359 | GDB_REGNAME /* gdb specific */
361 write_exp_elt_opcode (OP_REGISTER);
362 write_exp_elt_longcst ($1);
363 write_exp_elt_opcode (OP_REGISTER);
365 | GDB_VARIABLE /* gdb specific */
367 write_exp_elt_opcode (OP_INTERNALVAR);
368 write_exp_elt_intern ($1);
369 write_exp_elt_opcode (OP_INTERNALVAR);
379 expression_list : expression
383 | expression_list ',' expression
390 primitive_value : location_contents
406 | value_string_element
414 | value_array_element
422 | value_structure_field
426 | expression_conversion
430 | value_procedure_call
434 | value_built_in_routine_call
446 | parenthesised_expression
454 location_contents: location
462 value_name : synonym_name
466 | value_enumeration_name
478 | GENERAL_PROCEDURE_NAME
480 write_exp_elt_opcode (OP_VAR_VALUE);
481 write_exp_elt_block (NULL);
482 write_exp_elt_sym ($1.sym);
483 write_exp_elt_opcode (OP_VAR_VALUE);
489 literal : INTEGER_LITERAL
491 write_exp_elt_opcode (OP_LONG);
492 write_exp_elt_type ($1.type);
493 write_exp_elt_longcst ((LONGEST) ($1.val));
494 write_exp_elt_opcode (OP_LONG);
498 write_exp_elt_opcode (OP_BOOL);
499 write_exp_elt_longcst ((LONGEST) $1);
500 write_exp_elt_opcode (OP_BOOL);
504 write_exp_elt_opcode (OP_LONG);
505 write_exp_elt_type ($1.type);
506 write_exp_elt_longcst ((LONGEST) ($1.val));
507 write_exp_elt_opcode (OP_LONG);
511 write_exp_elt_opcode (OP_DOUBLE);
512 write_exp_elt_type (builtin_type_double);
513 write_exp_elt_dblcst ($1);
514 write_exp_elt_opcode (OP_DOUBLE);
524 | CHARACTER_STRING_LITERAL
526 write_exp_elt_opcode (OP_STRING);
527 write_exp_string ($1);
528 write_exp_elt_opcode (OP_STRING);
532 write_exp_elt_opcode (OP_BITSTRING);
533 write_exp_bitstring ($1);
534 write_exp_elt_opcode (OP_BITSTRING);
549 value_string_element: string_primitive_value '(' start_element ')'
557 value_string_slice: string_primitive_value '(' left_element ':' right_element ')'
561 | string_primitive_value '(' start_element UP slice_size ')'
569 value_array_element: array_primitive_value '('
570 /* This is to save the value of arglist_len
571 being accumulated for each dimension. */
572 { start_arglist (); }
575 write_exp_elt_opcode (MULTI_SUBSCRIPT);
576 write_exp_elt_longcst ((LONGEST) end_arglist ());
577 write_exp_elt_opcode (MULTI_SUBSCRIPT);
583 value_array_slice: array_primitive_value '(' lower_element ':' upper_element ')'
587 | array_primitive_value '(' first_element UP slice_size ')'
595 value_structure_field: primitive_value FIELD_NAME
596 { write_exp_elt_opcode (STRUCTOP_STRUCT);
597 write_exp_string ($2);
598 write_exp_elt_opcode (STRUCTOP_STRUCT);
604 expression_conversion: mode_name parenthesised_expression
606 write_exp_elt_opcode (UNOP_CAST);
607 write_exp_elt_type ($1.type);
608 write_exp_elt_opcode (UNOP_CAST);
614 value_procedure_call: FIXME_05
622 value_built_in_routine_call: chill_value_built_in_routine_call
630 start_expression: FIXME_06
633 } /* Not in GNU-Chill */
638 zero_adic_operator: FIXME_07
646 parenthesised_expression: '(' expression ')'
654 expression : operand_0
658 | single_assignment_action
662 | conditional_expression
668 conditional_expression : IF boolean_expression then_alternative else_alternative FI
672 | CASE case_selector_list OF value_case_alternative '[' ELSE sub_expression ']' ESAC
678 then_alternative: THEN subexpression
684 else_alternative: ELSE subexpression
688 | ELSIF boolean_expression then_alternative else_alternative
694 sub_expression : expression
700 value_case_alternative: case_label_specification ':' sub_expression ';'
708 operand_0 : operand_1
712 | operand_0 LOGIOR operand_1
714 write_exp_elt_opcode (BINOP_BITWISE_IOR);
716 | operand_0 ORIF operand_1
720 | operand_0 LOGXOR operand_1
722 write_exp_elt_opcode (BINOP_BITWISE_XOR);
728 operand_1 : operand_2
732 | operand_1 LOGAND operand_2
734 write_exp_elt_opcode (BINOP_BITWISE_AND);
736 | operand_1 ANDIF operand_2
744 operand_2 : operand_3
748 | operand_2 '=' operand_3
750 write_exp_elt_opcode (BINOP_EQUAL);
752 | operand_2 NOTEQUAL operand_3
754 write_exp_elt_opcode (BINOP_NOTEQUAL);
756 | operand_2 '>' operand_3
758 write_exp_elt_opcode (BINOP_GTR);
760 | operand_2 GTR operand_3
762 write_exp_elt_opcode (BINOP_GEQ);
764 | operand_2 '<' operand_3
766 write_exp_elt_opcode (BINOP_LESS);
768 | operand_2 LEQ operand_3
770 write_exp_elt_opcode (BINOP_LEQ);
772 | operand_2 IN operand_3
781 operand_3 : operand_4
785 | operand_3 '+' operand_4
787 write_exp_elt_opcode (BINOP_ADD);
789 | operand_3 '-' operand_4
791 write_exp_elt_opcode (BINOP_SUB);
793 | operand_3 SLASH_SLASH operand_4
795 write_exp_elt_opcode (BINOP_CONCAT);
801 operand_4 : operand_5
805 | operand_4 '*' operand_5
807 write_exp_elt_opcode (BINOP_MUL);
809 | operand_4 '/' operand_5
811 write_exp_elt_opcode (BINOP_DIV);
813 | operand_4 MOD operand_5
815 write_exp_elt_opcode (BINOP_MOD);
817 | operand_4 REM operand_5
819 write_exp_elt_opcode (BINOP_REM);
825 operand_5 : operand_6
831 write_exp_elt_opcode (UNOP_NEG);
835 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
837 | parenthesised_expression literal
838 /* We require the string operand to be a literal, to avoid some
839 nasty parsing ambiguities. */
841 write_exp_elt_opcode (BINOP_CONCAT);
847 operand_6 : POINTER location
849 write_exp_elt_opcode (UNOP_ADDR);
851 | RECEIVE buffer_location
864 single_assignment_action :
865 location GDB_ASSIGNMENT value
867 write_exp_elt_opcode (BINOP_ASSIGN);
873 chill_value_built_in_routine_call :
874 NUM '(' expression ')'
878 | PRED '(' expression ')'
882 | SUCC '(' expression ')'
886 | ABS '(' expression ')'
890 | CARD '(' expression ')'
894 | MAX_TOKEN '(' expression ')'
898 | MIN_TOKEN '(' expression ')'
902 | SIZE '(' location ')'
906 | SIZE '(' mode_argument ')'
910 | UPPER '(' upper_lower_argument ')'
914 | LOWER '(' upper_lower_argument ')'
918 | LENGTH '(' length_argument ')'
924 mode_argument : mode_name
928 | array_mode_name '(' expression ')'
932 | string_mode_name '(' expression ')'
936 | variant_structure_mode_name '(' expression_list ')'
945 upper_lower_argument : expression
955 length_argument : expression
963 array_primitive_value : primitive_value
970 /* Things which still need productions... */
972 array_mode_name : FIXME_08 { $$ = 0; }
973 string_mode_name : FIXME_09 { $$ = 0; }
974 variant_structure_mode_name: FIXME_10 { $$ = 0; }
975 synonym_name : FIXME_11 { $$ = 0; }
976 value_enumeration_name : FIXME_12 { $$ = 0; }
977 value_do_with_name : FIXME_13 { $$ = 0; }
978 value_receive_name : FIXME_14 { $$ = 0; }
979 string_primitive_value : FIXME_15 { $$ = 0; }
980 start_element : FIXME_16 { $$ = 0; }
981 left_element : FIXME_17 { $$ = 0; }
982 right_element : FIXME_18 { $$ = 0; }
983 slice_size : FIXME_19 { $$ = 0; }
984 lower_element : FIXME_20 { $$ = 0; }
985 upper_element : FIXME_21 { $$ = 0; }
986 first_element : FIXME_22 { $$ = 0; }
987 boolean_expression : FIXME_26 { $$ = 0; }
988 case_selector_list : FIXME_27 { $$ = 0; }
989 subexpression : FIXME_28 { $$ = 0; }
990 case_label_specification: FIXME_29 { $$ = 0; }
991 buffer_location : FIXME_30 { $$ = 0; }
995 /* Implementation of a dynamically expandable buffer for processing input
996 characters acquired through lexptr and building a value to return in
999 static char *tempbuf; /* Current buffer contents */
1000 static int tempbufsize; /* Size of allocated buffer */
1001 static int tempbufindex; /* Current index into buffer */
1003 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
1005 #define CHECKBUF(size) \
1007 if (tempbufindex + (size) >= tempbufsize) \
1009 growbuf_by_size (size); \
1013 /* Grow the static temp buffer if necessary, including allocating the first one
1017 growbuf_by_size (count)
1022 growby = max (count, GROWBY_MIN_SIZE);
1023 tempbufsize += growby;
1024 if (tempbuf == NULL)
1026 tempbuf = (char *) malloc (tempbufsize);
1030 tempbuf = (char *) realloc (tempbuf, tempbufsize);
1034 /* Try to consume a simple name string token. If successful, returns
1035 a pointer to a nullbyte terminated copy of the name that can be used
1036 in symbol table lookups. If not successful, returns NULL. */
1039 match_simple_name_string ()
1041 char *tokptr = lexptr;
1043 if (isalpha (*tokptr))
1048 } while (isalnum (*tokptr) || (*tokptr == '_'));
1049 yylval.sval.ptr = lexptr;
1050 yylval.sval.length = tokptr - lexptr;
1052 result = copy_name (yylval.sval);
1053 for (tokptr = result; *tokptr; tokptr++)
1054 if (isupper (*tokptr))
1055 *tokptr = tolower(*tokptr);
1061 /* Start looking for a value composed of valid digits as set by the base
1062 in use. Note that '_' characters are valid anywhere, in any quantity,
1063 and are simply ignored. Since we must find at least one valid digit,
1064 or reject this token as an integer literal, we keep track of how many
1065 digits we have encountered. */
1068 decode_integer_value (base, tokptrptr, ivalptr)
1073 char *tokptr = *tokptrptr;
1077 while (*tokptr != '\0')
1079 temp = tolower (*tokptr);
1085 case '0': case '1': case '2': case '3': case '4':
1086 case '5': case '6': case '7': case '8': case '9':
1089 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1105 /* Found something not in domain for current base. */
1106 tokptr--; /* Unconsume what gave us indigestion. */
1111 /* If we didn't find any digits, then we don't have a valid integer
1112 value, so reject the entire token. Otherwise, update the lexical
1113 scan pointer, and return non-zero for success. */
1121 *tokptrptr = tokptr;
1127 decode_integer_literal (valptr, tokptrptr)
1131 char *tokptr = *tokptrptr;
1134 int explicit_base = 0;
1136 /* Look for an explicit base specifier, which is optional. */
1169 /* If we found an explicit base ensure that the character after the
1170 explicit base is a single quote. */
1172 if (explicit_base && (*tokptr++ != '\''))
1177 /* Attempt to decode whatever follows as an integer value in the
1178 indicated base, updating the token pointer in the process and
1179 computing the value into ival. Also, if we have an explicit
1180 base, then the next character must not be a single quote, or we
1181 have a bitstring literal, so reject the entire token in this case.
1182 Otherwise, update the lexical scan pointer, and return non-zero
1185 if (!decode_integer_value (base, &tokptr, &ival))
1189 else if (explicit_base && (*tokptr == '\''))
1196 *tokptrptr = tokptr;
1201 /* If it wasn't for the fact that floating point values can contain '_'
1202 characters, we could just let strtod do all the hard work by letting it
1203 try to consume as much of the current token buffer as possible and
1204 find a legal conversion. Unfortunately we need to filter out the '_'
1205 characters before calling strtod, which we do by copying the other
1206 legal chars to a local buffer to be converted. However since we also
1207 need to keep track of where the last unconsumed character in the input
1208 buffer is, we have transfer only as many characters as may compose a
1209 legal floating point value. */
1212 match_float_literal ()
1214 char *tokptr = lexptr;
1218 extern double strtod ();
1220 /* Make local buffer in which to build the string to convert. This is
1221 required because underscores are valid in chill floating point numbers
1222 but not in the string passed to strtod to convert. The string will be
1223 no longer than our input string. */
1225 copy = buf = (char *) alloca (strlen (tokptr) + 1);
1227 /* Transfer all leading digits to the conversion buffer, discarding any
1230 while (isdigit (*tokptr) || *tokptr == '_')
1239 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1240 of whether we found any leading digits, and we simply accept it and
1241 continue on to look for the fractional part and/or exponent. One of
1242 [eEdD] is legal only if we have seen digits, and means that there
1243 is no fractional part. If we find neither of these, then this is
1244 not a floating point number, so return failure. */
1249 /* Accept and then look for fractional part and/or exponent. */
1262 goto collect_exponent;
1270 /* We found a '.', copy any fractional digits to the conversion buffer, up
1271 to the first nondigit, non-underscore character. */
1273 while (isdigit (*tokptr) || *tokptr == '_')
1282 /* Look for an exponent, which must start with one of [eEdD]. If none
1283 is found, jump directly to trying to convert what we have collected
1300 /* Accept an optional '-' or '+' following one of [eEdD]. */
1303 if (*tokptr == '+' || *tokptr == '-')
1305 *copy++ = *tokptr++;
1308 /* Now copy an exponent into the conversion buffer. Note that at the
1309 moment underscores are *not* allowed in exponents. */
1311 while (isdigit (*tokptr))
1313 *copy++ = *tokptr++;
1316 /* If we transfered any chars to the conversion buffer, try to interpret its
1317 contents as a floating point value. If any characters remain, then we
1318 must not have a valid floating point string. */
1324 dval = strtod (buf, ©);
1329 return (FLOAT_LITERAL);
1335 /* Recognize a string literal. A string literal is a nonzero sequence
1336 of characters enclosed in matching single or double quotes, except that
1337 a single character inside single quotes is a character literal, which
1338 we reject as a string literal. To embed the terminator character inside
1339 a string, it is simply doubled (I.E. "this""is""one""string") */
1342 match_string_literal ()
1344 char *tokptr = lexptr;
1346 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1349 if (*tokptr == *lexptr)
1351 if (*(tokptr + 1) == *lexptr)
1360 tempbuf[tempbufindex++] = *tokptr;
1362 if (*tokptr == '\0' /* no terminator */
1363 || tempbufindex == 0 /* no string */
1364 || (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
1370 tempbuf[tempbufindex] = '\0';
1371 yylval.sval.ptr = tempbuf;
1372 yylval.sval.length = tempbufindex;
1374 return (CHARACTER_STRING_LITERAL);
1378 /* Recognize a character literal. A character literal is single character
1379 or a control sequence, enclosed in single quotes. A control sequence
1380 is a comma separated list of one or more integer literals, enclosed
1381 in parenthesis and introduced with a circumflex character.
1383 EX: 'a' '^(7)' '^(7,8)'
1385 As a GNU chill extension, the syntax C'xx' is also recognized as a
1386 character literal, where xx is a hex value for the character.
1388 Note that more than a single character, enclosed in single quotes, is
1391 Also note that the control sequence form is not in GNU Chill since it
1392 is ambiguous with the string literal form using single quotes. I.E.
1393 is '^(7)' a character literal or a string literal. In theory it it
1394 possible to tell by context, but GNU Chill doesn't accept the control
1395 sequence form, so neither do we (for now the code is disabled).
1397 Returns CHARACTER_LITERAL if a match is found.
1401 match_character_literal ()
1403 char *tokptr = lexptr;
1406 if ((tolower (*tokptr) == 'c') && (*(tokptr + 1) == '\''))
1408 /* We have a GNU chill extension form, so skip the leading "C'",
1409 decode the hex value, and then ensure that we have a trailing
1410 single quote character. */
1412 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1418 else if (*tokptr == '\'')
1422 /* Determine which form we have, either a control sequence or the
1423 single character form. */
1425 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1427 #if 0 /* Disable, see note above. -fnf */
1428 /* Match and decode a control sequence. Return zero if we don't
1429 find a valid integer literal, or if the next unconsumed character
1430 after the integer literal is not the trailing ')'.
1431 FIXME: We currently don't handle the multiple integer literal
1434 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1447 /* The trailing quote has not yet been consumed. If we don't find
1448 it, then we have no match. */
1450 if (*tokptr++ != '\'')
1457 /* Not a character literal. */
1460 yylval.typed_val.val = ival;
1461 yylval.typed_val.type = builtin_type_chill_char;
1463 return (CHARACTER_LITERAL);
1466 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1467 Note that according to 5.2.4.2, a single "_" is also a valid integer
1468 literal, however GNU-chill requires there to be at least one "digit"
1469 in any integer literal. */
1472 match_integer_literal ()
1474 char *tokptr = lexptr;
1477 if (!decode_integer_literal (&ival, &tokptr))
1483 yylval.typed_val.val = ival;
1484 yylval.typed_val.type = builtin_type_int;
1486 return (INTEGER_LITERAL);
1490 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1491 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1492 literal, however GNU-chill requires there to be at least one "digit"
1493 in any bit-string literal. */
1496 match_bitstring_literal ()
1498 char *tokptr = lexptr;
1507 /* Look for the required explicit base specifier. */
1528 /* Ensure that the character after the explicit base is a single quote. */
1530 if (*tokptr++ != '\'')
1535 while (*tokptr != '\0' && *tokptr != '\'')
1537 digit = tolower (*tokptr);
1543 case '0': case '1': case '2': case '3': case '4':
1544 case '5': case '6': case '7': case '8': case '9':
1547 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1557 /* Found something not in domain for current base. */
1562 /* Extract bits from digit, starting with the msbit appropriate for
1563 the current base, and packing them into the bitstring byte,
1564 starting at the lsbit. */
1565 for (mask = (base >> 1); mask > 0; mask >>= 1)
1571 tempbuf[tempbufindex] |= (1 << bitoffset);
1574 if (bitoffset == HOST_CHAR_BIT)
1583 /* Verify that we consumed everything up to the trailing single quote,
1584 and that we found some bits (IE not just underbars). */
1586 if (*tokptr++ != '\'')
1592 yylval.sval.ptr = tempbuf;
1593 yylval.sval.length = bitcount;
1595 return (BIT_STRING_LITERAL);
1599 /* Recognize tokens that start with '$'. These include:
1601 $regname A native register name or a "standard
1603 Return token GDB_REGNAME.
1605 $variable A convenience variable with a name chosen
1607 Return token GDB_VARIABLE.
1609 $digits Value history with index <digits>, starting
1610 from the first value which has index 1.
1613 $$digits Value history with index <digits> relative
1614 to the last value. I.E. $$0 is the last
1615 value, $$1 is the one previous to that, $$2
1616 is the one previous to $$1, etc.
1617 Return token GDB_LAST.
1619 $ | $0 | $$0 The last value in the value history.
1620 Return token GDB_LAST.
1622 $$ An abbreviation for the second to the last
1623 value in the value history, I.E. $$1
1624 Return token GDB_LAST.
1626 Note that we currently assume that register names and convenience
1627 variables follow the convention of starting with a letter or '_'.
1632 match_dollar_tokens ()
1640 /* We will always have a successful match, even if it is just for
1641 a single '$', the abbreviation for $$0. So advance lexptr. */
1645 if (*tokptr == '_' || isalpha (*tokptr))
1647 /* Look for a match with a native register name, usually something
1648 like "r0" for example. */
1650 for (regno = 0; regno < NUM_REGS; regno++)
1652 namelength = strlen (reg_names[regno]);
1653 if (STREQN (tokptr, reg_names[regno], namelength)
1654 && !isalnum (tokptr[namelength]))
1656 yylval.lval = regno;
1657 lexptr += namelength + 1;
1658 return (GDB_REGNAME);
1662 /* Look for a match with a standard register name, usually something
1663 like "pc", which gdb always recognizes as the program counter
1664 regardless of what the native register name is. */
1666 for (regno = 0; regno < num_std_regs; regno++)
1668 namelength = strlen (std_regs[regno].name);
1669 if (STREQN (tokptr, std_regs[regno].name, namelength)
1670 && !isalnum (tokptr[namelength]))
1672 yylval.lval = std_regs[regno].regnum;
1673 lexptr += namelength;
1674 return (GDB_REGNAME);
1678 /* Attempt to match against a convenience variable. Note that
1679 this will always succeed, because if no variable of that name
1680 already exists, the lookup_internalvar will create one for us.
1681 Also note that both lexptr and tokptr currently point to the
1682 start of the input string we are trying to match, and that we
1683 have already tested the first character for non-numeric, so we
1684 don't have to treat it specially. */
1686 while (*tokptr == '_' || isalnum (*tokptr))
1690 yylval.sval.ptr = lexptr;
1691 yylval.sval.length = tokptr - lexptr;
1692 yylval.ivar = lookup_internalvar (copy_name (yylval.sval));
1694 return (GDB_VARIABLE);
1697 /* Since we didn't match against a register name or convenience
1698 variable, our only choice left is a history value. */
1712 /* Attempt to decode more characters as an integer value giving
1713 the index in the history list. If successful, the value will
1714 overwrite ival (currently 0 or 1), and if not, ival will be
1715 left alone, which is good since it is currently correct for
1716 the '$' or '$$' case. */
1718 decode_integer_literal (&ival, &tokptr);
1719 yylval.lval = negate ? -ival : ival;
1730 static const struct token idtokentab[] =
1732 { "length", LENGTH },
1743 { "max", MAX_TOKEN },
1744 { "min", MIN_TOKEN },
1754 static const struct token tokentab2[] =
1756 { ":=", GDB_ASSIGNMENT },
1757 { "//", SLASH_SLASH },
1764 /* Read one token, getting characters through lexptr. */
1765 /* This is where we will check to make sure that the language and the
1766 operators used are compatible. */
1776 /* Skip over any leading whitespace. */
1777 while (isspace (*lexptr))
1781 /* Look for special single character cases which can't be the first
1782 character of some other multicharacter token. */
1799 /* Look for characters which start a particular kind of multicharacter
1800 token, such as a character literal, register name, convenience
1801 variable name, string literal, etc. */
1806 /* First try to match a string literal, which is any nonzero
1807 sequence of characters enclosed in matching single or double
1808 quotes, except that a single character inside single quotes
1809 is a character literal, so we have to catch that case also. */
1810 token = match_string_literal ();
1815 if (*lexptr == '\'')
1817 token = match_character_literal ();
1826 token = match_character_literal ();
1833 token = match_dollar_tokens ();
1840 /* See if it is a special token of length 2. */
1841 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1843 if (STREQN (lexptr, tokentab2[i].operator, 2))
1846 return (tokentab2[i].token);
1849 /* Look for single character cases which which could be the first
1850 character of some other multicharacter token, but aren't, or we
1851 would already have found it. */
1861 /* Look for a float literal before looking for an integer literal, so
1862 we match as much of the input stream as possible. */
1863 token = match_float_literal ();
1868 token = match_bitstring_literal ();
1873 token = match_integer_literal ();
1879 /* Try to match a simple name string, and if a match is found, then
1880 further classify what sort of name it is and return an appropriate
1881 token. Note that attempting to match a simple name string consumes
1882 the token from lexptr, so we can't back out if we later find that
1883 we can't classify what sort of name it is. */
1885 simplename = match_simple_name_string ();
1887 if (simplename != NULL)
1889 /* See if it is a reserved identifier. */
1890 for (i = 0; i < sizeof (idtokentab) / sizeof (idtokentab[0]); i++)
1892 if (STREQ (simplename, idtokentab[i].operator))
1894 return (idtokentab[i].token);
1898 /* Look for other special tokens. */
1899 if (STREQ (simplename, "true"))
1902 return (BOOLEAN_LITERAL);
1904 if (STREQ (simplename, "false"))
1907 return (BOOLEAN_LITERAL);
1910 sym = lookup_symbol (simplename, expression_context_block,
1911 VAR_NAMESPACE, (int *) NULL,
1912 (struct symtab **) NULL);
1915 yylval.ssym.stoken.ptr = NULL;
1916 yylval.ssym.stoken.length = 0;
1917 yylval.ssym.sym = sym;
1918 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1919 switch (SYMBOL_CLASS (sym))
1922 /* Found a procedure name. */
1923 return (GENERAL_PROCEDURE_NAME);
1925 /* Found a global or local static variable. */
1926 return (LOCATION_NAME);
1931 case LOC_REGPARM_ADDR:
1935 case LOC_BASEREG_ARG:
1936 if (innermost_block == NULL
1937 || contained_in (block_found, innermost_block))
1939 innermost_block = block_found;
1941 return (LOCATION_NAME);
1945 return (LOCATION_NAME);
1948 yylval.tsym.type = SYMBOL_TYPE (sym);
1951 case LOC_CONST_BYTES:
1952 case LOC_OPTIMIZED_OUT:
1953 error ("Symbol \"%s\" names no location.", simplename);
1957 else if (!have_full_symbols () && !have_partial_symbols ())
1959 error ("No symbol table is loaded. Use the \"file\" command.");
1963 error ("No symbol \"%s\" in current context.", simplename);
1967 /* Catch single character tokens which are not part of some
1972 case '.': /* Not float for example. */
1974 while (isspace (*lexptr)) lexptr++;
1975 simplename = match_simple_name_string ();
1981 return (ILLEGAL_TOKEN);
1986 char *msg; /* unused */
1988 printf ("Parsing: %s\n", lexptr);
1991 error ("Invalid syntax in expression near character '%c'.", yychar);
1995 error ("Invalid syntax in expression");