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.
62 #include "expression.h"
65 #include "parser-defs.h"
70 /* These MUST be included in any grammar file!!!! Please choose unique names!
71 Note that this are a combined list of variables that can be produced
72 by any one of bison, byacc, or yacc. */
73 #define yymaxdepth chill_maxdepth
74 #define yyparse chill_parse
75 #define yylex chill_lex
76 #define yyerror chill_error
77 #define yylval chill_lval
78 #define yychar chill_char
79 #define yydebug chill_debug
80 #define yypact chill_pact
83 #define yydef chill_def
84 #define yychk chill_chk
85 #define yypgo chill_pgo
86 #define yyact chill_act
87 #define yyexca chill_exca
88 #define yyerrflag chill_errflag
89 #define yynerrs chill_nerrs
93 #define yy_yys chill_yys
94 #define yystate chill_state
95 #define yytmp chill_tmp
97 #define yy_yyv chill_yyv
98 #define yyval chill_val
99 #define yylloc chill_lloc
100 #define yyss chill_yyss /* byacc */
101 #define yyssp chill_yysp /* byacc */
102 #define yyvs chill_yyvs /* byacc */
103 #define yyvsp chill_yyvsp /* byacc */
106 yylex PARAMS ((void));
109 yyerror PARAMS ((char *));
112 yyparse PARAMS ((void));
114 /* #define YYDEBUG 1 */
118 /* Although the yacc "value" of an expression is not used,
119 since the result is stored in the structure being created,
120 other node types do have values. */
125 unsigned LONGEST ulval;
135 struct symtoken ssym;
138 enum exp_opcode opcode;
139 struct internalvar *ivar;
146 static int parse_number PARAMS ((void));
149 %token <voidval> FIXME
151 %token <typed_val> INTEGER_LITERAL
152 %token <ulval> BOOLEAN_LITERAL
153 %token <typed_val> CHARACTER_LITERAL
154 %token <voidval> SET_LITERAL
155 %token <voidval> EMPTINESS_LITERAL
156 %token <voidval> CHARACTER_STRING_LITERAL
157 %token <voidval> BIT_STRING_LITERAL
159 %token <voidval> STRING
160 %token <voidval> CONSTANT
164 %token <voidval> CASE
166 %token <voidval> ESAC
167 %token <voidval> LOGIOR
168 %token <voidval> ORIF
169 %token <voidval> LOGXOR
170 %token <voidval> LOGAND
171 %token <voidval> ANDIF
173 %token <voidval> NOTEQUAL
183 %token <voidval> SLASH_SLASH
187 %token <voidval> POINTER
188 %token <voidval> RECEIVE
196 %token <voidval> THEN
197 %token <voidval> ELSE
199 %token <voidval> ELSIF
200 %token <voidval> ILLEGAL_TOKEN
202 %type <voidval> location
203 %type <voidval> primitive_value
204 %type <voidval> location_contents
205 %type <voidval> value_name
206 %type <voidval> literal
207 %type <voidval> tuple
208 %type <voidval> value_string_element
209 %type <voidval> value_string_slice
210 %type <voidval> value_array_element
211 %type <voidval> value_array_slice
212 %type <voidval> value_structure_field
213 %type <voidval> expression_conversion
214 %type <voidval> value_procedure_call
215 %type <voidval> value_built_in_routine_call
216 %type <voidval> start_expression
217 %type <voidval> zero_adic_operator
218 %type <voidval> parenthesised_expression
219 %type <voidval> value
220 %type <voidval> undefined_value
221 %type <voidval> expression
222 %type <voidval> conditional_expression
223 %type <voidval> then_alternative
224 %type <voidval> else_alternative
225 %type <voidval> sub_expression
226 %type <voidval> value_case_alternative
227 %type <voidval> operand_0
228 %type <voidval> operand_1
229 %type <voidval> operand_2
230 %type <voidval> operand_3
231 %type <voidval> operand_4
232 %type <voidval> operand_5
233 %type <voidval> operand_6
234 %type <voidval> integer_literal_expression
235 %type <voidval> synonym_name
236 %type <voidval> value_enumeration_name
237 %type <voidval> value_do_with_name
238 %type <voidval> value_receive_name
239 %type <voidval> general_procedure_name
240 %type <voidval> string_primitive_value
241 %type <voidval> start_element
242 %type <voidval> left_element
243 %type <voidval> right_element
244 %type <voidval> slice_size
245 %type <voidval> array_primitive_value
246 %type <voidval> expression_list
247 %type <voidval> lower_element
248 %type <voidval> upper_element
249 %type <voidval> first_element
250 %type <voidval> structure_primitive_value
251 %type <voidval> field_name
252 %type <voidval> mode_name
253 %type <voidval> boolean_expression
254 %type <voidval> case_selector_list
255 %type <voidval> subexpression
256 %type <voidval> case_label_specification
257 %type <voidval> buffer_location
273 undefined_value : FIXME
289 primitive_value : location_contents
305 | value_string_element
313 | value_array_element
321 | value_structure_field
325 | expression_conversion
329 | value_procedure_call
333 | value_built_in_routine_call
345 | parenthesised_expression
353 location_contents: location
361 value_name : synonym_name
365 | value_enumeration_name
377 | general_procedure_name
385 literal : INTEGER_LITERAL
387 write_exp_elt_opcode (OP_LONG);
388 write_exp_elt_type ($1.type);
389 write_exp_elt_longcst ((LONGEST) ($1.val));
390 write_exp_elt_opcode (OP_LONG);
394 write_exp_elt_opcode (OP_BOOL);
395 write_exp_elt_longcst ((LONGEST) $1);
396 write_exp_elt_opcode (OP_BOOL);
400 write_exp_elt_opcode (OP_LONG);
401 write_exp_elt_type ($1.type);
402 write_exp_elt_longcst ((LONGEST) ($1.val));
403 write_exp_elt_opcode (OP_LONG);
413 | CHARACTER_STRING_LITERAL
434 value_string_element: string_primitive_value '(' start_element ')'
442 value_string_slice: string_primitive_value '(' left_element ':' right_element ')'
446 | string_primitive_value '(' start_element UP slice_size ')'
454 value_array_element: array_primitive_value '(' expression_list ')'
462 value_array_slice: array_primitive_value '(' lower_element ':' upper_element ')'
466 | array_primitive_value '(' first_element UP slice_size '('
474 value_structure_field: structure_primitive_value '.' field_name
482 expression_conversion: mode_name '(' expression ')'
490 value_procedure_call: FIXME
498 value_built_in_routine_call: FIXME
506 start_expression: FIXME
509 } /* Not in GNU-Chill */
514 zero_adic_operator: FIXME
522 parenthesised_expression: '(' expression ')'
530 expression : operand_0
534 | conditional_expression
540 conditional_expression : IF boolean_expression then_alternative else_alternative FI
544 | CASE case_selector_list OF value_case_alternative '[' ELSE sub_expression ']' ESAC
550 then_alternative: THEN subexpression
556 else_alternative: ELSE subexpression
560 | ELSIF boolean_expression then_alternative else_alternative
566 sub_expression : expression
572 value_case_alternative: case_label_specification ':' sub_expression ';'
580 operand_0 : operand_1
584 | operand_0 LOGIOR operand_1
586 write_exp_elt_opcode (BINOP_BITWISE_IOR);
588 | operand_0 ORIF operand_1
592 | operand_0 LOGXOR operand_1
594 write_exp_elt_opcode (BINOP_BITWISE_XOR);
600 operand_1 : operand_2
604 | operand_1 LOGAND operand_2
606 write_exp_elt_opcode (BINOP_BITWISE_AND);
608 | operand_1 ANDIF operand_2
616 operand_2 : operand_3
620 | operand_2 '=' operand_3
622 write_exp_elt_opcode (BINOP_EQUAL);
624 | operand_2 NOTEQUAL operand_3
626 write_exp_elt_opcode (BINOP_NOTEQUAL);
628 | operand_2 '>' operand_3
630 write_exp_elt_opcode (BINOP_GTR);
632 | operand_2 GTR operand_3
634 write_exp_elt_opcode (BINOP_GEQ);
636 | operand_2 '<' operand_3
638 write_exp_elt_opcode (BINOP_LESS);
640 | operand_2 LEQ operand_3
642 write_exp_elt_opcode (BINOP_LEQ);
644 | operand_2 IN operand_3
653 operand_3 : operand_4
657 | operand_3 '+' operand_4
659 write_exp_elt_opcode (BINOP_ADD);
661 | operand_3 '-' operand_4
663 write_exp_elt_opcode (BINOP_SUB);
665 | operand_3 SLASH_SLASH operand_4
673 operand_4 : operand_5
677 | operand_4 '*' operand_5
679 write_exp_elt_opcode (BINOP_MUL);
681 | operand_4 '/' operand_5
683 write_exp_elt_opcode (BINOP_DIV);
685 | operand_4 MOD operand_5
689 | operand_4 REM operand_5
697 operand_5 : operand_6
703 write_exp_elt_opcode (UNOP_NEG);
707 write_exp_elt_opcode (UNOP_LOGICAL_NOT);
709 | '(' integer_literal_expression ')' operand_6
717 operand_6 : POINTER location
721 | RECEIVE buffer_location
733 /* FIXME: For now we just accept only a single integer literal. */
735 integer_literal_expression:
741 /* Things which still need productions... */
742 synonym_name : FIXME { $$ = 0; }
743 value_enumeration_name : FIXME { $$ = 0; }
744 value_do_with_name : FIXME { $$ = 0; }
745 value_receive_name : FIXME { $$ = 0; }
746 general_procedure_name : FIXME { $$ = 0; }
747 string_primitive_value : FIXME { $$ = 0; }
748 start_element : FIXME { $$ = 0; }
749 left_element : FIXME { $$ = 0; }
750 right_element : FIXME { $$ = 0; }
751 slice_size : FIXME { $$ = 0; }
752 array_primitive_value : FIXME { $$ = 0; }
753 expression_list : FIXME { $$ = 0; }
754 lower_element : FIXME { $$ = 0; }
755 upper_element : FIXME { $$ = 0; }
756 first_element : FIXME { $$ = 0; }
757 structure_primitive_value: FIXME { $$ = 0; }
758 field_name : FIXME { $$ = 0; }
759 mode_name : FIXME { $$ = 0; }
760 boolean_expression : FIXME { $$ = 0; }
761 case_selector_list : FIXME { $$ = 0; }
762 subexpression : FIXME { $$ = 0; }
763 case_label_specification: FIXME { $$ = 0; }
764 buffer_location : FIXME { $$ = 0; }
768 /* Start looking for a value composed of valid digits as set by the base
769 in use. Note that '_' characters are valid anywhere, in any quantity,
770 and are simply ignored. Since we must find at least one valid digit,
771 or reject this token as an integer literal, we keep track of how many
772 digits we have encountered. */
775 decode_integer_value (base, tokptrptr, ivalptr)
780 char *tokptr = *tokptrptr;
784 while (*tokptr != '\0')
786 temp = tolower (*tokptr);
792 case '0': case '1': case '2': case '3': case '4':
793 case '5': case '6': case '7': case '8': case '9':
796 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
812 /* Found something not in domain for current base. */
813 tokptr--; /* Unconsume what gave us indigestion. */
818 /* If we didn't find any digits, then we don't have a valid integer
819 value, so reject the entire token. Otherwise, update the lexical
820 scan pointer, and return non-zero for success. */
834 decode_integer_literal (valptr, tokptrptr)
838 char *tokptr = *tokptrptr;
843 int explicit_base = 0;
845 /* Look for an explicit base specifier, which is optional. */
878 /* If we found an explicit base ensure that the character after the
879 explicit base is a single quote. */
881 if (explicit_base && (*tokptr++ != '\''))
886 /* Attempt to decode whatever follows as an integer value in the
887 indicated base, updating the token pointer in the process and
888 computing the value into ival. Also, if we have an explicit
889 base, then the next character must not be a single quote, or we
890 have a bitstring literal, so reject the entire token in this case.
891 Otherwise, update the lexical scan pointer, and return non-zero
894 if (!decode_integer_value (base, &tokptr, &ival))
898 else if (explicit_base && (*tokptr == '\''))
910 /* Recognize a character literal. A character literal is single character
911 or a control sequence, enclosed in single quotes. A control sequence
912 is a comma separated list of one or more integer literals, enclosed
913 in parenthesis and introduced with a circumflex character.
915 EX: 'a' '^(7)' '^(7,8)'
917 As a GNU chill extension, the syntax C'xx' is also recognized as a
918 character literal, where xx is a hex value for the character.
920 Returns CHARACTER_LITERAL if a match is found.
924 match_character_literal ()
926 char *tokptr = lexptr;
929 if ((tolower (*tokptr) == 'c') && (*(tokptr + 1) == '\''))
931 /* We have a GNU chill extension form, so skip the leading "C'",
932 decode the hex value, and then ensure that we have a trailing
933 single quote character. */
935 if (!decode_integer_value (16, &tokptr, &ival) || (*tokptr != '\''))
941 else if (*tokptr == '\'')
945 /* Determine which form we have, either a control sequence or the
946 single character form. */
948 if ((*tokptr == '^') && (*(tokptr + 1) == '('))
950 /* Match and decode a control sequence. Return zero if we don't
951 find a valid integer literal, or if the next unconsumed character
952 after the integer literal is not the trailing ')'.
953 FIXME: We currently don't handle the multiple integer literal
956 if (!decode_integer_literal (&ival, &tokptr) || (*tokptr++ != ')'))
966 /* The trailing quote has not yet been consumed. If we don't find
967 it, then we have no match. */
969 if (*tokptr++ != '\'')
974 yylval.typed_val.val = ival;
975 yylval.typed_val.type = builtin_type_chill_char;
977 return (CHARACTER_LITERAL);
980 /* Recognize an integer literal, as specified in Z.200 sec 5.2.4.2.
981 Note that according to 5.2.4.2, a single "_" is also a valid integer
982 literal, however GNU-chill requires there to be at least one "digit"
983 in any integer literal. */
986 match_integer_literal ()
988 char *tokptr = lexptr;
991 if (!decode_integer_literal (&ival, &tokptr))
997 yylval.typed_val.val = ival;
998 yylval.typed_val.type = builtin_type_int;
1000 return (INTEGER_LITERAL);
1004 static void convert_float ()
1007 extern double strtod ();
1010 char *p = yytext, *p1 = tmp;
1030 d = strtod (tmp, &p1);
1033 /* add error handling here */
1040 /* Take care of parsing a number (anything that starts with a digit).
1041 Set yylval and return the token type; update lexptr.
1042 LEN is the number of characters in it. */
1044 /*** Needs some error checking for the float case ***/
1057 const static struct token tokentab5[] =
1062 const static struct token tokentab4[] =
1067 const static struct token tokentab3[] =
1074 const static struct token tokentab2[] =
1076 { "//", SLASH_SLASH },
1084 /* Read one token, getting characters through lexptr. */
1085 /* This is where we will check to make sure that the language and the
1086 operators used are compatible. */
1094 /* Skip over any leading whitespace. */
1095 while (isspace (*lexptr))
1099 /* Look for special single character cases which can't be the first
1100 character of some other multicharacter token. */
1120 /* Look for characters which start a particular kind of multicharacter
1121 token, such as a character literal. */
1127 token = match_character_literal ();
1134 /* See if it is a special token of length 5. */
1135 for (i = 0; i < sizeof (tokentab5) / sizeof (tokentab5[0]); i++)
1137 if (strncmp (lexptr, tokentab5[i].operator, 5) == 0)
1140 return (tokentab5[i].token);
1143 /* See if it is a special token of length 4. */
1144 for (i = 0; i < sizeof (tokentab4) / sizeof (tokentab4[0]); i++)
1146 if (strncmp (lexptr, tokentab4[i].operator, 4) == 0)
1149 return (tokentab4[i].token);
1152 /* See if it is a special token of length 3. */
1153 for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++)
1155 if (strncmp (lexptr, tokentab3[i].operator, 3) == 0)
1158 return (tokentab3[i].token);
1161 /* See if it is a special token of length 2. */
1162 for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++)
1164 if (strncmp (lexptr, tokentab2[i].operator, 2) == 0)
1167 return (tokentab2[i].token);
1170 /* Look for single character cases which which could be the first
1171 character of some other multicharacter token, but aren't, or we
1172 would already have found it. */
1180 /* Look for other special tokens. */
1181 if (strncmp (lexptr, "TRUE", 4) == 0) /* FIXME: What about lowercase? */
1185 return (BOOLEAN_LITERAL);
1187 if (strncmp (lexptr, "FALSE", 5) == 0) /* FIXME: What about lowercase? */
1191 return (BOOLEAN_LITERAL);
1193 token = match_integer_literal ();
1198 return (ILLEGAL_TOKEN);
1203 char *msg; /* unused */
1205 printf ("Parsing: %s\n", lexptr);
1208 error ("Invalid syntax in expression near character '%c'.", yychar);
1212 error ("Invalid syntax in expression");
1218 chill_printchar (c, stream)
1222 c &= 0xFF; /* Avoid sign bit follies */
1224 if ( c < 0x20 || /* Low control chars */
1225 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1226 (sevenbit_strings && c >= 0x80)) /* high order bit set */
1228 fprintf_filtered (stream, "C'%.2x'", (unsigned int) c);
1232 fprintf_filtered (stream, "'%c'", c);
1236 /* Print the character string STRING, printing at most LENGTH characters.
1237 Printing stops early if the number hits print_max; repeat counts
1238 are printed as appropriate. Print ellipses at the end if we
1239 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
1243 chill_printstr (stream, string, length, force_ellipses)
1246 unsigned int length;
1249 error ("internal error - unimplemented function chill_printstr called.");
1253 /* Table of operators and their precedences for printing expressions. */
1255 const static struct op_print chill_op_print_tab[] = {
1256 {"AND", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
1257 {"OR", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
1258 {"NOT", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
1259 {"MOD", BINOP_REM, PREC_MUL, 0},
1260 {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
1261 {"=", BINOP_EQUAL, PREC_EQUAL, 0},
1262 {"/=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
1263 {"<=", BINOP_LEQ, PREC_ORDER, 0},
1264 {">=", BINOP_GEQ, PREC_ORDER, 0},
1265 {">", BINOP_GTR, PREC_ORDER, 0},
1266 {"<", BINOP_LESS, PREC_ORDER, 0},
1267 {"+", BINOP_ADD, PREC_ADD, 0},
1268 {"-", BINOP_SUB, PREC_ADD, 0},
1269 {"*", BINOP_MUL, PREC_MUL, 0},
1270 {"/", BINOP_DIV, PREC_MUL, 0},
1271 {"-", UNOP_NEG, PREC_PREFIX, 0},
1276 /* The built-in types of Chill. */
1278 struct type *builtin_type_chill_bool;
1279 struct type *builtin_type_chill_char;
1280 struct type *builtin_type_chill_long;
1281 struct type *builtin_type_chill_ulong;
1282 struct type *builtin_type_chill_real;
1284 struct type ** const (chill_builtin_types[]) =
1286 &builtin_type_chill_bool,
1287 &builtin_type_chill_char,
1288 &builtin_type_chill_long,
1289 &builtin_type_chill_ulong,
1290 &builtin_type_chill_real,
1294 const struct language_defn chill_language_defn = {
1297 chill_builtin_types,
1300 chill_parse, /* parser */
1301 chill_error, /* parser error function */
1302 chill_printchar, /* print a character constant */
1303 chill_printstr, /* function to print a string constant */
1304 &BUILTIN_TYPE_LONGEST, /* longest signed integral type */
1305 &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
1306 &builtin_type_chill_real, /* longest floating point type */
1307 {"", "B'", "", ""}, /* Binary format info */
1308 {"O'%o", "O'", "o", ""}, /* Octal format info */
1309 {"D'%d", "D'", "d", ""}, /* Decimal format info */
1310 {"H'%x", "H'", "x", ""}, /* Hex format info */
1311 chill_op_print_tab, /* expression operators for printing */
1315 /* Initialization for Chill */
1318 _initialize_chill_exp ()
1320 builtin_type_chill_bool =
1321 init_type (TYPE_CODE_BOOL, TARGET_INT_BIT / TARGET_CHAR_BIT,
1323 "BOOL", (struct objfile *) NULL);
1324 builtin_type_chill_char =
1325 init_type (TYPE_CODE_CHAR, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
1327 "CHAR", (struct objfile *) NULL);
1328 builtin_type_chill_long =
1329 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1331 "LONG", (struct objfile *) NULL);
1332 builtin_type_chill_ulong =
1333 init_type (TYPE_CODE_INT, TARGET_LONG_BIT / TARGET_CHAR_BIT,
1335 "ULONG", (struct objfile *) NULL);
1336 builtin_type_chill_real =
1337 init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
1339 "LONG_REAL", (struct objfile *) NULL);
1341 add_language (&chill_language_defn);