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 <sval> 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
496 write_exp_elt_opcode (OP_STRING);
497 write_exp_string ($1);
498 write_exp_elt_opcode (OP_STRING);
502 write_exp_elt_opcode (OP_BITSTRING);
503 write_exp_bitstring ($1);
504 write_exp_elt_opcode (OP_BITSTRING);
519 value_string_element: string_primitive_value '(' start_element ')'
527 value_string_slice: string_primitive_value '(' left_element ':' right_element ')'
531 | string_primitive_value '(' start_element UP slice_size ')'
539 value_array_element: array_primitive_value '('
540 /* This is to save the value of arglist_len
541 being accumulated for each dimension. */
542 { start_arglist (); }
545 write_exp_elt_opcode (MULTI_SUBSCRIPT);
546 write_exp_elt_longcst ((LONGEST) end_arglist ());
547 write_exp_elt_opcode (MULTI_SUBSCRIPT);
553 value_array_slice: array_primitive_value '(' lower_element ':' upper_element ')'
557 | array_primitive_value '(' first_element UP slice_size ')'
565 value_structure_field: structure_primitive_value '.' field_name
573 expression_conversion: mode_name '(' expression ')'
581 value_procedure_call: FIXME
589 value_built_in_routine_call: chill_value_built_in_routine_call
597 start_expression: FIXME
600 } /* Not in GNU-Chill */
605 zero_adic_operator: FIXME
613 parenthesised_expression: '(' expression ')'
621 expression : operand_0
625 | conditional_expression
631 conditional_expression : IF boolean_expression then_alternative else_alternative FI
635 | CASE case_selector_list OF value_case_alternative '[' ELSE sub_expression ']' ESAC
641 then_alternative: THEN subexpression
647 else_alternative: ELSE subexpression
651 | ELSIF boolean_expression then_alternative else_alternative
657 sub_expression : expression
663 value_case_alternative: case_label_specification ':' sub_expression ';'
671 operand_0 : operand_1
675 | operand_0 LOGIOR operand_1
677 write_exp_elt_opcode (BINOP_BITWISE_IOR);
679 | operand_0 ORIF operand_1
683 | operand_0 LOGXOR operand_1
685 write_exp_elt_opcode (BINOP_BITWISE_XOR);
687 | single_assignment_action
695 operand_1 : operand_2
699 | operand_1 LOGAND operand_2
701 write_exp_elt_opcode (BINOP_BITWISE_AND);
703 | operand_1 ANDIF operand_2
711 operand_2 : operand_3
715 | operand_2 '=' operand_3
717 write_exp_elt_opcode (BINOP_EQUAL);
719 | operand_2 NOTEQUAL operand_3
721 write_exp_elt_opcode (BINOP_NOTEQUAL);
723 | operand_2 '>' operand_3
725 write_exp_elt_opcode (BINOP_GTR);
727 | operand_2 GTR operand_3
729 write_exp_elt_opcode (BINOP_GEQ);
731 | operand_2 '<' operand_3
733 write_exp_elt_opcode (BINOP_LESS);
735 | operand_2 LEQ operand_3
737 write_exp_elt_opcode (BINOP_LEQ);
739 | operand_2 IN operand_3
748 operand_3 : operand_4
752 | operand_3 '+' operand_4
754 write_exp_elt_opcode (BINOP_ADD);
756 | operand_3 '-' operand_4
758 write_exp_elt_opcode (BINOP_SUB);
760 | operand_3 SLASH_SLASH operand_4
768 operand_4 : operand_5
772 | operand_4 '*' operand_5
774 write_exp_elt_opcode (BINOP_MUL);
776 | operand_4 '/' operand_5
778 write_exp_elt_opcode (BINOP_DIV);
780 | operand_4 MOD operand_5
782 write_exp_elt_opcode (BINOP_MOD);
784 | operand_4 REM operand_5
786 write_exp_elt_opcode (BINOP_REM);
792 operand_5 : operand_6
798 write_exp_elt_opcode (UNOP_NEG);
802 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
804 | '(' integer_literal_expression ')' operand_6
812 operand_6 : POINTER location
816 | RECEIVE buffer_location
829 single_assignment_action :
830 location GDB_ASSIGNMENT value
832 write_exp_elt_opcode (BINOP_ASSIGN);
838 chill_value_built_in_routine_call :
839 NUM '(' expression ')'
843 | PRED '(' expression ')'
847 | SUCC '(' expression ')'
851 | ABS '(' expression ')'
855 | CARD '(' expression ')'
859 | MAX '(' expression ')'
863 | MIN '(' expression ')'
867 | SIZE '(' location ')'
871 | SIZE '(' mode_argument ')'
875 | UPPER '(' upper_lower_argument ')'
879 | LOWER '(' upper_lower_argument ')'
883 | LENGTH '(' length_argument ')'
889 mode_argument : mode_name
893 | array_mode_name '(' expression ')'
897 | string_mode_name '(' expression ')'
901 | variant_structure_mode_name '(' expression_list ')'
907 upper_lower_argument : location
921 length_argument : location
932 /* FIXME: For now we just accept only a single integer literal. */
934 integer_literal_expression:
943 array_primitive_value : primitive_value
950 /* Things which still need productions... */
952 array_mode_name : FIXME { $$ = 0; }
953 string_mode_name : FIXME { $$ = 0; }
954 variant_structure_mode_name: FIXME { $$ = 0; }
955 synonym_name : FIXME { $$ = 0; }
956 value_enumeration_name : FIXME { $$ = 0; }
957 value_do_with_name : FIXME { $$ = 0; }
958 value_receive_name : FIXME { $$ = 0; }
959 string_primitive_value : FIXME { $$ = 0; }
960 start_element : FIXME { $$ = 0; }
961 left_element : FIXME { $$ = 0; }
962 right_element : FIXME { $$ = 0; }
963 slice_size : FIXME { $$ = 0; }
964 lower_element : FIXME { $$ = 0; }
965 upper_element : FIXME { $$ = 0; }
966 first_element : FIXME { $$ = 0; }
967 structure_primitive_value: FIXME { $$ = 0; }
968 field_name : FIXME { $$ = 0; }
969 mode_name : FIXME { $$ = 0; }
970 boolean_expression : FIXME { $$ = 0; }
971 case_selector_list : FIXME { $$ = 0; }
972 subexpression : FIXME { $$ = 0; }
973 case_label_specification: FIXME { $$ = 0; }
974 buffer_location : FIXME { $$ = 0; }
978 /* Implementation of a dynamically expandable buffer for processing input
979 characters acquired through lexptr and building a value to return in
982 static char *tempbuf; /* Current buffer contents */
983 static int tempbufsize; /* Size of allocated buffer */
984 static int tempbufindex; /* Current index into buffer */
986 #define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
988 #define CHECKBUF(size) \
990 if (tempbufindex + (size) >= tempbufsize) \
992 growbuf_by_size (size); \
996 /* Grow the static temp buffer if necessary, including allocating the first one
1000 growbuf_by_size (count)
1005 growby = max (count, GROWBY_MIN_SIZE);
1006 tempbufsize += growby;
1007 if (tempbuf == NULL)
1009 tempbuf = (char *) malloc (tempbufsize);
1013 tempbuf = (char *) realloc (tempbuf, tempbufsize);
1017 /* Try to consume a simple name string token. If successful, returns
1018 a pointer to a nullbyte terminated copy of the name that can be used
1019 in symbol table lookups. If not successful, returns NULL. */
1022 match_simple_name_string ()
1024 char *tokptr = lexptr;
1026 if (isalpha (*tokptr))
1030 } while (isalpha (*tokptr) || isdigit (*tokptr) || (*tokptr == '_'));
1031 yylval.sval.ptr = lexptr;
1032 yylval.sval.length = tokptr - lexptr;
1034 return (copy_name (yylval.sval));
1039 /* Start looking for a value composed of valid digits as set by the base
1040 in use. Note that '_' characters are valid anywhere, in any quantity,
1041 and are simply ignored. Since we must find at least one valid digit,
1042 or reject this token as an integer literal, we keep track of how many
1043 digits we have encountered. */
1046 decode_integer_value (base, tokptrptr, ivalptr)
1051 char *tokptr = *tokptrptr;
1055 while (*tokptr != '\0')
1057 temp = tolower (*tokptr);
1063 case '0': case '1': case '2': case '3': case '4':
1064 case '5': case '6': case '7': case '8': case '9':
1067 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1083 /* Found something not in domain for current base. */
1084 tokptr--; /* Unconsume what gave us indigestion. */
1089 /* If we didn't find any digits, then we don't have a valid integer
1090 value, so reject the entire token. Otherwise, update the lexical
1091 scan pointer, and return non-zero for success. */
1099 *tokptrptr = tokptr;
1105 decode_integer_literal (valptr, tokptrptr)
1109 char *tokptr = *tokptrptr;
1112 int explicit_base = 0;
1114 /* Look for an explicit base specifier, which is optional. */
1147 /* If we found an explicit base ensure that the character after the
1148 explicit base is a single quote. */
1150 if (explicit_base && (*tokptr++ != '\''))
1155 /* Attempt to decode whatever follows as an integer value in the
1156 indicated base, updating the token pointer in the process and
1157 computing the value into ival. Also, if we have an explicit
1158 base, then the next character must not be a single quote, or we
1159 have a bitstring literal, so reject the entire token in this case.
1160 Otherwise, update the lexical scan pointer, and return non-zero
1163 if (!decode_integer_value (base, &tokptr, &ival))
1167 else if (explicit_base && (*tokptr == '\''))
1174 *tokptrptr = tokptr;
1179 /* If it wasn't for the fact that floating point values can contain '_'
1180 characters, we could just let strtod do all the hard work by letting it
1181 try to consume as much of the current token buffer as possible and
1182 find a legal conversion. Unfortunately we need to filter out the '_'
1183 characters before calling strtod, which we do by copying the other
1184 legal chars to a local buffer to be converted. However since we also
1185 need to keep track of where the last unconsumed character in the input
1186 buffer is, we have transfer only as many characters as may compose a
1187 legal floating point value. */
1190 match_float_literal ()
1192 char *tokptr = lexptr;
1197 extern double strtod ();
1199 /* Make local buffer in which to build the string to convert. This is
1200 required because underscores are valid in chill floating point numbers
1201 but not in the string passed to strtod to convert. The string will be
1202 no longer than our input string. */
1204 copy = buf = (char *) alloca (strlen (tokptr) + 1);
1206 /* Transfer all leading digits to the conversion buffer, discarding any
1209 while (isdigit (*tokptr) || *tokptr == '_')
1218 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1219 of whether we found any leading digits, and we simply accept it and
1220 continue on to look for the fractional part and/or exponent. One of
1221 [eEdD] is legal only if we have seen digits, and means that there
1222 is no fractional part. If we find neither of these, then this is
1223 not a floating point number, so return failure. */
1228 /* Accept and then look for fractional part and/or exponent. */
1241 goto collect_exponent;
1249 /* We found a '.', copy any fractional digits to the conversion buffer, up
1250 to the first nondigit, non-underscore character. */
1252 while (isdigit (*tokptr) || *tokptr == '_')
1261 /* Look for an exponent, which must start with one of [eEdD]. If none
1262 is found, jump directly to trying to convert what we have collected
1279 /* Accept an optional '-' or '+' following one of [eEdD]. */
1282 if (*tokptr == '+' || *tokptr == '-')
1284 *copy++ = *tokptr++;
1287 /* Now copy an exponent into the conversion buffer. Note that at the
1288 moment underscores are *not* allowed in exponents. */
1290 while (isdigit (*tokptr))
1292 *copy++ = *tokptr++;
1295 /* If we transfered any chars to the conversion buffer, try to interpret its
1296 contents as a floating point value. If any characters remain, then we
1297 must not have a valid floating point string. */
1303 dval = strtod (buf, ©);
1308 return (FLOAT_LITERAL);
1314 /* Recognize a string literal. A string literal is a nonzero sequence
1315 of characters enclosed in matching single or double quotes, except that
1316 a single character inside single quotes is a character literal, which
1317 we reject as a string literal. To embed the terminator character inside
1318 a string, it is simply doubled (I.E. "this""is""one""string") */
1321 match_string_literal ()
1323 char *tokptr = lexptr;
1325 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1328 if (*tokptr == *lexptr)
1330 if (*(tokptr + 1) == *lexptr)
1339 tempbuf[tempbufindex++] = *tokptr;
1341 if (*tokptr == '\0' /* no terminator */
1342 || tempbufindex == 0 /* no string */
1343 || (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
1349 tempbuf[tempbufindex] = '\0';
1350 yylval.sval.ptr = tempbuf;
1351 yylval.sval.length = tempbufindex;
1353 return (CHARACTER_STRING_LITERAL);
1357 /* Recognize a character literal. A character literal is single character
1358 or a control sequence, enclosed in single quotes. A control sequence
1359 is a comma separated list of one or more integer literals, enclosed
1360 in parenthesis and introduced with a circumflex character.
1362 EX: 'a' '^(7)' '^(7,8)'
1364 As a GNU chill extension, the syntax C'xx' is also recognized as a
1365 character literal, where xx is a hex value for the character.
1367 Note that more than a single character, enclosed in single quotes, is
1370 Also note that the control sequence form is not in GNU Chill since it
1371 is ambiguous with the string literal form using single quotes. I.E.
1372 is '^(7)' a character literal or a string literal. In theory it it
1373 possible to tell by context, but GNU Chill doesn't accept the control
1374 sequence form, so neither do we (for now the code is disabled).
1376 Returns CHARACTER_LITERAL if a match is found.
1380 match_character_literal ()
1382 char *tokptr = lexptr;
1385 if ((tolower (*tokptr) == 'c') && (*(tokptr + 1) == '\''))
1387 /* We have a GNU chill extension form, so skip the leading "C'",
1388 decode the hex value, and then ensure that we have a trailing
1389 single quote character. */
1391 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1397 else if (*tokptr == '\'')
1401 /* Determine which form we have, either a control sequence or the
1402 single character form. */
1404 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1406 return (0); /* Disable, see note above. */
1407 /* Match and decode a control sequence. Return zero if we don't
1408 find a valid integer literal, or if the next unconsumed character
1409 after the integer literal is not the trailing ')'.
1410 FIXME: We currently don't handle the multiple integer literal
1413 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1423 /* The trailing quote has not yet been consumed. If we don't find
1424 it, then we have no match. */
1426 if (*tokptr++ != '\'')
1433 /* Not a character literal. */
1436 yylval.typed_val.val = ival;
1437 yylval.typed_val.type = builtin_type_chill_char;
1439 return (CHARACTER_LITERAL);
1442 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1443 Note that according to 5.2.4.2, a single "_" is also a valid integer
1444 literal, however GNU-chill requires there to be at least one "digit"
1445 in any integer literal. */
1448 match_integer_literal ()
1450 char *tokptr = lexptr;
1453 if (!decode_integer_literal (&ival, &tokptr))
1459 yylval.typed_val.val = ival;
1460 yylval.typed_val.type = builtin_type_int;
1462 return (INTEGER_LITERAL);
1466 /* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1467 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1468 literal, however GNU-chill requires there to be at least one "digit"
1469 in any bit-string literal. */
1472 match_bitstring_literal ()
1474 char *tokptr = lexptr;
1483 /* Look for the required explicit base specifier. */
1504 /* Ensure that the character after the explicit base is a single quote. */
1506 if (*tokptr++ != '\'')
1511 while (*tokptr != '\0' && *tokptr != '\'')
1513 digit = tolower (*tokptr);
1519 case '0': case '1': case '2': case '3': case '4':
1520 case '5': case '6': case '7': case '8': case '9':
1523 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1533 /* Found something not in domain for current base. */
1538 /* Extract bits from digit, starting with the msbit appropriate for
1539 the current base, and packing them into the bitstring byte,
1540 starting at the lsbit. */
1541 for (mask = (base >> 1); mask > 0; mask >>= 1)
1547 tempbuf[tempbufindex] |= (1 << bitoffset);
1550 if (bitoffset == HOST_CHAR_BIT)
1559 /* Verify that we consumed everything up to the trailing single quote,
1560 and that we found some bits (IE not just underbars). */
1562 if (*tokptr++ != '\'')
1568 yylval.sval.ptr = tempbuf;
1569 yylval.sval.length = bitcount;
1571 return (BIT_STRING_LITERAL);
1575 /* Recognize tokens that start with '$'. These include:
1577 $regname A native register name or a "standard
1579 Return token GDB_REGNAME.
1581 $variable A convenience variable with a name chosen
1583 Return token GDB_VARIABLE.
1585 $digits Value history with index <digits>, starting
1586 from the first value which has index 1.
1589 $$digits Value history with index <digits> relative
1590 to the last value. I.E. $$0 is the last
1591 value, $$1 is the one previous to that, $$2
1592 is the one previous to $$1, etc.
1593 Return token GDB_LAST.
1595 $ | $0 | $$0 The last value in the value history.
1596 Return token GDB_LAST.
1598 $$ An abbreviation for the second to the last
1599 value in the value history, I.E. $$1
1600 Return token GDB_LAST.
1602 Note that we currently assume that register names and convenience
1603 variables follow the convention of starting with a letter or '_'.
1608 match_dollar_tokens ()
1616 /* We will always have a successful match, even if it is just for
1617 a single '$', the abbreviation for $$0. So advance lexptr. */
1621 if (*tokptr == '_' || isalpha (*tokptr))
1623 /* Look for a match with a native register name, usually something
1624 like "r0" for example. */
1626 for (regno = 0; regno < NUM_REGS; regno++)
1628 namelength = strlen (reg_names[regno]);
1629 if (STREQN (tokptr, reg_names[regno], namelength)
1630 && !isalnum (tokptr[namelength]))
1632 yylval.lval = regno;
1633 lexptr += namelength + 1;
1634 return (GDB_REGNAME);
1638 /* Look for a match with a standard register name, usually something
1639 like "pc", which gdb always recognizes as the program counter
1640 regardless of what the native register name is. */
1642 for (regno = 0; regno < num_std_regs; regno++)
1644 namelength = strlen (std_regs[regno].name);
1645 if (STREQN (tokptr, std_regs[regno].name, namelength)
1646 && !isalnum (tokptr[namelength]))
1648 yylval.lval = std_regs[regno].regnum;
1649 lexptr += namelength;
1650 return (GDB_REGNAME);
1654 /* Attempt to match against a convenience variable. Note that
1655 this will always succeed, because if no variable of that name
1656 already exists, the lookup_internalvar will create one for us.
1657 Also note that both lexptr and tokptr currently point to the
1658 start of the input string we are trying to match, and that we
1659 have already tested the first character for non-numeric, so we
1660 don't have to treat it specially. */
1662 while (*tokptr == '_' || isalnum (*tokptr))
1666 yylval.sval.ptr = lexptr;
1667 yylval.sval.length = tokptr - lexptr;
1668 yylval.ivar = lookup_internalvar (copy_name (yylval.sval));
1670 return (GDB_VARIABLE);
1673 /* Since we didn't match against a register name or convenience
1674 variable, our only choice left is a history value. */
1688 /* Attempt to decode more characters as an integer value giving
1689 the index in the history list. If successful, the value will
1690 overwrite ival (currently 0 or 1), and if not, ival will be
1691 left alone, which is good since it is currently correct for
1692 the '$' or '$$' case. */
1694 decode_integer_literal (&ival, &tokptr);
1695 yylval.lval = negate ? -ival : ival;
1706 static const struct token tokentab6[] =
1708 { "LENGTH", LENGTH }
1711 static const struct token tokentab5[] =
1718 static const struct token tokentab4[] =
1727 static const struct token tokentab3[] =
1740 static const struct token tokentab2[] =
1742 { ":=", GDB_ASSIGNMENT },
1743 { "//", SLASH_SLASH },
1751 /* Read one token, getting characters through lexptr. */
1752 /* This is where we will check to make sure that the language and the
1753 operators used are compatible. */
1763 /* Skip over any leading whitespace. */
1764 while (isspace (*lexptr))
1768 /* Look for special single character cases which can't be the first
1769 character of some other multicharacter token. */
1788 /* Look for characters which start a particular kind of multicharacter
1789 token, such as a character literal, register name, convenience
1790 variable name, string literal, etc. */
1795 /* First try to match a string literal, which is any nonzero
1796 sequence of characters enclosed in matching single or double
1797 quotes, except that a single character inside single quotes
1798 is a character literal, so we have to catch that case also. */
1799 token = match_string_literal ();
1804 if (*lexptr == '\'')
1806 token = match_character_literal ();
1815 token = match_character_literal ();
1822 token = match_dollar_tokens ();
1829 /* See if it is a special token of length 6. */
1830 for (i = 0; i < sizeof (tokentab6) / sizeof (tokentab6[0]); i++)
1832 if (STREQN (lexptr, tokentab6[i].operator, 6))
1835 return (tokentab6[i].token);
1838 /* See if it is a special token of length 5. */
1839 for (i = 0; i < sizeof (tokentab5) / sizeof (tokentab5[0]); i++)
1841 if (STREQN (lexptr, tokentab5[i].operator, 5))
1844 return (tokentab5[i].token);
1847 /* See if it is a special token of length 4. */
1848 for (i = 0; i < sizeof (tokentab4) / sizeof (tokentab4[0]); i++)
1850 if (STREQN (lexptr, tokentab4[i].operator, 4))
1853 return (tokentab4[i].token);
1856 /* See if it is a special token of length 3. */
1857 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1859 if (STREQN (lexptr, tokentab3[i].operator, 3))
1862 return (tokentab3[i].token);
1865 /* See if it is a special token of length 2. */
1866 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1868 if (STREQN (lexptr, tokentab2[i].operator, 2))
1871 return (tokentab2[i].token);
1874 /* Look for single character cases which which could be the first
1875 character of some other multicharacter token, but aren't, or we
1876 would already have found it. */
1885 /* Look for other special tokens. */
1886 if (STREQN (lexptr, "TRUE", 4)) /* FIXME: What about lowercase? */
1890 return (BOOLEAN_LITERAL);
1892 if (STREQN (lexptr, "FALSE", 5)) /* FIXME: What about lowercase? */
1896 return (BOOLEAN_LITERAL);
1898 /* Look for a float literal before looking for an integer literal, so
1899 we match as much of the input stream as possible. */
1900 token = match_float_literal ();
1905 token = match_bitstring_literal ();
1910 token = match_integer_literal ();
1916 /* Try to match a simple name string, and if a match is found, then
1917 further classify what sort of name it is and return an appropriate
1918 token. Note that attempting to match a simple name string consumes
1919 the token from lexptr, so we can't back out if we later find that
1920 we can't classify what sort of name it is. */
1922 simplename = match_simple_name_string ();
1923 if (simplename != NULL)
1925 sym = lookup_symbol (simplename, expression_context_block,
1926 VAR_NAMESPACE, (int *) NULL,
1927 (struct symtab **) NULL);
1930 yylval.ssym.stoken.ptr = NULL;
1931 yylval.ssym.stoken.length = 0;
1932 yylval.ssym.sym = sym;
1933 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1934 switch (SYMBOL_CLASS (sym))
1937 /* Found a procedure name. */
1938 return (GENERAL_PROCEDURE_NAME);
1940 /* Found a global or local static variable. */
1941 return (LOCATION_NAME);
1948 if (innermost_block == NULL
1949 || contained_in (block_found, innermost_block))
1951 innermost_block = block_found;
1953 return (LOCATION_NAME);
1957 return (LOCATION_NAME);
1961 case LOC_CONST_BYTES:
1962 error ("Symbol \"%s\" names no location.", simplename);
1966 else if (!have_full_symbols () && !have_partial_symbols ())
1968 error ("No symbol table is loaded. Use the \"file\" command.");
1972 error ("No symbol \"%s\" in current context.", simplename);
1976 /* Catch single character tokens which are not part of some
1981 case '.': /* Not float for example. */
1985 return (ILLEGAL_TOKEN);
1990 char *msg; /* unused */
1992 printf ("Parsing: %s\n", lexptr);
1995 error ("Invalid syntax in expression near character '%c'.", yychar);
1999 error ("Invalid syntax in expression");