* Makefile.in (BISON): Add comment that when bison is used, it
[binutils-gdb.git] / gdb / ch-exp.y
1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
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
56 #include "defs.h"
57 #include "expression.h"
58 #include "language.h"
59 #include "value.h"
60 #include "parser-defs.h"
61 #include "ch-lang.h"
62
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. */
69
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
78 #define yyr1 chill_r1
79 #define yyr2 chill_r2
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
87 #define yyps chill_ps
88 #define yypv chill_pv
89 #define yys chill_s
90 #define yy_yys chill_yys
91 #define yystate chill_state
92 #define yytmp chill_tmp
93 #define yyv chill_v
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 */
99
100 #ifndef YYDEBUG
101 #define YYDEBUG 0 /* Default to no yydebug support */
102 #endif
103
104 int
105 yyparse PARAMS ((void));
106
107 static int
108 yylex PARAMS ((void));
109
110 void
111 yyerror PARAMS ((char *));
112
113 %}
114
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. */
118
119 %union
120 {
121 LONGEST lval;
122 unsigned LONGEST ulval;
123 struct {
124 LONGEST val;
125 struct type *type;
126 } typed_val;
127 double dval;
128 struct symbol *sym;
129 struct type *tval;
130 struct stoken sval;
131 struct ttype tsym;
132 struct symtoken ssym;
133 int voidval;
134 struct block *bval;
135 enum exp_opcode opcode;
136 struct internalvar *ivar;
137
138 struct type **tvec;
139 int *ivec;
140 }
141
142 %token <voidval> FIXME
143
144 %token <typed_val> INTEGER_LITERAL
145 %token <ulval> BOOLEAN_LITERAL
146 %token <typed_val> CHARACTER_LITERAL
147 %token <ssym> GENERAL_PROCEDURE_NAME
148 %token <ssym> LOCATION_NAME
149 %token <voidval> SET_LITERAL
150 %token <voidval> EMPTINESS_LITERAL
151 %token <voidval> CHARACTER_STRING_LITERAL
152 %token <voidval> BIT_STRING_LITERAL
153
154 %token <voidval> STRING
155 %token <voidval> CONSTANT
156 %token <voidval> '.'
157 %token <voidval> ';'
158 %token <voidval> ':'
159 %token <voidval> CASE
160 %token <voidval> OF
161 %token <voidval> ESAC
162 %token <voidval> LOGIOR
163 %token <voidval> ORIF
164 %token <voidval> LOGXOR
165 %token <voidval> LOGAND
166 %token <voidval> ANDIF
167 %token <voidval> '='
168 %token <voidval> NOTEQUAL
169 %token <voidval> '>'
170 %token <voidval> GTR
171 %token <voidval> '<'
172 %token <voidval> LEQ
173 %token <voidval> IN
174 %token <voidval> '+'
175 %token <voidval> '-'
176 %token <voidval> '*'
177 %token <voidval> '/'
178 %token <voidval> SLASH_SLASH
179 %token <voidval> MOD
180 %token <voidval> REM
181 %token <voidval> NOT
182 %token <voidval> POINTER
183 %token <voidval> RECEIVE
184 %token <voidval> SC
185 %token <voidval> '['
186 %token <voidval> ']'
187 %token <voidval> '('
188 %token <voidval> ')'
189 %token <voidval> UP
190 %token <voidval> IF
191 %token <voidval> THEN
192 %token <voidval> ELSE
193 %token <voidval> FI
194 %token <voidval> ELSIF
195 %token <voidval> ILLEGAL_TOKEN
196
197 /* Tokens which are not Chill tokens used in expressions, but rather GDB
198 specific things that we recognize in the same context as Chill tokens
199 (register names for example). */
200
201 %token <lval> GDB_REGNAME /* Machine register name */
202 %token <lval> GDB_LAST /* Value history */
203 %token <ivar> GDB_VARIABLE /* Convenience variable */
204 %token <voidval> GDB_ASSIGNMENT /* Assign value to somewhere */
205
206 %type <voidval> location
207 %type <voidval> access_name
208 %type <voidval> primitive_value
209 %type <voidval> location_contents
210 %type <voidval> value_name
211 %type <voidval> literal
212 %type <voidval> tuple
213 %type <voidval> value_string_element
214 %type <voidval> value_string_slice
215 %type <voidval> value_array_element
216 %type <voidval> value_array_slice
217 %type <voidval> value_structure_field
218 %type <voidval> expression_conversion
219 %type <voidval> value_procedure_call
220 %type <voidval> value_built_in_routine_call
221 %type <voidval> start_expression
222 %type <voidval> zero_adic_operator
223 %type <voidval> parenthesised_expression
224 %type <voidval> value
225 %type <voidval> undefined_value
226 %type <voidval> expression
227 %type <voidval> conditional_expression
228 %type <voidval> then_alternative
229 %type <voidval> else_alternative
230 %type <voidval> sub_expression
231 %type <voidval> value_case_alternative
232 %type <voidval> operand_0
233 %type <voidval> operand_1
234 %type <voidval> operand_2
235 %type <voidval> operand_3
236 %type <voidval> operand_4
237 %type <voidval> operand_5
238 %type <voidval> operand_6
239 %type <voidval> integer_literal_expression
240 %type <voidval> synonym_name
241 %type <voidval> value_enumeration_name
242 %type <voidval> value_do_with_name
243 %type <voidval> value_receive_name
244 %type <voidval> string_primitive_value
245 %type <voidval> start_element
246 %type <voidval> left_element
247 %type <voidval> right_element
248 %type <voidval> slice_size
249 %type <voidval> array_primitive_value
250 %type <voidval> expression_list
251 %type <voidval> lower_element
252 %type <voidval> upper_element
253 %type <voidval> first_element
254 %type <voidval> structure_primitive_value
255 %type <voidval> field_name
256 %type <voidval> mode_name
257 %type <voidval> boolean_expression
258 %type <voidval> case_selector_list
259 %type <voidval> subexpression
260 %type <voidval> case_label_specification
261 %type <voidval> buffer_location
262
263 %type <voidval> single_assignment_action
264
265 %%
266
267 /* Z.200, 5.3.1 */
268
269 value : expression
270 {
271 $$ = 0; /* FIXME */
272 }
273 | undefined_value
274 {
275 $$ = 0; /* FIXME */
276 }
277 ;
278
279 undefined_value : FIXME
280 {
281 $$ = 0; /* FIXME */
282 }
283 ;
284
285 /* Z.200, 4.2.1 */
286
287 location : access_name
288 {
289 $$ = 0; /* FIXME */
290 }
291 | FIXME
292 {
293 $$ = 0; /* FIXME */
294 }
295 ;
296
297 /* Z.200, 4.2.2 */
298
299 access_name : LOCATION_NAME
300 {
301 write_exp_elt_opcode (OP_VAR_VALUE);
302 write_exp_elt_sym ($1.sym);
303 write_exp_elt_opcode (OP_VAR_VALUE);
304 }
305 | GDB_LAST /* gdb specific */
306 {
307 write_exp_elt_opcode (OP_LAST);
308 write_exp_elt_longcst ($1);
309 write_exp_elt_opcode (OP_LAST);
310 }
311 | GDB_REGNAME /* gdb specific */
312 {
313 write_exp_elt_opcode (OP_REGISTER);
314 write_exp_elt_longcst ($1);
315 write_exp_elt_opcode (OP_REGISTER);
316 }
317 | GDB_VARIABLE /* gdb specific */
318 {
319 write_exp_elt_opcode (OP_INTERNALVAR);
320 write_exp_elt_intern ($1);
321 write_exp_elt_opcode (OP_INTERNALVAR);
322 }
323 | FIXME
324 {
325 $$ = 0; /* FIXME */
326 }
327 ;
328
329 /* Z.200, 5.2.1 */
330
331 primitive_value : location_contents
332 {
333 $$ = 0; /* FIXME */
334 }
335 | value_name
336 {
337 $$ = 0; /* FIXME */
338 }
339 | literal
340 {
341 $$ = 0; /* FIXME */
342 }
343 | tuple
344 {
345 $$ = 0; /* FIXME */
346 }
347 | value_string_element
348 {
349 $$ = 0; /* FIXME */
350 }
351 | value_string_slice
352 {
353 $$ = 0; /* FIXME */
354 }
355 | value_array_element
356 {
357 $$ = 0; /* FIXME */
358 }
359 | value_array_slice
360 {
361 $$ = 0; /* FIXME */
362 }
363 | value_structure_field
364 {
365 $$ = 0; /* FIXME */
366 }
367 | expression_conversion
368 {
369 $$ = 0; /* FIXME */
370 }
371 | value_procedure_call
372 {
373 $$ = 0; /* FIXME */
374 }
375 | value_built_in_routine_call
376 {
377 $$ = 0; /* FIXME */
378 }
379 | start_expression
380 {
381 $$ = 0; /* FIXME */
382 }
383 | zero_adic_operator
384 {
385 $$ = 0; /* FIXME */
386 }
387 | parenthesised_expression
388 {
389 $$ = 0; /* FIXME */
390 }
391 ;
392
393 /* Z.200, 5.2.2 */
394
395 location_contents: location
396 {
397 $$ = 0; /* FIXME */
398 }
399 ;
400
401 /* Z.200, 5.2.3 */
402
403 value_name : synonym_name
404 {
405 $$ = 0; /* FIXME */
406 }
407 | value_enumeration_name
408 {
409 $$ = 0; /* FIXME */
410 }
411 | value_do_with_name
412 {
413 $$ = 0; /* FIXME */
414 }
415 | value_receive_name
416 {
417 $$ = 0; /* FIXME */
418 }
419 | GENERAL_PROCEDURE_NAME
420 {
421 write_exp_elt_opcode (OP_VAR_VALUE);
422 write_exp_elt_sym ($1.sym);
423 write_exp_elt_opcode (OP_VAR_VALUE);
424 }
425 ;
426
427 /* Z.200, 5.2.4.1 */
428
429 literal : INTEGER_LITERAL
430 {
431 write_exp_elt_opcode (OP_LONG);
432 write_exp_elt_type ($1.type);
433 write_exp_elt_longcst ((LONGEST) ($1.val));
434 write_exp_elt_opcode (OP_LONG);
435 }
436 | BOOLEAN_LITERAL
437 {
438 write_exp_elt_opcode (OP_BOOL);
439 write_exp_elt_longcst ((LONGEST) $1);
440 write_exp_elt_opcode (OP_BOOL);
441 }
442 | CHARACTER_LITERAL
443 {
444 write_exp_elt_opcode (OP_LONG);
445 write_exp_elt_type ($1.type);
446 write_exp_elt_longcst ((LONGEST) ($1.val));
447 write_exp_elt_opcode (OP_LONG);
448 }
449 | SET_LITERAL
450 {
451 $$ = 0; /* FIXME */
452 }
453 | EMPTINESS_LITERAL
454 {
455 $$ = 0; /* FIXME */
456 }
457 | CHARACTER_STRING_LITERAL
458 {
459 $$ = 0; /* FIXME */
460 }
461 | BIT_STRING_LITERAL
462 {
463 $$ = 0; /* FIXME */
464 }
465 ;
466
467 /* Z.200, 5.2.5 */
468
469 tuple : FIXME
470 {
471 $$ = 0; /* FIXME */
472 }
473 ;
474
475
476 /* Z.200, 5.2.6 */
477
478 value_string_element: string_primitive_value '(' start_element ')'
479 {
480 $$ = 0; /* FIXME */
481 }
482 ;
483
484 /* Z.200, 5.2.7 */
485
486 value_string_slice: string_primitive_value '(' left_element ':' right_element ')'
487 {
488 $$ = 0; /* FIXME */
489 }
490 | string_primitive_value '(' start_element UP slice_size ')'
491 {
492 $$ = 0; /* FIXME */
493 }
494 ;
495
496 /* Z.200, 5.2.8 */
497
498 value_array_element: array_primitive_value '(' expression_list ')'
499 {
500 $$ = 0; /* FIXME */
501 }
502 ;
503
504 /* Z.200, 5.2.9 */
505
506 value_array_slice: array_primitive_value '(' lower_element ':' upper_element ')'
507 {
508 $$ = 0; /* FIXME */
509 }
510 | array_primitive_value '(' first_element UP slice_size ')'
511 {
512 $$ = 0; /* FIXME */
513 }
514 ;
515
516 /* Z.200, 5.2.10 */
517
518 value_structure_field: structure_primitive_value '.' field_name
519 {
520 $$ = 0; /* FIXME */
521 }
522 ;
523
524 /* Z.200, 5.2.11 */
525
526 expression_conversion: mode_name '(' expression ')'
527 {
528 $$ = 0; /* FIXME */
529 }
530 ;
531
532 /* Z.200, 5.2.12 */
533
534 value_procedure_call: FIXME
535 {
536 $$ = 0; /* FIXME */
537 }
538 ;
539
540 /* Z.200, 5.2.13 */
541
542 value_built_in_routine_call: FIXME
543 {
544 $$ = 0; /* FIXME */
545 }
546 ;
547
548 /* Z.200, 5.2.14 */
549
550 start_expression: FIXME
551 {
552 $$ = 0; /* FIXME */
553 } /* Not in GNU-Chill */
554 ;
555
556 /* Z.200, 5.2.15 */
557
558 zero_adic_operator: FIXME
559 {
560 $$ = 0; /* FIXME */
561 }
562 ;
563
564 /* Z.200, 5.2.16 */
565
566 parenthesised_expression: '(' expression ')'
567 {
568 $$ = 0; /* FIXME */
569 }
570 ;
571
572 /* Z.200, 5.3.2 */
573
574 expression : operand_0
575 {
576 $$ = 0; /* FIXME */
577 }
578 | conditional_expression
579 {
580 $$ = 0; /* FIXME */
581 }
582 ;
583
584 conditional_expression : IF boolean_expression then_alternative else_alternative FI
585 {
586 $$ = 0; /* FIXME */
587 }
588 | CASE case_selector_list OF value_case_alternative '[' ELSE sub_expression ']' ESAC
589 {
590 $$ = 0; /* FIXME */
591 }
592 ;
593
594 then_alternative: THEN subexpression
595 {
596 $$ = 0; /* FIXME */
597 }
598 ;
599
600 else_alternative: ELSE subexpression
601 {
602 $$ = 0; /* FIXME */
603 }
604 | ELSIF boolean_expression then_alternative else_alternative
605 {
606 $$ = 0; /* FIXME */
607 }
608 ;
609
610 sub_expression : expression
611 {
612 $$ = 0; /* FIXME */
613 }
614 ;
615
616 value_case_alternative: case_label_specification ':' sub_expression ';'
617 {
618 $$ = 0; /* FIXME */
619 }
620 ;
621
622 /* Z.200, 5.3.3 */
623
624 operand_0 : operand_1
625 {
626 $$ = 0; /* FIXME */
627 }
628 | operand_0 LOGIOR operand_1
629 {
630 write_exp_elt_opcode (BINOP_BITWISE_IOR);
631 }
632 | operand_0 ORIF operand_1
633 {
634 $$ = 0; /* FIXME */
635 }
636 | operand_0 LOGXOR operand_1
637 {
638 write_exp_elt_opcode (BINOP_BITWISE_XOR);
639 }
640 | single_assignment_action
641 {
642 $$ = 0; /* FIXME */
643 }
644 ;
645
646 /* Z.200, 5.3.4 */
647
648 operand_1 : operand_2
649 {
650 $$ = 0; /* FIXME */
651 }
652 | operand_1 LOGAND operand_2
653 {
654 write_exp_elt_opcode (BINOP_BITWISE_AND);
655 }
656 | operand_1 ANDIF operand_2
657 {
658 $$ = 0; /* FIXME */
659 }
660 ;
661
662 /* Z.200, 5.3.5 */
663
664 operand_2 : operand_3
665 {
666 $$ = 0; /* FIXME */
667 }
668 | operand_2 '=' operand_3
669 {
670 write_exp_elt_opcode (BINOP_EQUAL);
671 }
672 | operand_2 NOTEQUAL operand_3
673 {
674 write_exp_elt_opcode (BINOP_NOTEQUAL);
675 }
676 | operand_2 '>' operand_3
677 {
678 write_exp_elt_opcode (BINOP_GTR);
679 }
680 | operand_2 GTR operand_3
681 {
682 write_exp_elt_opcode (BINOP_GEQ);
683 }
684 | operand_2 '<' operand_3
685 {
686 write_exp_elt_opcode (BINOP_LESS);
687 }
688 | operand_2 LEQ operand_3
689 {
690 write_exp_elt_opcode (BINOP_LEQ);
691 }
692 | operand_2 IN operand_3
693 {
694 $$ = 0; /* FIXME */
695 }
696 ;
697
698
699 /* Z.200, 5.3.6 */
700
701 operand_3 : operand_4
702 {
703 $$ = 0; /* FIXME */
704 }
705 | operand_3 '+' operand_4
706 {
707 write_exp_elt_opcode (BINOP_ADD);
708 }
709 | operand_3 '-' operand_4
710 {
711 write_exp_elt_opcode (BINOP_SUB);
712 }
713 | operand_3 SLASH_SLASH operand_4
714 {
715 $$ = 0; /* FIXME */
716 }
717 ;
718
719 /* Z.200, 5.3.7 */
720
721 operand_4 : operand_5
722 {
723 $$ = 0; /* FIXME */
724 }
725 | operand_4 '*' operand_5
726 {
727 write_exp_elt_opcode (BINOP_MUL);
728 }
729 | operand_4 '/' operand_5
730 {
731 write_exp_elt_opcode (BINOP_DIV);
732 }
733 | operand_4 MOD operand_5
734 {
735 $$ = 0; /* FIXME */
736 }
737 | operand_4 REM operand_5
738 {
739 $$ = 0; /* FIXME */
740 }
741 ;
742
743 /* Z.200, 5.3.8 */
744
745 operand_5 : operand_6
746 {
747 $$ = 0; /* FIXME */
748 }
749 | '-' operand_6
750 {
751 write_exp_elt_opcode (UNOP_NEG);
752 }
753 | NOT operand_6
754 {
755 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
756 }
757 | '(' integer_literal_expression ')' operand_6
758 {
759 $$ = 0; /* FIXME */
760 }
761 ;
762
763 /* Z.200, 5.3.9 */
764
765 operand_6 : POINTER location
766 {
767 $$ = 0; /* FIXME */
768 }
769 | RECEIVE buffer_location
770 {
771 $$ = 0; /* FIXME */
772 }
773 | primitive_value
774 {
775 $$ = 0; /* FIXME */
776 }
777 ;
778
779
780 /* Z.200, 6.2 */
781
782 single_assignment_action : location GDB_ASSIGNMENT value
783 {
784 write_exp_elt_opcode (BINOP_ASSIGN);
785 }
786
787 /* Z.200, 12.4.3 */
788 /* FIXME: For now we just accept only a single integer literal. */
789
790 integer_literal_expression:
791 INTEGER_LITERAL
792 {
793 $$ = 0;
794 }
795
796 /* Things which still need productions... */
797 synonym_name : FIXME { $$ = 0; }
798 value_enumeration_name : FIXME { $$ = 0; }
799 value_do_with_name : FIXME { $$ = 0; }
800 value_receive_name : FIXME { $$ = 0; }
801 string_primitive_value : FIXME { $$ = 0; }
802 start_element : FIXME { $$ = 0; }
803 left_element : FIXME { $$ = 0; }
804 right_element : FIXME { $$ = 0; }
805 slice_size : FIXME { $$ = 0; }
806 array_primitive_value : FIXME { $$ = 0; }
807 expression_list : FIXME { $$ = 0; }
808 lower_element : FIXME { $$ = 0; }
809 upper_element : FIXME { $$ = 0; }
810 first_element : FIXME { $$ = 0; }
811 structure_primitive_value: FIXME { $$ = 0; }
812 field_name : FIXME { $$ = 0; }
813 mode_name : FIXME { $$ = 0; }
814 boolean_expression : FIXME { $$ = 0; }
815 case_selector_list : FIXME { $$ = 0; }
816 subexpression : FIXME { $$ = 0; }
817 case_label_specification: FIXME { $$ = 0; }
818 buffer_location : FIXME { $$ = 0; }
819
820 %%
821
822 /* Try to consume a simple name string token. If successful, returns
823 a pointer to a nullbyte terminated copy of the name that can be used
824 in symbol table lookups. If not successful, returns NULL. */
825
826 static char *
827 match_simple_name_string ()
828 {
829 char *tokptr = lexptr;
830
831 if (isalpha (*tokptr))
832 {
833 do {
834 tokptr++;
835 } while (isalpha (*tokptr) || isdigit (*tokptr) || (*tokptr == '_'));
836 yylval.sval.ptr = lexptr;
837 yylval.sval.length = tokptr - lexptr;
838 lexptr = tokptr;
839 return (copy_name (yylval.sval));
840 }
841 return (NULL);
842 }
843
844 /* Start looking for a value composed of valid digits as set by the base
845 in use. Note that '_' characters are valid anywhere, in any quantity,
846 and are simply ignored. Since we must find at least one valid digit,
847 or reject this token as an integer literal, we keep track of how many
848 digits we have encountered. */
849
850 static int
851 decode_integer_value (base, tokptrptr, ivalptr)
852 int base;
853 char **tokptrptr;
854 int *ivalptr;
855 {
856 char *tokptr = *tokptrptr;
857 int temp;
858 int digits = 0;
859
860 while (*tokptr != '\0')
861 {
862 temp = tolower (*tokptr);
863 tokptr++;
864 switch (temp)
865 {
866 case '_':
867 continue;
868 case '0': case '1': case '2': case '3': case '4':
869 case '5': case '6': case '7': case '8': case '9':
870 temp -= '0';
871 break;
872 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
873 temp -= 'a';
874 temp += 10;
875 break;
876 default:
877 temp = base;
878 break;
879 }
880 if (temp < base)
881 {
882 digits++;
883 *ivalptr *= base;
884 *ivalptr += temp;
885 }
886 else
887 {
888 /* Found something not in domain for current base. */
889 tokptr--; /* Unconsume what gave us indigestion. */
890 break;
891 }
892 }
893
894 /* If we didn't find any digits, then we don't have a valid integer
895 value, so reject the entire token. Otherwise, update the lexical
896 scan pointer, and return non-zero for success. */
897
898 if (digits == 0)
899 {
900 return (0);
901 }
902 else
903 {
904 *tokptrptr = tokptr;
905 return (1);
906 }
907 }
908
909 static int
910 decode_integer_literal (valptr, tokptrptr)
911 int *valptr;
912 char **tokptrptr;
913 {
914 char *tokptr = *tokptrptr;
915 int base = 0;
916 int ival = 0;
917 int explicit_base = 0;
918
919 /* Look for an explicit base specifier, which is optional. */
920
921 switch (*tokptr)
922 {
923 case 'd':
924 case 'D':
925 explicit_base++;
926 base = 10;
927 tokptr++;
928 break;
929 case 'b':
930 case 'B':
931 explicit_base++;
932 base = 2;
933 tokptr++;
934 break;
935 case 'h':
936 case 'H':
937 explicit_base++;
938 base = 16;
939 tokptr++;
940 break;
941 case 'o':
942 case 'O':
943 explicit_base++;
944 base = 8;
945 tokptr++;
946 break;
947 default:
948 base = 10;
949 break;
950 }
951
952 /* If we found an explicit base ensure that the character after the
953 explicit base is a single quote. */
954
955 if (explicit_base && (*tokptr++ != '\''))
956 {
957 return (0);
958 }
959
960 /* Attempt to decode whatever follows as an integer value in the
961 indicated base, updating the token pointer in the process and
962 computing the value into ival. Also, if we have an explicit
963 base, then the next character must not be a single quote, or we
964 have a bitstring literal, so reject the entire token in this case.
965 Otherwise, update the lexical scan pointer, and return non-zero
966 for success. */
967
968 if (!decode_integer_value (base, &tokptr, &ival))
969 {
970 return (0);
971 }
972 else if (explicit_base && (*tokptr == '\''))
973 {
974 return (0);
975 }
976 else
977 {
978 *valptr = ival;
979 *tokptrptr = tokptr;
980 return (1);
981 }
982 }
983
984 /* Recognize a character literal. A character literal is single character
985 or a control sequence, enclosed in single quotes. A control sequence
986 is a comma separated list of one or more integer literals, enclosed
987 in parenthesis and introduced with a circumflex character.
988
989 EX: 'a' '^(7)' '^(7,8)'
990
991 As a GNU chill extension, the syntax C'xx' is also recognized as a
992 character literal, where xx is a hex value for the character.
993
994 Returns CHARACTER_LITERAL if a match is found.
995 */
996
997 static int
998 match_character_literal ()
999 {
1000 char *tokptr = lexptr;
1001 int ival = 0;
1002
1003 if ((tolower (*tokptr) == 'c') && (*(tokptr + 1) == '\''))
1004 {
1005 /* We have a GNU chill extension form, so skip the leading "C'",
1006 decode the hex value, and then ensure that we have a trailing
1007 single quote character. */
1008 tokptr += 2;
1009 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
1010 {
1011 return (0);
1012 }
1013 tokptr++;
1014 }
1015 else if (*tokptr == '\'')
1016 {
1017 tokptr++;
1018
1019 /* Determine which form we have, either a control sequence or the
1020 single character form. */
1021
1022 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
1023 {
1024 /* Match and decode a control sequence. Return zero if we don't
1025 find a valid integer literal, or if the next unconsumed character
1026 after the integer literal is not the trailing ')'.
1027 FIXME: We currently don't handle the multiple integer literal
1028 form. */
1029 tokptr += 2;
1030 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
1031 {
1032 return (0);
1033 }
1034 }
1035 else
1036 {
1037 ival = *tokptr++;
1038 }
1039
1040 /* The trailing quote has not yet been consumed. If we don't find
1041 it, then we have no match. */
1042
1043 if (*tokptr++ != '\'')
1044 {
1045 return (0);
1046 }
1047 }
1048 else
1049 {
1050 /* Not a character literal. */
1051 return (0);
1052 }
1053 yylval.typed_val.val = ival;
1054 yylval.typed_val.type = builtin_type_chill_char;
1055 lexptr = tokptr;
1056 return (CHARACTER_LITERAL);
1057 }
1058
1059 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1060 Note that according to 5.2.4.2, a single "_" is also a valid integer
1061 literal, however GNU-chill requires there to be at least one "digit"
1062 in any integer literal. */
1063
1064 static int
1065 match_integer_literal ()
1066 {
1067 char *tokptr = lexptr;
1068 int ival;
1069
1070 if (!decode_integer_literal (&ival, &tokptr))
1071 {
1072 return (0);
1073 }
1074 else
1075 {
1076 yylval.typed_val.val = ival;
1077 yylval.typed_val.type = builtin_type_int;
1078 lexptr = tokptr;
1079 return (INTEGER_LITERAL);
1080 }
1081 }
1082
1083 /* Recognize tokens that start with '$'. These include:
1084
1085 $regname A native register name or a "standard
1086 register name".
1087 Return token GDB_REGNAME.
1088
1089 $variable A convenience variable with a name chosen
1090 by the user.
1091 Return token GDB_VARIABLE.
1092
1093 $digits Value history with index <digits>, starting
1094 from the first value which has index 1.
1095 Return GDB_LAST.
1096
1097 $$digits Value history with index <digits> relative
1098 to the last value. I.E. $$0 is the last
1099 value, $$1 is the one previous to that, $$2
1100 is the one previous to $$1, etc.
1101 Return token GDB_LAST.
1102
1103 $ | $0 | $$0 The last value in the value history.
1104 Return token GDB_LAST.
1105
1106 $$ An abbreviation for the second to the last
1107 value in the value history, I.E. $$1
1108 Return token GDB_LAST.
1109
1110 Note that we currently assume that register names and convenience
1111 variables follow the convention of starting with a letter or '_'.
1112
1113 */
1114
1115 static int
1116 match_dollar_tokens ()
1117 {
1118 char *tokptr;
1119 int regno;
1120 int namelength;
1121 int negate;
1122 int ival;
1123
1124 /* We will always have a successful match, even if it is just for
1125 a single '$', the abbreviation for $$0. So advance lexptr. */
1126
1127 tokptr = ++lexptr;
1128
1129 if (*tokptr == '_' || isalpha (*tokptr))
1130 {
1131 /* Look for a match with a native register name, usually something
1132 like "r0" for example. */
1133
1134 for (regno = 0; regno < NUM_REGS; regno++)
1135 {
1136 namelength = strlen (reg_names[regno]);
1137 if (STREQN (tokptr, reg_names[regno], namelength)
1138 && !isalnum (tokptr[namelength]))
1139 {
1140 yylval.lval = regno;
1141 lexptr += namelength + 1;
1142 return (GDB_REGNAME);
1143 }
1144 }
1145
1146 /* Look for a match with a standard register name, usually something
1147 like "pc", which gdb always recognizes as the program counter
1148 regardless of what the native register name is. */
1149
1150 for (regno = 0; regno < num_std_regs; regno++)
1151 {
1152 namelength = strlen (std_regs[regno].name);
1153 if (STREQN (tokptr, std_regs[regno].name, namelength)
1154 && !isalnum (tokptr[namelength]))
1155 {
1156 yylval.lval = std_regs[regno].regnum;
1157 lexptr += namelength;
1158 return (GDB_REGNAME);
1159 }
1160 }
1161
1162 /* Attempt to match against a convenience variable. Note that
1163 this will always succeed, because if no variable of that name
1164 already exists, the lookup_internalvar will create one for us.
1165 Also note that both lexptr and tokptr currently point to the
1166 start of the input string we are trying to match, and that we
1167 have already tested the first character for non-numeric, so we
1168 don't have to treat it specially. */
1169
1170 while (*tokptr == '_' || isalnum (*tokptr))
1171 {
1172 tokptr++;
1173 }
1174 yylval.sval.ptr = lexptr;
1175 yylval.sval.length = tokptr - lexptr;
1176 yylval.ivar = lookup_internalvar (copy_name (yylval.sval));
1177 lexptr = tokptr;
1178 return (GDB_VARIABLE);
1179 }
1180
1181 /* Since we didn't match against a register name or convenience
1182 variable, our only choice left is a history value. */
1183
1184 if (*tokptr == '$')
1185 {
1186 negate = 1;
1187 ival = 1;
1188 tokptr++;
1189 }
1190 else
1191 {
1192 negate = 0;
1193 ival = 0;
1194 }
1195
1196 /* Attempt to decode more characters as an integer value giving
1197 the index in the history list. If successful, the value will
1198 overwrite ival (currently 0 or 1), and if not, ival will be
1199 left alone, which is good since it is currently correct for
1200 the '$' or '$$' case. */
1201
1202 decode_integer_literal (&ival, &tokptr);
1203 yylval.lval = negate ? -ival : ival;
1204 lexptr = tokptr;
1205 return (GDB_LAST);
1206 }
1207
1208 #if 0
1209 static void convert_float ()
1210 {
1211 extern double strtod ();
1212 double d;
1213 char tmp[256];
1214 char *p = yytext, *p1 = tmp;
1215 char c;
1216
1217 while (c = *p++)
1218 {
1219 switch (c)
1220 {
1221 case '_':
1222 break;
1223 case 'E':
1224 case 'd':
1225 case 'D':
1226 *p1++ = 'e';
1227 break;
1228 default:
1229 *p1++ = c;
1230 break;
1231 }
1232 }
1233 *p1 = '\0';
1234 d = strtod (tmp, &p1);
1235 if (*p1)
1236 {
1237 /* add error handling here */
1238 ;
1239 }
1240 yylval.dval = d;
1241 }
1242 #endif
1243
1244 /* Take care of parsing a number (anything that starts with a digit).
1245 Set yylval and return the token type; update lexptr.
1246 LEN is the number of characters in it. */
1247
1248 /*** Needs some error checking for the float case ***/
1249
1250 struct token
1251 {
1252 char *operator;
1253 int token;
1254 };
1255
1256 static const struct token tokentab5[] =
1257 {
1258 { "ANDIF", ANDIF }
1259 };
1260
1261 static const struct token tokentab4[] =
1262 {
1263 { "ORIF", ORIF }
1264 };
1265
1266 static const struct token tokentab3[] =
1267 {
1268 { "NOT", NOT },
1269 { "XOR", LOGXOR },
1270 { "AND", LOGAND }
1271 };
1272
1273 static const struct token tokentab2[] =
1274 {
1275 { ":=", GDB_ASSIGNMENT },
1276 { "//", SLASH_SLASH },
1277 { "/=", NOTEQUAL },
1278 { "<=", LEQ },
1279 { ">=", GTR },
1280 { "IN", IN },
1281 { "OR", LOGIOR }
1282 };
1283
1284 /* Read one token, getting characters through lexptr. */
1285 /* This is where we will check to make sure that the language and the
1286 operators used are compatible. */
1287
1288 static int
1289 yylex ()
1290 {
1291 unsigned int i;
1292 int token;
1293 char *simplename;
1294 struct symbol *sym;
1295
1296 /* Skip over any leading whitespace. */
1297 while (isspace (*lexptr))
1298 {
1299 lexptr++;
1300 }
1301 /* Look for special single character cases which can't be the first
1302 character of some other multicharacter token. */
1303 switch (*lexptr)
1304 {
1305 case '\0':
1306 return (0);
1307 case '.':
1308 case '=':
1309 case ';':
1310 case '!':
1311 case '+':
1312 case '-':
1313 case '*':
1314 case '/':
1315 case '(':
1316 case ')':
1317 case '[':
1318 case ']':
1319 return (*lexptr++);
1320 }
1321 /* Look for characters which start a particular kind of multicharacter
1322 token, such as a character literal, register name, convenience
1323 variable name, etc. */
1324 switch (*lexptr)
1325 {
1326 case 'C':
1327 case 'c':
1328 case '\'':
1329 token = match_character_literal ();
1330 if (token != 0)
1331 {
1332 return (token);
1333 }
1334 break;
1335 case '$':
1336 token = match_dollar_tokens ();
1337 if (token != 0)
1338 {
1339 return (token);
1340 }
1341 break;
1342 }
1343 /* See if it is a special token of length 5. */
1344 for (i = 0; i < sizeof (tokentab5) / sizeof (tokentab5[0]); i++)
1345 {
1346 if (STREQN (lexptr, tokentab5[i].operator, 5))
1347 {
1348 lexptr += 5;
1349 return (tokentab5[i].token);
1350 }
1351 }
1352 /* See if it is a special token of length 4. */
1353 for (i = 0; i < sizeof (tokentab4) / sizeof (tokentab4[0]); i++)
1354 {
1355 if (STREQN (lexptr, tokentab4[i].operator, 4))
1356 {
1357 lexptr += 4;
1358 return (tokentab4[i].token);
1359 }
1360 }
1361 /* See if it is a special token of length 3. */
1362 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1363 {
1364 if (STREQN (lexptr, tokentab3[i].operator, 3))
1365 {
1366 lexptr += 3;
1367 return (tokentab3[i].token);
1368 }
1369 }
1370 /* See if it is a special token of length 2. */
1371 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1372 {
1373 if (STREQN (lexptr, tokentab2[i].operator, 2))
1374 {
1375 lexptr += 2;
1376 return (tokentab2[i].token);
1377 }
1378 }
1379 /* Look for single character cases which which could be the first
1380 character of some other multicharacter token, but aren't, or we
1381 would already have found it. */
1382 switch (*lexptr)
1383 {
1384 case ':':
1385 case '/':
1386 case '<':
1387 case '>':
1388 return (*lexptr++);
1389 }
1390 /* Look for other special tokens. */
1391 if (STREQN (lexptr, "TRUE", 4)) /* FIXME: What about lowercase? */
1392 {
1393 yylval.ulval = 1;
1394 lexptr += 4;
1395 return (BOOLEAN_LITERAL);
1396 }
1397 if (STREQN (lexptr, "FALSE", 5)) /* FIXME: What about lowercase? */
1398 {
1399 yylval.ulval = 0;
1400 lexptr += 5;
1401 return (BOOLEAN_LITERAL);
1402 }
1403 token = match_integer_literal ();
1404 if (token != 0)
1405 {
1406 return (token);
1407 }
1408
1409 /* Try to match a simple name string, and if a match is found, then
1410 further classify what sort of name it is and return an appropriate
1411 token. Note that attempting to match a simple name string consumes
1412 the token from lexptr, so we can't back out if we later find that
1413 we can't classify what sort of name it is. */
1414
1415 simplename = match_simple_name_string ();
1416 if (simplename != NULL)
1417 {
1418 sym = lookup_symbol (simplename, expression_context_block,
1419 VAR_NAMESPACE, (int *) NULL,
1420 (struct symtab **) NULL);
1421 if (sym != NULL)
1422 {
1423 yylval.ssym.stoken.ptr = NULL;
1424 yylval.ssym.stoken.length = 0;
1425 yylval.ssym.sym = sym;
1426 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1427 switch (SYMBOL_CLASS (sym))
1428 {
1429 case LOC_BLOCK:
1430 /* Found a procedure name. */
1431 return (GENERAL_PROCEDURE_NAME);
1432 case LOC_STATIC:
1433 /* Found a global or local static variable. */
1434 return (LOCATION_NAME);
1435 case LOC_UNDEF:
1436 case LOC_CONST:
1437 case LOC_REGISTER:
1438 case LOC_ARG:
1439 case LOC_REF_ARG:
1440 case LOC_REGPARM:
1441 case LOC_LOCAL:
1442 case LOC_TYPEDEF:
1443 case LOC_LABEL:
1444 case LOC_CONST_BYTES:
1445 case LOC_LOCAL_ARG:
1446 break;
1447 }
1448 }
1449 else if (!have_full_symbols () && !have_partial_symbols ())
1450 {
1451 error ("No symbol table is loaded. Use the \"file\" command.");
1452 }
1453 else
1454 {
1455 error ("No symbol \"%s\" in current context.", simplename);
1456 }
1457 }
1458
1459 return (ILLEGAL_TOKEN);
1460 }
1461
1462 void
1463 yyerror (msg)
1464 char *msg; /* unused */
1465 {
1466 printf ("Parsing: %s\n", lexptr);
1467 if (yychar < 256)
1468 {
1469 error ("Invalid syntax in expression near character '%c'.", yychar);
1470 }
1471 else
1472 {
1473 error ("Invalid syntax in expression");
1474 }
1475 }