1 /* YACC grammar for Chill expressions, for GDB.
2 Copyright (C) 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Parse a Chill expression from text in a string,
21 and return the result as a struct expression pointer.
22 That structure contains arithmetic operations in reverse polish,
23 with constants represented by operations that are followed by special data.
24 See expression.h for the details of the format.
25 What is important here is that it can be built up sequentially
26 during the process of parsing; the lower levels of the tree always
27 come first in the result.
29 Note that malloc's and realloc's in this file are transformed to
30 xmalloc and xrealloc respectively by the same sed command in the
31 makefile that remaps any other malloc/realloc inserted by the parser
32 generator. Doing this with #defines and trying to control the interaction
33 with include files (<malloc.h> and <stdlib.h> for example) just became
34 too messy, particularly when such includes can be inserted at random
35 times by the parser generator.
37 Also note that the language accepted by this parser is more liberal
38 than the one accepted by an actual Chill compiler. For example, the
39 language rule that a simple name string can not be one of the reserved
40 simple name strings is not enforced (e.g "case" is not treated as a
41 reserved name). Another example is that Chill is a strongly typed
42 language, and certain expressions that violate the type constraints
43 may still be evaluated if gdb can do so in a meaningful manner, while
44 such expressions would be rejected by the compiler. The reason for
45 this more liberal behavior is the philosophy that the debugger
46 is intended to be a tool that is used by the programmer when things
47 go wrong, and as such, it should provide as few artificial barriers
48 to it's use as possible. If it can do something meaningful, even
49 something that violates language contraints that are enforced by the
50 compiler, it should do so without complaint.
57 #include "expression.h"
60 #include "parser-defs.h"
63 /* 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
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
86 #define yy_yys chill_yys
87 #define yystate chill_state
88 #define yytmp chill_tmp
90 #define yy_yyv chill_yyv
91 #define yyval chill_val
92 #define yylloc chill_lloc
93 #define yyss chill_yyss /* byacc */
94 #define yyssp chill_yysp /* byacc */
95 #define yyvs chill_yyvs /* byacc */
96 #define yyvsp chill_yyvsp /* byacc */
99 yylex PARAMS ((void));
102 yyerror PARAMS ((char *));
105 yyparse PARAMS ((void));
107 /* #define YYDEBUG 1 */
111 /* Although the yacc "value" of an expression is not used,
112 since the result is stored in the structure being created,
113 other node types do have values. */
118 unsigned LONGEST ulval;
128 struct symtoken ssym;
131 enum exp_opcode opcode;
132 struct internalvar *ivar;
138 %token <voidval> FIXME
140 %token <typed_val> INTEGER_LITERAL
141 %token <ulval> BOOLEAN_LITERAL
142 %token <typed_val> CHARACTER_LITERAL
143 %token <ssym> GENERAL_PROCEDURE_NAME
144 %token <ssym> LOCATION_NAME
145 %token <voidval> SET_LITERAL
146 %token <voidval> EMPTINESS_LITERAL
147 %token <voidval> CHARACTER_STRING_LITERAL
148 %token <voidval> BIT_STRING_LITERAL
150 %token <voidval> STRING
151 %token <voidval> CONSTANT
155 %token <voidval> CASE
157 %token <voidval> ESAC
158 %token <voidval> LOGIOR
159 %token <voidval> ORIF
160 %token <voidval> LOGXOR
161 %token <voidval> LOGAND
162 %token <voidval> ANDIF
164 %token <voidval> NOTEQUAL
174 %token <voidval> SLASH_SLASH
178 %token <voidval> POINTER
179 %token <voidval> RECEIVE
187 %token <voidval> THEN
188 %token <voidval> ELSE
190 %token <voidval> ELSIF
191 %token <voidval> ILLEGAL_TOKEN
193 %type <voidval> location
194 %type <voidval> access_name
195 %type <voidval> primitive_value
196 %type <voidval> location_contents
197 %type <voidval> value_name
198 %type <voidval> literal
199 %type <voidval> tuple
200 %type <voidval> value_string_element
201 %type <voidval> value_string_slice
202 %type <voidval> value_array_element
203 %type <voidval> value_array_slice
204 %type <voidval> value_structure_field
205 %type <voidval> expression_conversion
206 %type <voidval> value_procedure_call
207 %type <voidval> value_built_in_routine_call
208 %type <voidval> start_expression
209 %type <voidval> zero_adic_operator
210 %type <voidval> parenthesised_expression
211 %type <voidval> value
212 %type <voidval> undefined_value
213 %type <voidval> expression
214 %type <voidval> conditional_expression
215 %type <voidval> then_alternative
216 %type <voidval> else_alternative
217 %type <voidval> sub_expression
218 %type <voidval> value_case_alternative
219 %type <voidval> operand_0
220 %type <voidval> operand_1
221 %type <voidval> operand_2
222 %type <voidval> operand_3
223 %type <voidval> operand_4
224 %type <voidval> operand_5
225 %type <voidval> operand_6
226 %type <voidval> integer_literal_expression
227 %type <voidval> synonym_name
228 %type <voidval> value_enumeration_name
229 %type <voidval> value_do_with_name
230 %type <voidval> value_receive_name
231 %type <voidval> string_primitive_value
232 %type <voidval> start_element
233 %type <voidval> left_element
234 %type <voidval> right_element
235 %type <voidval> slice_size
236 %type <voidval> array_primitive_value
237 %type <voidval> expression_list
238 %type <voidval> lower_element
239 %type <voidval> upper_element
240 %type <voidval> first_element
241 %type <voidval> structure_primitive_value
242 %type <voidval> field_name
243 %type <voidval> mode_name
244 %type <voidval> boolean_expression
245 %type <voidval> case_selector_list
246 %type <voidval> subexpression
247 %type <voidval> case_label_specification
248 %type <voidval> buffer_location
264 undefined_value : FIXME
272 location : access_name
284 access_name : LOCATION_NAME
286 write_exp_elt_opcode (OP_VAR_VALUE);
287 write_exp_elt_sym ($1.sym);
288 write_exp_elt_opcode (OP_VAR_VALUE);
298 primitive_value : location_contents
314 | value_string_element
322 | value_array_element
330 | value_structure_field
334 | expression_conversion
338 | value_procedure_call
342 | value_built_in_routine_call
354 | parenthesised_expression
362 location_contents: location
370 value_name : synonym_name
374 | value_enumeration_name
386 | GENERAL_PROCEDURE_NAME
388 write_exp_elt_opcode (OP_VAR_VALUE);
389 write_exp_elt_sym ($1.sym);
390 write_exp_elt_opcode (OP_VAR_VALUE);
396 literal : INTEGER_LITERAL
398 write_exp_elt_opcode (OP_LONG);
399 write_exp_elt_type ($1.type);
400 write_exp_elt_longcst ((LONGEST) ($1.val));
401 write_exp_elt_opcode (OP_LONG);
405 write_exp_elt_opcode (OP_BOOL);
406 write_exp_elt_longcst ((LONGEST) $1);
407 write_exp_elt_opcode (OP_BOOL);
411 write_exp_elt_opcode (OP_LONG);
412 write_exp_elt_type ($1.type);
413 write_exp_elt_longcst ((LONGEST) ($1.val));
414 write_exp_elt_opcode (OP_LONG);
424 | CHARACTER_STRING_LITERAL
445 value_string_element: string_primitive_value '(' start_element ')'
453 value_string_slice: string_primitive_value '(' left_element ':' right_element ')'
457 | string_primitive_value '(' start_element UP slice_size ')'
465 value_array_element: array_primitive_value '(' expression_list ')'
473 value_array_slice: array_primitive_value '(' lower_element ':' upper_element ')'
477 | array_primitive_value '(' first_element UP slice_size '('
485 value_structure_field: structure_primitive_value '.' field_name
493 expression_conversion: mode_name '(' expression ')'
501 value_procedure_call: FIXME
509 value_built_in_routine_call: FIXME
517 start_expression: FIXME
520 } /* Not in GNU-Chill */
525 zero_adic_operator: FIXME
533 parenthesised_expression: '(' expression ')'
541 expression : operand_0
545 | conditional_expression
551 conditional_expression : IF boolean_expression then_alternative else_alternative FI
555 | CASE case_selector_list OF value_case_alternative '[' ELSE sub_expression ']' ESAC
561 then_alternative: THEN subexpression
567 else_alternative: ELSE subexpression
571 | ELSIF boolean_expression then_alternative else_alternative
577 sub_expression : expression
583 value_case_alternative: case_label_specification ':' sub_expression ';'
591 operand_0 : operand_1
595 | operand_0 LOGIOR operand_1
597 write_exp_elt_opcode (BINOP_BITWISE_IOR);
599 | operand_0 ORIF operand_1
603 | operand_0 LOGXOR operand_1
605 write_exp_elt_opcode (BINOP_BITWISE_XOR);
611 operand_1 : operand_2
615 | operand_1 LOGAND operand_2
617 write_exp_elt_opcode (BINOP_BITWISE_AND);
619 | operand_1 ANDIF operand_2
627 operand_2 : operand_3
631 | operand_2 '=' operand_3
633 write_exp_elt_opcode (BINOP_EQUAL);
635 | operand_2 NOTEQUAL operand_3
637 write_exp_elt_opcode (BINOP_NOTEQUAL);
639 | operand_2 '>' operand_3
641 write_exp_elt_opcode (BINOP_GTR);
643 | operand_2 GTR operand_3
645 write_exp_elt_opcode (BINOP_GEQ);
647 | operand_2 '<' operand_3
649 write_exp_elt_opcode (BINOP_LESS);
651 | operand_2 LEQ operand_3
653 write_exp_elt_opcode (BINOP_LEQ);
655 | operand_2 IN operand_3
664 operand_3 : operand_4
668 | operand_3 '+' operand_4
670 write_exp_elt_opcode (BINOP_ADD);
672 | operand_3 '-' operand_4
674 write_exp_elt_opcode (BINOP_SUB);
676 | operand_3 SLASH_SLASH operand_4
684 operand_4 : operand_5
688 | operand_4 '*' operand_5
690 write_exp_elt_opcode (BINOP_MUL);
692 | operand_4 '/' operand_5
694 write_exp_elt_opcode (BINOP_DIV);
696 | operand_4 MOD operand_5
700 | operand_4 REM operand_5
708 operand_5 : operand_6
714 write_exp_elt_opcode (UNOP_NEG);
718 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
720 | '(' integer_literal_expression ')' operand_6
728 operand_6 : POINTER location
732 | RECEIVE buffer_location
744 /* FIXME: For now we just accept only a single integer literal. */
746 integer_literal_expression:
752 /* Things which still need productions... */
753 synonym_name : FIXME { $$ = 0; }
754 value_enumeration_name : FIXME { $$ = 0; }
755 value_do_with_name : FIXME { $$ = 0; }
756 value_receive_name : FIXME { $$ = 0; }
757 string_primitive_value : FIXME { $$ = 0; }
758 start_element : FIXME { $$ = 0; }
759 left_element : FIXME { $$ = 0; }
760 right_element : FIXME { $$ = 0; }
761 slice_size : FIXME { $$ = 0; }
762 array_primitive_value : FIXME { $$ = 0; }
763 expression_list : FIXME { $$ = 0; }
764 lower_element : FIXME { $$ = 0; }
765 upper_element : FIXME { $$ = 0; }
766 first_element : FIXME { $$ = 0; }
767 structure_primitive_value: FIXME { $$ = 0; }
768 field_name : FIXME { $$ = 0; }
769 mode_name : FIXME { $$ = 0; }
770 boolean_expression : FIXME { $$ = 0; }
771 case_selector_list : FIXME { $$ = 0; }
772 subexpression : FIXME { $$ = 0; }
773 case_label_specification: FIXME { $$ = 0; }
774 buffer_location : FIXME { $$ = 0; }
778 /* Try to consume a simple name string token. If successful, returns
779 a pointer to a nullbyte terminated copy of the name that can be used
780 in symbol table lookups. If not successful, returns NULL. */
783 match_simple_name_string ()
785 char *tokptr = lexptr;
787 if (isalpha (*tokptr))
791 } while (isalpha (*tokptr) || isdigit (*tokptr) || (*tokptr == '_'));
792 yylval.sval.ptr = lexptr;
793 yylval.sval.length = tokptr - lexptr;
795 return (copy_name (yylval.sval));
800 /* Start looking for a value composed of valid digits as set by the base
801 in use. Note that '_' characters are valid anywhere, in any quantity,
802 and are simply ignored. Since we must find at least one valid digit,
803 or reject this token as an integer literal, we keep track of how many
804 digits we have encountered. */
807 decode_integer_value (base, tokptrptr, ivalptr)
812 char *tokptr = *tokptrptr;
816 while (*tokptr != '\0')
818 temp = tolower (*tokptr);
824 case '0': case '1': case '2': case '3': case '4':
825 case '5': case '6': case '7': case '8': case '9':
828 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
844 /* Found something not in domain for current base. */
845 tokptr--; /* Unconsume what gave us indigestion. */
850 /* If we didn't find any digits, then we don't have a valid integer
851 value, so reject the entire token. Otherwise, update the lexical
852 scan pointer, and return non-zero for success. */
866 decode_integer_literal (valptr, tokptrptr)
870 char *tokptr = *tokptrptr;
873 int explicit_base = 0;
875 /* Look for an explicit base specifier, which is optional. */
908 /* If we found an explicit base ensure that the character after the
909 explicit base is a single quote. */
911 if (explicit_base && (*tokptr++ != '\''))
916 /* Attempt to decode whatever follows as an integer value in the
917 indicated base, updating the token pointer in the process and
918 computing the value into ival. Also, if we have an explicit
919 base, then the next character must not be a single quote, or we
920 have a bitstring literal, so reject the entire token in this case.
921 Otherwise, update the lexical scan pointer, and return non-zero
924 if (!decode_integer_value (base, &tokptr, &ival))
928 else if (explicit_base && (*tokptr == '\''))
940 /* Recognize a character literal. A character literal is single character
941 or a control sequence, enclosed in single quotes. A control sequence
942 is a comma separated list of one or more integer literals, enclosed
943 in parenthesis and introduced with a circumflex character.
945 EX: 'a' '^(7)' '^(7,8)'
947 As a GNU chill extension, the syntax C'xx' is also recognized as a
948 character literal, where xx is a hex value for the character.
950 Returns CHARACTER_LITERAL if a match is found.
954 match_character_literal ()
956 char *tokptr = lexptr;
959 if ((tolower (*tokptr) == 'c') && (*(tokptr + 1) == '\''))
961 /* We have a GNU chill extension form, so skip the leading "C'",
962 decode the hex value, and then ensure that we have a trailing
963 single quote character. */
965 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
971 else if (*tokptr == '\'')
975 /* Determine which form we have, either a control sequence or the
976 single character form. */
978 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
980 /* Match and decode a control sequence. Return zero if we don't
981 find a valid integer literal, or if the next unconsumed character
982 after the integer literal is not the trailing ')'.
983 FIXME: We currently don't handle the multiple integer literal
986 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
996 /* The trailing quote has not yet been consumed. If we don't find
997 it, then we have no match. */
999 if (*tokptr++ != '\'')
1006 /* Not a character literal. */
1009 yylval.typed_val.val = ival;
1010 yylval.typed_val.type = builtin_type_chill_char;
1012 return (CHARACTER_LITERAL);
1015 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
1016 Note that according to 5.2.4.2, a single "_" is also a valid integer
1017 literal, however GNU-chill requires there to be at least one "digit"
1018 in any integer literal. */
1021 match_integer_literal ()
1023 char *tokptr = lexptr;
1026 if (!decode_integer_literal (&ival, &tokptr))
1032 yylval.typed_val.val = ival;
1033 yylval.typed_val.type = builtin_type_int;
1035 return (INTEGER_LITERAL);
1040 static void convert_float ()
1042 extern double strtod ();
1045 char *p = yytext, *p1 = tmp;
1065 d = strtod (tmp, &p1);
1068 /* add error handling here */
1075 /* Take care of parsing a number (anything that starts with a digit).
1076 Set yylval and return the token type; update lexptr.
1077 LEN is the number of characters in it. */
1079 /*** Needs some error checking for the float case ***/
1087 static const struct token tokentab5[] =
1092 static const struct token tokentab4[] =
1097 static const struct token tokentab3[] =
1104 static const struct token tokentab2[] =
1106 { "//", SLASH_SLASH },
1114 /* Read one token, getting characters through lexptr. */
1115 /* This is where we will check to make sure that the language and the
1116 operators used are compatible. */
1126 /* Skip over any leading whitespace. */
1127 while (isspace (*lexptr))
1131 /* Look for special single character cases which can't be the first
1132 character of some other multicharacter token. */
1152 /* Look for characters which start a particular kind of multicharacter
1153 token, such as a character literal. */
1159 token = match_character_literal ();
1166 /* See if it is a special token of length 5. */
1167 for (i = 0; i < sizeof (tokentab5) / sizeof (tokentab5[0]); i++)
1169 if (strncmp (lexptr, tokentab5[i].operator, 5) == 0)
1172 return (tokentab5[i].token);
1175 /* See if it is a special token of length 4. */
1176 for (i = 0; i < sizeof (tokentab4) / sizeof (tokentab4[0]); i++)
1178 if (strncmp (lexptr, tokentab4[i].operator, 4) == 0)
1181 return (tokentab4[i].token);
1184 /* See if it is a special token of length 3. */
1185 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1187 if (strncmp (lexptr, tokentab3[i].operator, 3) == 0)
1190 return (tokentab3[i].token);
1193 /* See if it is a special token of length 2. */
1194 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1196 if (strncmp (lexptr, tokentab2[i].operator, 2) == 0)
1199 return (tokentab2[i].token);
1202 /* Look for single character cases which which could be the first
1203 character of some other multicharacter token, but aren't, or we
1204 would already have found it. */
1212 /* Look for other special tokens. */
1213 if (strncmp (lexptr, "TRUE", 4) == 0) /* FIXME: What about lowercase? */
1217 return (BOOLEAN_LITERAL);
1219 if (strncmp (lexptr, "FALSE", 5) == 0) /* FIXME: What about lowercase? */
1223 return (BOOLEAN_LITERAL);
1225 token = match_integer_literal ();
1231 /* Try to match a simple name string, and if a match is found, then
1232 further classify what sort of name it is and return an appropriate
1233 token. Note that attempting to match a simple name string consumes
1234 the token from lexptr, so we can't back out if we later find that
1235 we can't classify what sort of name it is. */
1237 simplename = match_simple_name_string ();
1238 if (simplename != NULL)
1240 sym = lookup_symbol (simplename, expression_context_block,
1241 VAR_NAMESPACE, (int *) NULL,
1242 (struct symtab **) NULL);
1245 yylval.ssym.stoken.ptr = NULL;
1246 yylval.ssym.stoken.length = 0;
1247 yylval.ssym.sym = sym;
1248 yylval.ssym.is_a_field_of_this = 0; /* FIXME, C++'ism */
1249 switch (SYMBOL_CLASS (sym))
1252 /* Found a procedure name. */
1253 return (GENERAL_PROCEDURE_NAME);
1255 /* Found a global or local static variable. */
1256 return (LOCATION_NAME);
1266 case LOC_CONST_BYTES:
1271 else if (!have_full_symbols () && !have_partial_symbols ())
1273 error ("No symbol table is loaded. Use the \"file\" command.");
1277 error ("No symbol \"%s\" in current context.", simplename);
1281 return (ILLEGAL_TOKEN);
1286 char *msg; /* unused */
1288 printf ("Parsing: %s\n", lexptr);
1291 error ("Invalid syntax in expression near character '%c'.", yychar);
1295 error ("Invalid syntax in expression");