* monitor.c: Include gnu-regex.h rather than system regex.h.
[deliverable/binutils-gdb.git] / gdb / ch-exp.y
CommitLineData
e58de8a2 1/* YACC grammar for Chill expressions, for GDB.
ba47c66a 2 Copyright 1992, 1993, 1994 Free Software Foundation, Inc.
e58de8a2
FF
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
6c9638b4 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
e58de8a2
FF
19
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.
28
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.
36
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.
51
52 */
53
54%{
55
e58de8a2 56#include "defs.h"
ba47c66a 57#include <string.h>
00cea52f 58#include <ctype.h>
e58de8a2
FF
59#include "expression.h"
60#include "language.h"
61#include "value.h"
62#include "parser-defs.h"
22e39759 63#include "ch-lang.h"
100f92e2
JK
64#include "bfd.h" /* Required by objfiles.h. */
65#include "symfile.h" /* Required by objfiles.h. */
66#include "objfiles.h" /* For have_full_symbols and have_partial_symbols */
e58de8a2 67
19d0f3f4
FF
68/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
69 as well as gratuitiously global symbol names, so we can have multiple
70 yacc generated parsers in gdb. Note that these are only the variables
71 produced by yacc. If other parser generators (bison, byacc, etc) produce
72 additional global names that conflict at link time, then those parser
73 generators need to be fixed instead of adding those names to this list. */
74
e58de8a2
FF
75#define yymaxdepth chill_maxdepth
76#define yyparse chill_parse
77#define yylex chill_lex
78#define yyerror chill_error
79#define yylval chill_lval
80#define yychar chill_char
81#define yydebug chill_debug
82#define yypact chill_pact
83#define yyr1 chill_r1
84#define yyr2 chill_r2
85#define yydef chill_def
86#define yychk chill_chk
87#define yypgo chill_pgo
88#define yyact chill_act
89#define yyexca chill_exca
4015bfb9
BK
90#define yyerrflag chill_errflag
91#define yynerrs chill_nerrs
e58de8a2
FF
92#define yyps chill_ps
93#define yypv chill_pv
94#define yys chill_s
95#define yy_yys chill_yys
96#define yystate chill_state
97#define yytmp chill_tmp
98#define yyv chill_v
99#define yy_yyv chill_yyv
100#define yyval chill_val
101#define yylloc chill_lloc
4015bfb9
BK
102#define yyreds chill_reds /* With YYDEBUG defined */
103#define yytoks chill_toks /* With YYDEBUG defined */
ea082c0a
MM
104#define yylhs chill_yylhs
105#define yylen chill_yylen
106#define yydefred chill_yydefred
107#define yydgoto chill_yydgoto
108#define yysindex chill_yysindex
109#define yyrindex chill_yyrindex
110#define yygindex chill_yygindex
111#define yytable chill_yytable
112#define yycheck chill_yycheck
19d0f3f4
FF
113
114#ifndef YYDEBUG
115#define YYDEBUG 0 /* Default to no yydebug support */
116#endif
117
ef8e137c
FF
118static void
119write_lower_upper_value PARAMS ((enum exp_opcode, struct type *type));
120
19d0f3f4
FF
121int
122yyparse PARAMS ((void));
e58de8a2
FF
123
124static int
125yylex PARAMS ((void));
126
22e39759 127void
e58de8a2
FF
128yyerror PARAMS ((char *));
129
e58de8a2
FF
130%}
131
132/* Although the yacc "value" of an expression is not used,
133 since the result is stored in the structure being created,
134 other node types do have values. */
135
136%union
137 {
138 LONGEST lval;
139 unsigned LONGEST ulval;
140 struct {
141 LONGEST val;
142 struct type *type;
143 } typed_val;
144 double dval;
145 struct symbol *sym;
146 struct type *tval;
147 struct stoken sval;
148 struct ttype tsym;
149 struct symtoken ssym;
150 int voidval;
151 struct block *bval;
152 enum exp_opcode opcode;
153 struct internalvar *ivar;
154
155 struct type **tvec;
156 int *ivec;
157 }
158
e58de8a2
FF
159%token <typed_val> INTEGER_LITERAL
160%token <ulval> BOOLEAN_LITERAL
2e66cf7d 161%token <typed_val> CHARACTER_LITERAL
1188fbbf 162%token <dval> FLOAT_LITERAL
cbd1bdc3
FF
163%token <ssym> GENERAL_PROCEDURE_NAME
164%token <ssym> LOCATION_NAME
e58de8a2 165%token <voidval> EMPTINESS_LITERAL
c7da3ed3 166%token <sval> CHARACTER_STRING_LITERAL
81028ab0 167%token <sval> BIT_STRING_LITERAL
8a177da6
PB
168%token <tsym> TYPENAME
169%token <sval> FIELD_NAME
e58de8a2 170
e58de8a2
FF
171%token <voidval> '.'
172%token <voidval> ';'
173%token <voidval> ':'
174%token <voidval> CASE
175%token <voidval> OF
176%token <voidval> ESAC
177%token <voidval> LOGIOR
178%token <voidval> ORIF
179%token <voidval> LOGXOR
180%token <voidval> LOGAND
181%token <voidval> ANDIF
182%token <voidval> '='
183%token <voidval> NOTEQUAL
184%token <voidval> '>'
185%token <voidval> GTR
186%token <voidval> '<'
187%token <voidval> LEQ
188%token <voidval> IN
189%token <voidval> '+'
190%token <voidval> '-'
191%token <voidval> '*'
192%token <voidval> '/'
193%token <voidval> SLASH_SLASH
194%token <voidval> MOD
195%token <voidval> REM
196%token <voidval> NOT
197%token <voidval> POINTER
198%token <voidval> RECEIVE
e58de8a2
FF
199%token <voidval> '['
200%token <voidval> ']'
201%token <voidval> '('
202%token <voidval> ')'
203%token <voidval> UP
204%token <voidval> IF
205%token <voidval> THEN
206%token <voidval> ELSE
207%token <voidval> FI
208%token <voidval> ELSIF
209%token <voidval> ILLEGAL_TOKEN
81028ab0
FF
210%token <voidval> NUM
211%token <voidval> PRED
212%token <voidval> SUCC
213%token <voidval> ABS
214%token <voidval> CARD
57ffffe3
JG
215%token <voidval> MAX_TOKEN
216%token <voidval> MIN_TOKEN
0faa3247 217%token <voidval> ADDR_TOKEN
81028ab0
FF
218%token <voidval> SIZE
219%token <voidval> UPPER
220%token <voidval> LOWER
221%token <voidval> LENGTH
835c2559 222%token <voidval> ARRAY
e58de8a2 223
45fe3db4
FF
224/* Tokens which are not Chill tokens used in expressions, but rather GDB
225 specific things that we recognize in the same context as Chill tokens
226 (register names for example). */
227
c700638c 228%token <voidval> GDB_VARIABLE /* Convenience variable */
45fe3db4
FF
229%token <voidval> GDB_ASSIGNMENT /* Assign value to somewhere */
230
cbd1bdc3 231%type <voidval> access_name
e58de8a2 232%type <voidval> primitive_value
e58de8a2
FF
233%type <voidval> value_name
234%type <voidval> literal
235%type <voidval> tuple
f91a9e05 236%type <voidval> slice
e58de8a2 237%type <voidval> expression_conversion
e58de8a2 238%type <voidval> value_built_in_routine_call
e58de8a2
FF
239%type <voidval> parenthesised_expression
240%type <voidval> value
e58de8a2
FF
241%type <voidval> expression
242%type <voidval> conditional_expression
243%type <voidval> then_alternative
244%type <voidval> else_alternative
e58de8a2
FF
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
e58de8a2 252%type <voidval> expression_list
58cda66e 253%type <tval> mode_argument
45fe3db4 254%type <voidval> single_assignment_action
8a177da6 255%type <tsym> mode_name
f91a9e05 256%type <lval> rparen
45fe3db4 257
0faa3247
PB
258/* Not implemented:
259%type <voidval> undefined_value
260%type <voidval> array_mode_name
261%type <voidval> string_mode_name
262%type <voidval> variant_structure_mode_name
263*/
264
e58de8a2
FF
265%%
266
267/* Z.200, 5.3.1 */
268
f39a2631 269start : value { }
8a177da6
PB
270 | mode_name
271 { write_exp_elt_opcode(OP_TYPE);
272 write_exp_elt_type($1.type);
273 write_exp_elt_opcode(OP_TYPE);}
274 ;
275
e58de8a2 276value : expression
0faa3247 277/*
e58de8a2 278 | undefined_value
0faa3247
PB
279 { ??? }
280*/
e58de8a2
FF
281 ;
282
cbd1bdc3
FF
283/* Z.200, 4.2.2 */
284
285access_name : LOCATION_NAME
286 {
287 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26 288 write_exp_elt_block (NULL);
cbd1bdc3
FF
289 write_exp_elt_sym ($1.sym);
290 write_exp_elt_opcode (OP_VAR_VALUE);
291 }
45fe3db4 292 | GDB_VARIABLE /* gdb specific */
e58de8a2
FF
293 ;
294
54bbbfb4
FF
295/* Z.200, 4.2.8 */
296
297expression_list : expression
298 {
299 arglist_len = 1;
300 }
301 | expression_list ',' expression
302 {
303 arglist_len++;
304 }
dcda44a0
PB
305 ;
306
60438e8e
PB
307maybe_expression_list: /* EMPTY */
308 {
309 arglist_len = 0;
310 }
311 | expression_list
312 ;
313
54bbbfb4 314
e58de8a2
FF
315/* Z.200, 5.2.1 */
316
f91a9e05 317primitive_value_lparen: primitive_value '('
bdef6b60
PB
318 /* This is to save the value of arglist_len
319 being accumulated for each dimension. */
320 { start_arglist (); }
f91a9e05
PB
321 ;
322
323rparen : ')'
324 { $$ = end_arglist (); }
325 ;
326
327primitive_value :
328 access_name
60438e8e 329 | primitive_value_lparen maybe_expression_list rparen
e58de8a2 330 {
bdef6b60 331 write_exp_elt_opcode (MULTI_SUBSCRIPT);
f91a9e05 332 write_exp_elt_longcst ($3);
bdef6b60
PB
333 write_exp_elt_opcode (MULTI_SUBSCRIPT);
334 }
335 | primitive_value FIELD_NAME
336 { write_exp_elt_opcode (STRUCTOP_STRUCT);
337 write_exp_string ($2);
338 write_exp_elt_opcode (STRUCTOP_STRUCT);
339 }
340 | primitive_value POINTER
341 {
342 write_exp_elt_opcode (UNOP_IND);
e58de8a2 343 }
3029162c
PB
344 | primitive_value POINTER mode_name
345 {
346 write_exp_elt_opcode (UNOP_CAST);
347 write_exp_elt_type (lookup_pointer_type ($3.type));
348 write_exp_elt_opcode (UNOP_CAST);
349 write_exp_elt_opcode (UNOP_IND);
350 }
e58de8a2 351 | value_name
e58de8a2 352 | literal
e58de8a2 353 | tuple
f91a9e05 354 | slice
e58de8a2 355 | expression_conversion
e58de8a2 356 | value_built_in_routine_call
0faa3247 357/*
e58de8a2 358 | start_expression
0faa3247 359 { ??? }
e58de8a2 360 | zero_adic_operator
0faa3247
PB
361 { ??? }
362*/
e58de8a2 363 | parenthesised_expression
e58de8a2
FF
364 ;
365
e58de8a2
FF
366/* Z.200, 5.2.3 */
367
0faa3247 368value_name : GENERAL_PROCEDURE_NAME
e58de8a2 369 {
cbd1bdc3 370 write_exp_elt_opcode (OP_VAR_VALUE);
479fdd26 371 write_exp_elt_block (NULL);
cbd1bdc3
FF
372 write_exp_elt_sym ($1.sym);
373 write_exp_elt_opcode (OP_VAR_VALUE);
e58de8a2
FF
374 }
375 ;
376
377/* Z.200, 5.2.4.1 */
378
379literal : INTEGER_LITERAL
380 {
2e66cf7d
FF
381 write_exp_elt_opcode (OP_LONG);
382 write_exp_elt_type ($1.type);
383 write_exp_elt_longcst ((LONGEST) ($1.val));
384 write_exp_elt_opcode (OP_LONG);
e58de8a2
FF
385 }
386 | BOOLEAN_LITERAL
387 {
2e66cf7d
FF
388 write_exp_elt_opcode (OP_BOOL);
389 write_exp_elt_longcst ((LONGEST) $1);
390 write_exp_elt_opcode (OP_BOOL);
e58de8a2
FF
391 }
392 | CHARACTER_LITERAL
393 {
2e66cf7d
FF
394 write_exp_elt_opcode (OP_LONG);
395 write_exp_elt_type ($1.type);
396 write_exp_elt_longcst ((LONGEST) ($1.val));
397 write_exp_elt_opcode (OP_LONG);
e58de8a2 398 }
1188fbbf
FF
399 | FLOAT_LITERAL
400 {
401 write_exp_elt_opcode (OP_DOUBLE);
402 write_exp_elt_type (builtin_type_double);
403 write_exp_elt_dblcst ($1);
404 write_exp_elt_opcode (OP_DOUBLE);
405 }
e58de8a2
FF
406 | EMPTINESS_LITERAL
407 {
2d67c7e9
PB
408 struct type *void_ptr_type
409 = lookup_pointer_type (builtin_type_void);
410 write_exp_elt_opcode (OP_LONG);
411 write_exp_elt_type (void_ptr_type);
412 write_exp_elt_longcst (0);
413 write_exp_elt_opcode (OP_LONG);
e58de8a2
FF
414 }
415 | CHARACTER_STRING_LITERAL
416 {
c7da3ed3
FF
417 write_exp_elt_opcode (OP_STRING);
418 write_exp_string ($1);
419 write_exp_elt_opcode (OP_STRING);
e58de8a2
FF
420 }
421 | BIT_STRING_LITERAL
422 {
81028ab0
FF
423 write_exp_elt_opcode (OP_BITSTRING);
424 write_exp_bitstring ($1);
425 write_exp_elt_opcode (OP_BITSTRING);
e58de8a2
FF
426 }
427 ;
428
429/* Z.200, 5.2.5 */
430
dcda44a0
PB
431tuple_element : expression
432 | named_record_element
433 ;
434
435named_record_element: FIELD_NAME ',' named_record_element
436 { write_exp_elt_opcode (OP_LABELED);
437 write_exp_string ($1);
438 write_exp_elt_opcode (OP_LABELED);
439 }
440 | FIELD_NAME ':' expression
441 { write_exp_elt_opcode (OP_LABELED);
442 write_exp_string ($1);
443 write_exp_elt_opcode (OP_LABELED);
444 }
445 ;
446
447tuple_elements : tuple_element
448 {
449 arglist_len = 1;
450 }
451 | tuple_elements ',' tuple_element
452 {
453 arglist_len++;
454 }
455 ;
456
f91a9e05
PB
457maybe_tuple_elements : tuple_elements
458 | /* EMPTY */
459 ;
460
2d67c7e9
PB
461tuple : '['
462 { start_arglist (); }
f91a9e05 463 maybe_tuple_elements ']'
e58de8a2 464 {
2d67c7e9
PB
465 write_exp_elt_opcode (OP_ARRAY);
466 write_exp_elt_longcst ((LONGEST) 0);
467 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
468 write_exp_elt_opcode (OP_ARRAY);
469 }
470 |
471 mode_name '['
472 { start_arglist (); }
f91a9e05 473 maybe_tuple_elements ']'
2d67c7e9
PB
474 {
475 write_exp_elt_opcode (OP_ARRAY);
476 write_exp_elt_longcst ((LONGEST) 0);
477 write_exp_elt_longcst ((LONGEST) end_arglist () - 1);
478 write_exp_elt_opcode (OP_ARRAY);
479
480 write_exp_elt_opcode (UNOP_CAST);
481 write_exp_elt_type ($1.type);
482 write_exp_elt_opcode (UNOP_CAST);
e58de8a2
FF
483 }
484 ;
485
486
487/* Z.200, 5.2.6 */
488
e58de8a2 489
f91a9e05 490slice: primitive_value_lparen expression ':' expression rparen
e58de8a2 491 {
f91a9e05 492 write_exp_elt_opcode (TERNOP_SLICE);
e58de8a2 493 }
f91a9e05 494 | primitive_value_lparen expression UP expression rparen
e58de8a2 495 {
f91a9e05 496 write_exp_elt_opcode (TERNOP_SLICE_COUNT);
e58de8a2
FF
497 }
498 ;
499
e58de8a2
FF
500/* Z.200, 5.2.11 */
501
2fcc38b8 502expression_conversion: mode_name parenthesised_expression
e58de8a2 503 {
8a177da6
PB
504 write_exp_elt_opcode (UNOP_CAST);
505 write_exp_elt_type ($1.type);
506 write_exp_elt_opcode (UNOP_CAST);
e58de8a2 507 }
835c2559
PB
508 | ARRAY '(' ')' mode_name parenthesised_expression
509 /* This is pseudo-Chill, similar to C's '(TYPE[])EXPR'
510 which casts to an artificial array. */
511 {
512 struct type *range_type
513 = create_range_type ((struct type *) NULL,
514 builtin_type_int, 0, 0);
515 struct type *array_type
516 = create_array_type ((struct type *) NULL,
517 $4.type, range_type);
518 TYPE_ARRAY_UPPER_BOUND_TYPE(array_type)
519 = BOUND_CANNOT_BE_DETERMINED;
520 write_exp_elt_opcode (UNOP_CAST);
521 write_exp_elt_type (array_type);
522 write_exp_elt_opcode (UNOP_CAST);
523 }
e58de8a2
FF
524 ;
525
e58de8a2
FF
526/* Z.200, 5.2.16 */
527
528parenthesised_expression: '(' expression ')'
e58de8a2
FF
529 ;
530
531/* Z.200, 5.3.2 */
532
533expression : operand_0
8a177da6 534 | single_assignment_action
e58de8a2 535 | conditional_expression
e58de8a2
FF
536 ;
537
0faa3247
PB
538conditional_expression : IF expression then_alternative else_alternative FI
539 { write_exp_elt_opcode (TERNOP_COND); }
540/*
541 | CASE case_selector_list OF value_case_alternative ELSE expression ESAC
542 { error ("not implemented: CASE expression" }
543*/
e58de8a2
FF
544 ;
545
0faa3247 546then_alternative: THEN expression
e58de8a2
FF
547 ;
548
0faa3247
PB
549else_alternative: ELSE expression
550 | ELSIF expression then_alternative else_alternative
551 { write_exp_elt_opcode (TERNOP_COND); }
e58de8a2
FF
552 ;
553
554/* Z.200, 5.3.3 */
555
556operand_0 : operand_1
e58de8a2
FF
557 | operand_0 LOGIOR operand_1
558 {
2e66cf7d 559 write_exp_elt_opcode (BINOP_BITWISE_IOR);
e58de8a2
FF
560 }
561 | operand_0 ORIF operand_1
562 {
0faa3247 563 write_exp_elt_opcode (BINOP_LOGICAL_OR);
e58de8a2
FF
564 }
565 | operand_0 LOGXOR operand_1
566 {
2e66cf7d 567 write_exp_elt_opcode (BINOP_BITWISE_XOR);
e58de8a2
FF
568 }
569 ;
570
571/* Z.200, 5.3.4 */
572
573operand_1 : operand_2
e58de8a2
FF
574 | operand_1 LOGAND operand_2
575 {
2e66cf7d 576 write_exp_elt_opcode (BINOP_BITWISE_AND);
e58de8a2
FF
577 }
578 | operand_1 ANDIF operand_2
579 {
0faa3247 580 write_exp_elt_opcode (BINOP_LOGICAL_AND);
e58de8a2
FF
581 }
582 ;
583
584/* Z.200, 5.3.5 */
585
586operand_2 : operand_3
e58de8a2
FF
587 | operand_2 '=' operand_3
588 {
2e66cf7d 589 write_exp_elt_opcode (BINOP_EQUAL);
e58de8a2
FF
590 }
591 | operand_2 NOTEQUAL operand_3
592 {
2e66cf7d 593 write_exp_elt_opcode (BINOP_NOTEQUAL);
e58de8a2
FF
594 }
595 | operand_2 '>' operand_3
596 {
2e66cf7d 597 write_exp_elt_opcode (BINOP_GTR);
e58de8a2
FF
598 }
599 | operand_2 GTR operand_3
600 {
2e66cf7d 601 write_exp_elt_opcode (BINOP_GEQ);
e58de8a2
FF
602 }
603 | operand_2 '<' operand_3
604 {
2e66cf7d 605 write_exp_elt_opcode (BINOP_LESS);
e58de8a2
FF
606 }
607 | operand_2 LEQ operand_3
608 {
2e66cf7d 609 write_exp_elt_opcode (BINOP_LEQ);
e58de8a2
FF
610 }
611 | operand_2 IN operand_3
612 {
e909f287 613 write_exp_elt_opcode (BINOP_IN);
e58de8a2
FF
614 }
615 ;
616
617
618/* Z.200, 5.3.6 */
619
620operand_3 : operand_4
e58de8a2
FF
621 | operand_3 '+' operand_4
622 {
2e66cf7d 623 write_exp_elt_opcode (BINOP_ADD);
e58de8a2
FF
624 }
625 | operand_3 '-' operand_4
626 {
2e66cf7d 627 write_exp_elt_opcode (BINOP_SUB);
e58de8a2
FF
628 }
629 | operand_3 SLASH_SLASH operand_4
630 {
fcbadaee 631 write_exp_elt_opcode (BINOP_CONCAT);
e58de8a2
FF
632 }
633 ;
634
635/* Z.200, 5.3.7 */
636
637operand_4 : operand_5
e58de8a2
FF
638 | operand_4 '*' operand_5
639 {
2e66cf7d 640 write_exp_elt_opcode (BINOP_MUL);
e58de8a2
FF
641 }
642 | operand_4 '/' operand_5
643 {
2e66cf7d 644 write_exp_elt_opcode (BINOP_DIV);
e58de8a2
FF
645 }
646 | operand_4 MOD operand_5
647 {
76a0ffb4 648 write_exp_elt_opcode (BINOP_MOD);
e58de8a2
FF
649 }
650 | operand_4 REM operand_5
651 {
76a0ffb4 652 write_exp_elt_opcode (BINOP_REM);
e58de8a2
FF
653 }
654 ;
655
656/* Z.200, 5.3.8 */
657
658operand_5 : operand_6
e58de8a2
FF
659 | '-' operand_6
660 {
2e66cf7d 661 write_exp_elt_opcode (UNOP_NEG);
e58de8a2
FF
662 }
663 | NOT operand_6
664 {
2e66cf7d 665 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
e58de8a2 666 }
47f366bc 667 | parenthesised_expression literal
8a177da6
PB
668/* We require the string operand to be a literal, to avoid some
669 nasty parsing ambiguities. */
e58de8a2 670 {
2fcc38b8 671 write_exp_elt_opcode (BINOP_CONCAT);
e58de8a2
FF
672 }
673 ;
674
675/* Z.200, 5.3.9 */
676
bdef6b60 677operand_6 : POINTER primitive_value
e58de8a2 678 {
8a177da6 679 write_exp_elt_opcode (UNOP_ADDR);
e58de8a2 680 }
0faa3247
PB
681 | RECEIVE expression
682 { error ("not implemented: RECEIVE expression"); }
e58de8a2 683 | primitive_value
e58de8a2
FF
684 ;
685
686
45fe3db4
FF
687/* Z.200, 6.2 */
688
81028ab0 689single_assignment_action :
bdef6b60 690 primitive_value GDB_ASSIGNMENT value
45fe3db4
FF
691 {
692 write_exp_elt_opcode (BINOP_ASSIGN);
693 }
81028ab0
FF
694 ;
695
696/* Z.200, 6.20.3 */
697
0faa3247 698value_built_in_routine_call :
81028ab0
FF
699 NUM '(' expression ')'
700 {
0faa3247
PB
701 write_exp_elt_opcode (UNOP_CAST);
702 write_exp_elt_type (builtin_type_int);
703 write_exp_elt_opcode (UNOP_CAST);
81028ab0
FF
704 }
705 | PRED '(' expression ')'
0faa3247 706 { error ("not implemented: PRED builtin function"); }
81028ab0 707 | SUCC '(' expression ')'
0faa3247
PB
708 { error ("not implemented: SUCC builtin function"); }
709 | ADDR_TOKEN '(' expression ')'
710 { write_exp_elt_opcode (UNOP_ADDR); }
81028ab0 711 | ABS '(' expression ')'
0faa3247 712 { error ("not implemented: ABS builtin function"); }
81028ab0 713 | CARD '(' expression ')'
0faa3247 714 { error ("not implemented: CARD builtin function"); }
57ffffe3 715 | MAX_TOKEN '(' expression ')'
0faa3247 716 { error ("not implemented: MAX builtin function"); }
57ffffe3 717 | MIN_TOKEN '(' expression ')'
0faa3247 718 { error ("not implemented: MIN builtin function"); }
58cda66e
PB
719 | SIZE '(' expression ')'
720 { write_exp_elt_opcode (UNOP_SIZEOF); }
81028ab0 721 | SIZE '(' mode_argument ')'
58cda66e
PB
722 { write_exp_elt_opcode (OP_LONG);
723 write_exp_elt_type (builtin_type_int);
724 write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
725 write_exp_elt_opcode (OP_LONG); }
61379835
PB
726 | LOWER '(' mode_argument ')'
727 { write_lower_upper_value (UNOP_LOWER, $3); }
728 | UPPER '(' mode_argument ')'
729 { write_lower_upper_value (UNOP_UPPER, $3); }
730 | LOWER '(' expression ')'
731 { write_exp_elt_opcode (UNOP_LOWER); }
732 | UPPER '(' expression ')'
733 { write_exp_elt_opcode (UNOP_UPPER); }
734 | LENGTH '(' expression ')'
735 { write_exp_elt_opcode (UNOP_LENGTH); }
81028ab0
FF
736 ;
737
738mode_argument : mode_name
739 {
58cda66e 740 $$ = $1.type;
81028ab0 741 }
0faa3247 742/*
81028ab0 743 | array_mode_name '(' expression ')'
0faa3247 744 { ??? }
81028ab0 745 | string_mode_name '(' expression ')'
0faa3247 746 { ??? }
81028ab0 747 | variant_structure_mode_name '(' expression_list ')'
0faa3247
PB
748 { ??? }
749*/
81028ab0
FF
750 ;
751
8a177da6
PB
752mode_name : TYPENAME
753 ;
754
e58de8a2
FF
755%%
756
c7da3ed3
FF
757/* Implementation of a dynamically expandable buffer for processing input
758 characters acquired through lexptr and building a value to return in
759 yylval. */
760
761static char *tempbuf; /* Current buffer contents */
762static int tempbufsize; /* Size of allocated buffer */
763static int tempbufindex; /* Current index into buffer */
764
765#define GROWBY_MIN_SIZE 64 /* Minimum amount to grow buffer by */
766
767#define CHECKBUF(size) \
768 do { \
769 if (tempbufindex + (size) >= tempbufsize) \
770 { \
771 growbuf_by_size (size); \
772 } \
773 } while (0);
774
775/* Grow the static temp buffer if necessary, including allocating the first one
776 on demand. */
777
778static void
779growbuf_by_size (count)
780 int count;
781{
782 int growby;
783
784 growby = max (count, GROWBY_MIN_SIZE);
785 tempbufsize += growby;
786 if (tempbuf == NULL)
787 {
788 tempbuf = (char *) malloc (tempbufsize);
789 }
790 else
791 {
792 tempbuf = (char *) realloc (tempbuf, tempbufsize);
793 }
794}
795
cbd1bdc3
FF
796/* Try to consume a simple name string token. If successful, returns
797 a pointer to a nullbyte terminated copy of the name that can be used
798 in symbol table lookups. If not successful, returns NULL. */
799
800static char *
801match_simple_name_string ()
802{
803 char *tokptr = lexptr;
804
93dc3414 805 if (isalpha (*tokptr) || *tokptr == '_')
cbd1bdc3 806 {
5a7c9cce 807 char *result;
cbd1bdc3
FF
808 do {
809 tokptr++;
5a7c9cce 810 } while (isalnum (*tokptr) || (*tokptr == '_'));
cbd1bdc3
FF
811 yylval.sval.ptr = lexptr;
812 yylval.sval.length = tokptr - lexptr;
813 lexptr = tokptr;
5a7c9cce 814 result = copy_name (yylval.sval);
5a7c9cce 815 return result;
cbd1bdc3
FF
816 }
817 return (NULL);
818}
819
5d074aa9
FF
820/* Start looking for a value composed of valid digits as set by the base
821 in use. Note that '_' characters are valid anywhere, in any quantity,
822 and are simply ignored. Since we must find at least one valid digit,
823 or reject this token as an integer literal, we keep track of how many
824 digits we have encountered. */
825
826static int
827decode_integer_value (base, tokptrptr, ivalptr)
828 int base;
829 char **tokptrptr;
830 int *ivalptr;
831{
832 char *tokptr = *tokptrptr;
833 int temp;
834 int digits = 0;
835
836 while (*tokptr != '\0')
837 {
db2302cb
PS
838 temp = *tokptr;
839 if (isupper (temp))
840 temp = tolower (temp);
5d074aa9
FF
841 tokptr++;
842 switch (temp)
843 {
844 case '_':
845 continue;
846 case '0': case '1': case '2': case '3': case '4':
847 case '5': case '6': case '7': case '8': case '9':
848 temp -= '0';
849 break;
850 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
851 temp -= 'a';
852 temp += 10;
853 break;
854 default:
855 temp = base;
856 break;
857 }
858 if (temp < base)
859 {
860 digits++;
861 *ivalptr *= base;
862 *ivalptr += temp;
863 }
864 else
865 {
866 /* Found something not in domain for current base. */
867 tokptr--; /* Unconsume what gave us indigestion. */
868 break;
869 }
870 }
871
872 /* If we didn't find any digits, then we don't have a valid integer
873 value, so reject the entire token. Otherwise, update the lexical
874 scan pointer, and return non-zero for success. */
875
876 if (digits == 0)
877 {
878 return (0);
879 }
880 else
881 {
882 *tokptrptr = tokptr;
883 return (1);
884 }
885}
886
e58de8a2 887static int
2e66cf7d 888decode_integer_literal (valptr, tokptrptr)
5d074aa9
FF
889 int *valptr;
890 char **tokptrptr;
e58de8a2 891{
2e66cf7d
FF
892 char *tokptr = *tokptrptr;
893 int base = 0;
894 int ival = 0;
2e66cf7d
FF
895 int explicit_base = 0;
896
897 /* Look for an explicit base specifier, which is optional. */
898
899 switch (*tokptr)
900 {
901 case 'd':
902 case 'D':
903 explicit_base++;
904 base = 10;
905 tokptr++;
906 break;
907 case 'b':
908 case 'B':
909 explicit_base++;
910 base = 2;
911 tokptr++;
912 break;
913 case 'h':
914 case 'H':
915 explicit_base++;
916 base = 16;
917 tokptr++;
918 break;
919 case 'o':
920 case 'O':
921 explicit_base++;
922 base = 8;
923 tokptr++;
924 break;
925 default:
926 base = 10;
927 break;
928 }
929
930 /* If we found an explicit base ensure that the character after the
931 explicit base is a single quote. */
932
933 if (explicit_base && (*tokptr++ != '\''))
934 {
935 return (0);
936 }
937
5d074aa9
FF
938 /* Attempt to decode whatever follows as an integer value in the
939 indicated base, updating the token pointer in the process and
940 computing the value into ival. Also, if we have an explicit
2e66cf7d 941 base, then the next character must not be a single quote, or we
5d074aa9
FF
942 have a bitstring literal, so reject the entire token in this case.
943 Otherwise, update the lexical scan pointer, and return non-zero
944 for success. */
945
946 if (!decode_integer_value (base, &tokptr, &ival))
2e66cf7d
FF
947 {
948 return (0);
949 }
950 else if (explicit_base && (*tokptr == '\''))
951 {
952 return (0);
953 }
954 else
955 {
956 *valptr = ival;
957 *tokptrptr = tokptr;
958 return (1);
959 }
960}
961
1188fbbf
FF
962/* If it wasn't for the fact that floating point values can contain '_'
963 characters, we could just let strtod do all the hard work by letting it
964 try to consume as much of the current token buffer as possible and
965 find a legal conversion. Unfortunately we need to filter out the '_'
966 characters before calling strtod, which we do by copying the other
967 legal chars to a local buffer to be converted. However since we also
968 need to keep track of where the last unconsumed character in the input
969 buffer is, we have transfer only as many characters as may compose a
970 legal floating point value. */
971
972static int
973match_float_literal ()
974{
975 char *tokptr = lexptr;
976 char *buf;
977 char *copy;
1188fbbf
FF
978 double dval;
979 extern double strtod ();
980
981 /* Make local buffer in which to build the string to convert. This is
982 required because underscores are valid in chill floating point numbers
983 but not in the string passed to strtod to convert. The string will be
984 no longer than our input string. */
985
986 copy = buf = (char *) alloca (strlen (tokptr) + 1);
987
988 /* Transfer all leading digits to the conversion buffer, discarding any
989 underscores. */
990
991 while (isdigit (*tokptr) || *tokptr == '_')
992 {
993 if (*tokptr != '_')
994 {
995 *copy++ = *tokptr;
996 }
997 tokptr++;
998 }
999
1000 /* Now accept either a '.', or one of [eEdD]. Dot is legal regardless
1001 of whether we found any leading digits, and we simply accept it and
1002 continue on to look for the fractional part and/or exponent. One of
1003 [eEdD] is legal only if we have seen digits, and means that there
1004 is no fractional part. If we find neither of these, then this is
1005 not a floating point number, so return failure. */
1006
1007 switch (*tokptr++)
1008 {
1009 case '.':
1010 /* Accept and then look for fractional part and/or exponent. */
1011 *copy++ = '.';
1012 break;
1013
1014 case 'e':
1015 case 'E':
1016 case 'd':
1017 case 'D':
1018 if (copy == buf)
1019 {
1020 return (0);
1021 }
1022 *copy++ = 'e';
1023 goto collect_exponent;
1024 break;
1025
1026 default:
1027 return (0);
1028 break;
1029 }
1030
1031 /* We found a '.', copy any fractional digits to the conversion buffer, up
1032 to the first nondigit, non-underscore character. */
1033
1034 while (isdigit (*tokptr) || *tokptr == '_')
1035 {
1036 if (*tokptr != '_')
1037 {
1038 *copy++ = *tokptr;
1039 }
1040 tokptr++;
1041 }
1042
1043 /* Look for an exponent, which must start with one of [eEdD]. If none
1044 is found, jump directly to trying to convert what we have collected
1045 so far. */
1046
1047 switch (*tokptr)
1048 {
1049 case 'e':
1050 case 'E':
1051 case 'd':
1052 case 'D':
1053 *copy++ = 'e';
1054 tokptr++;
1055 break;
1056 default:
1057 goto convert_float;
1058 break;
1059 }
1060
1061 /* Accept an optional '-' or '+' following one of [eEdD]. */
1062
1063 collect_exponent:
1064 if (*tokptr == '+' || *tokptr == '-')
1065 {
1066 *copy++ = *tokptr++;
1067 }
1068
1069 /* Now copy an exponent into the conversion buffer. Note that at the
1070 moment underscores are *not* allowed in exponents. */
1071
1072 while (isdigit (*tokptr))
1073 {
1074 *copy++ = *tokptr++;
1075 }
1076
1077 /* If we transfered any chars to the conversion buffer, try to interpret its
1078 contents as a floating point value. If any characters remain, then we
1079 must not have a valid floating point string. */
1080
1081 convert_float:
1082 *copy = '\0';
1083 if (copy != buf)
1084 {
1085 dval = strtod (buf, &copy);
1086 if (*copy == '\0')
1087 {
1088 yylval.dval = dval;
1089 lexptr = tokptr;
1090 return (FLOAT_LITERAL);
1091 }
1092 }
1093 return (0);
1094}
1095
96b6b765 1096/* Recognize a string literal. A string literal is a sequence
c7da3ed3
FF
1097 of characters enclosed in matching single or double quotes, except that
1098 a single character inside single quotes is a character literal, which
1099 we reject as a string literal. To embed the terminator character inside
1100 a string, it is simply doubled (I.E. "this""is""one""string") */
1101
1102static int
1103match_string_literal ()
1104{
1105 char *tokptr = lexptr;
1106
1107 for (tempbufindex = 0, tokptr++; *tokptr != '\0'; tokptr++)
1108 {
1109 CHECKBUF (1);
1110 if (*tokptr == *lexptr)
1111 {
1112 if (*(tokptr + 1) == *lexptr)
1113 {
1114 tokptr++;
1115 }
1116 else
1117 {
1118 break;
1119 }
1120 }
1121 tempbuf[tempbufindex++] = *tokptr;
1122 }
1123 if (*tokptr == '\0' /* no terminator */
c7da3ed3
FF
1124 || (tempbufindex == 1 && *tokptr == '\'')) /* char literal */
1125 {
1126 return (0);
1127 }
1128 else
1129 {
1130 tempbuf[tempbufindex] = '\0';
1131 yylval.sval.ptr = tempbuf;
1132 yylval.sval.length = tempbufindex;
1133 lexptr = ++tokptr;
1134 return (CHARACTER_STRING_LITERAL);
1135 }
1136}
1137
2e66cf7d
FF
1138/* Recognize a character literal. A character literal is single character
1139 or a control sequence, enclosed in single quotes. A control sequence
1140 is a comma separated list of one or more integer literals, enclosed
1141 in parenthesis and introduced with a circumflex character.
1142
1143 EX: 'a' '^(7)' '^(7,8)'
1144
5d074aa9
FF
1145 As a GNU chill extension, the syntax C'xx' is also recognized as a
1146 character literal, where xx is a hex value for the character.
1147
c7da3ed3
FF
1148 Note that more than a single character, enclosed in single quotes, is
1149 a string literal.
1150
c4413e2c
FF
1151 Also note that the control sequence form is not in GNU Chill since it
1152 is ambiguous with the string literal form using single quotes. I.E.
1153 is '^(7)' a character literal or a string literal. In theory it it
1154 possible to tell by context, but GNU Chill doesn't accept the control
1155 sequence form, so neither do we (for now the code is disabled).
1156
2e66cf7d
FF
1157 Returns CHARACTER_LITERAL if a match is found.
1158 */
1159
1160static int
1161match_character_literal ()
1162{
1163 char *tokptr = lexptr;
1164 int ival = 0;
1165
db2302cb 1166 if ((*tokptr == 'c' || *tokptr == 'C') && (*(tokptr + 1) == '\''))
2e66cf7d 1167 {
5d074aa9
FF
1168 /* We have a GNU chill extension form, so skip the leading "C'",
1169 decode the hex value, and then ensure that we have a trailing
1170 single quote character. */
2e66cf7d 1171 tokptr += 2;
5d074aa9 1172 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
e58de8a2 1173 {
2e66cf7d 1174 return (0);
e58de8a2 1175 }
5d074aa9 1176 tokptr++;
2e66cf7d 1177 }
5d074aa9 1178 else if (*tokptr == '\'')
2e66cf7d 1179 {
5d074aa9 1180 tokptr++;
2e66cf7d 1181
5d074aa9
FF
1182 /* Determine which form we have, either a control sequence or the
1183 single character form. */
1184
1185 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1186 {
9da75ad3 1187#if 0 /* Disable, see note above. -fnf */
5d074aa9
FF
1188 /* Match and decode a control sequence. Return zero if we don't
1189 find a valid integer literal, or if the next unconsumed character
1190 after the integer literal is not the trailing ')'.
1191 FIXME: We currently don't handle the multiple integer literal
1192 form. */
1193 tokptr += 2;
1194 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1195 {
1196 return (0);
1197 }
9da75ad3
FF
1198#else
1199 return (0);
1200#endif
5d074aa9
FF
1201 }
1202 else
1203 {
1204 ival = *tokptr++;
1205 }
1206
1207 /* The trailing quote has not yet been consumed. If we don't find
1208 it, then we have no match. */
1209
1210 if (*tokptr++ != '\'')
1211 {
1212 return (0);
1213 }
2e66cf7d 1214 }
aed656ba
FF
1215 else
1216 {
1217 /* Not a character literal. */
1218 return (0);
1219 }
2e66cf7d
FF
1220 yylval.typed_val.val = ival;
1221 yylval.typed_val.type = builtin_type_chill_char;
1222 lexptr = tokptr;
1223 return (CHARACTER_LITERAL);
e58de8a2
FF
1224}
1225
1226/* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1227 Note that according to 5.2.4.2, a single "_" is also a valid integer
1228 literal, however GNU-chill requires there to be at least one "digit"
1229 in any integer literal. */
1230
1231static int
2e66cf7d 1232match_integer_literal ()
e58de8a2 1233{
2e66cf7d 1234 char *tokptr = lexptr;
ae0afa4b 1235 int ival;
2e66cf7d 1236
ae0afa4b 1237 if (!decode_integer_literal (&ival, &tokptr))
2e66cf7d
FF
1238 {
1239 return (0);
1240 }
ae0afa4b 1241 else
2e66cf7d
FF
1242 {
1243 yylval.typed_val.val = ival;
1244 yylval.typed_val.type = builtin_type_int;
1245 lexptr = tokptr;
1246 return (INTEGER_LITERAL);
1247 }
e58de8a2
FF
1248}
1249
81028ab0
FF
1250/* Recognize a bit-string literal, as specified in Z.200 sec 5.2.4.8
1251 Note that according to 5.2.4.8, a single "_" is also a valid bit-string
1252 literal, however GNU-chill requires there to be at least one "digit"
1253 in any bit-string literal. */
1254
1255static int
1256match_bitstring_literal ()
1257{
6d34c236 1258 register char *tokptr = lexptr;
81028ab0
FF
1259 int bitoffset = 0;
1260 int bitcount = 0;
6d34c236 1261 int bits_per_char;
81028ab0 1262 int digit;
81028ab0 1263
c7da3ed3 1264 tempbufindex = 0;
6d34c236
PB
1265 CHECKBUF (1);
1266 tempbuf[0] = 0;
c7da3ed3 1267
81028ab0
FF
1268 /* Look for the required explicit base specifier. */
1269
1270 switch (*tokptr++)
1271 {
1272 case 'b':
1273 case 'B':
6d34c236 1274 bits_per_char = 1;
81028ab0
FF
1275 break;
1276 case 'o':
1277 case 'O':
6d34c236 1278 bits_per_char = 3;
81028ab0
FF
1279 break;
1280 case 'h':
1281 case 'H':
6d34c236 1282 bits_per_char = 4;
81028ab0
FF
1283 break;
1284 default:
1285 return (0);
1286 break;
1287 }
6d34c236 1288
81028ab0
FF
1289 /* Ensure that the character after the explicit base is a single quote. */
1290
1291 if (*tokptr++ != '\'')
1292 {
1293 return (0);
1294 }
1295
1296 while (*tokptr != '\0' && *tokptr != '\'')
1297 {
db2302cb
PS
1298 digit = *tokptr;
1299 if (isupper (digit))
1300 digit = tolower (digit);
81028ab0
FF
1301 tokptr++;
1302 switch (digit)
1303 {
1304 case '_':
1305 continue;
1306 case '0': case '1': case '2': case '3': case '4':
1307 case '5': case '6': case '7': case '8': case '9':
1308 digit -= '0';
1309 break;
1310 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1311 digit -= 'a';
1312 digit += 10;
1313 break;
1314 default:
1315 return (0);
1316 break;
1317 }
6d34c236 1318 if (digit >= 1 << bits_per_char)
81028ab0
FF
1319 {
1320 /* Found something not in domain for current base. */
1321 return (0);
1322 }
1323 else
1324 {
6d34c236
PB
1325 /* Extract bits from digit, packing them into the bitstring byte. */
1326 int k = TARGET_BYTE_ORDER == BIG_ENDIAN ? bits_per_char - 1 : 0;
1327 for (; TARGET_BYTE_ORDER == BIG_ENDIAN ? k >= 0 : k < bits_per_char;
1328 TARGET_BYTE_ORDER == BIG_ENDIAN ? k-- : k++)
81028ab0
FF
1329 {
1330 bitcount++;
6d34c236 1331 if (digit & (1 << k))
81028ab0 1332 {
6d34c236
PB
1333 tempbuf[tempbufindex] |=
1334 (TARGET_BYTE_ORDER == BIG_ENDIAN)
1335 ? (1 << (HOST_CHAR_BIT - 1 - bitoffset))
1336 : (1 << bitoffset);
81028ab0
FF
1337 }
1338 bitoffset++;
1339 if (bitoffset == HOST_CHAR_BIT)
1340 {
1341 bitoffset = 0;
1342 tempbufindex++;
6d34c236
PB
1343 CHECKBUF(1);
1344 tempbuf[tempbufindex] = 0;
81028ab0
FF
1345 }
1346 }
1347 }
1348 }
1349
1350 /* Verify that we consumed everything up to the trailing single quote,
1351 and that we found some bits (IE not just underbars). */
1352
1353 if (*tokptr++ != '\'')
1354 {
1355 return (0);
1356 }
1357 else
1358 {
1359 yylval.sval.ptr = tempbuf;
1360 yylval.sval.length = bitcount;
1361 lexptr = tokptr;
1362 return (BIT_STRING_LITERAL);
1363 }
1364}
1365
e58de8a2
FF
1366struct token
1367{
1368 char *operator;
1369 int token;
1370};
1371
5a7c9cce 1372static const struct token idtokentab[] =
81028ab0 1373{
835c2559 1374 { "array", ARRAY },
5a7c9cce
PB
1375 { "length", LENGTH },
1376 { "lower", LOWER },
1377 { "upper", UPPER },
1378 { "andif", ANDIF },
1379 { "pred", PRED },
1380 { "succ", SUCC },
1381 { "card", CARD },
1382 { "size", SIZE },
1383 { "orif", ORIF },
1384 { "num", NUM },
1385 { "abs", ABS },
57ffffe3
JG
1386 { "max", MAX_TOKEN },
1387 { "min", MIN_TOKEN },
5a7c9cce
PB
1388 { "mod", MOD },
1389 { "rem", REM },
1390 { "not", NOT },
1391 { "xor", LOGXOR },
1392 { "and", LOGAND },
1393 { "in", IN },
2d67c7e9 1394 { "or", LOGIOR },
f91a9e05 1395 { "up", UP },
0faa3247 1396 { "addr", ADDR_TOKEN },
2d67c7e9 1397 { "null", EMPTINESS_LITERAL }
e58de8a2
FF
1398};
1399
a8a69e63 1400static const struct token tokentab2[] =
e58de8a2 1401{
45fe3db4 1402 { ":=", GDB_ASSIGNMENT },
e58de8a2 1403 { "//", SLASH_SLASH },
8a177da6 1404 { "->", POINTER },
e58de8a2
FF
1405 { "/=", NOTEQUAL },
1406 { "<=", LEQ },
5a7c9cce 1407 { ">=", GTR }
e58de8a2
FF
1408};
1409
1410/* Read one token, getting characters through lexptr. */
1411/* This is where we will check to make sure that the language and the
1412 operators used are compatible. */
1413
1414static int
1415yylex ()
1416{
1417 unsigned int i;
1418 int token;
ad86f717 1419 char *inputname;
cbd1bdc3 1420 struct symbol *sym;
e58de8a2
FF
1421
1422 /* Skip over any leading whitespace. */
1423 while (isspace (*lexptr))
1424 {
1425 lexptr++;
1426 }
1427 /* Look for special single character cases which can't be the first
1428 character of some other multicharacter token. */
1429 switch (*lexptr)
1430 {
1431 case '\0':
1432 return (0);
54bbbfb4 1433 case ',':
e58de8a2 1434 case '=':
e58de8a2
FF
1435 case ';':
1436 case '!':
1437 case '+':
e58de8a2 1438 case '*':
e58de8a2
FF
1439 case '(':
1440 case ')':
1441 case '[':
1442 case ']':
1443 return (*lexptr++);
1444 }
1445 /* Look for characters which start a particular kind of multicharacter
45fe3db4 1446 token, such as a character literal, register name, convenience
c7da3ed3 1447 variable name, string literal, etc. */
e58de8a2 1448 switch (*lexptr)
2e66cf7d 1449 {
c7da3ed3
FF
1450 case '\'':
1451 case '\"':
96b6b765 1452 /* First try to match a string literal, which is any
c7da3ed3
FF
1453 sequence of characters enclosed in matching single or double
1454 quotes, except that a single character inside single quotes
1455 is a character literal, so we have to catch that case also. */
1456 token = match_string_literal ();
1457 if (token != 0)
1458 {
1459 return (token);
1460 }
1461 if (*lexptr == '\'')
1462 {
1463 token = match_character_literal ();
1464 if (token != 0)
1465 {
1466 return (token);
1467 }
1468 }
1469 break;
5d074aa9
FF
1470 case 'C':
1471 case 'c':
2e66cf7d
FF
1472 token = match_character_literal ();
1473 if (token != 0)
1474 {
1475 return (token);
1476 }
1477 break;
45fe3db4 1478 case '$':
c700638c
PB
1479 yylval.sval.ptr = lexptr;
1480 do {
1481 lexptr++;
93f972b7 1482 } while (isalnum (*lexptr) || *lexptr == '_' || *lexptr == '$');
c700638c
PB
1483 yylval.sval.length = lexptr - yylval.sval.ptr;
1484 write_dollar_variable (yylval.sval);
1485 return GDB_VARIABLE;
45fe3db4 1486 break;
2e66cf7d 1487 }
e58de8a2
FF
1488 /* See if it is a special token of length 2. */
1489 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1490 {
45fe3db4 1491 if (STREQN (lexptr, tokentab2[i].operator, 2))
e58de8a2
FF
1492 {
1493 lexptr += 2;
1494 return (tokentab2[i].token);
1495 }
1496 }
1497 /* Look for single character cases which which could be the first
1498 character of some other multicharacter token, but aren't, or we
1499 would already have found it. */
1500 switch (*lexptr)
1501 {
8a177da6 1502 case '-':
45fe3db4 1503 case ':':
e58de8a2
FF
1504 case '/':
1505 case '<':
1506 case '>':
1507 return (*lexptr++);
1508 }
1188fbbf
FF
1509 /* Look for a float literal before looking for an integer literal, so
1510 we match as much of the input stream as possible. */
1511 token = match_float_literal ();
81028ab0
FF
1512 if (token != 0)
1513 {
1514 return (token);
1515 }
1516 token = match_bitstring_literal ();
1188fbbf
FF
1517 if (token != 0)
1518 {
1519 return (token);
1520 }
2e66cf7d 1521 token = match_integer_literal ();
cbd1bdc3 1522 if (token != 0)
e58de8a2
FF
1523 {
1524 return (token);
1525 }
cbd1bdc3
FF
1526
1527 /* Try to match a simple name string, and if a match is found, then
1528 further classify what sort of name it is and return an appropriate
1529 token. Note that attempting to match a simple name string consumes
1530 the token from lexptr, so we can't back out if we later find that
1531 we can't classify what sort of name it is. */
1532
ad86f717 1533 inputname = match_simple_name_string ();
5a7c9cce 1534
ad86f717 1535 if (inputname != NULL)
cbd1bdc3 1536 {
a30e2087 1537 char *simplename = (char*) alloca (strlen (inputname) + 1);
ad86f717
PB
1538
1539 char *dptr = simplename, *sptr = inputname;
1540 for (; *sptr; sptr++)
1541 *dptr++ = isupper (*sptr) ? tolower(*sptr) : *sptr;
a30e2087 1542 *dptr = '\0';
ad86f717 1543
d8f23320
PS
1544 /* See if it is a reserved identifier. */
1545 for (i = 0; i < sizeof (idtokentab) / sizeof (idtokentab[0]); i++)
1546 {
1547 if (STREQ (simplename, idtokentab[i].operator))
1548 {
1549 return (idtokentab[i].token);
1550 }
1551 }
1552
1553 /* Look for other special tokens. */
1554 if (STREQ (simplename, "true"))
1555 {
1556 yylval.ulval = 1;
1557 return (BOOLEAN_LITERAL);
1558 }
1559 if (STREQ (simplename, "false"))
1560 {
1561 yylval.ulval = 0;
1562 return (BOOLEAN_LITERAL);
1563 }
1564
ad86f717 1565 sym = lookup_symbol (inputname, expression_context_block,
cbd1bdc3
FF
1566 VAR_NAMESPACE, (int *) NULL,
1567 (struct symtab **) NULL);
ad86f717
PB
1568 if (sym == NULL && strcmp (inputname, simplename) != 0)
1569 {
1570 sym = lookup_symbol (simplename, expression_context_block,
1571 VAR_NAMESPACE, (int *) NULL,
1572 (struct symtab **) NULL);
1573 }
cbd1bdc3
FF
1574 if (sym != NULL)
1575 {
1576 yylval.ssym.stoken.ptr = NULL;
1577 yylval.ssym.stoken.length = 0;
1578 yylval.ssym.sym = sym;
1579 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1580 switch (SYMBOL_CLASS (sym))
1581 {
1582 case LOC_BLOCK:
1583 /* Found a procedure name. */
1584 return (GENERAL_PROCEDURE_NAME);
1585 case LOC_STATIC:
1586 /* Found a global or local static variable. */
1587 return (LOCATION_NAME);
a8a69e63
FF
1588 case LOC_REGISTER:
1589 case LOC_ARG:
1590 case LOC_REF_ARG:
1591 case LOC_REGPARM:
996ccb30 1592 case LOC_REGPARM_ADDR:
a8a69e63 1593 case LOC_LOCAL:
76a0ffb4 1594 case LOC_LOCAL_ARG:
a1c8d76e
JK
1595 case LOC_BASEREG:
1596 case LOC_BASEREG_ARG:
76a0ffb4
FF
1597 if (innermost_block == NULL
1598 || contained_in (block_found, innermost_block))
1599 {
1600 innermost_block = block_found;
1601 }
1602 return (LOCATION_NAME);
1603 break;
1604 case LOC_CONST:
a8a69e63 1605 case LOC_LABEL:
76a0ffb4
FF
1606 return (LOCATION_NAME);
1607 break;
76a0ffb4 1608 case LOC_TYPEDEF:
8a177da6
PB
1609 yylval.tsym.type = SYMBOL_TYPE (sym);
1610 return TYPENAME;
1611 case LOC_UNDEF:
a8a69e63 1612 case LOC_CONST_BYTES:
0848ad1c 1613 case LOC_OPTIMIZED_OUT:
ad86f717 1614 error ("Symbol \"%s\" names no location.", inputname);
a8a69e63 1615 break;
cbd1bdc3
FF
1616 }
1617 }
1618 else if (!have_full_symbols () && !have_partial_symbols ())
1619 {
1620 error ("No symbol table is loaded. Use the \"file\" command.");
1621 }
1622 else
1623 {
ad86f717 1624 error ("No symbol \"%s\" in current context.", inputname);
cbd1bdc3
FF
1625 }
1626 }
1627
1188fbbf
FF
1628 /* Catch single character tokens which are not part of some
1629 longer token. */
1630
1631 switch (*lexptr)
1632 {
1633 case '.': /* Not float for example. */
8a177da6
PB
1634 lexptr++;
1635 while (isspace (*lexptr)) lexptr++;
ad86f717
PB
1636 inputname = match_simple_name_string ();
1637 if (!inputname)
8a177da6
PB
1638 return '.';
1639 return FIELD_NAME;
1188fbbf
FF
1640 }
1641
e58de8a2
FF
1642 return (ILLEGAL_TOKEN);
1643}
1644
ef8e137c 1645static void
61379835
PB
1646write_lower_upper_value (opcode, type)
1647 enum exp_opcode opcode; /* Either UNOP_LOWER or UNOP_UPPER */
1648 struct type *type;
1649{
1650 extern LONGEST type_lower_upper ();
1651 struct type *result_type;
1652 LONGEST val = type_lower_upper (opcode, type, &result_type);
1653 write_exp_elt_opcode (OP_LONG);
1654 write_exp_elt_type (result_type);
1655 write_exp_elt_longcst (val);
1656 write_exp_elt_opcode (OP_LONG);
1657}
1658
22e39759 1659void
e58de8a2 1660yyerror (msg)
8db1a922 1661 char *msg;
e58de8a2 1662{
8db1a922 1663 error ("A %s in expression, near `%s'.", (msg ? msg : "error"), lexptr);
e58de8a2 1664}
This page took 0.225489 seconds and 4 git commands to generate.