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