Debian Codegraph @

gcc/cobol/parse.y

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of the Symas Corporation nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 %code requires {
31 #include "config.h"
32 #include <fstream> // Before cobol-system because it uses poisoned functions
33 #include "cobol-system.h"
34 #include "coretypes.h"
35 #include "../../libgcobol/io.h"
36 #include "../../libgcobol/ec.h"
37 #include "tree.h"
38
39 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
40
41 enum radix_t {
42 decimal_e = 10,
43 hexadecimal_e = 16,
44 boolean_e = 2,
45 };
46
47 enum accept_func_t {
48 accept_e,
49 accept_done_e,
50 accept_command_line_e,
51 accept_envar_e,
52 };
53
54 class literal_t {
55 size_t isym;
56 public:
57 char prefix[3];
58 size_t len;
59 char *data;
60
61 bool empty() const { return data == NULL; }
62 size_t isymbol() const { return isym; }
63 const char * symbol_name() const {
64 return isym? cbl_field_of(symbol_at(isym))->name : "";
65 }
66
67 literal_t&
68 set( size_t len, char *data, const char prefix[] ) {
69 set_prefix(prefix, strlen(prefix));
70 set_data(len, data);
71 return *this;
72 }
73
74 literal_t&
75 set( const cbl_field_t * field ) {
76 assert(field->has_attr(constant_e));
77 assert(is_literal(field));
78
79 set_prefix( "", 0 );
80 set_data( field->data.capacity,
81 const_cast<char*>(field->data.initial),
82 field_index(field) );
83 return *this;
84 }
85 literal_t&
86 set_data( size_t len, char *data, size_t isym = 0 ) {
87 this->isym = isym;
88 this->len = len;
89 this->data = data;
90 if( this->prefix[0] == 'Z' ) {
91 this->data = new char[++this->len];
92 auto p = std::copy(data, data + len, this->data);
93 *p = '\0';
94 }
95 return *this;
96 }
97 literal_t&
98 set_prefix( const char *input, size_t len ) {
99 assert(len < sizeof(prefix));
100 std::fill(prefix, prefix + sizeof(prefix), '\0');
101 std::transform(input, input + len, prefix, toupper);
102 return *this;
103 }
104 bool
105 compatible_prefix( const literal_t& that ) const {
106 if( prefix[0] != that.prefix[0] ) {
107 return prefix[0] != 'N' && that.prefix[0] != 'N';
108 }
109 return true;
110 }
111 };
112
113 struct acrc_t { // Abbreviated combined relation condition
114 cbl_refer_t *term;
115 relop_t op;
116 bool invert;
117 acrc_t& init( cbl_refer_t *term = NULL,
118 relop_t op = relop_t(-1),
119 bool invert = false )
120 {
121 this->term = term;
122 this->op = op;
123 this->invert = invert;
124 return *this;
125 }
126 static acrc_t make( cbl_refer_t *term = NULL,
127 relop_t op = relop_t(-1),
128 bool invert = false )
129 {
130 acrc_t output;
131 return output.init( term, op, invert );
132 }
133 relop_t relop_from( relop_t ante_op ) const {
134 assert(ante_op != -1);
135 return op != -1? op : ante_op;
136 }
137 bool is_relation_condition() const { return term && term->field; }
138 };
139 typedef std::list<acrc_t> acrcs_t;
140
141 enum data_category_t { data_category_none,
142 data_category_all,
143 data_alphabetic_e,
144 data_alphanumeric_e,
145 data_alphanumeric_edited_e,
146 data_boolean_e,
147 data_data_pointer_e,
148 data_function_pointer_e,
149 data_msg_tag_e,
150 data_dbcs_e,
151 data_egcs_e,
152 data_national_e,
153 data_national_edited_e,
154 data_numeric_e,
155 data_numeric_edited_e,
156 data_object_referenc_e,
157 data_program_pointer_e,
158 };
159
160 const char * data_category_str( data_category_t category );
161
162 typedef std::map<data_category_t, struct cbl_refer_t*> category_map_t;
163
164 struct substitution_t {
165 enum subst_fl_t { subst_all_e, subst_first_e = 'F', subst_last_e = 'L' };
166 bool anycase;
167 subst_fl_t first_last;
168 cbl_refer_t *orig, *replacement;
169
170 substitution_t& init( bool anycase, char first_last,
171 cbl_refer_t *orig, cbl_refer_t *replacement ) {
172 this->anycase = anycase;
173 switch(first_last) {
174 case 'F': this->first_last = subst_first_e; break;
175 case 'L': this->first_last = subst_last_e; break;
176 default:
177 this->first_last = subst_all_e;
178 break;
179 }
180 this->orig = orig;
181 this->replacement = replacement;
182 return *this;
183 }
184 };
185 typedef std::list<substitution_t> substitutions_t;
186
187 struct init_statement_t {
188 bool to_value;
189 data_category_t category;
190 category_map_t replacement;
191
192 explicit init_statement_t( const category_map_t& replacement )
193 : to_value(false)
194 , category(data_category_none)
195 , replacement(replacement)
196
197 {}
198
199 explicit init_statement_t( bool to_value = false )
200 : to_value(to_value)
201 , category(data_category_none)
202 , replacement(category_map_t())
203 {}
204
205 };
206
207 #pragma GCC diagnostic push
208 #pragma GCC diagnostic ignored "-Wunused-function"
209 static data_category_t
210 data_category_of( const cbl_refer_t& refer );
211
212 static REAL_VALUE_TYPE
213 numstr2i( const char input[], radix_t radix );
214
215 struct cbl_field_t;
216 static inline cbl_field_t *
217 new_literal( const char initial[], enum radix_t radix );
218 #pragma GCC diagnostic pop
219
220 enum select_clause_t {
221 access_clause_e = 0x0001,
222 alt_key_clause_e = 0x0002,
223 assign_clause_e = 0x0004,
224 collating_clause_e = 0x0008,
225 file_status_clause_e = 0x0010,
226 lock_mode_clause_e = 0x0020,
227 organization_clause_e = 0x0040,
228 padding_clause_e = 0x0080,
229 record_delim_clause_e = 0x0100,
230 record_key_clause_e = 0x0200,
231 relative_key_clause_e = 0x0400,
232 reserve_clause_e = 0x0800,
233 sharing_clause_e = 0x1000,
234 };
235
236 struct symbol_elem_t;
237 struct symbol_elem_t * symbols_begin( size_t first );
238 struct symbol_elem_t * symbols_end();
239
240 void field_done();
241
242 template <typename E>
243 struct Elem_list_t {
244 std::list<E> elems;
245 Elem_list_t() {}
246 explicit Elem_list_t( E elem ) {
247 elems.push_back(elem);
248 }
249 Elem_list_t * push_back( E elem ) {
250 elems.push_back(elem);
251 return this;
252 }
253 void clear() {
254 for( auto p = elems.begin(); p != elems.end(); p++ ) {
255 assert( !(symbols_begin(0) <= *p && *p < symbols_end()) );
256 delete *p;
257 }
258 elems.clear();
259 }
260 };
261
262 struct file_list_t;
263 struct cbl_label_t;
264 typedef struct Elem_list_t<cbl_label_t*> Label_list_t;
265
266 struct cbl_file_key_t;
267 typedef struct Elem_list_t<cbl_file_key_t*> key_list_t;
268
269 struct cbl_declarative_t;
270 typedef struct Elem_list_t<cbl_declarative_t*> declarative_list_t;
271 typedef struct Elem_list_t<ec_type_t> ec_list_t;
272 typedef struct Elem_list_t<size_t> isym_list_t;
273
274 struct rel_part_t;
275
276 bool set_debug(bool);
277
278 #include "../../libgcobol/ec.h"
279 #include "../../libgcobol/common-defs.h"
280 #include "inspect.h"
281 }
282
283 %{
284 #include "config.h"
285 #include <fstream> // Before cobol-system because it uses poisoned functions
286 #include "cobol-system.h"
287 #include "coretypes.h"
288 #include "tree.h"
289 #undef cobol_dialect
290 #undef cobol_exceptions
291 #undef yy_flex_debug
292 #include "cdfval.h"
293 #include "../../libgcobol/ec.h"
294 #include "../../libgcobol/common-defs.h"
295 #include "util.h"
296 #include "cbldiag.h"
297 #include "symbols.h"
298 #include "inspect.h"
299 #include "../../libgcobol/io.h"
300 #include "genapi.h"
301 #include "../../libgcobol/exceptl.h"
302 #include "exceptg.h"
303 #include "parse_ante.h"
304 %}
305
306 %token IDENTIFICATION_DIV "IDENTIFICATION DIVISION"
307 ENVIRONMENT_DIV "ENVIRONMENT DIVISION"
308 PROCEDURE_DIV "PROCEDURE DIVISION"
309 DATA_DIV "DATA DIVISION"
310 FILE_SECT "FILE SECTION"
311 INPUT_OUTPUT_SECT "INPUT-OUTPUT SECTION"
312 LINKAGE_SECT "LINKAGE SECTION"
313 LOCAL_STORAGE_SECT "LOCAL-STORAGE SECTION"
314 WORKING_STORAGE_SECT "WORKING-STORAGE SECTION"
315
316 %token OBJECT_COMPUTER "OBJECT COMPUTER"
317
318 %token DISPLAY_OF "DISPLAY OF"
319 END_FUNCTION "END FUNCTION"
320 END_PROGRAM "END PROGRAM"
321 END_SUBPROGRAM "END PROGRAM <contained program>"
322
323 %token JUSTIFIED RETURNING NO_CONDITION "invalid token"
324
325 %token <string> ALNUM ALPHED
326 %token <number> ERROR EXCEPTION SIZE_ERROR "SIZE ERROR"
327 %token <ec_type> EXCEPTION_NAME "EXCEPTION NAME"
328 %token <number> LEVEL LEVEL66 "66" LEVEL78 "78" LEVEL88 "88"
329 %token <string> CLASS_NAME "class name"
330 NAME
331 NAME88 "Level 88 NAME"
332 NUME "Name"
333 NUMED "NUMERIC-EDITED picture"
334 NUMED_CR "NUMERIC-EDITED CR picture"
335 NUMED_DB "NUMERIC-EDITED DB picture"
336 %token <number> NINEDOT NINES NINEV PIC_P ONES
337 %token <string> SPACES
338 %token <literal> LITERAL
339 %token <number> END EOP
340 %token <string> FILENAME
341 %token <number> INVALID
342 %token <number> NUMBER NEGATIVE
343 %token <numstr> NUMSTR "numeric literal"
344 %token <number> OVERFLOW_kw "OVERFLOW"
345 %token <computational> BINARY_INTEGER COMPUTATIONAL
346
347 %token <boolean> PERFORM BACKWARD
348 %token <number> POSITIVE
349 %token <field_attr> POINTER
350 %token <string> SECTION
351 %token <number> STANDARD_ALPHABET "STANDARD ALPHABET"
352 %token <string> SWITCH
353 %token <string> UPSI
354 %token <number> ZERO
355
356 /* environment names */
357 %token <number> SYSIN SYSIPT SYSOUT SYSLIST SYSLST SYSPUNCH SYSPCH CONSOLE
358 %token <number> C01 C02 C03 C04 C05 C06 C07 C08 C09 C10 C11 C12 CSP
359 %token <number> S01 S02 S03 S04 S05 AFP_5A "AFP 5A"
360 %token <number> STDIN STDOUT STDERR
361
362 /* intrinsics */
363 %token <string> LIST MAP NOLIST NOMAP NOSOURCE
364 %token <number> MIGHT_BE "IS or IS NOT"
365 FUNCTION_UDF "UDF name"
366 FUNCTION_UDF_0 "UDF"
367
368 %token <string> DATE_FMT "date format"
369 TIME_FMT "time format"
370 DATETIME_FMT "datetime format"
371
372 /* tokens without semantic value */
373 /* CDF (COPY and >> defined here but used in cdf.y) */
374 %token BASIS CBL CONSTANT COPY
375 DEFINED ENTER FEATURE INSERTT
376 LSUB "("
377 PARAMETER_kw "PARAMETER"
378 OVERRIDE READY RESET
379 RSUB")"
380 SERVICE_RELOAD "SERVICE RELOAD" STAR_CBL "*CBL"
381 SUBSCRIPT SUPPRESS TITLE TRACE USE
382
383 COBOL_WORDS ">>COBOL-WORDS" EQUATE UNDEFINE
384 CDF_DEFINE ">>DEFINE" CDF_DISPLAY ">>DISPLAY"
385 CDF_IF ">>IF" CDF_ELSE ">>ELSE" CDF_END_IF ">>END-IF"
386 CDF_EVALUATE ">>EVALUATE"
387 CDF_WHEN ">>WHEN"
388 CDF_END_EVALUATE ">>END-EVALUATE"
389 CALL_CONVENTION ">>CALL-CONVENTION"
390 CALL_COBOL "CALL" CALL_VERBATIM "CALL (as C)"
391 CDF_PUSH ">>PUSH" CDF_POP ">>POP"
392 SOURCE_FORMAT ">>SOURCE FORMAT"
393
394 IF THEN ELSE
395 SENTENCE
396 ACCEPT ADD ALTER CALL CANCEL CLOSE COMPUTE CONTINUE
397 DELETE DISPLAY DIVIDE EVALUATE EXIT FILLER_kw "FILLER"
398 GOBACK GOTO
399 INITIALIZE INSPECT
400 MERGE MOVE MULTIPLY OPEN PARAGRAPH
401 READ RELEASE RETURN REWRITE
402 SEARCH SET SELECT SORT SORT_MERGE "SORT-MERGE"
403 STRING_kw "STRING" STOP SUBTRACT START
404 UNSTRING WRITE WHEN
405
406 ARGUMENT_NUMBER ARGUMENT_VALUE
407 ENVIRONMENT_NAME ENVIRONMENT_VALUE
408
409 ABS ACCESS ACOS ACTUAL ADVANCING AFTER ALL
410 ALLOCATE
411 ALPHABET ALPHABETIC ALPHABETIC_LOWER "ALPHABETIC-LOWER"
412 ALPHABETIC_UPPER "ALPHABETIC-UPPER"
413 ALPHANUMERIC
414 ALPHANUMERIC_EDITED "ALPHANUMERIC-EDITED"
415 ALSO ALTERNATE ANNUITY ANUM ANY ANYCASE APPLY ARE
416 AREA AREAS AS
417 ASCENDING ACTIVATING ASIN ASSIGN AT ATAN
418
419 BASED BASECONVERT
420 BEFORE BINARY BIT BIT_OF "BIT-OF" BIT_TO_CHAR "BIT-TO-CHAR"
421 BLANK BLOCK_kw
422 BOOLEAN_OF_INTEGER "BOOLEAN-OF-INTEGER"
423 BOTTOM BY
424 BYTE BYTE_LENGTH "BYTE-LENGTH"
425
426 CF CH
427 CHANGED CHAR CHAR_NATIONAL "CHAR-NATIONAL"
428 CHARACTER CHARACTERS CHECKING CLASS
429 COBOL CODE CODESET COLLATING
430 COLUMN COMBINED_DATETIME "COMBINED-DATETIME"
431 COMMA COMMAND_LINE "COMMAND-LINE"
432 COMMAND_LINE_COUNT "COMMAND-LINE-COUNT"
433 COMMIT COMMON
434
435 CONCAT CONDITION CONFIGURATION_SECT "CONFIGURATION SECTION"
436 CONTAINS
437 CONTENT CONTROL CONTROLS CONVERT CONVERTING CORRESPONDING COS
438 COUNT CURRENCY CURRENT CURRENT_DATE
439
440 DATA DATE DATE_COMPILED
441 DATE_OF_INTEGER "DATE-OF-INTEGER"
442 DATE_TO_YYYYMMDD "DATE-TO-YYYYMMDD"
443 DATE_WRITTEN "DATE-WRITTEN"
444 DAY DAY_OF_INTEGER "DAY-OF-INTEGER"
445 DAY_OF_WEEK "DAY-OF-WEEK"
446 DAY_TO_YYYYDDD "DAY-TO-YYYYDDD"
447 DBCS DE DEBUGGING DECIMAL_POINT
448 DECLARATIVES DEFAULT DELIMITED DELIMITER DEPENDING
449 DESCENDING DETAIL DIRECT
450 DIRECT_ACCESS "DIRECT-ACCESS"
451 DOWN DUPLICATES
452 DYNAMIC
453
454 E EBCDIC EC EGCS ENTRY ENVIRONMENT EQUAL EVERY
455 EXAMINE EXHIBIT EXP EXP10 EXTEND EXTERNAL
456
457 EXCEPTION_FILE "EXCEPTION-FILE"
458 EXCEPTION_FILE_N "EXCEPTION-FILE-N"
459 EXCEPTION_LOCATION "EXCEPTION-LOCATION"
460 EXCEPTION_LOCATION_N "EXCEPTION-LOCATION-N"
461 EXCEPTION_STATEMENT "EXCEPTION-STATEMENT"
462 EXCEPTION_STATUS "EXCEPTION-STATUS"
463
464 FACTORIAL FALSE_kw "False" FD
465 FILE_CONTROL "FILE-CONTROL"
466 FILE_KW "File"
467 FILE_LIMIT "FILE-LIMIT"
468 FINAL FINALLY
469 FIND_STRING "FIND-STRING"
470 FIRST FIXED FOOTING FOR
471 FORMATTED_CURRENT_DATE "FORMATTED-CURRENT-DATE"
472 FORMATTED_DATE "FORMATTED-DATE"
473 FORMATTED_DATETIME "FORMATTED-DATETIME"
474 FORMATTED_TIME "FORMATTED-TIME"
475 FORM_OVERFLOW "FORM-OVERFLOW"
476 FREE
477 FRACTION_PART "FRACTION-PART"
478 FROM FUNCTION
479
480 GENERATE GIVING GLOBAL GO GROUP
481
482 HEADING HEX
483 HEX_OF "HEX-OF"
484 HEX_TO_CHAR "HEX-TO-CHAR"
485 HIGH_VALUES "HIGH-VALUES"
486 HIGHEST_ALGEBRAIC "HIGHEST-ALGEBRAIC"
487 HOLD
488
489 IBM_360 IN INCLUDE INDEX INDEXED INDICATE INITIAL_kw "INITIAL"
490 INITIATE INPUT INSTALLATION INTERFACE
491 INTEGER
492 INTEGER_OF_BOOLEAN "INTEGER-OF-BOOLEAN"
493 INTEGER_OF_DATE "INTEGER-OF-DATE"
494 INTEGER_OF_DAY "INTEGER-OF-DAY"
495 INTEGER_OF_FORMATTED_DATE "INTEGER-OF-FORMATTED-DATE"
496 INTEGER_PART "INTEGER-PART"
497 INTO INTRINSIC INVOKE IO IO_CONTROL "IO-CONTROL"
498 IS ISNT "IS NOT"
499
500 KANJI KEY
501
502 LABEL LAST LEADING LEFT LENGTH
503 LENGTH_OF "LENGTH-OF"
504 LIMIT LIMITS LINE LINES
505 LINE_COUNTER "LINE-COUNTER"
506 LINAGE LINKAGE LOCALE LOCALE_COMPARE "LOCALE-COMPARE"
507 LOCALE_DATE "LOCALE-DATE"
508 LOCALE_TIME "LOCALE-TIME"
509 LOCALE_TIME_FROM_SECONDS "LOCALE-TIME-FROM-SECONDS"
510 LOCAL_STORAGE "LOCAL-STORAGE"
511 LOCATION
512 LOCK LOCK_ON LOG LOG10
513 LOWER_CASE "LOWER-CASE"
514 LOW_VALUES "LOW-VALUES"
515 LOWEST_ALGEBRAIC "LOWEST-ALGEBRAIC"
516 LPAREN " )"
517
518 MANUAL MAXX "Max" MEAN MEDIAN MIDRANGE
519 MINN "Min" MULTIPLE MOD MODE
520 MODULE_NAME "MODULE-NAME "
521
522 NAMED NAT NATIONAL
523 NATIONAL_EDITED "NATIONAL-EDITED"
524 NATIONAL_OF "NATIONAL-OF"
525 NATIVE NESTED NEXT
526 NO NOTE
527 NULLS NULLPTR
528 NUMERIC
529 NUMERIC_EDITED NUMVAL
530 NUMVAL_C "NUMVAL-C"
531 NUMVAL_F "NUMVAL-F"
532
533 OCCURS OF OFF OMITTED ON ONLY OPTIONAL OPTIONS ORD ORDER
534 ORD_MAX "ORD-MAX"
535 ORD_MIN "ORD-MIN"
536 ORGANIZATION OTHER OTHERWISE OUTPUT
537
538 PACKED_DECIMAL PADDING PAGE
539 PAGE_COUNTER "PAGE-COUNTER"
540 PF PH PI PIC PICTURE
541 PLUS PRESENT_VALUE PRINT_SWITCH
542 PROCEDURE PROCEDURES PROCEED PROCESS
543 PROGRAM_ID "PROGRAM-ID"
544 PROGRAM_kw "Program" PROPERTY PROTOTYPE PSEUDOTEXT
545
546 QUOTES "QUOTE"
547
548 RANDOM RANDOM_SEED RANGE RAISE RAISING
549 RD RECORD RECORDING RECORDS RECURSIVE
550 REDEFINES REEL REFERENCE RELATIVE REM REMAINDER REMARKS
551 REMOVAL RENAMES REPLACE REPLACING REPORT REPORTING REPORTS
552 REPOSITORY RERUN RESERVE RESTRICTED RESUME
553 REVERSE REVERSED REWIND RF RH RIGHT ROUNDED RUN
554
555 SAME SCREEN SD
556 SECONDS_FROM_FORMATTED_TIME "SECONDS-FROM-FORMATTED-TIME"
557 SECONDS_PAST_MIDNIGHT "SECONDS-PAST-MIDNIGHT"
558 SECURITY
559 SEPARATE SEQUENCE SEQUENTIAL SHARING
560 SIMPLE_EXIT "(simple) EXIT"
561 SIGN SIN SIZE
562 SMALLEST_ALGEBRAIC "SMALLEST-ALGEBRAIC"
563 SOURCE
564 SOURCE_COMPUTER "SOURCE-COMPUTER"
565 SPECIAL_NAMES SQRT STACK
566 STANDARD
567 STANDARD_1 "STANDARD-1"
568 STANDARD_DEVIATION "STANDARD-DEVIATION "
569 STANDARD_COMPARE "STANDARD-COMPARE"
570 STATUS STRONG
571 SUBSTITUTE SUM SYMBOL SYMBOLIC SYNCHRONIZED
572
573 TALLY TALLYING TAN TERMINATE TEST
574 TEST_DATE_YYYYMMDD "TEST-DATE-YYYYMMDD"
575 TEST_DAY_YYYYDDD "TEST-DAY-YYYYDDD"
576 TEST_FORMATTED_DATETIME "TEST-FORMATTED-DATETIME"
577 TEST_NUMVAL "TEST-NUMVAL"
578 TEST_NUMVAL_C "TEST-NUMVAL-C"
579 TEST_NUMVAL_F "TEST-NUMVAL-F"
580 THAN TIME TIMES
581 TO TOP
582 TOP_LEVEL
583 TRACKS TRACK_AREA TRAILING TRANSFORM TRIM TRUE_kw "True"
584 TRY TURN TYPE TYPEDEF
585
586 ULENGTH UNBOUNDED UNIT UNITS UNIT_RECORD UNTIL
587 UP UPON UPOS UPPER_CASE USAGE USING
588 USUBSTR USUPPLEMENTARY UTILITY UUID4 UVALID UWIDTH
589
590 VALUE VARIANCE VARYING VOLATILE
591
592 WHEN_COMPILED WITH WORKING_STORAGE
593 XML XMLGENERATE XMLPARSE
594 YEAR_TO_YYYY YYYYDDD YYYYMMDD
595
596 /* unused Context Words */
597 ARITHMETIC ATTRIBUTE AUTO AUTOMATIC
598 AWAY_FROM_ZERO "AWAY-FROM-ZERO"
599 BACKGROUND_COLOR "BACKGROUND-COLOR"
600 BELL
601 BINARY_ENCODING "BINARY-ENCODING"
602 BLINK
603 CAPACITY CENTER CLASSIFICATION CYCLE
604 DECIMAL_ENCODING "DECIMAL-ENCODING"
605 ENTRY_CONVENTION EOL EOS ERASE EXPANDS
606 FLOAT_BINARY "FLOAT-BINARY"
607 FLOAT_DECIMAL "FLOAT-DECIMAL"
608 FOREGROUND_COLOR FOREVER FULL
609 HIGHLIGHT
610 HIGH_ORDER_LEFT "HIGH-ORDER-LEFT"
611 HIGH_ORDER_RIGHT "HIGH-ORDER-RIGHT"
612 IGNORING IMPLEMENTS INITIALIZED INTERMEDIATE
613 LC_ALL_kw "LC-ALL"
614 LC_COLLATE_kw "LC-COLLATE"
615 LC_CTYPE_kw "LC-CTYPE"
616 LC_MESSAGES_kw "LC-MESSAGES"
617 LC_MONETARY_kw "LC-MONETARY"
618 LC_NUMERIC_kw "LC-NUMERIC"
619 LC_TIME_kw "LC-TIME"
620 LOWLIGHT
621 NEAREST_AWAY_FROM_ZERO "NEAREST-AWAY-FROM-ZERO"
622 NEAREST_EVEN NEAREST_TOWARD_ZERO "NEAREST-EVEN NEAREST-TOWARD-ZERO"
623 NONE NORMAL NUMBERS
624 PREFIXED PREVIOUS PROHIBITED RELATION REQUIRED
625 REVERSE_VIDEO ROUNDING
626 SECONDS SECURE SHORT SIGNED_kw
627 STANDARD_BINARY "STANDARD-BINARY"
628 STANDARD_DECIMAL "STANDARD-DECIMAL"
629 STATEMENT STEP STRUCTURE
630 TOWARD_GREATER "TOWARD-GREATER"
631 TOWARD_LESSER "TOWARD-LESSER"
632 TRUNCATION
633 UCS_4 "UCS-4"
634 UNDERLINE UNSIGNED_kw
635 UTF_16 "UTF-16"
636 UTF_8 "UTF-8"
637
638 ADDRESS
639 END_ACCEPT "END-ACCEPT"
640 END_ADD "END-ADD"
641 END_CALL "END-CALL"
642 END_COMPUTE "END-COMPUTE"
643 END_DELETE "END-DELETE"
644 END_DISPLAY "END-DISPLAY"
645 END_DIVIDE "END-DIVIDE"
646 END_EVALUATE "END-EVALUATE"
647 END_MULTIPLY "END-MULTIPLY"
648 END_PERFORM "END-PERFORM"
649 END_READ "END-READ"
650 END_RETURN "END-RETURN"
651 END_REWRITE "END-REWRITE"
652 END_SEARCH "END-SEARCH"
653 END_START "END-START"
654 END_STRING "END-STRING"
655 END_SUBTRACT "END-SUBTRACT"
656 END_UNSTRING "END-UNSTRING"
657 END_WRITE "END-WRITE"
658 END_IF "END-IF"
659 /* end tokens without semantic value */
660
661 // YYEOF added for compatibility with Bison 3.5
662 // https://savannah.gnu.org/forum/forum.php?forum_id=9735
663 %token YYEOF 0 "end of file"
664
665 %type <number> sentence statements statement
666 %type <number> star_cbl_opt close_how
667
668 %type <number> test_before usage_clause1 might_be
669 %type <boolean> all optional sign_leading on_off initialized strong is_signed
670 %type <number> count data_clauses data_clause
671 %type <number> nine nines nps relop spaces_etc reserved_value signed
672 %type <number> variable_type binary_type
673 %type <number> true_false posneg eval_posneg
674 %type <number> open_io alphabet_etc
675 %type <special_type> device_name
676 %type <string> numed collating_sequence context_word ctx_name locale_spec
677 %type <literal> namestr alphabet_lit program_as repo_as
678 %type <field> perform_cond kind_of_name
679 %type <refer> alloc_ret
680
681 %type <field> log_term rel_expr rel_abbr eval_abbr
682 %type <refer> num_value num_term value factor
683 %type <refer> simple_cond bool_expr
684 %type <log_expr_t> log_expr rel_abbrs eval_abbrs
685 %type <rel_term_t> rel_term rel_term1
686
687 %type <field_data> value78
688 %type <field> literal name nume typename
689 %type <field> num_literal signed_literal
690
691 %type <number> perform_start
692 %type <refer> perform_times
693 %type <perf> perform_verb
694 perform_inline perform_except
695
696 %type <refer> eval_subject1
697 %type <vargs> vargs disp_vargs;
698 %type <field> level_name
699 %type <string> fd_name picture_sym name66 paragraph_name
700 %type <literal> literalism
701 %type <number> bound advance_when org_clause1 read_next
702 %type <number> access_mode multiple lock_how lock_mode org_is
703 %type <select_clauses> select_clauses
704 %type <select_clause> select_clause access_clause alt_key_clause
705 assign_clause collate_clause status_clause
706 lock_mode_clause org_clause padding_clause
707 record_delim_clause record_key_clause
708 relative_key_clause reserve_clause sharing_clause
709
710 %type <file> filename read_body write_body delete_body
711 %type <file> start_impl start_cond start_body
712 %type <rewrite_t> rewrite_body
713 %type <min_max> record_vary rec_contains from_to record_desc
714 %type <file_op> read_file rewrite1 write_file
715 %type <field> data_descr data_descr1 write_what file_record
716 %type <field> name88
717 %type <refer> advancing advance_by
718 %type <refer> alphaval alpha_val numeref scalar scalar88
719 %type <refer> tableref tableish
720 %type <refer> varg varg1 varg1a
721 %type <refer> expr expr_term compute_expr free_tgt by_value_arg
722 %type <refer> move_tgt selected_name read_key read_into vary_by
723 %type <refer> accept_refer num_operand envar search_expr any_arg
724 %type <accept_func> accept_body
725 %type <refers> subscript_exprs subscripts arg_list free_tgts
726 %type <targets> move_tgts set_tgts
727 %type <field> search_varying
728 %type <field> search_term search_terms
729 %type <label> label_name
730 %type <tgt> sort_target
731 %type <files> filenames cdf_use_files
732 %type <field> one_switch
733 %type <fields> field_list switches key_sources key_source
734 %type <sort_keys> sort_keys
735 %type <sort_key> sort_key
736 %type <sort_io> sort_input sort_output
737 %type <boolean> sort_dup forward_order unique_key sign_separate
738 %type <number> befter cardinal initial first_leading
739
740 %type <refer> inspected
741 %type <insp_qual> insp_qual
742 %type <insp_match> insp_quals insp_mtqual tally_match
743 %type <insp_replace> x_by_y
744 %type <insp_oper> replace_oper x_by_ys
745 %type <insp_oper> tally_forth tally_matches
746 %type <inspect> tally
747 %type <insp_one> replacement tally_fors
748 %type <insp_all> tallies replacements
749
750 %type <arith> add_body subtract_body multiply_body divide_body
751 %type <arith> add_impl subtract_impl multiply_impl divide_impl
752 %type <arith> add_cond subtract_cond multiply_cond divide_cond
753 %type <arith> divide_into divide_by
754
755 %type <refer> intrinsic_call
756 %type <field> intrinsic intrinsic_locale
757
758 %type <field> intrinsic0
759 %type <number> intrinsic_v intrinsic_I intrinsic_N intrinsic_X
760 %type <number> intrinsic_I2 intrinsic_N2 intrinsic_X2
761 %type <number> lopper_case
762 %type <number> return_body return_file
763 %type <field> trim_trailing function_udf
764
765 %type <refer> str_input str_size
766 %type <refer2> str_into
767
768 %type <refers> sum scalar88s ffi_names
769 %type <delimited_1> str_delimited
770 %type <delimiteds> str_delimiteds
771 %type <str_body> string_body
772
773 %type <refmod_parts> refmod
774
775 %type <uns_body> unstring_body
776 %type <refers> uns_delimiters uns_delimited
777 %type <refer> uns_delimiter
778 %type <uns_into> uns_into
779 %type <uns_tgts> uns_tgts
780 %type <uns_tgt> uns_tgt
781
782 %type <error> on_overflow on_overflows
783 %type <error> arith_err arith_errs
784 %type <error> accept_except accept_excepts call_except call_excepts
785 %type <compute_body_t> compute_body
786
787 %type <refer> ffi_name set_operand set_tgt scalar_arg unstring_src
788 %type <number> /* addr_len_of */ alphanum_pic
789 %type <pic_part> alphanum_part
790
791 %type <ffi_arg> parameter ffi_by_ref ffi_by_con ffi_by_val
792 %type <ffi_args> parameters
793 %type <ffi_impl> call_body call_impl
794
795 %type <ffi_arg> procedure_use
796 %type <ffi_args> procedure_uses
797
798 %type <comminit> comminit comminits program_attrs
799
800 %type <error_clauses> io_invalids read_eofs write_eops
801 %type <boolean> io_invalid read_eof write_eop
802 global is_global anycase backward
803 end_display
804 exh_changed exh_named
805 %type <number> mistake globally first_last
806 %type <io_mode> io_mode
807
808 %type <labels> labels
809 %type <label> label_1 section_name
810
811 %type <switches> upsi_entry
812
813 %type <special> acceptable disp_upon
814 %type <display> disp_body
815
816 %type <false_domain> domains domain
817 %type <colseq> alphabet_seq
818 %type <alphasym> alphabet_name alphabet_seqs sort_seq
819
820 %type <init_stmt> init_clause init_value
821 %type <data_category> init_categora init_category
822 %type <replacement> init_by
823 %type <replacements> init_bys init_replace
824 %type <refer> init_data exit_with stop_status
825 %type <float128> cce_expr cce_factor const_value
826 %type <prog_end> end_program1
827 %type <substitution> subst_input
828 %type <substitutions> subst_inputs
829 %type <numval_locale_t> numval_locale
830
831 %type <ec_type> except_name exit_raising
832 %type <ec_list> except_names
833 %type <isym_list> except_files
834 %type <dcl_list_t> perform_ec
835
836 %type <opt_init_sects> opt_init_sects
837 %type <opt_init_sect> opt_init_sect
838 %type <number> opt_init_value
839 %type <opt_round> rounded round_between rounded_type rounded_mode
840 %type <opt_arith> opt_arith_type
841 %type <module_type> module_type
842
843 %type <nameloc> repo_func_name
844 %type <namelocs> repo_func_names
845
846 %union {
847 bool boolean;
848 int number;
849 char *string;
850 REAL_VALUE_TYPE float128;
851 literal_t literal;
852 cbl_field_attr_t field_attr;
853 ec_type_t ec_type;
854 ec_list_t* ec_list;
855 cbl_nameloc_t *nameloc;
856 cbl_namelocs_t *namelocs;
857 declarative_list_t* dcl_list_t;
858 isym_list_t* isym_list;
859 struct { radix_t radix; char *string; } numstr;
860 struct { YYLTYPE loc; int token; literal_t name; } prog_end;
861 struct { int token; special_name_t id; } special_type;
862 struct { cbl_field_type_t type;
863 uint32_t capacity; bool signable; } computational;
864 struct cbl_special_name_t *special;
865 struct cbl_alphabet_t *alphasym;
866 struct tgt_list_t *targets;
867 struct cbl_file_t *file;
868 struct { bool varying; size_t min, max; } min_max;
869 struct { cbl_file_t *file; cbl_field_t *buffer; } rewrite_t;
870 struct { cbl_file_t *file; file_status_t handled; } file_op;
871 struct cbl_label_t *label;
872 struct { cbl_label_t *label; int token; } exception;
873 struct cbl_field_data_t *field_data;
874 struct cbl_field_t *field;
875 struct { bool tf; cbl_field_t *field; } bool_field;
876 struct { int token; cbl_field_t *cond; } cond_field;
877 struct cbl_refer_t *refer;
878
879 struct rel_term_type { bool invert; cbl_refer_t *term; } rel_term_t;
880 struct log_expr_t *log_expr_t;
881 struct vargs_t* vargs;
882 struct perform_t *perf;
883 struct cbl_perform_tgt_t *tgt;
884 Label_list_t *labels;
885 key_list_t *file_keys;
886 cbl_file_mode_t io_mode;
887 struct cbl_file_key_t *file_key;
888 struct file_list_t *files;
889 struct field_list_t *fields;
890 struct refer_list_t *refers;
891 struct sort_key_t *sort_key;
892 struct sort_keys_t *sort_keys;
893 struct file_sort_io_t *sort_io;
894 struct arith_t *arith;
895 struct { size_t ntgt; cbl_num_result_t *tgts;
896 cbl_refer_t *expr; } compute_body_t;
897 struct cbl_inspect_t *insp_one;
898 cbl_inspect_opers_t *insp_all;
899 struct cbl_inspect_oper_t *insp_oper;
900 struct { bool before; cbl_inspect_qual_t *qual; } insp_qual;
901 cbl_inspect_t *inspect;
902 cbl_inspect_match_t *insp_match;
903 cbl_inspect_replace_t *insp_replace;
904
905 struct { cbl_refer_t *delimited; refer_list_t *inputs; } delimited;
906 struct { cbl_refer_t *input, *delimiter; } delimited_1;
907 struct { cbl_refer_t *from, *len; } refmod_parts;
908 struct refer_collection_t *delimiteds;
909 struct { cbl_label_t *on_error, *not_error; } error;
910 struct { unsigned int nclause; bool tf; } error_clauses;
911 struct refer_pair_t { cbl_refer_t *first, *second; } refer2;
912 struct { refer_collection_t *inputs; refer_pair_t into; } str_body;
913
914 struct { accept_func_t func; cbl_refer_t *into, *from; special_name_t special;} accept_func;
915 struct unstring_into_t *uns_into;
916 struct unstring_tgt_list_t *uns_tgts;
917 struct unstring_tgt_t *uns_tgt;
918 struct { cbl_refer_t *input;
919 refer_list_t *delimited; unstring_into_t *into; } uns_body;
920
921 struct cbl_ffi_arg_t *ffi_arg;
922 struct ffi_args_t *ffi_args;
923 struct { YYLTYPE loc; cbl_refer_t *ffi_name, *ffi_returning;
924 ffi_args_t *using_params; } ffi_impl;
925
926 struct { bool common, initial, recursive; } comminit;
927 struct { enum select_clause_t clause; cbl_file_t *file; } select_clause;
928 struct { size_t clauses; cbl_file_t *file; } select_clauses;
929 struct { YYLTYPE loc; char *on, *off; } switches;
930 struct cbl_domain_t *false_domain;
931 struct { size_t also; unsigned char *low, *high; } colseq;
932 struct { cbl_field_attr_t attr; int nbyte; } pic_part;
933
934 data_category_t data_category;
935 struct { data_category_t category; cbl_refer_t* replacement; } replacement;
936 category_map_t *replacements;
937 init_statement_t *init_stmt;
938 struct { cbl_special_name_t *special; vargs_t *vargs; } display;
939 substitution_t substitution;
940 substitutions_t *substitutions;
941 struct { bool is_locale; cbl_refer_t *arg2; } numval_locale_t;
942
943 cbl_options_t::arith_t opt_arith;
944 cbl_round_t opt_round;
945 cbl_section_type_t opt_init_sect;
946 struct { bool local, working; } opt_init_sects;
947 module_type_t module_type;
948 }
949
950 %printer { fprintf(yyo, "clauses: 0x%04x", $$); } data_clauses
951 %printer { fprintf(yyo, "%s %s", refer_type_str($$), $$? $$->name() : "<none>"); } <refer>
952 %printer { fprintf(yyo, "%s", $$->field? name_of($$->field) : "[omitted]"); } alloc_ret
953 %printer { fprintf(yyo, "%s %s '%s' (%s)",
954 $$? cbl_field_type_str($$->type) : "<%empty>",
955 $$? name_of($$) : "",
956 $$? $$->data.initial? $$->data.initial : "<nil>" : "",
957 $$? $$->value_str() : "" ); } <field>
958
959 %printer { fprintf(yyo, "%c %s",
960 $$.invert? '!' : ' ',
961 $$.term? name_of($$.term->field) : "<none>"); } <rel_term_t>
962
963 %printer { fprintf(yyo, "%s (token %d)", keyword_str($$), $$ ); } relop
964 %printer { fprintf(yyo, "'%s'", $$? $$ : "" ); } NAME <string>
965 %printer { fprintf(yyo, "%s'%.*s'{" HOST_SIZE_T_PRINT_UNSIGNED "} %s",
966 $$.prefix, int($$.len), $$.data, (fmt_size_t)$$.len,
967 $$.symbol_name()); } <literal>
968 %printer { fprintf(yyo,"%s (1st of" HOST_SIZE_T_PRINT_UNSIGNED")",
969 $$->targets.empty()? "" : $$->targets.front().refer.field->name,
970 (fmt_size_t)$$->targets.size() ); } <targets>
971 %printer { fprintf(yyo, "#" HOST_SIZE_T_PRINT_UNSIGNED ": %s",
972 is_temporary($$)? 0 : (fmt_size_t)field_index($$),
973 $$? name_of($$) : "<nil>" ); } name
974 %printer { fprintf(yyo, "{" HOST_SIZE_T_PRINT_UNSIGNED "-" HOST_SIZE_T_PRINT_UNSIGNED "}",
975 (fmt_size_t)$$.min, (fmt_size_t)$$.max ); } <min_max>
976 %printer { fprintf(yyo, "{%s}", $$? "+/-" : "" ); } signed
977 %printer { fprintf(yyo, "{%s of " HOST_SIZE_T_PRINT_UNSIGNED "}",
978 teed_up_names().front(), (fmt_size_t) teed_up_names().size() ); } qname
979 %printer { fprintf(yyo, "{%d}", $$ ); } <number>
980 %printer { fprintf(yyo, "'%s'", $$.string ); } <numstr>
981 %printer { const char *s = string_of($$);
982 fprintf(yyo, "{%s}", s? s : "??" ); } <float128>
983 %printer { fprintf(yyo, "{%s %c%u}", cbl_field_type_str($$.type),
984 $$.signable? '+' : ' ',
985 $$.capacity ); } <computational>
986 %printer { fprintf(yyo, "{'%s'-'%s'%s}",
987 $$.low? (const char*) $$.low : "",
988 $$.high? (const char*) $$.high : "",
989 $$.also? "+" : "" ); } <colseq>
990 %printer { fprintf(yyo, "{%s, " HOST_SIZE_T_PRINT_UNSIGNED " parameters}",
991 name_of($$.ffi_name->field), !$$.using_params? 0 :
992 (fmt_size_t)$$.using_params->elems.size()); } call_body
993 %printer { fprintf(yyo, "%s <- %s", data_category_str($$.category),
994 name_of($$.replacement->field)); } init_by
995
996 /* CDF (COPY and >> defined here but used in cdf.y) */
997 %left BASIS CBL CONSTANT COPY
998 DEFINED ENTER FEATURE INSERTT
999 LIST LSUB MAP NOLIST NOMAP NOSOURCE
1000 PARAMETER_kw OVERRIDE READY RESET RSUB
1001 SERVICE_RELOAD STAR_CBL
1002 SUBSCRIPT SUPPRESS TITLE TRACE USE
1003
1004 COBOL_WORDS EQUATE UNDEFINE
1005
1006 CDF_DEFINE CDF_DISPLAY
1007 CDF_IF CDF_ELSE CDF_END_IF
1008 CDF_EVALUATE
1009 CDF_WHEN
1010 CDF_END_EVALUATE
1011 CALL_COBOL CALL_VERBATIM
1012
1013 %right IF THEN ELSE
1014 SENTENCE
1015 ACCEPT ADD ALTER CALL CANCEL CLOSE COMPUTE CONTINUE
1016 DELETE DISPLAY DIVIDE
1017 EVALUATE END EOP EXIT
1018 FILLER_kw
1019 GOBACK GOTO
1020 INITIALIZE INSPECT
1021 MERGE MOVE MULTIPLY OPEN OVERFLOW_kw PARAGRAPH PERFORM
1022 READ RELEASE RETURN REWRITE
1023 SEARCH SET SELECT SORT SORT_MERGE
1024 STRING_kw STOP SUBTRACT START
1025 UNSTRING WRITE WHEN INVALID
1026
1027 %left ABS ACCESS ACOS ACTUAL ADVANCING AFP_5A AFTER ALL
1028 ALLOCATE
1029 ALPHABET ALPHABETIC ALPHABETIC_LOWER
1030 ALPHABETIC_UPPER
1031 ALPHANUMERIC
1032 ALPHANUMERIC_EDITED
1033 ALPHED ALSO ALTERNATE ANNUITY ANUM ANY ANYCASE APPLY ARE
1034 AREA AREAS AS
1035 ASCENDING ACTIVATING ASIN ASSIGN AT ATAN
1036
1037 BACKWARD BASED BASECONVERT
1038 BEFORE BINARY BIT BIT_OF BIT_TO_CHAR
1039 BLANK BLOCK_kw
1040 BOOLEAN_OF_INTEGER
1041 BOTTOM BY
1042 BYTE BYTE_LENGTH
1043
1044 C01 C02 C03 C04 C05 C06 C07 C08 C09 C10 C11 C12 CF CH
1045 CHANGED CHAR CHAR_NATIONAL
1046 CHARACTER CHARACTERS CHECKING CLASS
1047 COBOL CODE CODESET COLLATING
1048 COLUMN COMBINED_DATETIME
1049 COMMA COMMAND_LINE
1050 COMMAND_LINE_COUNT
1051 COMMIT COMMON COMPUTATIONAL
1052
1053 CONCAT CONDITION CONFIGURATION_SECT
1054 CONSOLE CONTAINS
1055 CONTENT CONTROL CONTROLS CONVERT CONVERTING CORRESPONDING COS
1056 COUNT CSP CURRENCY CURRENT CURRENT_DATE
1057
1058 DATA DATE DATE_COMPILED
1059 DATE_OF_INTEGER
1060 DATE_TO_YYYYMMDD
1061 DATE_FMT
1062 TIME_FMT
1063 DATETIME_FMT
1064 DATE_WRITTEN
1065 DAY DAY_OF_INTEGER
1066 DAY_OF_WEEK
1067 DAY_TO_YYYYDDD
1068 DBCS DE DEBUGGING DECIMAL_POINT
1069 DECLARATIVES DEFAULT DELIMITED DELIMITER DEPENDING
1070 DESCENDING DETAIL DIRECT
1071 DIRECT_ACCESS
1072 DOWN DUPLICATES
1073 DYNAMIC
1074
1075 E EBCDIC EC EGCS ENTRY ENVIRONMENT EQUAL ERROR EVERY
1076 EXAMINE EXCEPTION EXHIBIT EXP EXP10 EXTEND EXTERNAL
1077
1078 EXCEPTION_FILE
1079 EXCEPTION_FILE_N
1080 EXCEPTION_LOCATION
1081 EXCEPTION_LOCATION_N
1082 EXCEPTION_NAME
1083 EXCEPTION_STATEMENT
1084 EXCEPTION_STATUS
1085
1086 FACTORIAL FALSE_kw FD FILENAME
1087 FILE_CONTROL
1088 FILE_KW
1089 FILE_LIMIT
1090 FINAL FINALLY
1091 FIND_STRING
1092 FIRST FIXED FOOTING FOR
1093 FORMATTED_CURRENT_DATE
1094 FORMATTED_DATE
1095 FORMATTED_DATETIME
1096 FORMATTED_TIME
1097 FORM_OVERFLOW
1098 FREE
1099 FRACTION_PART
1100 FROM FUNCTION
1101 FUNCTION_UDF
1102
1103 GENERATE GIVING GLOBAL GO GROUP
1104
1105 HEADING HEX
1106 HEX_OF
1107 HEX_TO_CHAR
1108 HIGH_VALUES
1109 HIGHEST_ALGEBRAIC
1110 HOLD
1111
1112 IBM_360 IN INCLUDE INDEX INDEXED INDICATE INITIAL_kw
1113 INITIATE INPUT INSTALLATION INTERFACE
1114 INTEGER
1115 INTEGER_OF_BOOLEAN
1116 INTEGER_OF_DATE
1117 INTEGER_OF_DAY
1118 INTEGER_OF_FORMATTED_DATE
1119 INTEGER_PART
1120 INTO INTRINSIC INVOKE IO IO_CONTROL
1121 IS ISNT
1122
1123 KANJI KEY
1124
1125 LABEL LAST LEADING LEFT LENGTH
1126 LENGTH_OF
1127 LEVEL LEVEL66
1128 LEVEL88 LIMIT LIMITS LINE LINES
1129 LINE_COUNTER
1130 LINAGE LINKAGE LOCALE LOCALE_COMPARE
1131 LOCALE_DATE
1132 LOCALE_TIME
1133 LOCALE_TIME_FROM_SECONDS
1134 LOCAL_STORAGE
1135 LOCATION
1136 LOCK LOCK_ON LOG LOG10
1137 LOWER_CASE
1138 LOW_VALUES
1139 LOWEST_ALGEBRAIC
1140 LPAREN
1141
1142 MANUAL MAXX MEAN MEDIAN MIDRANGE
1143 MIGHT_BE MINN MULTIPLE MOD MODE
1144 MODULE_NAME
1145
1146 NAMED NAT NATIONAL
1147 NATIONAL_EDITED
1148 NATIONAL_OF
1149 NATIVE NEGATIVE NESTED NEXT
1150 NINEDOT NINES NINEV NO NOTE NO_CONDITION
1151 NULLS NULLPTR NUMBER
1152 NUME NUMED NUMED_CR NUMED_DB NUMERIC
1153 NUMERIC_EDITED NUMSTR NUMVAL
1154 NUMVAL_C
1155 NUMVAL_F
1156
1157 OCCURS OF OFF OMITTED ON ONLY OPTIONAL OPTIONS ORD ORDER
1158 ORD_MAX
1159 ORD_MIN
1160 ORGANIZATION OTHER OTHERWISE OUTPUT
1161
1162 PACKED_DECIMAL PADDING PAGE
1163 PAGE_COUNTER
1164 PF PH PI PIC PICTURE PIC_P
1165 PLUS POINTER POSITIVE PRESENT_VALUE PRINT_SWITCH
1166 PROCEDURE PROCEDURES PROCEED PROCESS
1167 PROGRAM_ID
1168 PROGRAM_kw PROPERTY PROTOTYPE PSEUDOTEXT
1169
1170 QUOTES
1171
1172 RANDOM RANDOM_SEED RANGE RAISE RAISING
1173 RD RECORD RECORDING RECORDS RECURSIVE
1174 REDEFINES REEL REFERENCE RELATIVE REM REMAINDER REMARKS
1175 REMOVAL RENAMES REPLACE REPLACING REPORT REPORTING REPORTS
1176 REPOSITORY RERUN RESERVE RESTRICTED RESUME
1177 REVERSE REVERSED REWIND RF RH RIGHT ROUNDED RUN
1178
1179 S01 S02 S03 S04 S05 SAME SCREEN SD
1180 SECONDS_FROM_FORMATTED_TIME
1181 SECONDS_PAST_MIDNIGHT
1182 SECTION SECURITY
1183 SEPARATE SEQUENCE SEQUENTIAL SHARING
1184 SIMPLE_EXIT
1185 SIGN SIN SIZE SIZE_ERROR
1186 SMALLEST_ALGEBRAIC
1187 SOURCE
1188 SOURCE_COMPUTER
1189 SPACES SPECIAL_NAMES SQRT STACK
1190 STANDARD
1191 STANDARD_ALPHABET
1192 STANDARD_1
1193 STANDARD_DEVIATION
1194 STANDARD_COMPARE
1195 STATUS STRONG STDERR STDIN STDOUT
1196 LITERAL SUBSTITUTE SUM SWITCH SYMBOL SYMBOLIC SYNCHRONIZED
1197 SYSIN SYSIPT SYSLST SYSOUT SYSPCH SYSPUNCH
1198
1199 TALLY TALLYING TAN TERMINATE TEST
1200 TEST_DATE_YYYYMMDD
1201 TEST_DAY_YYYYDDD
1202 TEST_FORMATTED_DATETIME
1203 TEST_NUMVAL
1204 TEST_NUMVAL_C
1205 TEST_NUMVAL_F
1206 THAN TIME TIMES
1207 TO TOP
1208 TOP_LEVEL
1209 TRACKS TRACK_AREA TRAILING TRANSFORM TRIM TRUE_kw TRY
1210 TURN TYPE TYPEDEF
1211
1212 ULENGTH UNBOUNDED UNIT UNITS UNIT_RECORD UNTIL UP UPON
1213 UPOS UPPER_CASE UPSI USAGE USING USUBSTR USUPPLEMENTARY
1214 UTILITY UUID4 UVALID UWIDTH
1215
1216 VALUE VARIANCE VARYING VOLATILE
1217
1218 WHEN_COMPILED WITH WORKING_STORAGE
1219 XML XMLGENERATE XMLPARSE
1220 YEAR_TO_YYYY YYYYDDD YYYYMMDD
1221 ZERO
1222
1223 /* unused Context Words */
1224 ARITHMETIC ATTRIBUTE AUTO AUTOMATIC
1225 AWAY_FROM_ZERO
1226 BACKGROUND_COLOR
1227 BELL
1228 BINARY_ENCODING
1229 BLINK
1230 CAPACITY CENTER CLASSIFICATION CYCLE
1231 DECIMAL_ENCODING
1232 ENTRY_CONVENTION EOL EOS ERASE EXPANDS
1233 FLOAT_BINARY
1234 FLOAT_DECIMAL
1235 FOREGROUND_COLOR FOREVER FULL
1236 HIGHLIGHT
1237 HIGH_ORDER_LEFT
1238 HIGH_ORDER_RIGHT
1239 IGNORING IMPLEMENTS INITIALIZED INTERMEDIATE
1240 LC_ALL_kw
1241 LC_COLLATE_kw
1242 LC_CTYPE_kw
1243 LC_MESSAGES_kw
1244 LC_MONETARY_kw
1245 LC_NUMERIC_kw
1246 LC_TIME_kw
1247 LOWLIGHT
1248 NEAREST_AWAY_FROM_ZERO
1249 NEAREST_EVEN NEAREST_TOWARD_ZERO
1250 NONE NORMAL NUMBERS
1251 PREFIXED PREVIOUS PROHIBITED RELATION REQUIRED
1252 REVERSE_VIDEO ROUNDING
1253 SECONDS SECURE SHORT SIGNED_kw
1254 STANDARD_BINARY
1255 STANDARD_DECIMAL
1256 STATEMENT STEP STRUCTURE
1257 TOWARD_GREATER
1258 TOWARD_LESSER
1259 TRUNCATION
1260 UCS_4
1261 UNDERLINE UNSIGNED_kw
1262 UTF_16
1263 UTF_8
1264
1265 %left CLASS_NAME NAME NAME88
1266 %left ADDRESS
1267 %left END_ACCEPT END_ADD END_CALL END_COMPUTE
1268 END_DELETE END_DISPLAY END_DIVIDE
1269 END_EVALUATE END_MULTIPLY END_PERFORM
1270 END_READ END_RETURN END_REWRITE
1271 END_SEARCH END_START END_STRING END_SUBTRACT
1272 END_UNSTRING END_WRITE
1273 error
1274 END_IF
1275
1276 %left THRU
1277 %left OR
1278 %left AND
1279 %right NOT
1280 %left '<' '>' '=' NE LE GE
1281 %left '-' '+'
1282 %left '*' '/'
1283 %right POW
1284 %precedence NEG
1285
1286
1287
1288 %{
1289 static cbl_field_type_t
1290 set_operand_type(const cbl_refer_t& refer) {
1291 if( refer.field == NULL ) return FldInvalid;
1292 return refer.addr_of? FldPointer : refer.field->type;
1293 }
1294
1295 static bool
1296 refer_pointer( const cbl_num_result_t& elem ) {
1297 assert(elem.refer.field);
1298 return elem.refer.is_pointer();
1299 }
1300 static bool
1301 valid_set_targets( const tgt_list_t& tgts, bool want_pointers ) {
1302 bool ok = true;
1303 // The only targets that can have addr_of are BASED or in Linkage Section.
1304 auto baddie = std::find_if( tgts.targets.begin(),
1305 tgts.targets.end(),
1306 []( const auto& num_result ) {
1307 if( num_result.refer.addr_of ) {
1308 auto f = num_result.refer.field;
1309 if( ! (f->has_attr(based_e) || f->has_attr(linkage_e)) ) {
1310 return true;
1311 }
1312 }
1313 return false;
1314 } );
1315 if( baddie != tgts.targets.end() ) {
1316 auto loc = symbol_field_location(field_index(baddie->refer.field));
1317 error_msg(loc,"target %s must be BASED or in LINKAGE SECTION",
1318 baddie->refer.name() );
1319 return false;
1320 }
1321
1322 for( const auto& num_result : tgts.targets ) {
1323 auto loc = symbol_field_location(field_index(num_result.refer.field));
1324 if( refer_pointer(num_result) ) {
1325 if( !want_pointers ) {
1326 ok = false;
1327 error_msg( loc, "%s is a pointer", num_result.refer.name() );
1328 }
1329 } else {
1330 if( want_pointers ) {
1331 ok = false;
1332 error_msg( loc, "%s is not a pointer", num_result.refer.name() );
1333 }
1334 }
1335 }
1336 return ok;
1337 }
1338
1339 static void initialize_allocated( const cbl_refer_t& input );
1340 static void
1341 initialize_statement( std::list<cbl_num_result_t>& tgts,
1342 bool with_filler,
1343 data_category_t category,
1344 const category_map_t& replacement = category_map_t());
1345
1346
1347 unsigned char cbl_alphabet_t::nul_string[2] = ""; // 2 NULs lets us use one
1348 unsigned char *nul_string() { return cbl_alphabet_t::nul_string; }
1349
1350 static inline literal_t literal_of( char *s ) {
1351 literal_t output;
1352 return output.set( strlen(s), s, "" );
1353 }
1354 static inline char * string_of( const literal_t& lit ) {
1355 return strlen(lit.data) == lit.len? lit.data : NULL;
1356 }
1357
1358 static inline void strip_trailing_zeroes(char * const psz)
1359 {
1360 if( yydebug) return;
1361 // The idea here is to take the output of real_to_decimal and make it
1362 // more integer friendly. Any integer value that can be expressed in 1
1363 // to MAX_FIXED_POINT_DIGITS digits is converted to a string without a
1364 // decimal point and no exponent.
1365
1366 char *pdot = strchr(psz, '.');
1367 gcc_assert(pdot);
1368 char *pe = strchr(psz, 'e');
1369 if( !pe )
1370 {
1371 // The most likely cause of this is a "0.0" result.
1372 strcpy(psz, "0");
1373 return;
1374 }
1375 char *pnz = pe-1;
1376 while(*pnz == '0')
1377 {
1378 pnz--;
1379 }
1380 // pdot points to the decimal point.
1381 // pe points to the 'e'.
1382 // pnz points to the rightmost non-zero significand digit.
1383
1384 // Put the exponent on top of the trailing zeroes:
1385 memmove(pnz+1, pe, strlen(pe)+1);
1386 pe = pnz+1;
1387 int exp = atoi(pe+1);
1388 // Compute the number digits to the right of the decimal point:
1389 int non_zero_digits = pe - (pdot+1);
1390 if( exp >= 1 && exp <= MAX_FIXED_POINT_DIGITS && non_zero_digits <= exp)
1391 {
1392 // Further simplification is possible, because the value does not actually
1393 // need a decimal point. That's because we are dealing with something
1394 // like 1.e+0, or 1.23e2 or 1.23e3
1395
1396 // Terminate the value where the 'e' is now:
1397 *pe = '\0';
1398 // Figure out where the extra zeroes will go:
1399 pe -= 1;
1400 // Get rid of the decimal place:
1401 memmove(pdot, pdot+1, strlen(pdot)+1);
1402 // Tack on the additional zeroes:
1403 for(int i=0; i<exp - non_zero_digits; i++)
1404 {
1405 *pe++ = '0';
1406 }
1407 *pe++ = '\0';
1408 }
1409 }
1410
1411 static inline char * string_of( const REAL_VALUE_TYPE &cce ) {
1412 char output[64];
1413 real_to_decimal( output, &cce, sizeof(output), 32, 0 );
1414 strip_trailing_zeroes(output);
1415 char decimal = symbol_decimal_point();
1416 std::replace(output, output + strlen(output), '.', decimal);
1417 return xstrdup(output);
1418 }
1419
1420 static inline char * string_of( tree cce ) {
1421 return string_of (TREE_REAL_CST (cce));
1422 }
1423
1424 cbl_field_t *
1425 new_literal( const literal_t& lit, enum cbl_field_attr_t attr );
1426
1427 static YYLTYPE first_line_of( YYLTYPE loc );
1428 %}
1429
1430 %locations
1431 %token-table
1432 %define parse.error verbose // custom
1433 %expect 6
1434 %require "3.5.1" // 3.8.2 also works, but not 3.8.0
1435 %%
1436
1437 top: programs
1438 {
1439 if( ! goodnight_gracie() ) {
1440 YYABORT;
1441 }
1442 if( nparse_error > 0 ) YYABORT;
1443 }
1444 | programs end_program
1445 {
1446 if( nparse_error > 0 ) YYABORT;
1447 }
1448 ;
1449 programs: program
1450 | programs end_program program
1451 ;
1452 program: id_div options_para env_div data_div
1453 {
1454 if( ! data_division_ready() ) {
1455 mode_syntax_only(procedure_div_e);
1456 }
1457 current_division = procedure_div_e;
1458 }
1459 procedure_div
1460 {
1461 if( yydebug ) labels_dump();
1462 }
1463 ;
1464
1465 id_div: cdf_words IDENTIFICATION_DIV '.' program_id
1466 | cdf_words program_id
1467 | cdf_words IDENTIFICATION_DIV '.' function_id
1468 ;
1469
1470 cdf_words: %empty
1471 | cobol_words
1472 /* | error { error_msg(@1, "not a COBOL-WORD"); } */
1473 ;
1474 cobol_words: cobol_words1
1475 | cobol_words cobol_words1
1476 ;
1477 cobol_words1: COBOL_WORDS EQUATE NAME[keyword] WITH NAME[name] {
1478 if( ! cdf_tokens.equate(@keyword, $keyword, $name) ) { YYERROR; }
1479 }
1480 | COBOL_WORDS UNDEFINE NAME[keyword] {
1481 if( ! cdf_tokens.undefine(@keyword, $keyword) ) { YYERROR; }
1482 }
1483 | COBOL_WORDS SUBSTITUTE NAME[keyword] BY NAME[name] {
1484 if( ! cdf_tokens.substitute(@keyword, $keyword, $name) ) { YYERROR; }
1485 }
1486 | COBOL_WORDS RESERVE NAME[name] {
1487 if( ! cdf_tokens.reserve(@name, $name) ) { YYERROR; }
1488 }
1489 ;
1490
1491 program_id: PROGRAM_ID dot namestr[name] program_as program_attrs[attr] dot
1492 {
1493 internal_ebcdic_lock();
1494 current_division = identification_div_e;
1495 parser_division( identification_div_e, NULL, 0, NULL );
1496 location_set(@1);
1497 int main_error=0;
1498 const char *name = string_of($name);
1499 parser_enter_program( name, false, &main_error );
1500 if( main_error ) {
1501 error_msg(@name, "PROGRAM-ID 'main' is invalid with %<-main%> option");
1502 YYERROR;
1503 }
1504
1505 if( symbols_begin() == symbols_end() ) {
1506 symbol_table_init();
1507 }
1508 if( !current.new_program(@name, LblProgram, name,
1509 $program_as.data,
1510 $attr.common, $attr.initial) ) {
1511 auto L = symbol_program(current_program_index(), name);
1512 assert(L);
1513 error_msg(@name, "PROGRAM-ID %s already defined on line %d",
1514 name, L->line);
1515 YYERROR;
1516 }
1517 if( nparse_error > 0 ) YYABORT;
1518 }
1519 ;
1520 dot: %empty
1521 | '.'
1522 ;
1523 program_as: %empty { static const literal_t empty {}; $$ = empty; }
1524 | AS LITERAL { $$ = $2; }
1525 ;
1526
1527 function_id: FUNCTION NAME program_as program_attrs[attr] '.'
1528 {
1529 internal_ebcdic_lock();
1530 current_division = identification_div_e;
1531 parser_division( identification_div_e, NULL, 0, NULL );
1532 location_set(@1);
1533
1534 int main_error = 0;
1535 parser_enter_program( $NAME, true, &main_error );
1536 if( main_error ) {
1537 error_msg(@NAME, "FUNCTION-ID %<main%> is invalid "
1538 "with %<-main%> option");
1539 YYERROR;
1540 }
1541 if( symbols_begin() == symbols_end() ) {
1542 symbol_table_init();
1543 }
1544 if( !current.new_program(@NAME, LblFunction, $NAME,
1545 $program_as.data,
1546 $attr.common, $attr.initial) ) {
1547 auto L = symbol_program(current_program_index(), $NAME);
1548 assert(L);
1549 error_msg(@NAME, "FUNCTION %s already defined on line %d",
1550 $NAME, L->line);
1551 YYERROR;
1552 }
1553 if( keyword_tok($NAME, true) ) {
1554 error_msg(@NAME, "FUNCTION %s is an intrinsic function",
1555 $NAME);
1556 YYERROR;
1557 }
1558 current.udf_add(current_program_index());
1559 if( nparse_error > 0 ) YYABORT;
1560 }
1561 | FUNCTION NAME program_as is PROTOTYPE '.'
1562 {
1563 cbl_unimplemented("FUNCTION PROTOTYPE");
1564 }
1565 ;
1566
1567 options_para: %empty
1568 | OPTIONS opt_clauses '.'
1569 | OPTIONS
1570 ;
1571
1572 opt_clauses: opt_clause
1573 | opt_clauses opt_clause
1574 ;
1575 opt_clause: opt_arith
1576 | opt_round
1577 | opt_entry
1578 | opt_binary
1579 | opt_decimal {
1580 cbl_unimplemented("type FLOAT-DECIMAL");
1581 }
1582 | opt_intermediate
1583 | opt_init
1584 ;
1585
1586 opt_arith: ARITHMETIC is opt_arith_type {
1587 if( ! current.option($opt_arith_type) ) {
1588 error_msg(@3, "unable to set ARITHMETIC option");
1589 }
1590 }
1591 ;
1592 opt_arith_type: NATIVE { $$ = cbl_options_t::native_e; }
1593 | STANDARD { $$ = cbl_options_t::standard_e; }
1594 | STANDARD_BINARY { $$ = cbl_options_t::standard_binary_e; }
1595 | STANDARD_DECIMAL { $$ = cbl_options_t::standard_decimal_e; }
1596 ;
1597 opt_round: DEFAULT ROUNDED mode is rounded_type[type] {
1598 current_rounded_mode($type);
1599 }
1600 ;
1601 opt_entry: ENTRY_CONVENTION is COBOL {
1602 yywarn("ENTRY-CONVENTION IS COBOL, check");
1603 }
1604 ;
1605 opt_binary: FLOAT_BINARY default_kw is HIGH_ORDER_LEFT
1606 {
1607 cbl_unimplementedw("HIGH-ORDER-LEFT was ignored");
1608 if( ! current.option_binary(cbl_options_t::high_order_left_e) ) {
1609 error_msg(@3, "unable to set %<HIGH_ORDER_LEFT%>");
1610 }
1611 }
1612 | FLOAT_BINARY default_kw is HIGH_ORDER_RIGHT[opt]
1613 {
1614 cbl_unimplementedw("HIGH-ORDER-RIGHT was ignored");
1615 if( ! current.option_binary(cbl_options_t::high_order_right_e) ) {
1616 error_msg(@opt, "unable to set HIGH-ORDER-RIGHT");
1617 }
1618 }
1619 ;
1620 default_kw: %empty
1621 | DEFAULT
1622 ;
1623 opt_decimal: FLOAT_DECIMAL default_kw is HIGH_ORDER_LEFT[opt]
1624 {
1625 cbl_unimplementedw("HIGH-ORDER-LEFT was ignored");
1626 if( ! current.option_decimal(cbl_options_t::high_order_left_e) ) {
1627 error_msg(@opt, "unable to set HIGH-ORDER-LEFT");
1628 }
1629 }
1630 | FLOAT_DECIMAL default_kw is HIGH_ORDER_RIGHT[opt]
1631 {
1632 cbl_unimplementedw("HIGH-ORDER-RIGHT was ignored");
1633 if( ! current.option_decimal(cbl_options_t::high_order_right_e) ) {
1634 error_msg(@opt, "unable to set HIGH-ORDER-RIGHT");
1635 }
1636 }
1637 | FLOAT_DECIMAL default_kw is BINARY_ENCODING[opt]
1638 {
1639 cbl_unimplementedw("BINARY-ENCODING was ignored");
1640 if( ! current.option(cbl_options_t::binary_encoding_e) ) {
1641 error_msg(@opt, "unable to set BINARY-ENCODING option");
1642 }
1643 }
1644 | FLOAT_DECIMAL default_kw is DECIMAL_ENCODING[opt]
1645 {
1646 cbl_unimplementedw("DECIMAL-ENCODING was ignored");
1647 if( ! current.option(cbl_options_t::decimal_encoding_e) ) {
1648 error_msg(@opt, "unable to set DECIMAL-ENCODING option");
1649 }
1650 }
1651 ;
1652 opt_intermediate:
1653 INTERMEDIATE ROUNDING is round_between[round] {
1654 current.intermediate_round($round);
1655 }
1656 ;
1657
1658 opt_init: INITIALIZE opt_init_sects[sect] opt_section to opt_init_value[init]
1659 {
1660 if( $sect.local ) {
1661 current.initial_value(local_sect_e, $init);
1662 }
1663 if( $sect.working ) {
1664 current.initial_value(working_sect_e, $init);
1665 }
1666 }
1667 ;
1668 opt_section: %empty
1669 | SECTION
1670 ;
1671 opt_init_sects: ALL { $$.local = $$.working = true; }
1672 | opt_init_sect {
1673 $$.local = $$.working = false;
1674 switch($1) {
1675 case local_sect_e:
1676 $$.local = true; break;
1677 case working_sect_e:
1678 $$.working = true; break;
1679 default: gcc_unreachable();
1680 }
1681 }
1682 | opt_init_sects opt_init_sect {
1683 $$ = $1;
1684 switch($2) {
1685 case local_sect_e:
1686 if( $$.local ) {
1687 error_msg(@2, "LOCAL-STORAGE repeated");
1688 }
1689 $$.local = true; break;
1690 case working_sect_e:
1691 if( $$.working ) {
1692 error_msg(@2, "WORKING-STORAGE repeated");
1693 }
1694 $$.working = true; break;
1695 default: gcc_unreachable();
1696 }
1697 }
1698 ;
1699 opt_init_sect: LOCAL_STORAGE { $$ = local_sect_e; }
1700 | SCREEN { cbl_unimplemented("SCREEN SECTION"); }
1701 | WORKING_STORAGE { $$ = working_sect_e; }
1702 ;
1703 opt_init_value: BINARY ZERO { $$ = constant_index(NULLS); }
1704 | HIGH_VALUES { $$ = constant_index(HIGH_VALUES); }
1705 | LITERAL
1706 {
1707 if( $1.prefix[0] != 'X' ) {
1708 error_msg(@1, "hexadecimal literal required");
1709 }
1710 if( $1.len != 1 ) {
1711 error_msg(@1, "1-byte hexadecimal literal required");
1712 }
1713 char ach[16];
1714 sprintf(ach, "%d", (int)($1.data[0]));
1715 //auto f = new_literal($1.data);
1716 auto f = new_literal(ach);
1717 f = field_add(@1, f);
1718 $$ = field_index(f);
1719 }
1720 | LOW_VALUES { $$ = constant_index(LOW_VALUES); }
1721 | SPACES { $$ = constant_index(SPACES); }
1722 ;
1723
1724 namestr: ctx_name {
1725 $$ = literal_of($1);
1726 if( ! string_of($$) ) {
1727 error_msg(@1, "'%s' has embedded NUL", $$.data);
1728 YYERROR;
1729 }
1730 }
1731 | LITERAL {
1732 if( $$.prefix[0] != '\0' ) {
1733 error_msg(@1, "literal cannot use %s prefix in this context",
1734 $$.prefix);
1735 YYERROR;
1736 }
1737 if( !is_cobol_charset($$.data) ) {
1738 error_msg(@1, "literal '%s' must be a COBOL or C identifier",
1739 $$.data);
1740 }
1741 }
1742 ;
1743
1744 program_attrs: %empty { $$.common = $$.initial = $$.recursive = false; }
1745 | is comminits program_kw { $$ = $2; }
1746 ;
1747 comminits: comminit
1748 | comminits comminit {
1749 if( ($1.initial && $2.recursive) ||
1750 ($2.initial && $1.recursive) ) {
1751 auto loc = $1.initial? @1 : @2;
1752 error_msg(loc, "INITIAL cannot be used with RECURSIVE");
1753 }
1754 $$ = $1;
1755 if( $2.common ) {
1756 if( $1.common ) {
1757 error_msg(@2, "COMMON repeated");
1758 }
1759 $$.common = $2.common;
1760 }
1761 if( $2.initial ) {
1762 if( $1.initial ) {
1763 error_msg(@2, "INITIAL repeated");
1764 }
1765 $$.initial = $2.initial;
1766 }
1767 if( $2.recursive ) {
1768 if( $1.recursive ) {
1769 error_msg(@2, "RECURSIVE repeated");
1770 }
1771 $$.recursive = $2.recursive;
1772 }
1773 }
1774 ;
1775 comminit: COMMON {
1776 if( program_level() == 0 ) { // PROGRAM-ID being parsed not added yet.
1777 error_msg(@1, "COMMON may be used only in a contained program");
1778 }
1779 $$.common = true;
1780 $$.initial = $$.recursive = false;
1781 }
1782 | INITIAL_kw { $$.initial = true; $$.common = $$.recursive = false;}
1783 | RECURSIVE {
1784 $$.recursive = true; $$.common = $$.initial = false;
1785 }
1786 ;
1787
1788
1789 env_div: %empty { current_division = environment_div_e; }
1790 | ENVIRONMENT_DIV '.' { current_division = environment_div_e; }
1791 | ENVIRONMENT_DIV '.' {
1792 current_division = environment_div_e;
1793 } env_sections
1794 ;
1795
1796 env_sections: env_section
1797 | env_sections env_section
1798 ;
1799
1800 env_section: INPUT_OUTPUT_SECT '.'
1801 | INPUT_OUTPUT_SECT '.' io_sections
1802 | INPUT_OUTPUT_SECT '.' selects { /* IBM requires FILE CONTROL. */ }
1803 | CONFIGURATION_SECT '.'
1804 | CONFIGURATION_SECT '.' config_paragraphs
1805 | cdf
1806 ;
1807
1808 io_sections: io_section
1809 | io_sections io_section
1810 ;
1811
1812 io_section: FILE_CONTROL '.'
1813 | FILE_CONTROL '.' selects
1814 | IO_CONTROL '.'
1815 | IO_CONTROL '.' io_control_clauses '.'
1816 ;
1817
1818 io_control_clauses: io_control_clause
1819 | io_control_clauses io_control_clause
1820 ;
1821 io_control_clause:
1822 SAME record area for_kw filenames
1823 {
1824 symbol_file_same_record_area( $filenames->files );
1825 }
1826 | SAME smerge area for_kw filenames
1827 {
1828 symbol_file_same_record_area( $filenames->files );
1829 }
1830 | APPLY COMMIT on field_list
1831 {
1832 cbl_unimplementedw("I-O-CONTROL APPLY COMMIT");
1833 }
1834 ;
1835 area: %empty
1836 | AREA
1837 ;
1838 smerge: SORT
1839 | SORT_MERGE
1840 ;
1841
1842 selects: select
1843 | selects select
1844 ;
1845
1846 select: SELECT optional NAME[name] select_clauses[clauses] '.'
1847 {
1848 assert($clauses.file);
1849 cbl_file_t *file = $clauses.file;
1850
1851 file->optional = $optional;
1852 file->line = @name.first_line;
1853 if( !namcpy(@clauses, file->name, $name) ) YYERROR;
1854
1855 if( ! ($clauses.clauses & assign_clause_e) ) {
1856 error_msg(@name, "ASSIGN clause missing for %s", file->name);
1857 }
1858
1859 // key check
1860 switch(file->org) {
1861 case file_indexed_e:
1862 // indexed file cannot have relative key
1863 if( ($clauses.clauses & relative_key_clause_e) != 0) {
1864 assert(file->keys);
1865 auto ikey = file->nkey - 1;
1866 assert(file->keys[ikey].fields);
1867 auto f = cbl_field_of(symbol_at(file->keys[ikey].fields[0]));
1868 error_msg(@name, "INDEXED file %s cannot have RELATIVE key %s",
1869 file->name, f->name);
1870 break; // because next message would be redundant
1871 }
1872 if( ($clauses.clauses & record_key_clause_e) == 0 ) {
1873 error_msg(@name, "INDEXED file %s has no RECORD KEY",
1874 file->name);
1875 }
1876 break;
1877 case file_disorganized_e:
1878 file->org = file_sequential_e;
1879 __attribute__((fallthrough));
1880 default:
1881 if( ($clauses.clauses & record_key_clause_e) != 0 ) {
1882 assert(file->keys);
1883 auto ikey = file->nkey - 1;
1884 assert(file->keys[ikey].fields);
1885 auto f = cbl_field_of(symbol_at(file->keys[ikey].fields[0]));
1886 error_msg(@name, "%s file %s cannot have RECORD key %s",
1887 file_org_str(file->org), file->name, f->name);
1888 }
1889 break;
1890 }
1891
1892 // access check
1893 switch(file->access) {
1894 case file_access_rnd_e:
1895 case file_access_dyn_e:
1896 if( is_sequential(file) ) {
1897 error_msg(@name, "%s file %s cannot have ACCESS %s",
1898 file_org_str(file->org), file->name,
1899 file_access_str(file->access));
1900 }
1901 break;
1902 default:
1903 break;
1904 }
1905
1906 // install file, and set record area's name
1907 if( (file = file_add(@name, file)) == NULL ) YYERROR;
1908 auto ifile = symbol_index(symbol_elem_of(file));
1909 // update keys
1910 for( auto p = file->keys;
1911 p && p < file->keys + file->nkey; p++ )
1912 {
1913 if( p->name[0] == '\0' ) continue;
1914 auto f = symbol_field(PROGRAM, 0, p->name);
1915 cbl_field_of(f)->parent = ifile;
1916 size_t isym = field_index(cbl_field_of(f));
1917 update_symbol_map(symbol_at(isym));
1918 }
1919 }
1920 | SELECT optional NAME[name] '.'
1921 {
1922 cbl_file_t file = protofile;
1923
1924 file.optional = $optional;
1925 file.line = @name.first_line;
1926 if( !namcpy(@name, file.name, $name) ) YYERROR;
1927
1928 if( file_add(@name, &file) == NULL ) YYERROR;
1929 }
1930 ;
1931 selected_name: external scalar { $$ = $2; }
1932 | external LITERAL[name]
1933 {
1934 const char *name = string_of($name);
1935 if( ! name ) {
1936 error_msg(@name, "'%s' has embedded NUL", $name.data);
1937 YYERROR;
1938 }
1939 uint32_t len = $name.len;
1940 cbl_field_t field {
1941 0, FldLiteralA, FldInvalid, quoted_e | constant_e,
1942 0, 0, 0, nonarray, 0, "", 0, cbl_field_t::linkage_t(),
1943 {len,len,0,0, $name.data}, NULL };
1944 field.attr |= literal_attr($name.prefix);
1945 $$ = new cbl_refer_t( field_add(@name, &field) );
1946 }
1947 ;
1948 external: %empty /* GnuCOBOL uses EXTERNAL to control name resolution. */
1949 | EXTERNAL
1950 ;
1951
1952 select_clauses: select_clause { $$.clauses = $1.clause; $$.file = $1.file; }
1953 | select_clauses[total] select_clause[part]
1954 {
1955 $$ = $total;
1956 // The default organization is sequential.
1957 if( ($$.clauses & organization_clause_e) == 0 ) {
1958 $$.file->org = file_sequential_e;
1959 }
1960 const bool exists = ($$.clauses & $part.clause);
1961 $$.clauses |= $part.clause;
1962
1963 switch($part.clause) {
1964 case alt_key_clause_e:
1965 assert( $part.file->nkey == 1 );
1966 if( $$.file->nkey++ == 0 ) {
1967 // If no key yet exists, create room for it and the
1968 // present alternate.
1969 assert($$.file->keys == nullptr);
1970 $$.file->keys = new cbl_file_key_t[++$$.file->nkey];
1971 }
1972 {
1973 auto keys = new cbl_file_key_t[$$.file->nkey];
1974 auto alt = std::copy($$.file->keys,
1975 $$.file->keys +
1976 $$.file->nkey - 1,
1977 keys);
1978 // Assign the alternate key to the last element,
1979 // and update the pointer.
1980 *alt = $part.file->keys[0];
1981 $$.file->keys_update(keys);
1982 }
1983 break;
1984 case assign_clause_e:
1985 if( exists ) {
1986 error_msg(@part, "clause is repeated");
1987 YYERROR;
1988 }
1989 $$.file->filename = $part.file->filename;
1990 break;
1991 case collating_clause_e:
1992 if( exists ) {
1993 error_msg(@part, "clause is repeated");
1994 YYERROR;
1995 }
1996 break;
1997 case lock_mode_clause_e:
1998 if( exists ) {
1999 error_msg(@part, "clause is repeated");
2000 YYERROR;
2001 }
2002 $$.file->lock = $part.file->lock;
2003 break;
2004 case organization_clause_e:
2005 if( exists ) {
2006 error_msg(@part, "clause is repeated");
2007 YYERROR;
2008 }
2009 $$.file->org = $part.file->org;
2010 break;
2011 case padding_clause_e:
2012 case reserve_clause_e:
2013 case sharing_clause_e:
2014 case record_delim_clause_e:
2015 if( exists ) {
2016 error_msg(@part, "clause is repeated");
2017 YYERROR;
2018 }
2019 break;
2020 case access_clause_e:
2021 if( exists ) {
2022 error_msg(@part, "clause is repeated");
2023 YYERROR;
2024 }
2025 $$.file->access = $part.file->access;
2026 break;
2027 case relative_key_clause_e:
2028 if( exists ) {
2029 error_msg(@part, "clause is repeated");
2030 YYERROR;
2031 }
2032 if( $$.clauses & record_key_clause_e ) {
2033 error_msg(@part, "FILE %s is INDEXED, has no RELATIVE key",
2034 $$.file->name);
2035 YYERROR;
2036 }
2037 // fall thru
2038 case record_key_clause_e:
2039 if( exists ) {
2040 error_msg(@part, "clause is repeated");
2041 YYERROR;
2042 }
2043 if( ($$.clauses & relative_key_clause_e) &&
2044 $part.clause == record_key_clause_e ) {
2045 error_msg(@part, "FILE %s is RELATIVE, has no RECORD key",
2046 $$.file->name);
2047 YYERROR;
2048 }
2049 if( $$.file->nkey == 0 ) {
2050 assert( 1 == $part.file->nkey );
2051 $$.file->nkey = $part.file->nkey;
2052 $$.file->keys = new cbl_file_key_t[1];
2053 }
2054 $$.file->keys[0] = $part.file->keys[0];
2055 break;
2056 /* case password_clause_e: */
2057 case file_status_clause_e:
2058 if( exists ) {
2059 error_msg(@part, "clause is repeated");
2060 YYERROR;
2061 }
2062 $$.file->user_status = $part.file->user_status;
2063 $$.file->vsam_status = $part.file->vsam_status;
2064 break;
2065 }
2066 if( $$.file->lock.locked() ) {
2067 if( $$.file->org == file_sequential_e &&
2068 $$.file->lock.multiple ) {
2069 error_msg(@part, "SEQUENTIAL file cannot lock MULTIPLE records");
2070 }
2071 }
2072
2073 delete $part.file;
2074 }
2075 ;
2076
2077 select_clause: access_clause
2078 | alt_key_clause[alts]
2079 | assign_clause[alts]
2080 | collate_clause
2081 | /* file */ status_clause
2082 | lock_mode_clause
2083 | org_clause
2084 | padding_clause
2085 | record_delim_clause
2086 | record_key_clause
2087 | relative_key_clause
2088 | reserve_clause
2089 | sharing_clause
2090 ;
2091
2092 access_clause: ACCESS mode is access_mode[acc]
2093 {
2094 $$.clause = access_clause_e;
2095 $$.file = new cbl_file_t(protofile);
2096 $$.file->access = static_cast<cbl_file_access_t>($acc);
2097 }
2098 ;
2099 access_mode: RANDOM { $$ = file_access_rnd_e; }
2100 | DYNAMIC { $$ = file_access_dyn_e; }
2101 | SEQUENTIAL { $$ = file_access_seq_e; }
2102 ;
2103
2104 alt_key_clause: ALTERNATE record key is name key_source[fields] unique_key
2105 {
2106 $$.clause = alt_key_clause_e;
2107 $$.file = new cbl_file_t(protofile);
2108 $$.file->nkey = 1;
2109 if( $fields == NULL ) {
2110 $$.file->keys = new cbl_file_key_t(field_index($name),
2111 $unique_key);
2112 } else {
2113 $name->type = FldLiteralA;
2114 $name->data.initial = $name->name;
2115 $name->attr |= record_key_e;
2116 auto& name = *$name;
2117 $$.file->keys = new cbl_file_key_t(name.name,
2118 $fields->fields,
2119 $unique_key);
2120 }
2121 }
2122 ;
2123 key_source: %empty { $$ = NULL; }
2124 | SOURCE is key_sources[fields] { $$ = $fields; }
2125 ;
2126 key_sources: name { $$ = new field_list_t($1); }
2127 | key_sources name { $$ = $1; $$->fields.push_back($2); }
2128 ;
2129 unique_key: %empty { $$ = true; }
2130 | with DUPLICATES { $$ = false; }
2131 ;
2132
2133 assign_clause: ASSIGN to selected_name[selected] {
2134 $$.clause = assign_clause_e;
2135 $$.file = new cbl_file_t(protofile);
2136 $$.file->filename = field_index($selected->field);
2137 }
2138 | ASSIGN to device_name USING name {
2139 $$.clause = assign_clause_e;
2140 cbl_unimplemented("ASSIGN TO DEVICE");
2141 YYERROR;
2142 }
2143 | ASSIGN to device_name {
2144 $$.clause = assign_clause_e;
2145 cbl_unimplemented("ASSIGN TO DEVICE");
2146 YYERROR;
2147 }
2148 | ASSIGN USING name {
2149 $$.clause = assign_clause_e;
2150 $$.file = new cbl_file_t(protofile);
2151 $$.file->filename = field_index($name);
2152 }
2153 ;
2154
2155 collate_clause: collate_claus1 {
2156 $$.clause = collating_clause_e;
2157 $$.file = new cbl_file_t(protofile);
2158 }
2159 ;
2160 collate_claus1: collating SEQUENCE NAME /* SEQUENCE swallows IS/FOR */
2161 | collating SEQUENCE ALPHANUMERIC is NAME
2162 | collating SEQUENCE NATIONAL is NAME
2163 ;
2164
2165 status_clause: file STATUS is name[user]
2166 {
2167 $$.clause = file_status_clause_e;
2168 $$.file = new cbl_file_t(protofile);
2169 $$.file->user_status = field_index($user);
2170 }
2171 | file STATUS is name[user] name[vsam]
2172 {
2173 $$.clause = file_status_clause_e;
2174 $$.file = new cbl_file_t(protofile);
2175 $$.file->user_status = field_index($user);
2176 $$.file->vsam_status = field_index($vsam);
2177 }
2178 ;
2179
2180 lock_mode_clause: // ISO only
2181 LOCK mode is lock_mode lock_how[how]
2182 {
2183 $$.clause = lock_mode_clause_e;
2184 $$.file = new cbl_file_t(protofile);
2185 $$.file->lock.multiple = $how > 0;
2186 if( ! $$.file->lock.mode_set($lock_mode) ) {
2187 error_msg(@lock_mode, "logic error: %s is not a file lock mode",
2188 keyword_str($lock_mode) );
2189 }
2190 }
2191 lock_how: %empty { $$ = 0; }
2192 | with LOCK_ON multiple records { $$ = $multiple; }
2193 ;
2194 lock_mode: MANUAL { $$ = MANUAL; }
2195 | RECORD { $$ = RECORD; }
2196 | AUTOMATIC { $$ = AUTOMATIC; }
2197 ;
2198 multiple: %empty { $$ = 0; }
2199 | MULTIPLE { $$ = MULTIPLE; }
2200 ;
2201 records: RECORD
2202 | RECORDS
2203 ;
2204
2205 org_clause: org_clause1[org]
2206 {
2207 $$.clause = organization_clause_e;
2208 $$.file = new cbl_file_t(protofile);
2209 $$.file->org = static_cast<cbl_file_org_t>($org);
2210 }
2211 ;
2212 org_is: %empty { $$ = 0; }
2213 | ORGANIZATION is { $$ = 0; }
2214 | ORGANIZATION is RECORD { $$ = RECORD; }
2215 | RECORD { $$ = RECORD; }
2216 ;
2217 // file_sequential is the proper default
2218 org_clause1: org_is SEQUENTIAL {
2219 $$ = $1 == RECORD? file_line_sequential_e : file_sequential_e;
2220 }
2221 | org_is LINE SEQUENTIAL
2222 {
2223 if( $1 ) error_msg(@2, "syntax error: invalid %<RECORD%>");
2224 $$ = file_line_sequential_e;
2225 }
2226 | org_is RELATIVE {
2227 if( $1 ) error_msg(@2, "syntax error: invalid %<RECORD%>");
2228 $$ = file_relative_e;
2229 }
2230 | org_is INDEXED {
2231 if( $1 ) error_msg(@2, "syntax error: invalid %<RECORD%>");
2232 $$ = file_indexed_e;
2233 }
2234 ;
2235
2236 /*
2237 * "The PADDING CHARACTER clause is syntax checked, but has no
2238 * effect on the execution of the program."
2239 */
2240 padding_clause: PADDING character is padding_char
2241 {
2242 $$.clause = padding_clause_e;
2243 $$.file = new cbl_file_t(protofile);
2244 }
2245 ;
2246 character: %empty
2247 | CHARACTER
2248 ;
2249 padding_char: NAME
2250 | LITERAL
2251 | NUMSTR
2252 ;
2253
2254 record_delim_clause: RECORD DELIMITER is STANDARD_ALPHABET
2255 {
2256 $$.clause = record_delim_clause_e;
2257 $$.file = new cbl_file_t(protofile);
2258 }
2259 ;
2260
2261 record_key_clause: RECORD key is name key_source[fields]
2262 {
2263 $$.clause = record_key_clause_e;
2264 $$.file = new cbl_file_t(protofile);
2265 $$.file->nkey = 1;
2266 if( $fields == NULL ) {
2267 $$.file->keys = new cbl_file_key_t(field_index($name));
2268 } else { // "special" not-literal literal: a key name
2269 $name->type = FldLiteralA;
2270 $name->data.initial = $name->name;
2271 $name->attr |= record_key_e;
2272 $$.file->keys = new cbl_file_key_t($name->name,
2273 $fields->fields, true);
2274 }
2275 }
2276 ;
2277
2278 relative_key_clause: /* RELATIVE */ KEY is name
2279 { // lexer returns KEY for RELATIVE ... NAME
2280 $$.clause = relative_key_clause_e;
2281 $$.file = new cbl_file_t(protofile);
2282 $$.file->nkey = 1;
2283 $$.file->keys = new cbl_file_key_t(field_index($name));
2284 }
2285 ;
2286
2287 reserve_clause: RESERVE NUMSTR reserve_area
2288 {
2289 $$.clause = reserve_clause_e;
2290 $$.file = new cbl_file_t(protofile);
2291 }
2292 ;
2293 reserve_area: %empty
2294 | AREA
2295 | AREAS
2296 ;
2297
2298 sharing_clause: SHARING with sharing_who
2299 {
2300 $$.clause = sharing_clause_e;
2301 $$.file = new cbl_file_t(protofile);
2302 }
2303 ;
2304 sharing_who: ALL other
2305 | NO other
2306 | READ ONLY
2307 ;
2308 other: %empty
2309 | OTHER
2310 ;
2311
2312 config_paragraphs: config_paragraph
2313 | config_paragraphs config_paragraph
2314 ;
2315
2316 config_paragraph:
2317 SPECIAL_NAMES '.'
2318 | SPECIAL_NAMES '.' specials '.'
2319 | SOURCE_COMPUTER '.'
2320 | SOURCE_COMPUTER '.' NAME with_debug '.'
2321 | OBJECT_COMPUTER '.'
2322 | OBJECT_COMPUTER '.' NAME collating_sequence[name] '.'
2323 {
2324 if( $name ) {
2325 if( !current.collating_sequence($name) ) {
2326 error_msg(@name, "collating sequence already defined as '%s'",
2327 current.collating_sequence());
2328 YYERROR;
2329 }
2330 }
2331 }
2332 | REPOSITORY dot
2333 | REPOSITORY dot repo_members '.'
2334 ;
2335
2336 repo_members: repo_member
2337 | repo_members repo_member
2338 ;
2339 repo_member: repo_class
2340 { cbl_unimplemented("CLASS"); }
2341 | repo_interface
2342 { cbl_unimplemented("INTERFACE"); }
2343 | repo_func
2344 | repo_program
2345 | repo_property
2346 { cbl_unimplemented("PROPERTY"); }
2347 ;
2348
2349 repo_class: CLASS NAME repo_as repo_expands
2350 ;
2351 repo_as: %empty { $$ = literal_t(); }
2352 | AS LITERAL { $$ = $2; }
2353 ;
2354 repo_expands: %empty
2355 | EXPANDS NAME USING NAME
2356 ;
2357
2358 repo_interface: INTERFACE NAME repo_as repo_expands
2359 ;
2360
2361 repo_func: FUNCTION repo_func_names[namelocs] INTRINSIC {
2362 for( const auto& nameloc : *$namelocs ) {
2363 if( 0 == intrinsic_token_of(nameloc.name) ) {
2364 error_msg(nameloc.loc,
2365 "no such intrinsic function: %qs",
2366 nameloc.name);
2367 continue;
2368 }
2369 current.repository_add(nameloc.name);
2370 }
2371 }
2372 | FUNCTION ALL INTRINSIC
2373 {
2374 current.repository_add_all();
2375 }
2376 | FUNCTION repo_func_names[namelocs] {
2377 // We allow multiple names because GnuCOBOL does. ISO says 1.
2378 for( const auto& nameloc : *$namelocs ) {
2379 if( 0 != intrinsic_token_of(nameloc.name) ) {
2380 error_msg(nameloc.loc,
2381 "intrinsic function %qs requires INTRINSIC",
2382 nameloc.name);
2383 continue;
2384 }
2385 auto token = current.udf_in(nameloc.name);
2386 if( !token ) {
2387 error_msg(nameloc.loc,
2388 "%s is not defined here as a user-defined function",
2389 nameloc.name);
2390 continue;
2391 }
2392 auto e = symbol_function(0, nameloc.name);
2393 assert(e);
2394 current.repository_add(symbol_index(e)); // add UDF to repository
2395 }
2396 }
2397 ;
2398 repo_func_names:
2399 repo_func_name[name] {
2400 $$ = new cbl_namelocs_t(1, *$name);
2401 delete $name;
2402 }
2403 | repo_func_names repo_func_name[name] {
2404 $$ = $1;
2405 $$->push_back(*$name);
2406 delete $name;
2407 }
2408 ;
2409 repo_func_name: NAME repo_as {
2410 if( ! $repo_as.empty() ) {
2411 cbl_unimplemented_at(@repo_as, "%qs", $repo_as.data);
2412 }
2413 $$ = new cbl_nameloc_t(@NAME, $NAME);
2414 }
2415 ;
2416
2417 repo_program: PROGRAM_kw NAME repo_as
2418 {
2419 size_t parent = 0;
2420 auto program = symbol_label( PROGRAM, LblProgram, 0, $NAME );
2421 if( ! program ) {
2422 if( $repo_as.empty() ) {
2423 error_msg(@repo_as, "'%s' does not name an earlier program", $NAME);
2424 YYERROR;
2425 }
2426 program = symbol_label( PROGRAM, LblProgram, 0,
2427 "", $repo_as.data );
2428 }
2429 if( ! program ) {
2430 error_msg(@repo_as, "'%s' does not name an earlier program",
2431 $repo_as.data);
2432 YYERROR;
2433 }
2434 assert(program);
2435 parent = symbol_index(symbol_elem_of(program));
2436 // Literal field whose parent is the the aliased program.
2437 cbl_field_t prog = {};
2438 prog.type = FldLiteralA;
2439 prog.attr = quoted_e;
2440 prog.parent = parent;
2441 prog.data.initial = $repo_as.data;
2442 namcpy(@NAME, prog.name, $NAME);
2443 if( ! prog.data.initial ) {
2444 assert(program);
2445 prog.data.initial = program->name;
2446 }
2447 const auto e = symbol_field_add(PROGRAM, &prog);
2448 symbol_field_location(symbol_index(e), @NAME);
2449 }
2450 ;
2451
2452 repo_property: PROPERTY NAME repo_as
2453 ;
2454
2455 with_debug: %empty
2456 | with DEBUGGING MODE {
2457 if( ! set_debug(true) ) {
2458 error_msg(@2, "DEBUGGING MODE valid only in fixed format");
2459 }
2460 }
2461 ;
2462
2463 collating_sequence: %empty { $$ = NULL; }
2464 | PROGRAM_kw COLLATING SEQUENCE is NAME[name] { $$ = $name; }
2465 | PROGRAM_kw SEQUENCE is NAME[name] { $$ = $name; }
2466 | COLLATING SEQUENCE is NAME[name] { $$ = $name; }
2467 | SEQUENCE is NAME[name] { $$ = $name; }
2468 ;
2469
2470 specials: special_names
2471 ;
2472 special_names: special_name
2473 | special_names special_name
2474 ;
2475
2476 special_name: dev_mnemonic
2477 | ALPHABET NAME[name] is alphabet_name[abc]
2478 {
2479 if( !$abc ) YYERROR;
2480 assert($abc); // already in symbol table
2481 if( !namcpy(@name, $abc->name, $name) ) YYERROR;
2482 if( yydebug ) $abc->dump();
2483 }
2484 | CLASS NAME is domains
2485 {
2486 struct cbl_field_t field = { 0,
2487 FldClass, FldInvalid, 0, 0, 0, 0, nonarray, @NAME.first_line, "",
2488 0, cbl_field_t::linkage_t(),
2489 {}, NULL };
2490 if( !namcpy(@NAME, field.name, $2) ) YYERROR;
2491
2492 struct cbl_domain_t *domain =
2493 new cbl_domain_t[ domains.size() + 1 ] ;
2494
2495 std::copy(domains.begin(), domains.end(), domain);
2496
2497 field.data.false_value_as($domains);
2498 field.data.domain_as(domain);
2499 domains.clear();
2500
2501 if( field_add(@2, &field) == NULL ) {
2502 dbgmsg("failed class");
2503 YYERROR;
2504 }
2505 }
2506 | CURRENCY sign is LITERAL[lit] with picture_sym
2507 {
2508 // The COBOL is "CURRENCY sign SYMBOL PICTURE symbol"
2509 // In our processing, we flip the order, and refer to
2510 // symbol_currency_add (symbol, sign-string). 'symbol' is the
2511 // character in the PICTURE string, and 'sign' is the substitution
2512 // that gets made in memory.
2513 if( ! string_of($lit) ) {
2514 error_msg(@lit, "'%s' has embedded NUL", $lit.data);
2515 YYERROR;
2516 }
2517 symbol_currency_add( $picture_sym, $lit.data );
2518 }
2519 | DECIMAL_POINT is COMMA
2520 {
2521 symbol_decimal_point_set(',');
2522 }
2523 | LOCALE NAME is locale_spec
2524 {
2525 current.locale($NAME, $locale_spec);
2526 cbl_unimplemented("LOCALE syntax");
2527 }
2528 ;
2529 | upsi
2530 | SYMBOLIC characters symbolic is_alphabet
2531 {
2532 cbl_unimplemented("SYMBOLIC syntax");
2533 }
2534 ;
2535 locale_spec: NAME { $$ = $1; }
2536 | LITERAL { $$ = string_of($1); }
2537
2538 ;
2539 symbolic: NAME
2540 | NUMSTR
2541 ;
2542 is_alphabet: ARE NUMSTR
2543 | is NUMSTR
2544 ;
2545
2546 dev_mnemonic: device_name is NAME
2547 {
2548 cbl_special_name_t special = { $1.token, $1.id };
2549 if( !namcpy(@NAME, special.name, $NAME) ) YYERROR;
2550
2551 const char *filename;
2552
2553 switch( special.id ) {
2554 case STDIN_e: case SYSIN_e: case SYSIPT_e:
2555 filename = "/dev/stdin";
2556 break;
2557 case STDOUT_e: case SYSOUT_e:
2558 case SYSLIST_e: case SYSLST_e: case CONSOLE_e:
2559 filename ="/dev/stdout";
2560 break;
2561 case STDERR_e: case SYSPUNCH_e: case SYSPCH_e: case SYSERR_e:
2562 filename ="/dev/stderr";
2563 break;
2564 default:
2565 filename ="/dev/null";
2566 break;
2567 }
2568
2569 special.filename = symbol_index(symbol_literalA(0, filename));
2570
2571 symbol_special_add(PROGRAM, &special);
2572 }
2573 | NAME[device] is NAME[name]
2574 {
2575 auto p = cmd_or_env_special_of($device);
2576 if( !p ) {
2577 error_msg(@device, "%s is not a device name", $device);
2578 YYERROR;
2579 }
2580
2581 cbl_special_name_t special = { 0, *p };
2582 namcpy(@name, special.name, $name);
2583
2584 symbol_special_add(PROGRAM, &special);
2585 }
2586 ;
2587
2588 device_name: SYSIN { $$.token = SYSIN; $$.id = SYSIN_e; }
2589 | SYSIPT { $$.token = SYSIPT; $$.id = SYSIPT_e; }
2590 | SYSOUT { $$.token = SYSOUT; $$.id = SYSOUT_e; }
2591 | SYSLIST { $$.token = SYSLIST; $$.id = SYSLIST_e; }
2592 | SYSLST { $$.token = SYSLST; $$.id = SYSLST_e; }
2593 | SYSPUNCH { $$.token = SYSPUNCH; $$.id = SYSPUNCH_e; }
2594 | SYSPCH { $$.token = SYSPCH; $$.id = SYSPCH_e; }
2595 | CONSOLE { $$.token = CONSOLE; $$.id = CONSOLE_e; }
2596 | C01 { $$.token = C01; $$.id = C01_e; }
2597 | C02 { $$.token = C02; $$.id = C02_e; }
2598 | C03 { $$.token = C03; $$.id = C03_e; }
2599 | C04 { $$.token = C04; $$.id = C04_e; }
2600 | C05 { $$.token = C05; $$.id = C05_e; }
2601 | C06 { $$.token = C06; $$.id = C06_e; }
2602 | C07 { $$.token = C07; $$.id = C07_e; }
2603 | C08 { $$.token = C08; $$.id = C08_e; }
2604 | C09 { $$.token = C09; $$.id = C09_e; }
2605 | C10 { $$.token = C10; $$.id = C10_e; }
2606 | C11 { $$.token = C11; $$.id = C11_e; }
2607 | C12 { $$.token = C12; $$.id = C12_e; }
2608 | CSP { $$.token = CSP; $$.id = CSP_e; }
2609 | S01 { $$.token = S01; $$.id = S01_e; }
2610 | S02 { $$.token = S02; $$.id = S02_e; }
2611 | S03 { $$.token = S03; $$.id = S03_e; }
2612 | S04 { $$.token = S04; $$.id = S04_e; }
2613 | S05 { $$.token = S05; $$.id = S05_e; }
2614 | AFP_5A { $$.token = AFP_5A; $$.id = AFP_5A_e; }
2615 | STDIN { $$.token = STDIN; $$.id = STDIN_e; }
2616 | STDOUT { $$.token = STDOUT; $$.id = STDOUT_e; }
2617 | STDERR { $$.token = STDERR; $$.id = STDERR_e; }
2618 /* These cannot be both ctx_name and here. *
2619 /* ARGUMENT_NUMBER { $$.token=0; $$.id = ARG_NUM_e; } */
2620 /* ENVIRONMENT_NAME { $$.token=0; $$.id = ENV_NAME_e; } */
2621 /* ENVIRONMENT_VALUE { $$.token=0; $$.id = ENV_VALUE_e; } */
2622 ;
2623
2624 alphabet_name: STANDARD_ALPHABET { $$ = alphabet_add(@1, ASCII_e); }
2625 | NATIVE { $$ = alphabet_add(@1, EBCDIC_e); }
2626 | EBCDIC { $$ = alphabet_add(@1, EBCDIC_e); }
2627 | alphabet_seqs
2628 {
2629 $$ = cbl_alphabet_of(symbol_alphabet_add(PROGRAM, $1));
2630 }
2631 | error
2632 {
2633 error_msg(@1, "code-name-1 may be STANDARD-1, STANDARD-2, "
2634 "NATIVE, OR EBCDIC");
2635 $$ = NULL;
2636 }
2637 ;
2638 alphabet_seqs: alphabet_seq[seq]
2639 /*
2640 * The 1st element of the 1st sequence represents the
2641 * low-value; its index becomes cbl_alphabet_t::low_index. The
2642 * high_index belongs to the last element of the last sequence
2643 * that is not an ALSO.
2644 */
2645 {
2646 $$ = new cbl_alphabet_t(@seq, custom_encoding_e);
2647
2648 if( !$seq.low || $seq.also ) {
2649 error_msg(@1, "syntax error at ALSO");
2650 YYERROR;
2651 }
2652 $$->add_sequence(@seq, $seq.low);
2653 size_t len = $seq.low == nul_string()?
2654 1 : strlen((const char*)$seq.low);
2655 assert(len > 0);
2656 $$->add_interval(@seq, $seq.low[--len], $seq.high[0]);
2657 $$->add_sequence(@seq, $seq.high);
2658 }
2659 | alphabet_seqs alphabet_seq[seq]
2660 {
2661 // ALSO x'00' is valid, but in that case the low pointer is NULL
2662 if( !$seq.low ) {
2663 $$->also(@seq, $seq.also);
2664 } else {
2665 $$->add_sequence(@seq, $seq.low);
2666 size_t len = $seq.low == nul_string()? 1 : strlen((const char*)$seq.low);
2667 assert(len > 0);
2668 $$->add_interval(@seq, $seq.low[--len], $seq.high[0]);
2669 $$->add_sequence(@seq, $seq.high);
2670 }
2671 }
2672 ;
2673 alphabet_seq: alphabet_lit[low]
2674 {
2675 $$.also = 0;
2676 if( $low.len == 1 && $low.data[0] == '\0' ) {
2677 $$.high = $$.low = nul_string();
2678 } else {
2679 size_t size = 1 + $low.len;
2680 $$.low = new unsigned char[size];
2681 memcpy($$.low, $low.data, size);
2682 $$.high = $$.low + size - 1;
2683 assert($$.high[0] == '\0');
2684 }
2685 }
2686 | alphabet_lit[low] THRU alphabet_lit[high]
2687 {
2688 $$.also = 0;
2689 size_t size = 1 + $low.len;
2690 if( $low.len == 1 && $low.data[0] == '\0' ) {
2691 $$.low = nul_string();
2692 } else {
2693 $$.low = new unsigned char[size];
2694 memcpy($$.low, $low.data, size);
2695 }
2696 assert($high.len > 0);
2697 assert($high.data[0] != '\0');
2698 size = 1 + $high.len;
2699 $$.high = new unsigned char[size];
2700 memcpy($$.high, $high.data, size);
2701 }
2702 | ALSO alphabet_etc { $$ = {}; $$.also = $2; }
2703 ;
2704 alphabet_etc: alphabet_lit
2705 {
2706 if( $1.len > 1 ) {
2707 error_msg(@1, "%qs can be only a single letter", $1.data);
2708 YYERROR;
2709 }
2710 $$ = (unsigned char)$1.data[0];
2711 }
2712 | spaces_etc {
2713 // For figurative constants, pass the symbol table index,
2714 // marked with the high bit.
2715 static const auto bits = sizeof($$) * 8 - 1;
2716 unsigned int high_bit = 1L << bits;
2717 static_assert(sizeof($$) == sizeof(high_bit),
2718 "adjust high_bit to match size of nonterminal target");
2719 memcpy(&$$, &high_bit, sizeof($$));
2720 $$ |= constant_index($1);
2721 }
2722 ;
2723 alphabet_lit: LITERAL { $$ = $1; assert($$.len > 0); }
2724 | NUMSTR {
2725 assert( $1.radix == decimal_e);
2726 $$ = literal_of($1.string);
2727 }
2728 ;
2729
2730 upsi: UPSI is NAME
2731 {
2732 assert($UPSI);
2733 size_t parent = symbol_index(symbol_field(0,0,"UPSI-0"));
2734 cbl_field_t *field = field_alloc(@NAME, FldSwitch, parent, $NAME);
2735 if( !field ) YYERROR;
2736 field->attr = constant_e;
2737 field->data.initial = $UPSI;
2738 }
2739 | UPSI is NAME upsi_entry[entry]
2740 {
2741 assert($UPSI);
2742 size_t parent = symbol_index(symbol_field(0,0,"UPSI-0"));
2743 cbl_field_t *field = field_alloc(@NAME, FldSwitch, parent, $NAME);
2744 if( !field ) YYERROR;
2745 field->attr = constant_e;
2746 field->data.initial = $UPSI;
2747
2748 assert('0' <= $UPSI[0] && $UPSI[0] < '8');
2749 const uint32_t bitn = $UPSI[0] - '0', value = (1 << bitn);
2750
2751 if( $entry.on ) {
2752 cbl_field_t *on = field_alloc(@NAME, FldSwitch, parent, $entry.on);
2753 if( !on ) YYERROR;
2754 on->data = new cbl_upsi_mask_t(true, value);
2755 }
2756 if( $entry.off ) {
2757 cbl_field_t *off = field_alloc(@NAME, FldSwitch, parent, $entry.off);
2758 if( !off ) YYERROR;
2759 off->data = new cbl_upsi_mask_t(false, value);
2760 }
2761 }
2762 | UPSI upsi_entry[entry]
2763 {
2764 size_t parent = symbol_index(symbol_field(0,0,"UPSI-0"));
2765 assert('0' <= $UPSI[0] && $UPSI[0] < '8');
2766 const uint32_t bitn = $UPSI[0] - '0', value = (1 << bitn);
2767
2768 if( $entry.on ) {
2769 cbl_field_t *on = field_alloc($entry.loc, FldSwitch, parent, $entry.on);
2770 if( !on ) YYERROR;
2771 on->data = new cbl_upsi_mask_t(true, value);
2772 }
2773 if( $entry.off ) {
2774 cbl_field_t *off = field_alloc($entry.loc, FldSwitch, parent, $entry.off);
2775 if( !off ) YYERROR;
2776 off->data = new cbl_upsi_mask_t(false, value);
2777 }
2778 }
2779 ;
2780 upsi_entry: ON status is NAME
2781 {
2782 $$.loc = @NAME;
2783 $$.on = $NAME;
2784 $$.off = NULL;
2785 }
2786 | OFF status is NAME
2787 {
2788 $$.loc = @NAME;
2789 $$.on = NULL;
2790 $$.off = $NAME;
2791 }
2792 | OFF status is NAME[off] ON status is NAME[on]
2793 {
2794 $$.loc = @off;
2795 $$.on = $on;
2796 $$.off = $off;
2797 }
2798 | ON status is NAME[on] OFF status is NAME[off]
2799 {
2800 $$.loc = @on;
2801 $$.on = $on;
2802 $$.off = $off;
2803 }
2804 ;
2805
2806 picture_sym: %empty { $$ = NULL; }
2807 | PICTURE SYMBOL LITERAL[lit] {
2808 if( ! string_of($lit) ) {
2809 error_msg(@lit, "'%s' has embedded NUL", $lit.data);
2810 YYERROR;
2811 }
2812 $$ = string_of($lit);
2813 }
2814 ;
2815
2816 /*
2817 * The domains nonterminal ($domain) carries the FALSE value,
2818 * if any. The domains variable (global std::list) carries the
2819 * variable's DOMAIN, ending in a NULL. See the action for
2820 * "CLASS NAME is domains".
2821 */
2822 domains: domain
2823 | domains domain { $$ = $1? $1 : $2; }
2824 ;
2825
2826 domain: all LITERAL[a]
2827 {
2828 if( ! string_of($a) ) {
2829 gcc_location_set(@a);
2830 yywarn("'%s' has embedded NUL", $a.data);
2831 }
2832 $$ = NULL;
2833 cbl_domain_t domain(@a, $all, $a.len, $a.data);
2834 domains.push_back(domain);
2835 }
2836 | all[a_all] LITERAL[a] THRU all[z_all] LITERAL[z]
2837 {
2838 if( ! string_of($a) ) {
2839 yywarn("'%s' has embedded NUL", $a.data);
2840 }
2841 if( ! string_of($z) ) {
2842 yywarn("'%s' has embedded NUL", $z.data);
2843 }
2844 $$ = NULL;
2845 cbl_domain_elem_t first(@a, $a_all, $a.len, $a.data),
2846 last(@z, $z_all, $z.len, $z.data);
2847 domains.push_back(cbl_domain_t(first, last));
2848 }
2849 | all NUMSTR[n]
2850 {
2851 $$ = NULL;
2852 cbl_domain_t dom(@n, $all, strlen($n.string), $n.string, true);
2853 domains.push_back(dom);
2854 }
2855 | all[n_all] NUMSTR[n] THRU all[m_all] NUMSTR[m]
2856 {
2857 $$ = NULL;
2858 cbl_domain_elem_t first(@n, $n_all, strlen($n.string), $n.string, true),
2859 last(@m, $m_all, strlen($m.string), $m.string, true);
2860 domains.push_back(cbl_domain_t(first, last));
2861 }
2862 | all reserved_value {
2863 $$ = NULL;
2864 if( $2 == NULLS ) YYERROR;
2865 auto value = constant_of(constant_index($2))->data.initial;
2866 struct cbl_domain_t domain( @2, $all, strlen(value), value );
2867 domains.push_back(domain);
2868 }
2869 | all[a_all] reserved_value[a] THRU all[z_all] LITERAL[z] {
2870 if( ! string_of($z) ) {
2871 yywarn("'%s' has embedded NUL", $z.data);
2872 }
2873 $$ = NULL;
2874 if( $a == NULLS ) YYERROR;
2875 auto value = constant_of(constant_index($a))->data.initial;
2876 cbl_domain_elem_t first(@a, $a_all, strlen(value), value),
2877 last(@z, $z_all, $z.len, $z.data);
2878 domains.push_back(cbl_domain_t(first, last));
2879 }
2880 | all[a_all] reserved_value[a] THRU all[z_all] NUMSTR[z] {
2881 $$ = NULL;
2882 if( $a == NULLS ) YYERROR;
2883 auto value = constant_of(constant_index($a))->data.initial;
2884 cbl_domain_elem_t first(@a, $a_all, strlen(value), value, true),
2885 last(@z, $z_all, strlen($z.string), $z.string, true);
2886 domains.push_back(cbl_domain_t(first, last));
2887 }
2888 | when_set_to FALSE_kw is LITERAL[value]
2889 {
2890 if( ! string_of($value) ) {
2891 yywarn("'%s' has embedded NUL", $value.data);
2892 }
2893 const char *dom = $value.data;
2894 $$ = new cbl_domain_t(@value, false, $value.len, dom);
2895 }
2896 | when_set_to FALSE_kw is reserved_value
2897 {
2898 if( $4 == NULLS ) YYERROR;
2899 auto value = constant_of(constant_index($4))->data.initial;
2900 $$ = new cbl_domain_t(@4, false, strlen(value), value );
2901 }
2902 | when_set_to FALSE_kw is NUMSTR[n]
2903 {
2904 $$ = new cbl_domain_t(@n, false, strlen($n.string), $n.string, true);
2905 }
2906 ;
2907 when_set_to: %empty
2908 | WHEN
2909 | SET
2910 | TO
2911 | WHEN SET
2912 | SET TO
2913 | WHEN TO
2914 | WHEN SET TO
2915 ;
2916
2917 data_div: %empty
2918 | DATA_DIV
2919 | DATA_DIV { current_division = data_div_e; } data_sections
2920 {
2921 current_data_section = not_data_datasect_e;
2922 parser_division( data_div_e, NULL, 0, NULL );
2923 }
2924 ;
2925
2926 data_sections: data_section
2927 | data_sections data_section
2928 ;
2929
2930 data_section: FILE_SECT '.'
2931 | FILE_SECT '.' {
2932 current_data_section_set(@1, file_datasect_e);
2933 } file_descrs
2934 | WORKING_STORAGE_SECT '.' {
2935 current_data_section_set(@1, working_storage_datasect_e);
2936 } fields_maybe
2937 | LOCAL_STORAGE_SECT '.' {
2938 current_data_section_set(@1, local_storage_datasect_e);
2939 } fields_maybe
2940 | LINKAGE_SECT '.' {
2941 current_data_section_set(@1, linkage_datasect_e);
2942 } fields_maybe
2943 | SCREEN SECTION '.' {
2944 cbl_unimplemented("SCREEN SECTION");
2945 }
2946 ;
2947
2948 file_descrs: file_descr
2949 | file_descrs file_descr
2950 ;
2951 file_descr: fd_name '.' { field_done(); } fields
2952 | fd_name fd_clauses '.' { field_done(); } fields
2953 ;
2954
2955 fd_name: FD NAME { $$ = $2; file_section_fd_set(fd_e, $2, @2); }
2956 | SD NAME { $$ = $2; file_section_fd_set(sd_e, $2, @2); }
2957 ;
2958
2959 fd_clauses: fd_clause
2960 | fd_clauses fd_clause
2961 ;
2962 fd_clause: record_desc
2963 {
2964 auto f = cbl_file_of(symbol_at(file_section_fd));
2965 f->varying_size.min = $1.min;
2966 f->varying_size.max = $1.max;
2967 auto& cap = cbl_field_of(symbol_at(f->default_record))->data.capacity;
2968 cap = std::max(cap, uint32_t(f->varying_size.max));
2969 // If min != max now, we know varying is explicitly defined.
2970 f->varying_size.explicitly = f->varies();
2971 if( f->varying_size.max != 0 ) {
2972 if( !(f->varying_size.min <= f->varying_size.max) ) {
2973 error_msg(@1, "%zu must be less than or equal to %zu",
2974 f->varying_size.min, f->varying_size.max);
2975 YYERROR;
2976 }
2977 }
2978 }
2979 | block_desc
2980 | label_desc
2981 | DATA record_is field_list
2982 | RECORDING mode is NAME
2983 {
2984 switch( $NAME[0] ) {
2985 case 'F':
2986 case 'V':
2987 case 'U':
2988 case 'S':
2989 break;
2990 default:
2991 error_msg(@NAME, "invalid RECORDING MODE '%s'", $NAME);
2992 YYERROR;
2993 }
2994 cbl_unimplementedw("RECORDING MODE was ignored, not defined by ISO 2023");
2995 }
2996 | VALUE OF fd_values
2997 | CODESET is NAME
2998 | is GLOBAL
2999 {
3000 auto f = cbl_file_of(symbol_at(file_section_fd));
3001 f->attr |= global_e;
3002 }
3003 | is EXTERNAL
3004 {
3005 auto f = cbl_file_of(symbol_at(file_section_fd));
3006 f->attr |= external_e;
3007 }
3008 | is EXTERNAL as LITERAL
3009 {
3010 auto f = cbl_file_of(symbol_at(file_section_fd));
3011 f->attr |= external_e;
3012 cbl_unimplemented("AS LITERAL");
3013 }
3014 | fd_linage { cbl_unimplemented("LINAGE"); }
3015 | fd_report {
3016 cbl_unimplemented("REPORT WRITER");
3017 YYERROR;
3018 }
3019 ;
3020
3021 block_desc: BLOCK_kw contains rec_contains chars_recs
3022 ;
3023 rec_contains: NUMSTR[min] {
3024 REAL_VALUE_TYPE rn = numstr2i($min.string, $min.radix);
3025 ssize_t n = real_to_integer (&rn);
3026 if( n < 0 ) {
3027 error_msg(@min, "size %s cannot be negative", $min.string);
3028 YYERROR;
3029 }
3030 $$.min = $$.max = n; // fixed length
3031 }
3032 | NUMSTR[min] TO NUMSTR[max] {
3033 REAL_VALUE_TYPE rn = numstr2i($min.string, $min.radix);
3034 ssize_t n = real_to_integer (&rn);
3035 if( n < 0 ) {
3036 error_msg(@min, "size %s cannot be negative", $min.string);
3037 YYERROR;
3038 }
3039 $$.min = n;
3040
3041 rn = numstr2i($max.string, $max.radix);
3042 n = real_to_integer (&rn);
3043 if( n < 0 ) {
3044 error_msg(@max, "size %s cannot be negative", $max.string);
3045 YYERROR;
3046 }
3047 $$.max = n;
3048 if( !($$.min < $$.max) ) {
3049 error_msg(@max, "FROM (%zu) must be less than TO (%zu)",
3050 $$.min, $$.max);
3051 YYERROR;
3052 }
3053 }
3054 ;
3055 chars_recs: %empty
3056 | CHARACTERS
3057 | RECORDS
3058 ;
3059
3060 label_desc: LABEL record_is STANDARD
3061 | LABEL record_is OMITTED
3062 | LABEL record_is fd_labels
3063 ;
3064
3065 record_is: RECORD /* lexer swallows IS/ARE */
3066 | RECORDS
3067 ;
3068
3069 fd_values: fd_value
3070 | fd_values fd_value
3071 ;
3072 /* "The VALUE OF clause is syntax checked, but has
3073 no effect on the execution of the program." */
3074 fd_value: NAME is alpha_val
3075 ;
3076 alpha_val: alphaval
3077 | scalar
3078 ;
3079
3080 fd_labels: fd_label
3081 | fd_labels fd_label
3082 ;
3083 fd_label: NAME
3084 ;
3085
3086 record_desc: RECORD is record_vary[r] depending { $$ = $r; }
3087 | RECORD contains rec_contains[r] characters { $$ = $r; }
3088 ;
3089
3090 record_vary: VARYING in_size from_to { $$ = $from_to; }
3091 | VARYING from_to { $$ = $from_to; }
3092 | VARYING in_size { $$.min = 0; $$.max = 0; }
3093 | VARYING { $$.min = 0; $$.max = 0; }
3094 ;
3095
3096 in_size: IN SIZE
3097 | IN
3098 | SIZE
3099 ;
3100
3101 from_to: FROM NUMSTR[min] TO NUMSTR[max] characters {
3102 REAL_VALUE_TYPE rn = numstr2i($min.string, $min.radix);
3103 ssize_t n = real_to_integer (&rn);
3104 if( n < 0 ) {
3105 error_msg(@min, "size %s cannot be negative", $min.string);
3106 YYERROR;
3107 }
3108 $$.min = n;
3109 rn = numstr2i($max.string, $max.radix);
3110 n = real_to_integer (&rn);
3111 if( n < 0 ) {
3112 error_msg(@min, "size %s cannot be negative", $max.string);
3113 YYERROR;
3114 }
3115 $$.max = n;
3116 }
3117 | NUMSTR[min] TO NUMSTR[max] characters {
3118 REAL_VALUE_TYPE rn = numstr2i($min.string, $min.radix);
3119 ssize_t n = real_to_integer (&rn);
3120 if( n < 0 ) {
3121 error_msg(@min, "size %s cannot be negative", $min.string);
3122 YYERROR;
3123 }
3124 $$.min = n;
3125 rn = numstr2i($max.string, $max.radix);
3126 n = real_to_integer (&rn);
3127 if( n < 0 ) {
3128 error_msg(@max, "size %s cannot be negative", $max.string);
3129 YYERROR;
3130 }
3131 $$.max = n;
3132 }
3133
3134 | TO NUMSTR[max] characters {
3135 REAL_VALUE_TYPE rn = numstr2i($max.string, $max.radix);
3136 ssize_t n = real_to_integer (&rn);
3137 if( n < 0 ) {
3138 error_msg(@max, "size %s cannot be negative", $max.string);
3139 YYERROR;
3140 }
3141 $$.min = 0;
3142 $$.max = n;
3143 }
3144
3145 | FROM NUMSTR[min] characters {
3146 REAL_VALUE_TYPE rn = numstr2i($min.string, $min.radix);
3147 ssize_t n = real_to_integer (&rn);
3148 if( n < 0 ) {
3149 error_msg(@min, "size %s cannot be negative", $min.string);
3150 YYERROR;
3151 }
3152 $$.min = n;
3153 $$.max = size_t(-1);
3154 }
3155 | NUMSTR[min] characters {
3156 REAL_VALUE_TYPE rn = numstr2i($min.string, $min.radix);
3157 ssize_t n = real_to_integer (&rn);
3158 if( n < 0 ) {
3159 error_msg(@min, "size %s cannot be negative", $min.string);
3160 YYERROR;
3161 }
3162 $$.min = n;
3163 $$.max = size_t(-1);
3164 }
3165
3166 | CHARACTERS { $$.min = 0; $$.max = size_t(-1); }
3167 ;
3168
3169 depending: %empty
3170 | DEPENDING on NAME
3171 {
3172 assert(file_section_fd > 0);
3173 symbol_elem_t *e = symbol_at(file_section_fd);
3174 assert(e);
3175 auto file = cbl_file_of(e);
3176 size_t odo;
3177
3178 if( (e = symbol_field(PROGRAM, 0, $3)) != NULL ) {
3179 assert(e->type == SymField);
3180 odo = symbol_index(e);
3181 } else {
3182 e = symbol_field_forward_add(PROGRAM, 0, $NAME, @NAME.first_line);
3183 if( !e ) YYERROR;
3184 symbol_field_location( symbol_index(e), @NAME );
3185 odo = field_index(cbl_field_of(e));
3186 }
3187
3188 file->record_length = odo;
3189 assert( file->record_length > 0 );
3190 }
3191 ;
3192
3193 fd_linage: LINAGE is num_value with_footings
3194 | LINAGE is num_value lines
3195 ;
3196 with_footings: with_footing
3197 | with_footings with_footing
3198 ;
3199 with_footing: lines with FOOTING at num_value
3200 | lines at top_bot num_value
3201 ;
3202 top_bot: TOP
3203 | BOTTOM
3204 ;
3205
3206 fd_report: REPORT
3207 | REPORTS
3208 ;
3209
3210 fields_maybe: %empty
3211 | fields
3212 ;
3213 fields: field
3214 | fields field
3215 ;
3216
3217 field: cdf
3218 | data_descr '.'
3219 {
3220 if( in_file_section() && $data_descr->level == 1 ) {
3221 if( !file_section_parent_set($data_descr) ) {
3222 YYERROR;
3223 }
3224 }
3225 field_done();
3226
3227 const auto& field(*$data_descr);
3228
3229 // Format data.initial per picture
3230 if( 0 == pristine_values.count(field.data.initial) ) {
3231 if( field.data.digits > 0 && !field.is_zero() ) {
3232 char *initial;
3233 int rdigits = field.data.rdigits < 0?
3234 1 : field.data.rdigits + 1;
3235
3236 if( field.has_attr(scaled_e) ) {
3237 if( field.data.rdigits > 0 ) {
3238 rdigits = field.data.digits + field.data.rdigits;
3239 } else {
3240 rdigits = 0;
3241 }
3242 }
3243 initial = string_of(field.data.value_of());
3244 if( !initial ) {
3245 error_msg(@1, "could not convert value to string");
3246 YYERROR;
3247 }
3248 char decimal = symbol_decimal_point();
3249 std::replace(initial, initial + strlen(initial), '.', decimal);
3250 free(const_cast<char*>($data_descr->data.initial));
3251 $data_descr->data.initial = initial;
3252 if( yydebug ) {
3253 const char *value_str = string_of(field.data.value_of());
3254 dbgmsg("%s::data.initial is (%%%d.%d) %s ==> '%s'",
3255 field.name,
3256 field.data.digits,
3257 rdigits,
3258 value_str? value_str : "",
3259 field.data.initial);
3260 }
3261 }
3262 }
3263 }
3264 ;
3265
3266 occurs_clause: OCCURS cardinal_lb indexed
3267 | OCCURS cardinal_lb key_descrs indexed
3268 | OCCURS depending_on key_descrs indexed
3269 | OCCURS depending_on indexed
3270 | OCCURS name indexed
3271 {
3272 if( ! (is_constant($name) && $name->type == FldLiteralN) ) {
3273 error_msg(@name, "%s is not CONSTANT", $name->name);
3274 YYERROR;
3275 }
3276 cbl_occurs_t *occurs = &current_field()->occurs;
3277 occurs->bounds.lower =
3278 occurs->bounds.upper = $name->as_integer();
3279 }
3280 ;
3281 cardinal_lb: cardinal times {
3282 current_field()->occurs.bounds.lower = $cardinal;
3283 current_field()->occurs.bounds.upper = $cardinal;
3284 }
3285 ;
3286
3287 cardinal: NUMSTR[input]
3288 {
3289 REAL_VALUE_TYPE rn = numstr2i($input.string, $input.radix);
3290 $$ = real_to_integer (&rn);
3291 }
3292 ;
3293
3294 depending_on: cardinal[lower] TO bound DEPENDING on name
3295 {
3296 cbl_occurs_t *occurs = &current_field()->occurs;
3297 occurs->bounds.lower = (size_t)$lower;
3298 occurs->bounds.upper = (size_t)$bound;
3299 occurs->depending_on = field_index($name);
3300 }
3301 | bound DEPENDING on name
3302 {
3303 cbl_occurs_t *occurs = &current_field()->occurs;
3304 occurs->bounds.lower = 1;
3305 occurs->bounds.upper = (size_t)$bound;
3306 occurs->depending_on = field_index($name);
3307 }
3308 ;
3309 bound: cardinal times
3310 | UNBOUNDED times { $$ = -1; }
3311 ;
3312
3313 key_descrs: key_descr
3314 | key_descrs key_descr
3315 ;
3316 key_descr: ordering key is key_fields
3317 ;
3318 ordering: ASCENDING
3319 {
3320 current_field()->occurs.key_alloc(true);
3321 }
3322 | DESCENDING
3323 {
3324 current_field()->occurs.key_alloc(false);
3325 }
3326 ;
3327 key_fields: key_field1
3328 | key_fields key_field1
3329 ;
3330 key_field1: name
3331 {
3332 current_field()->occurs.key_field_add($1);
3333 }
3334 ;
3335
3336 indexed: %empty
3337 | INDEXED by index_fields
3338 ;
3339 index_fields: index_field1
3340 | index_fields index_field1
3341 ;
3342 index_field1: ctx_name[name]
3343 {
3344 static const cbl_field_data_t data { 0, 8 }; // capacity 8
3345 cbl_field_t field = {};
3346 field.type = FldIndex;
3347 field.parent = field_index(current_field());
3348 field.data = data;
3349 if( !namcpy(@name, field.name, $name) ) YYERROR;
3350
3351 auto symbol = symbol_field(PROGRAM, field.parent, $name);
3352 if( symbol ) {
3353 auto f( cbl_field_of(symbol) );
3354 error_msg(@name, "'%s' already defined on line %d",
3355 f->name, f->line );
3356 YYERROR;
3357 }
3358
3359 auto index = field_add(@name, &field);
3360 if( !index ) {
3361 YYERROR;
3362 }
3363
3364 current_field()->occurs.index_add(index);
3365 }
3366 ;
3367
3368 level_name: LEVEL ctx_name
3369 {
3370 switch($LEVEL) {
3371 case 66:
3372 case 77:
3373 case 88:
3374 break;
3375 default:
3376 if( 1 <= $LEVEL && $LEVEL <= 49 ) break;
3377 error_msg(@LEVEL, "LEVEL %d not supported", $LEVEL);
3378 YYERROR;
3379 }
3380 struct cbl_field_t field = { 0,
3381 FldInvalid, FldInvalid, 0, 0, 0, capacity_cast($1),
3382 nonarray, @ctx_name.first_line, "",
3383 0, cbl_field_t::linkage_t(),
3384 {}, NULL };
3385 if( !namcpy(@ctx_name, field.name, $2) ) YYERROR;
3386
3387 $$ = field_add(@$, &field);
3388 if( !$$ ) {
3389 YYERROR;
3390 }
3391 current_field($$); // make available for data_clauses
3392 }
3393 | LEVEL
3394 {
3395 switch($LEVEL) {
3396 case 66:
3397 case 77:
3398 case 88:
3399 break;
3400 default:
3401 if( 1 <= $LEVEL && $LEVEL <= 49 ) break;
3402 error_msg(@LEVEL, "LEVEL %d not supported", $LEVEL);
3403 YYERROR;
3404 }
3405 struct cbl_field_t field = { 0,
3406 FldInvalid, FldInvalid, 0, 0, 0, capacity_cast($1),
3407 nonarray, @LEVEL.first_line, "",
3408 0, {}, {}, NULL };
3409
3410 $$ = field_add(@1, &field);
3411 if( !$$ ) {
3412 YYERROR;
3413 }
3414 current_field($$); // make available for data_clauses
3415 }
3416 ;
3417
3418 data_descr: data_descr1
3419 {
3420 $$ = current_field($1); // make available for occurs, etc.
3421 }
3422 | error { static cbl_field_t none = {}; $$ = &none; }
3423 ;
3424
3425 const_value: cce_expr
3426 | BYTE_LENGTH of name { set_real_from_capacity(@name, $name, &$$); }
3427 | LENGTH of name { set_real_from_capacity(@name, $name, &$$); }
3428 | LENGTH_OF of name { set_real_from_capacity(@name, $name, &$$); }
3429 | LENGTH_OF of binary_type[type] {
3430 real_from_integer(&$$, VOIDmode, $type, SIGNED); }
3431 ;
3432
3433 value78: literalism
3434 {
3435 cbl_field_data_t data = {};
3436 data.capacity = capacity_cast(strlen($1.data));
3437 data.initial = $1.data;
3438 $$ = new cbl_field_data_t(data);
3439 }
3440 | const_value
3441 {
3442 cbl_field_data_t data = {};
3443 data = build_real (float128_type_node, $1);
3444 $$ = new cbl_field_data_t(data);
3445 }
3446 | reserved_value[value]
3447 {
3448 const auto field = constant_of(constant_index($value));
3449 $$ = new cbl_field_data_t(field->data);
3450 }
3451
3452 | true_false
3453 {
3454 cbl_unimplemented("Boolean constant");
3455 YYERROR;
3456 }
3457 ;
3458
3459 data_descr1: level_name
3460 {
3461 assert($1 == current_field());
3462 if( $1->usage == FldIndex ) {
3463 field_type_update($1, $1->usage, @1, true);
3464 }
3465 }
3466
3467 | level_name CONSTANT is_global as const_value
3468 {
3469 cbl_field_t& field = *$1;
3470 if( field.level != 1 ) {
3471 error_msg(@1, "%s must be an 01-level data item", field.name);
3472 YYERROR;
3473 }
3474
3475 field.attr |= constant_e;
3476 if( $is_global ) field.attr |= global_e;
3477 field.type = FldLiteralN;
3478 field.data = build_real (float128_type_node, $const_value);
3479 field.data.initial = string_of($const_value);
3480
3481 if( !cdf_value(field.name, cdfval_t($const_value)) ) {
3482 error_msg(@1, "%s was defined by CDF", field.name);
3483 }
3484 }
3485
3486 | level_name CONSTANT is_global as reserved_value[value]
3487 {
3488 cbl_field_t& field = *$1;
3489 if( field.level != 1 ) {
3490 error_msg(@1, "%s must be an 01-level data item", field.name);
3491 YYERROR;
3492 }
3493 field.attr |= constant_e;
3494 if( $is_global ) field.attr |= global_e;
3495 field.type = FldLiteralA;
3496 auto fig = constant_of(constant_index($value));
3497 field.data = fig->data;
3498 }
3499
3500 | level_name CONSTANT is_global as literalism[lit]
3501 {
3502 cbl_field_t& field = *$1;
3503 field.attr |= constant_e;
3504 if( $is_global ) field.attr |= global_e;
3505 field.type = FldLiteralA;
3506 field.data.capacity = $lit.len;
3507 field.data.initial = $lit.data;
3508 field.attr |= literal_attr($lit.prefix);
3509 if( field.level != 1 ) {
3510 error_msg(@lit, "%s must be an 01-level data item", field.name);
3511 YYERROR;
3512 }
3513 if( !cdf_value(field.name, $lit.data) ) {
3514 error_msg(@1, "%s was defined by CDF", field.name);
3515 }
3516 value_encoding_check(@lit, $1);
3517 }
3518 | level_name CONSTANT is_global FROM NAME
3519 {
3520 assert($1 == current_field());
3521 const cdfval_t *cdfval = cdf_value($NAME);
3522 if( !cdfval ) {
3523 error_msg(@1, "%s was defined by CDF", $NAME);
3524 YYERROR;
3525 }
3526 cbl_field_t& field = *$1;
3527 field.attr |= ($is_global | constant_e);
3528 field.data.capacity = cdfval->string ? strlen(cdfval->string)
3529 : sizeof(field.data.value_of());
3530 field.data.initial = cdfval->string;
3531 field.data = cdfval->number;
3532 if( !cdf_value(field.name, *cdfval) ) {
3533 error_msg(@1, "%s was defined by CDF", field.name);
3534 }
3535 }
3536
3537 | LEVEL78 NAME[name] VALUE is value78[data]
3538 {
3539 if( ! (dialect_mf() || dialect_gnu()) ) {
3540 dialect_error(@1, "level 78", "mf or gnu");
3541 YYERROR;
3542 }
3543 struct cbl_field_t field = { 0, FldLiteralA, FldInvalid,
3544 constant_e, 0, 0, 78, nonarray,
3545 @name.first_line, "", 0, {}, *$data, NULL };
3546 if( !namcpy(@name, field.name, $name) ) YYERROR;
3547 if( field.data.initial ) {
3548 field.attr |= quoted_e;
3549 if( !cdf_value(field.name, field.data.initial) ) {
3550 yywarn("%s was defined by CDF", field.name);
3551 }
3552 } else {
3553 field.type = FldLiteralN;
3554 field.data.initial = string_of(field.data.value_of());
3555 if( !cdf_value(field.name, field.as_integer()) ) {
3556 yywarn("%s was defined by CDF", field.name);
3557 }
3558 }
3559 if( ($$ = field_add(@name, &field)) == NULL ) {
3560 error_msg(@name, "failed level 78");
3561 YYERROR;
3562 }
3563 }
3564
3565 | LEVEL88 NAME /* VALUE */ NULLPTR
3566 {
3567 struct cbl_field_t field = { 0,
3568 FldClass, FldInvalid, 0, 0, 0, 88, nonarray, @NAME.first_line, "",
3569 0, cbl_field_t::linkage_t(),
3570 {}, NULL };
3571 if( !namcpy(@NAME, field.name, $2) ) YYERROR;
3572
3573 auto fig = constant_of(constant_index(NULLS))->data.initial;
3574 struct cbl_domain_t *domain = new cbl_domain_t[2];
3575
3576 domain[0] = cbl_domain_t(@NAME, false, strlen(fig), fig);
3577
3578 field.data.domain_as(domain);
3579
3580 if( ($$ = field_add(@2, &field)) == NULL ) {
3581 error_msg(@NAME, "failed level 88");
3582 YYERROR;
3583 }
3584 auto parent = cbl_field_of(symbol_at($$->parent));
3585 if( parent->type != FldPointer ) {
3586 error_msg(@NAME, "LEVEL 88 %s VALUE NULLS invalid for "
3587 "%s %s, which is not a POINTER",
3588 $$->name, parent->level_str(), parent->name);
3589 }
3590 }
3591 | LEVEL88 NAME VALUE domains
3592 {
3593 struct cbl_field_t field = { 0,
3594 FldClass, FldInvalid, 0, 0, 0, 88, nonarray, @NAME.first_line, "",
3595 0, cbl_field_t::linkage_t(),
3596 {}, NULL };
3597 if( !namcpy(@NAME, field.name, $2) ) YYERROR;
3598
3599 struct cbl_domain_t *domain =
3600 new cbl_domain_t[ domains.size() + 1];
3601
3602 std::copy(domains.begin(), domains.end(), domain);
3603
3604 field.data.domain_as(domain);
3605 field.data.false_value_as($domains);
3606 domains.clear();
3607
3608 if( ($$ = field_add(@2, &field)) == NULL ) {
3609 error_msg(@NAME, "failed level 88");
3610 YYERROR;
3611 }
3612 }
3613
3614 | name66[alias] RENAMES name[orig]
3615 {
3616 symbol_field_alias_end();
3617 if( is_literal($orig) ) {
3618 error_msg(@orig, "cannot RENAME '%s'", name_of($orig));
3619 YYERROR;
3620 }
3621 if( !immediately_follows($orig) ) {
3622 error_msg(@orig, "%s must immediately follow %s to RENAME it",
3623 $alias, name_of($orig));
3624 YYERROR;
3625 }
3626 if( $orig->occurs.ntimes() ) {
3627 error_msg(@orig, "cannot RENAME table %s %s",
3628 $orig->level_str(), name_of($orig));
3629 YYERROR;
3630 }
3631 auto table = occurs_in($orig);
3632 if( table ) {
3633 error_msg(@orig, "cannot RENAME '%s' OF %s",
3634 name_of($orig), table->name);
3635 YYERROR;
3636 }
3637 if( ! $orig->rename_level_ok() ) {
3638 error_msg(@orig, "cannot RENAME %s %s",
3639 $orig->level_str(), name_of($orig));
3640 YYERROR;
3641 }
3642 symbol_elem_t *orig = symbol_at(field_index($orig));
3643 $$ = cbl_field_of(symbol_field_alias(orig, $alias));
3644 symbol_field_location(field_index($$), @alias);
3645 }
3646
3647 | name66[alias] RENAMES name[orig] THRU name[thru]
3648 {
3649 symbol_field_alias_end();
3650 if( !immediately_follows($orig) ) {
3651 error_msg(@orig, "RENAMES: %s must immediately follow %s",
3652 $alias, name_of($orig));
3653 YYERROR;
3654 }
3655 if( is_literal($orig) ) {
3656 error_msg(@orig, "cannot RENAME '%s'", name_of($orig));
3657 YYERROR;
3658 }
3659 if( is_literal($thru) ) {
3660 error_msg(@thru, "cannot RENAME '%s'", name_of($thru));
3661 YYERROR;
3662 }
3663 auto table = occurs_in($orig);
3664 if( table ) {
3665 error_msg(@orig, "cannot RENAME '%s' OF %s",
3666 name_of($orig), table->name);
3667 YYERROR;
3668 }
3669 table = occurs_in($thru);
3670 if( table ) {
3671 error_msg(@thru, "cannot RENAME '%s' OF %s",
3672 name_of($thru), table->name);
3673 YYERROR;
3674 }
3675 if( ! $orig->rename_level_ok() ) {
3676 error_msg(@orig, "cannot RENAME %s %s",
3677 $orig->level_str(), name_of($orig));
3678 YYERROR;
3679 }
3680 if( $orig->has_subordinate($thru) ) {
3681 error_msg(@orig, "cannot RENAME %s %s THRU %s %s "
3682 "because %s is subordinate to %s",
3683 $orig->level_str(), name_of($orig),
3684 $thru->level_str(), name_of($thru),
3685 name_of($thru), name_of($orig));
3686 YYERROR;
3687 }
3688 auto not_ok = rename_not_ok($orig, $thru);
3689 if( not_ok ) {
3690 error_msg(@orig, "cannot RENAME %s %s THRU %s %s "
3691 "because %s %s cannot be renamed",
3692 $orig->level_str(), name_of($orig),
3693 $thru->level_str(), name_of($thru),
3694 not_ok->level_str(), name_of(not_ok));
3695 YYERROR;
3696 }
3697 if( field_index($thru) <= field_index($orig) ) {
3698 error_msg(@orig, "cannot RENAME %s %s THRU %s %s "
3699 "because they are in the wrong order",
3700 $orig->level_str(), name_of($orig),
3701 $thru->level_str(), name_of($thru));
3702 YYERROR;
3703 }
3704 symbol_elem_t *orig = symbol_at(field_index($orig));
3705 symbol_elem_t *last = symbol_at(field_index($thru));
3706 $$ = cbl_field_of(symbol_field_alias2(orig, last, $alias));
3707 symbol_field_location(field_index($$), @alias);
3708 }
3709
3710 | level_name[field] data_clauses
3711 {
3712 gcc_assert($field == current_field());
3713 if( $data_clauses == value_clause_e ) { // only VALUE, no PIC
3714 // Error unless VALUE is a figurative constant or (quoted) string.
3715 if( $field->type != FldPointer &&
3716 ! $field->has_attr(quoted_e) &&
3717 normal_value_e == cbl_figconst_of($field->data.initial) )
3718 {
3719 error_msg(@field, "%s numeric VALUE %s requires PICTURE",
3720 $field->name, $field->data.initial);
3721 }
3722 if( null_value_e == cbl_figconst_of($field->data.initial) ) {
3723 // don't change the type
3724 assert(FldPointer == $field->type);
3725 } else {
3726 // alphanumeric VALUE by itself implies alphanumeric type
3727 assert(FldPointer != $field->type);
3728 $field->type = FldAlphanumeric;
3729 if( $field->data.initial ) {
3730 $field->data.capacity = strlen($field->data.initial);
3731 }
3732 }
3733 }
3734
3735 // Verify BLANK WHEN ZERO
3736 if( $field->has_attr(blank_zero_e) ) {
3737 switch($field->type) {
3738 case FldNumericEdited:
3739 if( $field->has_attr(signable_e) ) {
3740 error_msg(@2, "%s has 'S' in PICTURE, cannot be BLANK WHEN ZERO",
3741 $field->name );
3742 }
3743 break;
3744 default:
3745 error_msg(@2, "%s must be "
3746 "NUMERIC DISPLAY or NUMERIC-EDITED, not %s",
3747 $field->name, cbl_field_type_str($field->type) );
3748 }
3749 $field->data.picture = original_picture();
3750 }
3751
3752 // SIGN clause valid only with "S" in picture
3753 if( $field->type == FldNumericDisplay && !is_signable($field) ) {
3754 static const uint64_t sign_attrs = leading_e | separate_e;
3755 static_assert(sizeof(sign_attrs) == sizeof($field->attr),
3756 "size matters");
3757
3758 // remove inapplicable inherited sign attributes
3759 uint64_t group_sign = group_attr($field) & sign_attrs;
3760 $field->attr &= ~group_sign;
3761
3762 if( $field->attr & sign_attrs ) {
3763 dbgmsg("%s:%d: %s", __func__, __LINE__, field_str($field));
3764 error_msg(@field, "%s must be signed for SIGN IS",
3765 $field->name );
3766 YYERROR;
3767 }
3768 }
3769
3770 // Increase numeric display capacity by 1 for SIGN SEPARATE.
3771 if( $field->type == FldNumericDisplay &&
3772 is_signable($field) &&
3773 $field->has_attr(separate_e) ){
3774 $field->data.capacity++;
3775 }
3776
3777 // Set Packed-Decimal capacity
3778 if( $field->type == FldPacked ) {
3779 $field->data.capacity = type_capacity($field->type,
3780 $field->data.digits);
3781 if( $field->attr & separate_e )
3782 {
3783 // This is a gentle kludge required by the the belated
3784 // introduction of COMP-6, which is like COMP-3 but with no
3785 // sign nybble. The code in type_capacity assumes a sign
3786 // nybble.
3787 $field->data.capacity = ($field->data.digits+1)/2;
3788 }
3789 }
3790
3791 // Check COMP-5 capacity
3792 // No capacity means no PICTURE, valid only for a (potential) group
3793 if( $field->type == FldNumericBin5 && $field->data.capacity == 0 ) {
3794 if( has_clause ($data_clauses, usage_clause_e) &&
3795 !has_clause ($data_clauses, picture_clause_e) ) {
3796 // invalidate until a child is born
3797 $field->type = FldInvalid;
3798 }
3799 }
3800
3801 // Ensure signed initial VALUE is for signed numeric type
3802 if( is_numeric($field) &&
3803 $field->data.initial &&
3804 $field->type != FldFloat )
3805 {
3806 switch( $field->data.initial[0] ) {
3807 case '-':
3808 if( !$field->has_attr(signable_e) ) {
3809 error_msg(@field, "%s is unsigned but has signed VALUE '%s'",
3810 $field->name, $field->data.initial);
3811 }
3812 }
3813 }
3814
3815 // Verify VALUE
3816 $field->report_invalid_initial_value(@data_clauses);
3817
3818 // verify REDEFINES
3819 const auto parent = parent_of($field);
3820 if( parent && $field->level == parent->level ) {
3821 valid_redefine(@field, $field, parent); // calls yyerror
3822 }
3823 }
3824 ;
3825
3826 literalism: LITERAL { $$ = $1; }
3827 | literalism[first] '&' LITERAL[second]
3828 {
3829 $$ = $first;
3830 literal_t& output($$);
3831
3832 output.len += $second.len;
3833 output.data = reinterpret_cast<char*>(xrealloc(output.data,
3834 output.len + 1));
3835 memcpy( output.data + $first.len, $second.data, $second.len );
3836 output.data[output.len] = '\0';
3837
3838 if( $second.prefix[0] ) { strcpy(output.prefix, $second.prefix); }
3839 if( ! $first.compatible_prefix($second) ) {
3840 yywarn("dissimilar literals, '%s' prevails",
3841 output.prefix);
3842 }
3843 }
3844 ;
3845
3846 name66: LEVEL66 NAME[alias]
3847 {
3848 build_symbol_map();
3849 if( ! symbol_field_alias_begin() ) {
3850 error_msg(@alias, "no Level 01 record exists "
3851 "for %s to redefine", $alias);
3852 }
3853 $$ = $alias;
3854 }
3855 ;
3856
3857 data_clauses: data_clause
3858 {
3859 if( $data_clause == redefines_clause_e ) {
3860 auto parent = parent_of(current_field());
3861 if( !parent ) {
3862 error_msg(@1, "%s invalid REDEFINES",
3863 current_field()->name);
3864 YYERROR;
3865 }
3866 if( parent->occurs.ntimes() > 0 ) {
3867 error_msg(@1, "%s cannot REDEFINE table %s",
3868 current_field()->name,
3869 parent->name);
3870 YYERROR;
3871 }
3872 }
3873 }
3874 | data_clauses data_clause {
3875 const char *clause = "data";
3876 switch($2) {
3877 case occurs_clause_e: clause = "OCCURS"; break;
3878 case picture_clause_e: clause = "PIC"; break;
3879 case usage_clause_e: clause = "USAGE"; break;
3880 case value_clause_e: clause = "VALUE"; break;
3881 case global_clause_e: clause = "GLOBAL"; break;
3882 case external_clause_e: clause = "EXTERNAL"; break;
3883 case justified_clause_e: clause = "JUSTIFIED"; break;
3884 case redefines_clause_e: clause = "REDEFINES"; break;
3885 case blank_zero_clause_e: clause = "BLANK WHEN ZERO"; break;
3886 case synched_clause_e: clause = "SYNCHRONIZED"; break;
3887 case sign_clause_e: clause = "SIGN"; break;
3888 case based_clause_e: clause = "BASED"; break;
3889 case same_clause_e: clause = "SAME AS"; break;
3890 case volatile_clause_e: clause = "VOLATILE"; break;
3891 case type_clause_e: clause = "TYPE"; break;
3892 case typedef_clause_e: clause = "TYPEDEF"; break;
3893 }
3894 if( ($$ & $2) == $2 ) {
3895 error_msg(@2, "%s clause repeated", clause);
3896 YYERROR;
3897 }
3898
3899 if( $data_clause == redefines_clause_e ) {
3900 error_msg(@2, "REDEFINES must appear "
3901 "immediately after LEVEL and NAME");
3902 YYERROR;
3903 }
3904 cbl_field_t *field = current_field();
3905 const int globex = (global_e | external_e);
3906 if( (($$ | $2) & globex) == globex ) {
3907 error_msg(@2, "GLOBAL and EXTERNAL specified");
3908 YYERROR;
3909 }
3910
3911 $$ |= $2;
3912
3913 // If any implied TYPE bits are on in addition to
3914 // type_clause_e, they're in conflict.
3915 static const uint64_t type_implies =
3916 // ALIGNED clause not implemented
3917 blank_zero_clause_e | justified_clause_e | picture_clause_e
3918 | sign_clause_e | synched_clause_e | usage_clause_e;
3919
3920 if( type_clause_e < ($$ & (type_clause_e | type_implies)) ) {
3921 if( $2 == type_clause_e ) {
3922 error_msg(@2, "TYPE TO incompatible with ALIGNED, "
3923 "BLANK WHEN ZERO, JUSTIFIED, PICTURE, SIGN, "
3924 "SYNCHRONIZED, and USAGE");
3925 } else {
3926 error_msg(@2, "%s incompatible with TYPE TO", clause);
3927 }
3928 YYERROR;
3929 }
3930
3931 if( ($$ & same_clause_e) == same_clause_e ) {
3932 if( 0 < ($$ & ~same_clause_e) ) {
3933 error_msg(@2, "%s %s SAME AS "
3934 "precludes other DATA DIVISION clauses",
3935 field->level_str(), field->name);
3936 YYERROR;
3937 }
3938 }
3939
3940 if( is_numeric(field->type) && field->type != FldNumericDisplay ) {
3941 if( $$ & sign_clause_e ) {
3942 error_msg(@2, "%s is binary NUMERIC type, "
3943 "incompatible with SIGN IS", field->name);
3944 }
3945 }
3946
3947 if( gcobol_feature_embiggen() ) {
3948 if( field->is_binary_integer() && field->data.capacity == 4) {
3949 auto redefined = symbol_redefines(field);
3950 if( redefined && redefined->type == FldPointer ) {
3951 if( yydebug ) {
3952 yywarn("expanding %s size from %u bytes to %wd "
3953 "because it redefines %s with %<USAGE POINTER%>",
3954 field->name, field->size(),
3955 int_size_in_bytes(ptr_type_node),
3956 redefined->name);
3957 }
3958 field->embiggen();
3959 }
3960 }
3961 }
3962
3963 switch( field->type ) {
3964 case FldFloat:
3965 if( ($$ & picture_clause_e) == picture_clause_e ) {
3966 error_msg(@2, "%s: FLOAT types do not allow PICTURE",
3967 field->name);
3968 }
3969 break;
3970 default:
3971 break;
3972 }
3973
3974 if( ! field->is_justifiable() ) {
3975 error_msg(@2, "%s: %s is incompatible with JUSTIFIED",
3976 field->name, 3 + cbl_field_type_str(field->type));
3977 }
3978 }
3979 ;
3980
3981 data_clause: any_length { $$ = any_length_e; }
3982 | based_clause { $$ = based_clause_e; }
3983 | blank_zero_clause { $$ = blank_zero_clause_e; }
3984 | external_clause { $$ = external_clause_e; }
3985 | global_clause { $$ = global_clause_e; }
3986 | justified_clause { $$ = justified_clause_e; }
3987 | occurs_clause { $$ = occurs_clause_e;
3988 cbl_field_t *field = current_field();
3989 switch( field->level ) {
3990 case 1:
3991 if( dialect_mf() ) break;
3992 __attribute__((fallthrough));
3993 case 77:
3994 case 88:
3995 error_msg(@$, "%s %s: invalid LEVEL for OCCURS",
3996 field->level_str(), field->name );
3997 break;
3998 default:
3999 assert( field->parent > 0 );
4000 }
4001 }
4002 | picture_clause { $$ = picture_clause_e; }
4003 | redefines_clause { $$ = redefines_clause_e; }
4004 | same_clause { $$ = same_clause_e; }
4005 | sign_clause { $$ = sign_clause_e; }
4006 | synched_clause { $$ = synched_clause_e; }
4007 | type_clause { $$ = type_clause_e; }
4008 | typedef_clause { $$ = typedef_clause_e; }
4009 | usage_clause { $$ = usage_clause_e; }
4010 | value_clause { $$ = value_clause_e;
4011 cbl_field_t *field = current_field();
4012
4013 if( field->type != FldAlphanumeric &&
4014 field->data.initial && field->data.initial[0] )
4015 {
4016 // Embedded NULs are valid only in FldAlphanumeric, and are
4017 // already handled.
4018 if( strlen(field->data.initial) < field->data.capacity ) {
4019 auto p = blank_pad_initial( field->data.initial,
4020 strlen(field->data.initial),
4021 field->data.capacity );
4022 if( !p ) YYERROR;
4023 field->data.initial = p;
4024 }
4025 }
4026 const cbl_field_t *parent;
4027 if( (parent = parent_has_value(field)) != NULL ) {
4028 error_msg(@1, "VALUE invalid because group %s has VALUE clause",
4029 parent->name);
4030 }
4031 }
4032 | volatile_clause { $$ = volatile_clause_e; }
4033 ;
4034
4035 picture_clause: PIC signed nps[fore] nines nps[aft]
4036 {
4037 cbl_field_t *field = current_field();
4038 if( !field_type_update(field, FldNumericDisplay, @$) ) {
4039 YYERROR;
4040 }
4041 ERROR_IF_CAPACITY(@PIC, field);
4042 field->attr |= $signed;
4043 field->data.capacity = type_capacity(field->type, $4);
4044 field->data.digits = $4;
4045 if( long(field->data.digits) != $4 ) {
4046 error_msg(@2, "indicated size would be %d bytes, "
4047 "maximum data item size is %u",
4048 $4, UINT32_MAX);
4049 }
4050
4051 if( $fore && $aft ) { // leading and trailing P's
4052 error_msg(@2, "PIC cannot have both leading and trailing P");
4053 YYERROR;
4054 }
4055 if( $fore || $aft ) {
4056 field->attr |= scaled_e;
4057 field->data.rdigits = $fore? $fore : -$aft;
4058 }
4059 if( ! field->reasonable_capacity() ) {
4060 error_msg(@2, "%s limited to capacity of %d (would need %u)",
4061 field->name, MAX_FIXED_POINT_DIGITS, field->data.capacity);
4062 }
4063 }
4064
4065 | PIC signed NINEV[left] nine[rdigits]
4066 {
4067 cbl_field_t *field = current_field();
4068 field->data.digits = $left + $rdigits;
4069
4070 if( field->is_binary_integer() ) {
4071 field->data.capacity = type_capacity(field->type,
4072 field->data.digits);
4073 } else {
4074 if( !field_type_update(field, FldNumericDisplay, @$) ) {
4075 YYERROR;
4076 }
4077 ERROR_IF_CAPACITY(@PIC, field);
4078 field->attr |= $signed;
4079 field->data.capacity = field->data.digits;
4080 field->data.rdigits = $rdigits;
4081 }
4082 if( ! field->reasonable_capacity() ) {
4083 error_msg(@2, "%s limited to capacity of %d (would need %u)",
4084 field->name, MAX_FIXED_POINT_DIGITS, field->data.capacity);
4085 }
4086 }
4087 | PIC signed NINEDOT[left] nine[rdigits]
4088 {
4089 uint32_t size = $left + $rdigits;
4090
4091 cbl_field_t *field = current_field();
4092 if( !field_type_update(field, FldNumericEdited, @$) ) {
4093 YYERROR;
4094 }
4095 ERROR_IF_CAPACITY(@PIC, field);
4096 field->attr |= $signed;
4097 field->data.digits = size;
4098 field->data.capacity = ++size;
4099 field->data.rdigits = $rdigits;
4100
4101 if( ! field->reasonable_capacity() ) {
4102 error_msg(@2, "%s limited to capacity of %d (would need %u)",
4103 field->name, MAX_FIXED_POINT_DIGITS, field->data.capacity);
4104 }
4105 }
4106
4107 | PIC alphanum_pic[size]
4108 {
4109 cbl_field_t *field = current_field();
4110
4111 if( field->type == FldNumericBin5 &&
4112 field->data.capacity == 0xFF &&
4113 (dialect_gnu() || dialect_mf()) )
4114 { // PIC X COMP-X or COMP-9
4115 if( ! field->has_attr(all_x_e) ) {
4116 error_msg(@2, "COMP PICTURE requires all X%'s or all 9%'s");
4117 YYERROR;
4118 }
4119 } else {
4120 if( !field_type_update(field, FldAlphanumeric, @$) ) {
4121 dbgmsg("alnum_pic: %s", field_str(field));
4122 YYERROR;
4123 }
4124 }
4125 assert(0 < $size);
4126 if( field->data.initial != NULL ) {
4127 if( 0 < field->data.capacity &&
4128 field->data.capacity < uint32_t($size) ) {
4129 auto p = blank_pad_initial( field->data.initial,
4130 field->data.capacity, $size );
4131 if( !p ) YYERROR;
4132 field->data.initial = p;
4133 }
4134 }
4135
4136 field->data.capacity = $size;
4137 field->data.picture = NULL;
4138
4139 if( false ) dbgmsg("PIC alphanum_pic[size]:%d: %s",
4140 field->line, field_str(field));
4141 }
4142
4143 | PIC numed[picture]
4144 {
4145 cbl_field_t *field = current_field();
4146 if( !field_type_update(field, FldNumericEdited, @$) ) {
4147 YYERROR;
4148 }
4149 ERROR_IF_CAPACITY(@PIC, field);
4150 if( !is_numeric_edited($picture) ) {
4151 error_msg(@picture, "%s", numed_message);
4152 YYERROR;
4153 }
4154 field->data.picture = $picture;
4155 field->data.capacity = length_of_picture($picture);
4156 field->data.digits = digits_of_picture($picture, false);
4157 field->data.rdigits = rdigits_of_picture($picture);
4158 if( is_picture_scaled($picture) ) field->attr |= scaled_e;
4159 }
4160
4161 | PIC ALPHED[picture]
4162 {
4163 bool is_alpha_edited( const char picture[] );
4164
4165 cbl_field_t *field = current_field();
4166 ERROR_IF_CAPACITY(@PIC, field);
4167 field->data.capacity = length_of_picture($picture);
4168 field->data.picture = $picture;
4169
4170 // In case the lexer guesses wrong.
4171 cbl_field_type_t type = is_numeric_edited($picture)?
4172 FldNumericEdited : FldAlphaEdited;
4173 if( !field_type_update(field, type, @$) ) {
4174 YYERROR;
4175 }
4176
4177 switch( type ) {
4178 case FldNumericEdited:
4179 field->data.digits = digits_of_picture($picture, false);
4180 field->data.rdigits = rdigits_of_picture($picture);
4181 if( is_picture_scaled($picture) ) field->attr |= scaled_e;
4182 break;
4183 case FldAlphaEdited:
4184 if( !is_alpha_edited(field->data.picture) ) {
4185 error_msg(@picture, "invalid picture for Alphanumeric-edited");
4186 YYERROR;
4187 }
4188 break;
4189 default:
4190 gcc_unreachable();
4191 }
4192 }
4193 | PIC ones
4194 ;
4195 ones: ONES
4196 {
4197 cbl_unimplemented("Boolean type not implemented");
4198 }
4199 ;
4200
4201 alphanum_pic: alphanum_part {
4202 current_field()->set_attr($1.attr);
4203 $$ = $1.nbyte;
4204 }
4205 | alphanum_pic alphanum_part
4206 {
4207 auto field = current_field();
4208 dbgmsg("%s has %s against %s",
4209 field->name, field_attr_str(field),
4210 cbl_field_attr_str($2.attr));
4211
4212 if( ! field->has_attr($2.attr) ) {
4213 field->clear_attr(all_ax_e); // clears 2 bits
4214 }
4215 $$ += $2.nbyte;
4216
4217 dbgmsg("%s attrs: %s", field->name, field_attr_str(field));
4218 }
4219 ;
4220 alphanum_part: ALNUM[picture] count
4221 {
4222 $$.attr = uniform_picture($picture);
4223 $$.nbyte = strlen($picture);
4224 auto count($count);
4225 if( count > 0 ) {
4226 --count;
4227 $$.nbyte += count; // AX9(3) has count 5
4228 }
4229 if( count < 0 ) {
4230 error_msg(@2, "PICTURE count %<(%d)%> is negative", count );
4231 YYERROR;
4232 }
4233 }
4234 ;
4235
4236 signed: %empty { $$ = 0; }
4237 | 'S' { $$ = signable_e; }
4238 ;
4239
4240 nps: %empty { $$ = 0; }
4241 | PIC_P { $$ = $1; }
4242 ;
4243
4244 nine: %empty { $$ = 0; }
4245 | nines
4246 {
4247 $$ = $1;
4248 if( $$ == 0 ) {
4249 error_msg(@1, "%<(0)%> invalid in PICTURE (ISO 2023 13.18.40.3)");
4250 }
4251 }
4252 ;
4253 nines: NINES
4254 | nines NINES { $$ = $1 + $2; }
4255 ;
4256
4257 count: %empty { $$ = 0; }
4258 | '(' NUMSTR ')'
4259 {
4260 REAL_VALUE_TYPE rn = numstr2i($NUMSTR.string, $NUMSTR.radix);
4261 $$ = real_to_integer (&rn);
4262 if( $$ == 0 ) {
4263 error_msg(@2, "%<0%> invalid in PICTURE (ISO 2023 13.18.40.3)");
4264 }
4265 }
4266 | '(' NAME ')'
4267 {
4268 auto value = cdf_value($NAME);
4269 if( ! (value && value->is_numeric()) ) {
4270 error_msg(@NAME, "PICTURE %qs requires a CONSTANT value", $NAME );
4271 YYERROR;
4272 }
4273 int nmsg = 0;
4274 auto e = symbol_field(PROGRAM, 0, $NAME);
4275 if( e ) { // verify not floating point with nonzero fraction
4276 auto field = cbl_field_of(e);
4277 assert(is_literal(field));
4278 REAL_VALUE_TYPE vi;
4279 real_from_integer (&vi, VOIDmode, field->as_integer(), SIGNED);
4280 if( !real_identical (TREE_REAL_CST_PTR (field->data.value_of()),
4281 &vi) ) {
4282 nmsg++;
4283 error_msg(@NAME, "invalid PICTURE count %<(%s)%>",
4284 field->data.initial );
4285 }
4286 }
4287 $$ = value->as_number();
4288 if( $$ <= 0 && !nmsg) {
4289 error_msg(@NAME, "invalid PICTURE count %<(%s)%>", $NAME );
4290 }
4291 }
4292 ;
4293
4294 numed: NUMED
4295 | NUMED_CR
4296 | NUMED_DB
4297 ;
4298
4299 usage_clause: usage_clause1[type]
4300 {
4301 cbl_field_t *field = current_field();
4302 cbl_field_type_t type = static_cast<cbl_field_type_t>($type);
4303 if( ! field_type_update(field, type, @$, true) ) {
4304 YYERROR;
4305 }
4306 }
4307 ;
4308 usage_clause1: usage BIT
4309 {
4310 cbl_unimplemented("Boolean type not implemented");
4311 }
4312 | usage BINARY_INTEGER [comp] is_signed
4313 {
4314 // action for BINARY_INTEGER is repeated for COMPUTATIONAL, below.
4315 // If it changes, consolidate in a function.
4316 bool infer = true;
4317 cbl_field_t *field = current_field();
4318
4319 if( ! $is_signed ) {
4320 $comp.signable = false;
4321 }
4322
4323 // Some binary types have defined capacity;
4324 switch($comp.type) {
4325 // COMPUTATIONAL and COMP-5 rely on PICTURE.
4326 case FldNumericBinary:
4327 field->attr |= big_endian_e;
4328 __attribute__((fallthrough));
4329 case FldNumericBin5:
4330 // If no capacity yet, then no picture, infer $comp.capacity.
4331 // If field has capacity, ensure USAGE is compatible.
4332 if( field->data.capacity > 0 ) { // PICTURE before USAGE
4333 infer = false;
4334 switch( field->type ) {
4335 case FldAlphanumeric: // PIC X COMP-5 or COMP-X
4336 assert( field->data.digits == 0 );
4337 assert( field->data.rdigits == 0 );
4338 if( (dialect_mf() || dialect_gnu()) ) {
4339 field->type = $comp.type;
4340 field->clear_attr(signable_e);
4341 } else {
4342 error_msg(@comp, "numeric USAGE invalid "
4343 "with Alpnanumeric PICTURE");
4344 dialect_error(@1, "Alpnanumeric COMP-5 or COMP-X", "mf or gnu");
4345 YYERROR;
4346 }
4347 break;
4348 case FldNumericDisplay: // PIC 9 COMP-5 or COMP-X
4349 if( $comp.capacity == 0xFF ) { // comp-x is a bit like comp-5
4350 assert( field->data.digits == field->data.capacity );
4351 if( ! (dialect_mf() || dialect_gnu()) ) {
4352 dialect_error(@1, "COMP-X", "mf or gnu");
4353 }
4354 }
4355 field->type = $comp.type;
4356 field->data.capacity = type_capacity(field->type,
4357 field->data.digits);
4358 break;
4359 default: break;
4360 }
4361 }
4362 break;
4363 case FldPacked: // comp-6 is unsigned comp-3
4364 assert(! $comp.signable); // else PACKED_DECIMAL from scanner
4365 field->attr |= separate_e;
4366 if( ! dialect_mf() ) {
4367 dialect_error(@1, "COMP-6", "mf");
4368 }
4369 if( field->type == FldNumericDisplay ) {// PICTURE before USAGE
4370 infer = false;
4371 assert(field->data.capacity > 0);
4372 field->type = $comp.type;
4373 field->data.capacity = type_capacity(field->type,
4374 field->data.digits);
4375 }
4376 break;
4377 default:
4378 break;
4379 }
4380
4381 if( infer ) {
4382 if( $comp.capacity > 0 ) {
4383 if( field->data.capacity > 0 ) {
4384 error_msg(@comp, "%s is BINARY type, incompatible with PICTURE",
4385 field->name);
4386 YYERROR;
4387 }
4388 field->data.capacity = $comp.capacity;
4389 field->type = $comp.type;
4390 if( $comp.signable ) {
4391 field->attr = (field->attr | signable_e);
4392 }
4393 }
4394 }
4395 $$ = $comp.type;
4396 }
4397 | usage COMPUTATIONAL[comp] native
4398 {
4399 // logic below duplicates BINARY_INTEGER, above.
4400 // If it changes, consolidate in a function.
4401 bool infer = true;
4402 cbl_field_t *field = current_field();
4403
4404 // Some binary types have defined capacity;
4405 switch($comp.type) {
4406 // COMPUTATIONAL and COMP-5 rely on PICTURE.
4407 case FldNumericBinary:
4408 field->attr |= big_endian_e;
4409 __attribute__((fallthrough));
4410 case FldNumericBin5:
4411 // If no capacity yet, then no picture, infer $comp.capacity.
4412 // If field has capacity, ensure USAGE is compatible.
4413 if( field->data.capacity > 0 ) { // PICTURE before USAGE
4414 infer = false;
4415 switch( field->type ) {
4416 case FldAlphanumeric: // PIC X COMP-5 or COMP-X
4417 assert( field->data.digits == 0 );
4418 assert( field->data.rdigits == 0 );
4419 if( (dialect_mf() || dialect_gnu()) ) {
4420 field->type = $comp.type;
4421 field->clear_attr(signable_e);
4422 } else {
4423 error_msg(@comp, "numeric USAGE invalid "
4424 "with Alpnanumeric PICTURE");
4425 dialect_error(@1, "Alpnanumeric COMP-5 or COMP-X", "mf or gnu");
4426 YYERROR;
4427 }
4428 break;
4429 case FldNumericDisplay: // PIC 9 COMP-5 or COMP-X
4430 if( $comp.capacity == 0xFF ) { // comp-x is a bit like comp-5
4431 assert( field->data.digits == field->data.capacity );
4432 if( ! (dialect_mf() || dialect_gnu()) ) {
4433 dialect_error(@1, "COMP-X", "mf or gnu");
4434 }
4435 }
4436 field->type = $comp.type;
4437 field->data.capacity = type_capacity(field->type,
4438 field->data.digits);
4439 break;
4440 default: break;
4441 }
4442 }
4443 break;
4444 case FldPacked: // comp-6 is unsigned comp-3
4445 assert(! $comp.signable); // else PACKED_DECIMAL from scanner
4446 field->attr |= separate_e;
4447 if( ! dialect_mf() ) {
4448 dialect_error(@1, "COMP-6", "mf");
4449 }
4450 if( field->type == FldNumericDisplay ) {// PICTURE before USAGE
4451 infer = false;
4452 assert(field->data.capacity > 0);
4453 field->type = $comp.type;
4454 field->data.capacity = type_capacity(field->type,
4455 field->data.digits);
4456 }
4457 break;
4458 default:
4459 break;
4460 }
4461
4462 if( infer ) {
4463 if( $comp.capacity > 0 ) {
4464 if( field->data.capacity > 0 ) {
4465 error_msg(@comp, "%s is BINARY type, incompatible with PICTURE",
4466 field->name);
4467 YYERROR;
4468 }
4469 field->data.capacity = $comp.capacity;
4470 field->type = $comp.type;
4471 if( $comp.signable ) {
4472 field->attr = (field->attr | signable_e);
4473 }
4474 }
4475 }
4476 $$ = $comp.type;
4477 }
4478 | usage DISPLAY native { $$ = FldDisplay; }
4479 | usage PACKED_DECIMAL native { $$ = FldPacked; }
4480 | usage PACKED_DECIMAL with NO SIGN
4481 {
4482 cbl_field_t *field = current_field();
4483 if( field->data.capacity > 0 &&
4484 field->type != FldNumericDisplay) {
4485 error_msg(@2, "%s PICTURE is incompatible with USAGE PACKED DECIMAL",
4486 field->name);
4487 YYERROR;
4488 }
4489 field->clear_attr(separate_e);
4490 field->clear_attr(signable_e);
4491 if( field->type == FldNumericDisplay ) {// PICTURE before USAGE
4492 assert(field->data.capacity > 0);
4493 field->data.capacity = type_capacity(FldPacked,
4494 field->data.digits);
4495 }
4496 $$ = field->type = FldPacked;
4497 }
4498 | usage INDEX {
4499 $$ = symbol_field_index_set( current_field() )->type;
4500 }
4501 // We should enforce data/code pointers with a different type.
4502 | usage POINTER
4503 {
4504 $$ = FldPointer;
4505 auto field = current_field();
4506 auto redefined = symbol_redefines(field);
4507
4508 if( $POINTER ) {
4509 field->set_attr($POINTER);
4510 }
4511 if( gcobol_feature_embiggen() && redefined &&
4512 is_numeric(redefined->type) && redefined->size() == 4) {
4513 // For now, we allow POINTER to expand a 32-bit item to 64 bits.
4514 field->data.capacity = int_size_in_bytes(ptr_type_node);
4515 dbgmsg("%s: expanding #" HOST_SIZE_T_PRINT_UNSIGNED
4516 " %s capacity %u => %u", __func__,
4517 (fmt_size_t)field_index(redefined), redefined->name,
4518 redefined->data.capacity, field->data.capacity);
4519
4520 redefined->embiggen();
4521
4522 if( redefined->data.initial ) {
4523 auto s = xasprintf( "%s ", redefined->data.initial);
4524 std::replace(s, s + strlen(s), '!', char(0x20));
4525 redefined->data.initial = s;
4526 }
4527 }
4528 }
4529 | usage POINTER TO error
4530 {
4531 cbl_unimplemented("POINTER TO");
4532 $$ = FldPointer;
4533 }
4534 ;
4535
4536 value_clause: VALUE all LITERAL[lit] {
4537 cbl_field_t *field = current_field();
4538 field->data.initial = $lit.data;
4539 field->attr |= literal_attr($lit.prefix);
4540 // The __gg__initialize_data routine needs to know that VALUE is a
4541 // quoted literal. This is critical for NumericEdited variables
4542 field->attr |= quoted_e;
4543
4544 if( field->data.capacity == 0 ) {
4545 field->data.capacity = $lit.len;
4546 } else {
4547 if( $all ) {
4548 field_value_all(field);
4549 } else {
4550 if( $lit.len < field->data.capacity ) {
4551 auto p = blank_pad_initial( $lit.data, $lit.len,
4552 field->data.capacity );
4553 if( !p ) YYERROR;
4554 field->data.initial = p;
4555 }
4556 }
4557 }
4558 value_encoding_check(@lit, field);
4559 }
4560 | VALUE all cce_expr[value] {
4561 cbl_field_t *field = current_field();
4562 auto orig_str = original_number();
4563 REAL_VALUE_TYPE orig_val;
4564 real_from_string3 (&orig_val, orig_str,
4565 TYPE_MODE (float128_type_node));
4566 char *initial = NULL;
4567
4568 if( real_identical (&orig_val, &$value) ) {
4569 initial = orig_str;
4570 pristine_values.insert(initial);
4571 } else {
4572 initial = string_of($value);
4573 gcc_assert(initial);
4574 }
4575
4576 char decimal = symbol_decimal_point();
4577 std::replace(initial, initial + strlen(initial), '.', decimal);
4578
4579 field->data.initial = initial;
4580 field->data = build_real (float128_type_node, $value);
4581
4582 if( $all ) field_value_all(field);
4583 }
4584 | VALUE all reserved_value[value]
4585 {
4586 if( $value != NULLS ) {
4587 auto fig = constant_of(constant_index($value));
4588 current_field()->data.initial = fig->data.initial;
4589 }
4590 }
4591 | /* VALUE is */ NULLPTR
4592 {
4593 auto fig = constant_of(constant_index(NULLS));
4594 current_field()->data.initial = fig->data.initial;
4595 }
4596 | VALUE error
4597 {
4598 error_msg(@2, "no valid VALUE supplied");
4599 }
4600 ;
4601
4602 global_clause: is GLOBAL
4603 {
4604 cbl_field_t *field = current_field();
4605 field->attr |= (field->attr | global_e);
4606 }
4607 ;
4608 external_clause: is EXTERNAL
4609 {
4610 cbl_field_t *field = current_field();
4611 field->attr |= (field->attr | external_e);
4612 }
4613 ;
4614
4615 justified_clause: is JUSTIFIED
4616 {
4617 cbl_field_t *field = current_field();
4618 field->attr |= rjust_e;
4619 }
4620 ;
4621
4622 redefines_clause: REDEFINES NAME[orig]
4623 {
4624 struct symbol_elem_t *e = symbol_field(PROGRAM, 0, $orig);
4625 if( !e ) {
4626 error_msg(@2, "REDEFINES target not defined");
4627 YYERROR;
4628 }
4629 cbl_field_t *field = current_field();
4630 cbl_field_t *orig = cbl_field_of(e);
4631 if( orig->has_attr(filler_e) ) {
4632 error_msg(@2, "%s may not REDEFINE %s",
4633 field->name, orig->name);
4634 }
4635 cbl_field_t *super = symbol_redefines(orig);
4636 if( super ) {
4637 error_msg(@2, "%s may not REDEFINE %s, "
4638 "which redefines %s",
4639 field->name, orig->name, super->name);
4640 }
4641 if( field->level != orig->level ) {
4642 error_msg(@2, "cannot redefine %s %s as %s %s "
4643 "because they have different levels",
4644 orig->level_str(), name_of(orig),
4645 field->level_str(), name_of(field));
4646 }
4647 // ISO 13.18.44.3
4648 auto parent( symbol_index(e) );
4649 auto p = std::find_if( symbol_elem_of(orig) + 1,
4650 symbol_elem_of(field),
4651 [parent, level = field->level]( const auto& elem ) {
4652 if( elem.type == SymField ) {
4653 auto f = cbl_field_of(&elem);
4654 return
4655 f->level == level &&
4656 f->parent != parent;
4657 }
4658 return false;
4659 } );
4660 if( p != symbol_elem_of(field) ) {
4661 auto mid( cbl_field_of(p) );
4662 error_msg(@2, "cannot redefine %s %s as %s %s "
4663 "because %s %s intervenes",
4664 orig->level_str(), name_of(orig),
4665 field->level_str(), name_of(field),
4666 mid->level_str(), name_of(mid));
4667 }
4668
4669 if( valid_redefine(@2, field, orig) ) {
4670 /*
4671 * Defer "inheriting" the parent's description until the
4672 * redefine is complete.
4673 */
4674 current_field()->parent = symbol_index(e);
4675 }
4676 }
4677 ;
4678
4679 any_length: ANY LENGTH
4680 { cbl_field_t *field = current_field();
4681 if( field->attr & any_length_e ) {
4682 error_msg(@1, "ANY LENGTH already set");
4683 }
4684 if( ! (field->level == 1 &&
4685 current_data_section == linkage_datasect_e &&
4686 (1 < current.program_level() ||
4687 current.program()->is_function())) ) {
4688 error_msg(@1, "ANY LENGTH valid only for 01 "
4689 "in LINKAGE SECTION of a function or contained program");
4690 YYERROR;
4691 }
4692 field->attr |= any_length_e;
4693 }
4694 ;
4695
4696 based_clause: BASED
4697 { cbl_field_t *field = current_field();
4698 if( field->attr & based_e ) {
4699 error_msg(@1, "BASED already set");
4700 }
4701 field->attr |= based_e;
4702 }
4703 ;
4704
4705 blank_zero_clause: blank_when_zero
4706 { cbl_field_t *field = current_field();
4707 // the BLANK WHEN ZERO clause defines the item as numeric-edited.
4708 if( !field_type_update(field, FldNumericEdited, @1) ) {
4709 YYERROR;
4710 }
4711 field->attr |= blank_zero_e;
4712 }
4713 ;
4714 blank_when_zero:
4715 BLANK WHEN ZERO
4716 | BLANK ZERO
4717 ;
4718
4719 synched_clause: SYNCHRONIZED
4720 | SYNCHRONIZED LEFT
4721 | SYNCHRONIZED RIGHT
4722 ;
4723
4724 same_clause: SAME AS name
4725 {
4726 cbl_field_t *field = current_field(), *other = $name;
4727 if( other->occurs.ntimes() > 0 ) {
4728 error_msg(@name, "SAME AS %s: cannot have OCCURS",
4729 other->name); // 13.18.49.2,P5
4730 YYERROR;
4731 }
4732 if( field->level == 77 and !is_elementary(other->type) ) {
4733 // ISO 2023 13.18.49.2,P8
4734 error_msg(@name, "%s %s SAME AS %s: must be elementary",
4735 field->level_str(), field->name, other->name);
4736 YYERROR;
4737 }
4738
4739 if( (other->attr & (sign_clause_e | usage_clause_e)) > 0 ) {
4740 error_msg(@name, "%s: source of SAME AS cannot have "
4741 "SIGN or USAGE clause", other->name);
4742 YYERROR;
4743 }
4744 if( other->usage == FldGroup ) {
4745 error_msg(@name, "%s: source of SAME AS cannot have "
4746 "GROUP-USAGE clause", other->name);
4747 YYERROR;
4748 }
4749 if( other->has_attr(constant_e ) ) {
4750 error_msg(@name, "%s: source of SAME AS cannot "
4751 "be constant", other->name);
4752 YYERROR;
4753 }
4754 if( field->parent == field_index(other) ) {
4755 error_msg(@name, "%s: SAME AS uses "
4756 "its own parent %s", field->name, other->name);
4757 YYERROR;
4758 }
4759
4760 const auto e = symbol_field_same_as( field, other );
4761 symbol_field_location( symbol_index(e), @name );
4762 }
4763 ;
4764
4765 sign_clause: sign_is sign_leading sign_separate
4766 {
4767 cbl_field_t *field = current_field();
4768 if( $sign_leading ) {
4769 field->attr |= leading_e;
4770 } else {
4771 field->attr &= ~uint64_t(leading_e); // turn off in case inherited
4772 field->attr |= signable_e;
4773 }
4774 if( $sign_separate ) field->attr |= separate_e;
4775 }
4776 ;
4777 sign_is: %empty
4778 | SIGN is
4779 ;
4780 sign_leading: LEADING { $$ = true; }
4781 | TRAILING { $$ = false; }
4782 ;
4783 sign_separate: %empty { $$ = false; }
4784 | SEPARATE CHARACTER { $$ = true; }
4785 | SEPARATE { $$ = true; }
4786 ;
4787
4788 /*
4789 * "The effect of the TYPE clause is as though the data description identified
4790 * by type-name-1 had been coded in place of the TYPE clause, excluding the
4791 * level-number, name, alignment, and the GLOBAL, SELECT WHEN, and TYPEDEF
4792 * clauses specified for type-name-1;"
4793 *
4794 * The essential characteristics of a type, which is identified by its
4795 * type-name, are the:
4796 * — relative positions and lengths of the elementary items
4797 * — ALIGNED clause
4798 * — BLANK WHEN ZERO clause
4799 * — JUSTIFIED clause
4800 * — PICTURE clause
4801 * — SIGN clause
4802 * — SYNCHRONIZED clause
4803 * — USAGE clause
4804 */
4805 type_clause: TYPE to typename
4806 {
4807 cbl_field_t *field = current_field();
4808 if( $typename ) {
4809 const auto e = symbol_field_same_as(field, $typename);
4810 symbol_field_location( symbol_index(e), @typename );
4811 }
4812 }
4813 | USAGE is typename
4814 {
4815 if( ! dialect_mf() ) {
4816 dialect_error(@typename, "USAGE TYPENAME", "mf");
4817 YYERROR;
4818 }
4819 cbl_field_t *field = current_field();
4820 if( $typename ) {
4821 const auto e = symbol_field_same_as(field, $typename);
4822 symbol_field_location( symbol_index(e), @typename );
4823 }
4824 }
4825 ;
4826
4827 typedef_clause: is TYPEDEF strong
4828 {
4829 cbl_field_t *field = current_field();
4830 switch( field->level ) {
4831 case 1: case 77: break;
4832 default:
4833 error_msg(@2, "%s %s IS TYPEDEF must be level 01",
4834 field->level_str(), field->name);
4835 }
4836 field->attr |= typedef_e;
4837 if( $strong ) field->attr |= strongdef_e;
4838 if( ! current.typedef_add(field) ) {
4839 auto prior = current.has_typedef(field);
4840 assert(prior);
4841 error_msg(@2, "%s %s IS TYPEDEF is not unique "
4842 "(see %s, line %d)",
4843 field->level_str(), field->name,
4844 prior->name, prior->line);
4845 }
4846 }
4847 ;
4848
4849 volatile_clause:
4850 VOLATILE
4851 {
4852 if( dialect_ibm() ) {
4853 yywarn("VOLATILE has no effect");
4854 } else {
4855 dialect_error(@1, "VOLATILE", "ibm");
4856 }
4857 }
4858 ;
4859
4860 procedure_div: %empty {
4861 if( !procedure_division_ready(@$, NULL, NULL) ) YYABORT;
4862 }
4863 | PROCEDURE_DIV '.' {
4864 if( !procedure_division_ready(@$, NULL, NULL) ) YYABORT;
4865 } declaratives sentences
4866 | PROCEDURE_DIV procedure_args '.' declaratives sentences
4867 | PROCEDURE_DIV procedure_args '.'
4868 ;
4869
4870 procedure_args: USING procedure_uses[args]
4871 {
4872 if( !procedure_division_ready(@args, NULL, $args) ) YYABORT;
4873 }
4874 | USING procedure_uses[args] RETURNING name[ret]
4875 {
4876 if( !procedure_division_ready(@ret, $ret, $args) ) YYABORT;
4877 if( ! $ret->has_attr(linkage_e) ) {
4878 error_msg(@ret, "RETURNING %s is not defined in LINKAGE SECTION",
4879 $ret->name);
4880 }
4881 }
4882 | RETURNING name[ret]
4883 {
4884 if( !procedure_division_ready(@ret, $ret, NULL) ) YYABORT;
4885 if( ! $ret->has_attr(linkage_e) ) {
4886 error_msg(@ret, "RETURNING %s is not defined in LINKAGE SECTION",
4887 $ret->name);
4888 }
4889 }
4890 ;
4891 procedure_uses: procedure_use { $$ = new ffi_args_t($1); }
4892 | procedure_uses procedure_use { $$->push_back($2); }
4893 ;
4894 procedure_use: optional scalar {
4895 $$ = new cbl_ffi_arg_t(by_default_e, $scalar);
4896 $$->optional = $optional;
4897 $$->validate(); // produces message
4898 }
4899 | by REFERENCE optional scalar {
4900 $$ = new cbl_ffi_arg_t(by_reference_e, $scalar);
4901 $$->optional = $optional;
4902 $$->validate(); // produces message
4903 }
4904 | by CONTENT error { // no "by content" in procedure definition
4905 $$ = new cbl_ffi_arg_t(by_content_e,
4906 new_reference(literally_zero));
4907 }
4908 | by VALUE by_value_arg[arg] {
4909 $$ = new cbl_ffi_arg_t(by_value_e, $arg);
4910 $$->validate(); // produces message
4911 }
4912 ;
4913 by_value_arg: scalar
4914 | LITERAL { $$ = new_reference(new_literal($1, quoted_e)); }
4915 | reserved_value
4916 {
4917 $$ = new_reference(constant_of(constant_index($1)));
4918 }
4919 ;
4920
4921 declaratives: %empty
4922 | DECLARATIVES '.'
4923 <label>{
4924 cbl_enabled_exceptions_t& enabled_exceptions( cdf_enabled_exceptions() );
4925 current.enabled_exception_cache = enabled_exceptions;
4926 enabled_exceptions.clear();
4927 current.doing_declaratives(true);
4928 $$ = label_add(LblString, "_end_declaratives", 0);
4929 assert($$);
4930 parser_label_goto($$);
4931 } [label]
4932 sentences END DECLARATIVES '.'
4933 {
4934 current.doing_declaratives(false);
4935 /* TODO: if( intradeclarative_reference() ) yyerror;
4936 * Test also at paragraph_reference, for non-forward
4937 * reference with good line numbers. See
4938 * utilcc::procedures_t and ambiguous_reference(). At this
4939 * point, no reference should pick up anything except a
4940 * forward reference, because we haven't yet begun to parse
4941 * nondeclarative procedures.
4942 */
4943 cbl_enabled_exceptions_t& enabled_exceptions( cdf_enabled_exceptions() );
4944 parser_label_label($label);
4945 enabled_exceptions = current.enabled_exception_cache;
4946 current.enabled_exception_cache.clear();
4947 ast_enter_section(implicit_section());
4948 }
4949 ;
4950
4951 sentences: sentence {
4952 ast_first_statement(@1);
4953 symbol_temporaries_free();
4954 }
4955 | section_name
4956 | paragraph_name[para] '.'
4957 {
4958 location_set(@para);
4959 cbl_label_t *label = label_add(@para, LblParagraph, $para);
4960 if( !label ) {
4961 YYERROR;
4962 }
4963 ast_enter_paragraph(label);
4964 current.new_paragraph(label);
4965 apply_declaratives();
4966 }
4967 | sentences sentence
4968 { // sentences might not be sentence
4969 ast_first_statement(@2);
4970 symbol_temporaries_free();
4971 }
4972 | sentences section_name
4973 | sentences paragraph_name[para] '.'
4974 {
4975 location_set(@para);
4976 cbl_label_t *label = label_add(@para, LblParagraph, $para);
4977 if( !label ) {
4978 YYERROR;
4979 }
4980 ast_enter_paragraph(label);
4981 current.new_paragraph(label);
4982 apply_declaratives();
4983 }
4984 ;
4985 paragraph_name: NAME
4986 | NUMSTR { $$ = $1.string; }
4987 ;
4988
4989 sentence: statements '.'
4990 | statements YYEOF
4991 {
4992 if( ! goodnight_gracie() ) {
4993 YYABORT;
4994 }
4995 if( nparse_error > 0 ) YYABORT;
4996 YYACCEPT;
4997 }
4998 | program END_SUBPROGRAM namestr[name] '.'
4999 { // a contained program (no prior END PROGRAM) is a "sentence"
5000 const cbl_label_t *prog = current.program();
5001 assert(prog);
5002 const char *name = string_of($name);
5003 if( !name || 0 != strcasecmp(prog->name, name) ) {
5004 error_msg(@name, "END PROGRAM '%s' does not match PROGRAM-ID '%s'",
5005 name? name : $name.data, prog->name);
5006 YYERROR;
5007 }
5008
5009 std::set<std::string> externals = current.end_program();
5010 if( !externals.empty() ) {
5011 for( const auto& name : externals ) {
5012 yywarn("%s calls external symbol '%s'",
5013 prog->name, name.c_str());
5014 }
5015 YYERROR;
5016 }
5017 // pointer still valid because name is in symbol table
5018 ast_end_program(prog->name);
5019 }
5020 | program YYEOF
5021 { // a contained program (no prior END PROGRAM) is a "sentence"
5022 if( nparse_error > 0 ) YYABORT;
5023 do {
5024 if( ! goodnight_gracie() ) YYABORT; // no recovery
5025 } while( current.program_level() > 0 );
5026 YYACCEPT;
5027 }
5028 ;
5029
5030 statements: statement { $$ = $1; }
5031 | statements statement { $$ = $2; }
5032 ;
5033
5034 statement: error {
5035 if( current.declarative_section_name() ) {
5036 error_msg(@1, "missing END DECLARATIVES or SECTION name");
5037 YYABORT;
5038 }
5039 if( max_errors_exceeded(nparse_error) ) {
5040 error_msg(@1, "max errors %zu reached", nparse_error);
5041 YYABORT;
5042 }
5043 }
5044 | accept { $$ = ACCEPT; }
5045 | add { $$ = ADD; }
5046 | allocate { $$ = ALLOCATE; }
5047 | alter { $$ = ALTER; }
5048 | call { $$ = CALL; }
5049 | cancel { $$ = CANCEL; }
5050 | close { $$ = CLOSE; }
5051 | compute { $$ = COMPUTE; }
5052 | continue_stmt { $$ = CONTINUE; }
5053 | delete { $$ = DELETE; }
5054 | display { $$ = DISPLAY; }
5055 | divide { $$ = DIVIDE; }
5056 | entry { $$ = ENTRY; }
5057 | evaluate { $$ = EVALUATE; }
5058 | exhibit_stmt { $$ = EXHIBIT; }
5059 | exit { $$ = EXIT; }
5060 | free { $$ = FREE; }
5061 | go_to { $$ = GOTO; }
5062 | if_stmt { $$ = IF; }
5063 | initialize { $$ = INITIALIZE; }
5064 | inspect { $$ = INSPECT; }
5065 | merge { $$ = MERGE; }
5066 | move { $$ = MOVE; }
5067 | multiply { $$ = MULTIPLY; }
5068 | open { $$ = OPEN; }
5069 | return_stmt { $$ = RETURN; }
5070 | perform { $$ = PERFORM; }
5071 | raise { $$ = RAISE; }
5072 | read { $$ = READ; }
5073 | release { $$ = RELEASE; }
5074 | resume { $$ = RESUME; }
5075 | rewrite { $$ = REWRITE; }
5076 | search { $$ = SEARCH; }
5077 | set { $$ = SET; }
5078 | sort { $$ = SORT; }
5079 | start { $$ = START; }
5080 | stop { $$ = STOP; }
5081 | string { $$ = STRING_kw; }
5082 | subtract { $$ = SUBTRACT; }
5083 | unstring { $$ = UNSTRING; }
5084 | write { $$ = WRITE; }
5085 ;
5086
5087 /*
5088 * ISO defines ON EXCEPTION only for Format 3 (screen). We
5089 * implement extensions defined by MF and Fujitsu to use ACCEPT
5090 * to interact with the command line and the environment.
5091 *
5092 * ISO ACCEPT and some others are implemented in accept_body,
5093 * before the parser sees any ON EXCEPTION. In those cases
5094 * accept_body returns accept_done_e to denote that the
5095 * statement has been handled. If ON EXCEPTION is then parsed,
5096 * it's an error. Otherwise, accept_body returns something
5097 * else, and the relevant parser_accept_foo function is called
5098 * in the "accept" action.
5099 */
5100 accept: accept_body end_accept {
5101 cbl_field_t *argi = register_find("_ARGI");
5102 switch( $accept_body.func ) {
5103 case accept_done_e:
5104 break;
5105 case accept_e:
5106 parser_accept(*$1.into, $1.special, nullptr, nullptr);
5107 break;
5108 case accept_command_line_e:
5109 if( $1.from->field == NULL ) { // take next command-line arg
5110 parser_accept_command_line(*$1.into, argi, NULL, NULL);
5111 cbl_num_result_t tgt { truncation_e, argi };
5112 parser_add2(tgt, literally_one); // increment argi
5113 } else if( $1.from->field == argi ) {
5114 parser_move(*$1.into, *$1.from);
5115 } else {
5116 parser_accept_command_line(*$1.into, *$1.from, NULL, NULL);
5117 }
5118 break;
5119 case accept_envar_e:
5120 parser_accept_envar(*$1.into, *$1.from, NULL, NULL);
5121 break;
5122 }
5123 }
5124 | accept_body accept_excepts[ec] end_accept {
5125 cbl_field_t *argi = register_find("_ARGI");
5126 switch( $accept_body.func ) {
5127 case accept_done_e:
5128 error_msg(@ec, "ON EXCEPTION valid only "
5129 "with ENVIRONMENT or COMMAND-LINE(n)");
5130 break;
5131 case accept_e:
5132 parser_accept(*$1.into, $1.special, $ec.on_error, $ec.not_error);
5133 break;
5134 case accept_command_line_e:
5135 if( $1.from->field == NULL ) { // take next command-line arg
5136 parser_accept_command_line(*$1.into, argi,
5137 $ec.on_error, $ec.not_error);
5138 cbl_num_result_t tgt { truncation_e, argi };
5139 parser_add2(tgt, literally_one); // increment argi
5140 } else if( $1.from->field == argi ) {
5141 parser_move(*$1.into, *$1.from);
5142 if( $ec.on_error || $ec.not_error ) {
5143 error_msg(@ec, "ON EXCEPTION valid only "
5144 "with ENVIRONMENT or COMMAND-LINE(n)");
5145 }
5146 } else {
5147 parser_accept_command_line(*$1.into, *$1.from,
5148 $ec.on_error, $ec.not_error);
5149 }
5150 break;
5151 case accept_envar_e:
5152 parser_accept_envar(*$1.into, *$1.from,
5153 $ec.on_error, $ec.not_error);
5154 break;
5155 }
5156 }
5157 ;
5158 end_accept: %empty %prec ACCEPT
5159 | END_ACCEPT
5160 ;
5161
5162 accept_body: accept_refer
5163 {
5164 $$.func = accept_done_e;
5165 parser_accept(*$1, CONSOLE_e, nullptr, nullptr);
5166 }
5167 | accept_refer FROM DATE
5168 {
5169 $$.func = accept_done_e;
5170 if( $1->is_reference() ) {
5171 error_msg(@1, "subscripts are unsupported here");
5172 YYERROR;
5173 }
5174 parser_accept_date_yymmdd($1->field);
5175 }
5176 | accept_refer FROM DATE YYYYMMDD
5177 {
5178 $$.func = accept_done_e;
5179 if( $1->is_reference() ) {
5180 error_msg(@1, "subscripts are unsupported here");
5181 YYERROR;
5182 }
5183 parser_accept_date_yyyymmdd($1->field);
5184 }
5185 | accept_refer FROM DAY
5186 {
5187 $$.func = accept_done_e;
5188 if( $1->is_reference() ) {
5189 error_msg(@1, "subscripts are unsupported here");
5190 YYERROR;
5191 }
5192 parser_accept_date_yyddd($1->field);
5193 }
5194 | accept_refer FROM DAY YYYYDDD
5195 {
5196 $$.func = accept_done_e;
5197 if( $1->is_reference() ) {
5198 error_msg(@1, "subscripts are unsupported here");
5199 YYERROR;
5200 }
5201 parser_accept_date_yyyyddd($1->field);
5202 }
5203 | accept_refer FROM DAY_OF_WEEK
5204 {
5205 $$.func = accept_done_e;
5206 if( $1->is_reference() ) {
5207 error_msg(@1, "subscripts are unsupported here");
5208 YYERROR;
5209 }
5210 parser_accept_date_dow($1->field);
5211 }
5212
5213 | accept_refer FROM TIME
5214 {
5215 $$.func = accept_done_e;
5216 if( $1->is_reference() ) {
5217 error_msg(@1, "subscripts are unsupported here");
5218 YYERROR;
5219 }
5220 parser_accept_date_hhmmssff($1->field);
5221 }
5222 | accept_refer FROM acceptable
5223 {
5224 $$.func = accept_e;
5225 $$.into = $1;
5226 $$.special = $acceptable->id;
5227 }
5228 | accept_refer FROM ENVIRONMENT envar
5229 {
5230 $$.func = accept_envar_e;
5231 $$.into = $1;
5232 $$.from = $envar;
5233 }
5234 | accept_refer FROM COMMAND_LINE
5235 {
5236 $$.func = accept_done_e;
5237 parser_accept_command_line(*$1, NULL, NULL, NULL );
5238 }
5239 | accept_refer FROM COMMAND_LINE '(' expr ')'
5240 {
5241 $$.func = accept_command_line_e;
5242 $$.into = $1;
5243 $$.from = $expr;
5244 }
5245 | accept_refer FROM COMMAND_LINE_COUNT {
5246 $$.func = accept_done_e;
5247 parser_accept_command_line_count(*$1);
5248 }
5249 ;
5250
5251 accept_refer: ACCEPT scalar { statement_begin(@1, ACCEPT); $$ = $2; }
5252 ;
5253
5254 accept_excepts: accept_excepts[a] accept_except[b] statements %prec ACCEPT
5255 {
5256 if( $a.on_error && $a.not_error ) {
5257 error_msg(@b, "too many ON EXCEPTION clauses");
5258 YYERROR;
5259 }
5260 // "ON" and "NOT ON" could be reversed, but not duplicated.
5261 if( $a.on_error && $b.on_error ) {
5262 error_msg(@b, "duplicate ON EXCEPTION clauses");
5263 YYERROR;
5264 }
5265 if( $a.not_error && $b.not_error ) {
5266 error_msg(@b, "duplicate NOT ON EXCEPTION clauses");
5267 YYERROR;
5268 }
5269 $$ = $a;
5270 if( $b.on_error ) {
5271 $$.on_error = $b.on_error;
5272 assert($a.not_error);
5273 } else {
5274 $$.not_error = $b.not_error;
5275 assert($a.on_error);
5276 }
5277 assert( $b.on_error || $b.not_error );
5278 assert( ! ($b.on_error && $b.not_error) );
5279 cbl_label_t *tgt = $b.on_error? $b.on_error : $b.not_error;
5280 parser_accept_exception_end(tgt);
5281 }
5282 | accept_except[a] statements %prec ACCEPT
5283 {
5284 $$ = $a;
5285 assert( $a.on_error || $a.not_error );
5286 assert( ! ($a.on_error && $a.not_error) );
5287 cbl_label_t *tgt = $a.on_error? $a.on_error : $a.not_error;
5288 parser_accept_exception_end(tgt);
5289 }
5290 ;
5291
5292 accept_except: EXCEPTION
5293 {
5294 $$.not_error = NULL;
5295 $$.on_error = label_add(LblArith,
5296 uniq_label("accept"), @1.first_line);
5297 if( !$$.on_error ) YYERROR;
5298 parser_accept_exception( $$.on_error );
5299
5300 assert( $1 == EXCEPTION || $1 == NOT );
5301 if( $1 == NOT ) {
5302 std::swap($$.on_error, $$.not_error);
5303 }
5304 }
5305 ;
5306
5307 envar: scalar { $$ = $1; $$->field->attr |= envar_e; }
5308 | LITERAL {
5309 $$ = new_reference(new_literal($1, quoted_e));
5310 $$->field->attr |= envar_e;
5311 }
5312 ;
5313
5314 acceptable: device_name
5315 {
5316 $$ = symbol_special( $1.id );
5317 if( !$$ ) {
5318 error_msg(@1, "no such environment name");
5319 YYERROR;
5320 }
5321 }
5322 | NAME
5323 {
5324 $$ = special_of($1);
5325 if( !$$ ) {
5326 const special_name_t *special_type = cmd_or_env_special_of($NAME);
5327 if( !special_type ) {
5328 error_msg(@NAME, "no such special name '%s'", $NAME);
5329 YYERROR;
5330 }
5331 if( ENV_NAME_e == *special_type ) {
5332 error_msg(@NAME, "cannot ACCEPT FROM %qs", $NAME);
5333 YYERROR;
5334 }
5335 // Add the name now, as a convenience.
5336 int token = 0;
5337 switch(*special_type) {
5338 case ARG_NUM_e: token = ARGUMENT_NUMBER; break;
5339 case ARG_VALUE_e: token = ARGUMENT_VALUE; break;
5340 case ENV_VALUE_e: token = ENVIRONMENT_VALUE; break;
5341
5342 case ENV_NAME_e:
5343 default:
5344 error_msg(@NAME, "cannot ACCEPT FROM %qs", $NAME);
5345 YYERROR;
5346 break;
5347 }
5348 cbl_special_name_t special = { token, *special_type };
5349 namcpy(@NAME, special.name, $NAME);
5350
5351 symbol_elem_t *e = symbol_special_add(PROGRAM, &special);
5352 $$ = cbl_special_name_of(e);
5353 cbl_special_name_t& unused(*$$);
5354 assert(unused.id);
5355 }
5356 assert($$);
5357 }
5358 | ENVIRONMENT_VALUE {
5359 // Add the name now, as a convenience.
5360 cbl_special_name_t special =
5361 { ENVIRONMENT_VALUE, ENV_VALUE_e, "ENVIRONMENT-VALUE" };
5362 symbol_elem_t *e = symbol_special_add(PROGRAM, &special);
5363 $$ = cbl_special_name_of(e);
5364 }
5365 | ARGUMENT_NUMBER {
5366 // Add the name now, as a convenience.
5367 cbl_special_name_t special =
5368 { ARGUMENT_NUMBER, ARG_NUM_e, "ARGUMENT-NUMBER" };
5369 symbol_elem_t *e = symbol_special_add(PROGRAM, &special);
5370 $$ = cbl_special_name_of(e);
5371 }
5372 | ARGUMENT_VALUE {
5373 // Add the name now, as a convenience.
5374 cbl_special_name_t special =
5375 { ARGUMENT_VALUE, ARG_VALUE_e, "ARGUMENT-VALUE" };
5376 symbol_elem_t *e = symbol_special_add(PROGRAM, &special);
5377 $$ = cbl_special_name_of(e);
5378 }
5379 ;
5380
5381 add: add_impl end_add { ast_add($1); }
5382 | add_cond end_add { ast_add($1); }
5383 ;
5384 add_impl: ADD add_body
5385 {
5386 statement_begin(@1, ADD);
5387 $$ = $2;
5388 }
5389 ;
5390 add_cond: ADD add_body[body] arith_errs[err]
5391 {
5392 statement_begin(@1, ADD);
5393 $body->on_error = $err.on_error;
5394 $body->not_error = $err.not_error;
5395 $$ = $body;
5396 }
5397 ;
5398 end_add: %empty %prec ADD
5399 | END_ADD
5400 ;
5401
5402 add_body: sum TO rnames
5403 {
5404 $$ = new arith_t(no_giving_e, $sum);
5405 std::copy( rhs.begin(),
5406 rhs.end(), back_inserter($$->tgts) );
5407 rhs.clear();
5408 }
5409 | sum TO num_operand[value] GIVING rnames
5410 {
5411 $$ = new arith_t(giving_e, $sum);
5412 $$->A.push_back(*$value);
5413 std::copy( rhs.begin(),
5414 rhs.end(), back_inserter($$->tgts) );
5415 rhs.clear();
5416 }
5417 | sum GIVING rnames
5418 { // implicit TO
5419 $$ = new arith_t(giving_e, $sum);
5420 std::copy( rhs.begin(),
5421 rhs.end(), back_inserter($$->tgts) );
5422 rhs.clear();
5423 }
5424 | CORRESPONDING sum TO rnames
5425 {
5426 corresponding_fields_t pairs =
5427 corresponding_arith_fields( $sum->refers.front().field,
5428 rhs.front().refer.field );
5429 if( pairs.empty() ) {
5430 yywarn( "%s and %s have no corresponding fields",
5431 $sum->refers.front().field->name,
5432 rhs.front().refer.field->name );
5433 }
5434 // First src/tgt elements are templates.
5435 // Their subscripts apply to the correspondents.
5436 $$ = new arith_t(corresponding_e, $sum);
5437 $$->tgts.push_front(rhs.front());
5438 // use arith_t functor to populate A and tgts
5439 *$$ = std::for_each( pairs.begin(), pairs.end(), *$$ );
5440 $$->A.pop_front();
5441 $$->tgts.pop_front();
5442 rhs.clear();
5443 }
5444 ;
5445
5446 rounded: %empty { $$ = truncation_e; }
5447 | ROUNDED { $$ = current_rounded_mode(); }
5448 | ROUNDED rounded_mode { $$ = rounded_of($rounded_mode); }
5449 ;
5450 rounded_mode: MODE is rounded_type { $$ = $rounded_type; }
5451 ;
5452 rounded_type: AWAY_FROM_ZERO { $$ = away_from_zero_e; }
5453 | NEAREST_TOWARD_ZERO { $$ = nearest_toward_zero_e; }
5454 | TOWARD_GREATER { $$ = toward_greater_e; }
5455 | TOWARD_LESSER { $$ = toward_lesser_e; }
5456 | round_between
5457 ;
5458 round_between: NEAREST_AWAY_FROM_ZERO { $$ = nearest_away_from_zero_e; }
5459 | NEAREST_EVEN { $$ = nearest_even_e; }
5460 | PROHIBITED { $$ = prohibited_e; }
5461 | TRUNCATION { $$ = truncation_e; }
5462 ;
5463
5464 might_be: %empty { $$ = IS; }
5465 | MIGHT_BE
5466 ;
5467
5468 posneg: POSITIVE { $$ = $1 == NOT? le_op : gt_op; }
5469 | NEGATIVE { $$ = $1 == NOT? ge_op : lt_op; }
5470 | ZERO { $$ = $1 == NOT? ne_op : eq_op; }
5471 ;
5472
5473 scalar88s: scalar88 { $$ = new refer_list_t($1); }
5474 | scalar88s scalar88 { $1->push_back($2); }
5475 ;
5476
5477 name88: NAME88 {
5478 name_queue.qualify(@1, $1);
5479 auto namelocs( name_queue.pop() );
5480 auto names( name_queue.namelist_of(namelocs) );
5481 if( ($$ = field_find(names)) == NULL ) {
5482 if( procedure_div_e == current_division ) {
5483 error_msg(namelocs.back().loc,
5484 "DATA-ITEM '%s' not found", names.back() );
5485 YYERROR;
5486 }
5487 }
5488 assert($$->level == 88);
5489 }
5490 ;
5491
5492 scalar88: name88 subscripts[subs] refmod[ref]
5493 {
5494 if( $ref.from->is_reference() || $ref.len->is_reference() ) {
5495 error_msg(@subs, "subscripts on start:len refmod "
5496 "parameters are unsupported");
5497 YYERROR;
5498 }
5499 cbl_span_t span( $ref.from, $ref.len );
5500 $$ = new cbl_refer_t($1, $subs->vectorize(), span);
5501 }
5502 | name88 refmod[ref]
5503 {
5504 if( $ref.from->is_reference() || $ref.len->is_reference() ) {
5505 error_msg(@ref, "subscripts on start:len refmod "
5506 "parameters are unsupported");
5507 YYERROR;
5508 }
5509 cbl_span_t span( $ref.from, $ref.len );
5510 $$ = new cbl_refer_t($1, span);
5511 }
5512 | name88 subscripts[subs]
5513 {
5514 $$ = new cbl_refer_t($1);
5515 if( $subs->refers.size() != $$->subscripts_set($subs->refers) ) {
5516 subscript_dimension_error(@subs, $subs->refers.size(), $$);
5517 }
5518 }
5519 | name88
5520 {
5521 $$ = new_reference($1);
5522 }
5523 ;
5524
5525 allocate: ALLOCATE expr[size] CHARACTERS initialized RETURNING scalar[returning]
5526 {
5527 statement_begin(@1, ALLOCATE);
5528 if( $size->field->type == FldLiteralN ) {
5529 const auto size = TREE_REAL_CST_PTR ($size->field->data.value_of());
5530 if( real_isneg(size) || real_iszero(size) ) {
5531 error_msg(@size, "size must be greater than 0");
5532 YYERROR;
5533 }
5534 }
5535 reject_refmod( @returning, *$returning );
5536 if( ! require_pointer(@returning, *$returning) ) YYERROR;
5537 parser_allocate( *$size, *$returning, $initialized );
5538 }
5539 | ALLOCATE scalar[based] initialized alloc_ret[returning]
5540 {
5541 statement_begin(@1, ALLOCATE);
5542 if( ! $based->field->has_attr(based_e) ) {
5543 error_msg(@based, "%s must be BASED", $based->name());
5544 YYERROR;
5545 }
5546 reject_refmod( @based, *$based );
5547 reject_refmod( @returning, *$returning );
5548 if( $returning->field &&
5549 ! require_pointer(@returning, *$returning) ) YYERROR;
5550 parser_allocate( *$based, *$returning, $initialized );
5551 if( $initialized ) {
5552 initialize_allocated(*$based);
5553 }
5554 }
5555 ;
5556 initialized: %empty { $$ = false; }
5557 | INITIALIZED { $$ = true; }
5558 ;
5559 alloc_ret: %empty { static cbl_refer_t empty; $$ = &empty; }
5560 | RETURNING scalar[name] { $$ = $name; }
5561 ;
5562
5563 compute: compute_impl end_compute { current.compute_end(); }
5564 | compute_cond end_compute { current.compute_end(); }
5565 ;
5566 compute_impl: COMPUTE compute_body[body]
5567 {
5568 parser_assign( $body.ntgt, $body.tgts, *$body.expr,
5569 NULL, NULL, current.compute_label() );
5570 current.declaratives_evaluate();
5571 }
5572 ;
5573 compute_cond: COMPUTE compute_body[body] arith_errs[err]
5574 {
5575 parser_assign( $body.ntgt, $body.tgts, *$body.expr,
5576 $err.on_error, $err.not_error,
5577 current.compute_label() );
5578 current.declaratives_evaluate();
5579 }
5580 ;
5581 end_compute: %empty %prec COMPUTE
5582 | END_COMPUTE
5583 ;
5584
5585 compute_body: rnames { statement_begin(@$, COMPUTE); } compute_expr[expr] {
5586 $$.ntgt = rhs.size();
5587 auto C = new cbl_num_result_t[$$.ntgt];
5588 $$.tgts = use_any(rhs, C);
5589 $$.expr = $expr;
5590 }
5591 ;
5592 compute_expr: '=' {
5593 current.compute_begin();
5594 } expr {
5595 $$ = $expr;
5596 }
5597 ;
5598 | EQUAL {
5599 if( ! dialect_ibm() ) {
5600 dialect_error(@1, "EQUAL invalid as assignment operator", "ibm");
5601 }
5602 current.compute_begin();
5603 } expr {
5604 $$ = $expr;
5605 }
5606 ;
5607
5608 display: disp_body end_display[advance]
5609 {
5610 std::vector <cbl_refer_t> args($1.vargs->args.begin(),
5611 $1.vargs->args.end());
5612 parser_display($1.special, args, $advance);
5613 current.declaratives_evaluate();
5614 }
5615 ;
5616 end_display: %empty { $$ = DISPLAY_ADVANCE; }
5617 | END_DISPLAY { $$ = DISPLAY_ADVANCE; }
5618 | NO ADVANCING { $$ = DISPLAY_NO_ADVANCE; }
5619 | NO ADVANCING END_DISPLAY { $$ = DISPLAY_NO_ADVANCE; }
5620 ;
5621 disp_body: disp_vargs[vargs]
5622 {
5623 $$.special = NULL;
5624 $$.vargs = $vargs;
5625 }
5626 | disp_vargs[vargs] UPON disp_upon[special]
5627 {
5628 $$.special = $special;
5629 $$.vargs = $vargs;
5630 }
5631 ;
5632 disp_vargs: DISPLAY vargs {
5633 statement_begin(@1, DISPLAY);
5634 $$ = $vargs;
5635 }
5636 ;
5637
5638 disp_upon: device_name {
5639 $$ = symbol_special($1.id);
5640 }
5641 | NAME
5642 {
5643 symbol_elem_t *e = symbol_special(PROGRAM, $NAME);
5644 if( !e ) {
5645 const special_name_t *special_type = cmd_or_env_special_of($NAME);
5646 if( !special_type ) {
5647 error_msg(@NAME, "no such special name '%s'", $NAME);
5648 YYERROR;
5649 }
5650 // Add the name now, as a convenience.
5651 // These may come through as a NAME, depending on how scanned.
5652 int token = 0;
5653 switch(*special_type) {
5654 case ARG_NUM_e: token = ARGUMENT_NUMBER; break;
5655 case ENV_NAME_e: token = ENVIRONMENT_NAME; break;
5656 case ENV_VALUE_e: token = ENVIRONMENT_VALUE; break;
5657
5658 case ARG_VALUE_e:
5659 default:
5660 error_msg(@NAME, "cannot DISPLAY UPON %qs", $NAME);
5661 YYERROR;
5662 break;
5663 }
5664 cbl_special_name_t special = { token, *special_type };
5665 namcpy(@NAME, special.name, $NAME);
5666
5667 e = symbol_special_add(PROGRAM, &special);
5668 }
5669 $$ = cbl_special_name_of(e);
5670 }
5671 | ARGUMENT_NUMBER {
5672 // Add the name now, as a convenience.
5673 cbl_special_name_t special =
5674 { ARGUMENT_NUMBER, ARG_NUM_e, "ARGUMENT-NUMBER" };
5675 symbol_elem_t *e = symbol_special_add(PROGRAM, &special);
5676 $$ = cbl_special_name_of(e);
5677 }
5678 | ENVIRONMENT_NAME {
5679 // Add the name now, as a convenience.
5680 cbl_special_name_t special =
5681 { ENVIRONMENT_NAME, ENV_NAME_e, "ENVIRONMENT-NAME" };
5682 symbol_elem_t *e = symbol_special_add(PROGRAM, &special);
5683 $$ = cbl_special_name_of(e);
5684 }
5685 | ENVIRONMENT_VALUE {
5686 // Add the name now, as a convenience.
5687 cbl_special_name_t special =
5688 { ENVIRONMENT_VALUE, ENV_VALUE_e, "ENVIRONMENT-VALUE" };
5689 symbol_elem_t *e = symbol_special_add(PROGRAM, &special);
5690 $$ = cbl_special_name_of(e);
5691 }
5692 ;
5693
5694 exhibit_stmt: EXHIBIT exh_changed exh_named vargs {
5695 statement_begin(@1, EXHIBIT);
5696 std::vector<cbl_refer_t> args( $vargs->args.begin(),
5697 $vargs->args.end() );
5698 parser_exhibit( $exh_changed, $exh_named, args );
5699 }
5700 ;
5701 exh_changed: %empty { $$ = false; }
5702 | CHANGED { $$ = true; }
5703 ;
5704 exh_named: %empty { $$ = false; }
5705 | NAMED { $$ = true; }
5706 ;
5707
5708 divide: divide_impl end_divide { ast_divide($1); }
5709 | divide_cond end_divide { ast_divide($1); }
5710 ;
5711
5712 divide_impl: DIVIDE divide_body[body]
5713 {
5714 statement_begin(@1, DIVIDE);
5715 $$ = $body;
5716 }
5717 ;
5718 divide_cond: DIVIDE divide_body[body] arith_errs[err]
5719 {
5720 statement_begin(@1, DIVIDE);
5721 $$ = $body;
5722 $$->on_error = $err.on_error;
5723 $$->not_error = $err.not_error;
5724 }
5725 ;
5726 end_divide: %empty %prec DIVIDE
5727 | END_DIVIDE
5728 ;
5729
5730 divide_body: num_operand INTO rnames
5731 { /* format 1 */
5732 $$ = new arith_t(no_giving_e);
5733 $$->A.push_back(*$num_operand);
5734 std::copy( rhs.begin(),
5735 rhs.end(), back_inserter($$->tgts) );
5736 rhs.clear();
5737 }
5738 | divide_into
5739 | divide_into REMAINDER scalar[rem]
5740 {
5741 if( $1->tgts.size() != 1 ) {
5742 error_msg(@1, "only 1 (not %zu) "
5743 "GIVING with REMAINDER", $1->tgts.size());
5744 YYERROR;
5745 }
5746 $$ = $1;
5747 $$->remainder = *$rem;
5748 }
5749 | divide_by
5750 | divide_by REMAINDER scalar[rem]
5751 {
5752 if( $1->tgts.size() != 1 ) {
5753 error_msg(@1, "only 1 (not %zu) "
5754 "GIVING with REMAINDER", $1->tgts.size());
5755 YYERROR;
5756 }
5757 $$ = $1;
5758 $$->remainder = *$rem;
5759 }
5760 ;
5761
5762 divide_into: num_operand[b] INTO num_operand[a] GIVING rnames
5763 { // format 2 & 4
5764 $$ = new arith_t(giving_e);
5765 $$->A.push_back(*$a);
5766 $$->B.push_back(*$b);
5767 std::copy( rhs.begin(),
5768 rhs.end(), back_inserter($$->tgts) );
5769 rhs.clear();
5770 }
5771 ;
5772 divide_by: num_operand[a] BY num_operand[b] GIVING rnames
5773 { // format 3 & 5
5774 $$ = new arith_t(giving_e);
5775 $$->A.push_back(*$a);
5776 $$->B.push_back(*$b);
5777 std::copy( rhs.begin(),
5778 rhs.end(), back_inserter($$->tgts) );
5779 rhs.clear();
5780 }
5781 ;
5782
5783 end_program: end_program1[end] '.'
5784 {
5785 const cbl_label_t *prog = current.program();
5786 assert(prog);
5787 const char *name = string_of($end.name);
5788
5789 bool matches = false;
5790 const char *token_name = keyword_str($end.token) + 4;
5791 switch($end.token) {
5792 case END_PROGRAM:
5793 matches = prog->type == LblProgram;
5794 break;
5795 case END_FUNCTION:
5796 matches = prog->type == LblFunction;
5797 break;
5798 default:
5799 error_msg(@end, "logic error: END token invalid '%s'", name);
5800 gcc_unreachable();
5801 }
5802 if( !matches ) {
5803 error_msg($end.loc, "END %s %s does not match "
5804 "%<IDENTIFICATION DIVISION %s%>",
5805 token_name, name, prog->name);
5806 YYERROR;
5807 }
5808
5809 if( 0 != strcasecmp(prog->name, name) ) {
5810 error_msg($end.loc, "END PROGRAM '%s' does not match PROGRAM-ID '%s'",
5811 name, prog->name);
5812 YYERROR;
5813 }
5814 std::set<std::string> externals = current.end_program();
5815 if( !externals.empty() ) {
5816 for( const auto& name : externals ) {
5817 yywarn("%s calls external symbol '%s'", prog->name, name.c_str());
5818 }
5819 YYERROR;
5820 }
5821 // pointer still valid because name is in symbol table
5822 ast_end_program(prog->name);
5823 }
5824 | end_program1[end] error
5825 {
5826 const char *token_name = "???";
5827 switch($end.token) {
5828 case END_PROGRAM:
5829 token_name = "PROGRAM";
5830 break;
5831 case END_FUNCTION:
5832 token_name = "FUNCTION";
5833 break;
5834 default:
5835 cbl_internal_error( "%<END%> token invalid");
5836 }
5837 error_msg(@end, "%<END%> %s requires %<NAME%> before %<.%>", token_name);
5838 YYERROR;
5839 }
5840 ;
5841 end_program1: END_PROGRAM namestr[name]
5842 {
5843 $$.loc = @name;
5844 $$.token = END_PROGRAM;
5845 $$.name = $name;
5846 }
5847 | END_FUNCTION namestr[name]
5848 {
5849 $$.loc = @name;
5850 $$.token = END_FUNCTION;
5851 $$.name = $name;
5852 }
5853 | END_PROGRAM '.' // error
5854 {
5855 $$.loc = @1;
5856 $$.token = END_PROGRAM;
5857 }
5858 | END_FUNCTION '.' // error
5859 {
5860 $$.loc = @1;
5861 $$.token = END_FUNCTION;
5862 }
5863 ;
5864
5865 continue_stmt: CONTINUE {
5866 statement_begin(@1, CONTINUE);
5867 parser_sleep(*cbl_refer_t::empty());
5868 }
5869 | CONTINUE AFTER expr SECONDS {
5870 statement_begin(@1, CONTINUE);
5871 parser_sleep(*$expr);
5872 }
5873 ;
5874
5875 exit: GOBACK exit_with[status]
5876 {
5877 statement_begin(@1, GOBACK);
5878 parser_exit(*$status);
5879 }
5880 | GOBACK exit_raising[ec]
5881 {
5882 statement_begin(@1, GOBACK);
5883 parser_exit(*cbl_refer_t::empty(), $ec);
5884 }
5885 | EXIT { statement_begin(@1, EXIT); } exit_what
5886 | SIMPLE_EXIT
5887 {
5888 error_msg(@1, "EXIT is invalid here");
5889 }
5890 ;
5891 /* Valid "simple" EXIT (Format 1) swallowed by lexer */
5892
5893 /*
5894 * If the EXIT PROGRAM statement is executed in a program that
5895 * is not under the control of a calling runtime element, the
5896 * EXIT PROGRAM statement is treated as if it were a CONTINUE
5897 * statement.
5898 * To indicate this, We pass a "magic" refer with prog_func set.
5899 */
5900 exit_with: %empty
5901 {
5902 /* "If a RETURNING phrase is specified in the procedure
5903 * division header of the program containing the GOBACK
5904 * statement, the value in the data item referenced by that
5905 * RETURNING phrase becomes the result of the program
5906 * activation. Execution continues in the calling element
5907 * as specified in the rules."
5908 */
5909 $$ = cbl_refer_t::empty();
5910 if( dialect_ibm() ) {
5911 static auto rt = cbl_field_of(symbol_at(return_code_register()));
5912 static cbl_refer_t status(rt);
5913 $$ = &status;
5914 }
5915 const auto prog = cbl_label_of(symbol_at(current_program_index()));
5916 if( prog->returning ) {
5917 $$ = new cbl_refer_t( cbl_field_of(symbol_at(prog->returning)) );
5918 }
5919 }
5920 | with NORMAL stop_status
5921 {
5922 $$ = $stop_status? $stop_status : new_reference(literally_zero);
5923 }
5924 | with ERROR stop_status
5925 {
5926 $$ = $stop_status? $stop_status : new_reference(literally_one);
5927 }
5928 | RETURNING stop_status
5929 {
5930 if( ! dialect_mf() ) {
5931 dialect_error(@2, "RETURNING <number>", "mf");
5932 }
5933 $$ = $stop_status? $stop_status : new_reference(literally_one);
5934 }
5935 ;
5936 exit_what: PROGRAM_kw { parser_exit_program(); }
5937 | PROGRAM_kw exit_raising[ec] { parser_exit_program(); }
5938 | SECTION { parser_exit_section(); }
5939 | PARAGRAPH { parser_exit_paragraph(); }
5940 | PERFORM {
5941 if( performs.empty() ) {
5942 error_msg(@$, "EXIT PERFORM valid only "
5943 "within inline PERFORM procedure" );
5944 YYERROR;
5945 }
5946 parser_exit_perform(&perform_current()->tgt, $1);
5947 }
5948 ;
5949
5950 exit_raising: RAISING EXCEPTION EXCEPTION_NAME[ec]
5951 {
5952 $$ = $ec;
5953 }
5954 | RAISING error {
5955 cbl_unimplemented("RAISING exception-object");
5956 $$ = ec_none_e;
5957 }
5958 | RAISING LAST /* lexer swallows EXCEPTION */
5959 {
5960 $$ = ec_all_e;
5961 }
5962 ;
5963
5964 free: FREE free_tgts
5965 {
5966 size_t n = $free_tgts->size();
5967 assert( n > 0 );
5968 auto tgts = new cbl_refer_t[n];
5969 parser_free( n, $free_tgts->use_list(tgts) );
5970 }
5971 ;
5972 free_tgts: free_tgt { $$ = new refer_list_t($1); }
5973 | free_tgts free_tgt { $$->push_back($2); }
5974 ;
5975 free_tgt: scalar {
5976 $$ = $1;
5977 reject_refmod(@scalar, *$1);
5978 }
5979 | ADDRESS OF scalar[name]
5980 {
5981 $$ = $name;
5982 $$->addr_of = true;
5983 reject_refmod(@name, *$name);
5984 }
5985 ;
5986
5987 /*
5988 * Conditional Expressions
5989 */
5990 simple_cond: kind_of_name
5991 {
5992 $$ = new_reference($1);
5993 }
5994 | SWITCH
5995 {
5996 $$ = new_reference(new_temporary(FldConditional));
5997 cbl_field_t *field = cbl_field_of(symbol_find(@1, $1));
5998 assert(field->type == FldSwitch);
5999 cbl_field_t *parent = parent_of(field);
6000 size_t value = field->data.upsi_mask_of()->value;
6001 bitop_t op = field->data.upsi_mask_of()->on_off?
6002 bit_on_op : bit_off_op;
6003 parser_bitop($$->cond(), parent, op, value );
6004 }
6005 | expr is CLASS_NAME[domain]
6006 {
6007 $$ = new_reference(new_temporary(FldConditional));
6008 // symbol_find does not find FldClass symbols
6009 struct symbol_elem_t *e = symbol_field(PROGRAM, 0, $domain);
6010 parser_setop($$->cond(), $1->field, is_op, cbl_field_of(e));
6011 }
6012 | expr NOT CLASS_NAME[domain] {
6013 $$ = new_reference(new_temporary(FldConditional));
6014 // symbol_find does not find FldClass symbols
6015 struct symbol_elem_t *e = symbol_field(PROGRAM, 0, $domain);
6016 parser_setop($$->cond(), $1->field, is_op, cbl_field_of(e));
6017 parser_logop($$->cond(), NULL, not_op, $$->cond());
6018 }
6019 | expr is OMITTED
6020 {
6021 auto lhs = cbl_refer_t($expr->field);
6022 lhs.addr_of = true;
6023 auto rhs = cbl_field_of(symbol_field(0,0, "NULLS"));
6024 $$ = new_reference(new_temporary(FldConditional));
6025 parser_relop($$->field, lhs, eq_op, rhs);
6026 }
6027 | expr NOT OMITTED
6028 {
6029 auto lhs = cbl_refer_t($expr->field);
6030 lhs.addr_of = true;
6031 auto rhs = cbl_field_of(symbol_field(0,0, "NULLS"));
6032 $$ = new_reference(new_temporary(FldConditional));
6033 parser_relop($$->field, lhs, ne_op, rhs);
6034 }
6035 | expr posneg[op] {
6036 $$ = new_reference(new_temporary(FldConditional));
6037 relop_t op = static_cast<relop_t>($op);
6038 cbl_field_t *zero = constant_of(constant_index(ZERO));
6039 parser_relop($$->cond(), *$1, op, zero);
6040 }
6041 | scalar88 {
6042 // copy the subscripts and set the parent field
6043 cbl_refer_t parent = *$scalar88;
6044 parent.field = parent_of($scalar88->field);
6045 if( !parent.field ) {
6046 cbl_internal_error("Type 88 has no referent");
6047 YYERROR;
6048 }
6049 $$ = new_reference(new_temporary(FldConditional));
6050 $$->field->parent = field_index($scalar88->field);
6051 parser_relop($$->cond(), parent, eq_op, *$scalar88);
6052 }
6053 ;
6054
6055 kind_of_name: expr might_be variable_type
6056 {
6057 $$ = new_temporary(FldConditional);
6058 enum classify_t type = classify_of($3);
6059 assert(type != ClassInvalidType );
6060
6061 parser_classify( $$, *$1, type );
6062 if( $2 == NOT ) {
6063 parser_logop($$, NULL, not_op, $$);
6064 }
6065 }
6066 ;
6067
6068 bool_expr: log_expr { $$ = new_reference($1->resolve()); }
6069 ;
6070
6071 log_expr: log_term { $$ = new log_expr_t($1); } %prec AND
6072 | log_expr[lhs] OR rel_abbr[rhs]
6073 {
6074 $$ = $1;
6075 $$->or_term($rhs);
6076 }
6077 | log_expr[lhs] OR log_expr[rhs]
6078 {
6079 $$ = $lhs;
6080 assert( ! $rhs->unresolved() ); // what to do?
6081 $$->or_term($rhs->and_term());
6082 }
6083 | log_expr[lhs] AND rel_abbr[rhs]
6084 {
6085 $$ = $1;
6086 $$->and_term($rhs);
6087 }
6088 | log_expr[lhs] AND log_expr[rhs]
6089 {
6090 $$ = $lhs;
6091 assert( ! $rhs->unresolved() ); // what to do?
6092 $$->and_term($rhs->and_term());
6093 }
6094 ;
6095
6096 log_term: '(' log_expr ')' {
6097 current.antecedent_reset();
6098 $$ = $log_expr->resolve();
6099 }
6100 | NOT '(' log_expr ')' {
6101 current.antecedent_reset();
6102 $$ = $log_expr->resolve();
6103 parser_logop($$, NULL, not_op, $$);
6104 }
6105 | rel_expr
6106 | simple_cond {
6107 current.antecedent_reset();
6108 $$ = $1->cond();
6109 }
6110 | NOT simple_cond {
6111 current.antecedent_reset();
6112 $$ = $2->cond();
6113 parser_logop($$, NULL, not_op, $$);
6114 }
6115 ;
6116
6117 rel_expr: rel_lhs rel_term[rhs]
6118 {
6119 rel_part_t& ante = current.antecedent();
6120 if( $rhs.invert ) {
6121 error_msg(@rhs, "NOT %s is invalid, cannot negate RHS",
6122 ante.operand->field->name);
6123 }
6124 auto op = ante.relop;
6125 if( ante.invert ) {
6126 op = relop_invert(op);
6127 ante.invert = false;
6128 }
6129 auto cond = new_temporary(FldConditional);
6130 parser_relop( cond, *ante.operand, op, *$rhs.term );
6131 $$ = cond;
6132 }
6133 | rel_lhs[lhs] '(' rel_abbrs ')' {
6134 $$ = $rel_abbrs->resolve();
6135 }
6136 ;
6137
6138 rel_abbrs: rel_abbr { $$ = new log_expr_t($1); }
6139 | '(' rel_abbrs ')' {
6140 $$ = $2;
6141 $$->resolve();
6142
6143 }
6144 | rel_abbrs OR rel_abbr[rhs] {
6145 $$ = $1;
6146 $$->or_term($rhs);
6147 }
6148 | rel_abbrs OR '(' rel_abbr[rhs] ')' {
6149 $$ = $1;
6150 $$->or_term($rhs);
6151 }
6152 | rel_abbrs AND rel_abbr[rhs] {
6153 $$ = $1;
6154 $$->and_term($rhs);
6155 }
6156 | rel_abbrs AND '(' rel_abbr[rhs] ')' {
6157 $$ = $1;
6158 $$->and_term($rhs);
6159 }
6160 ;
6161
6162 rel_lhs: rel_term[lhs] relop {
6163 // no value, just set current antecedent
6164 auto op = relop_of($relop);
6165 auto ante = new rel_part_t($lhs.term, op, $lhs.invert);
6166 current.antecedent(*ante);
6167 }
6168 ;
6169
6170 rel_abbr: rel_term {
6171 static rel_part_t ante;
6172 ante = current.antecedent();
6173 if( ! ante.operand ) {
6174 error_msg(@1, "'AND %s' invalid because "
6175 "LHS is not a relation condition",
6176 name_of($rel_term.term->field) );
6177 YYERROR;
6178 }
6179 assert(ante.has_relop);
6180 if( $rel_term.invert ) ante.relop = relop_invert(ante.relop);
6181 auto cond = new_temporary(FldConditional);
6182 parser_relop(cond, *ante.operand, ante.relop, *$rel_term.term);
6183 $$ = cond;
6184 }
6185 | relop rel_term {
6186 static rel_part_t ante;
6187 if( $rel_term.invert ) {
6188 error_msg(@2, "%s NOT %s is invalid",
6189 keyword_str($relop),
6190 name_of($rel_term.term->field));
6191 }
6192 auto op( relop_of($relop) );
6193 ante = current.antecedent().relop_set(op);
6194 if( ! ante.operand ) {
6195 error_msg(@1, "AND %s invalid because "
6196 "LHS is not a relation condition",
6197 name_of($rel_term.term->field) );
6198 YYERROR;
6199 }
6200 auto cond = new_temporary(FldConditional);
6201 parser_relop(cond, *ante.operand, ante.relop, *$rel_term.term);
6202 $$ = cond;
6203 }
6204 ;
6205
6206 rel_term: rel_term1
6207 ;
6208
6209 rel_term1: all LITERAL
6210 {
6211 $$.invert = false;
6212 $$.term = new_reference(new_literal($2, quoted_e));
6213 $$.term->all = $all;
6214 }
6215 | all spaces_etc[value]
6216 {
6217 $$.invert = false;
6218 $$.term = new_reference(constant_of(constant_index($value)));
6219 $$.term->all = $all;
6220 }
6221 | all NULLS
6222 {
6223 $$.invert = false;
6224 $$.term = new_reference(constant_of(constant_index(NULLS)));
6225 $$.term->all = $all;
6226 }
6227 | ALL ZERO
6228 { // ZERO without ALL comes from expr, from num_term.
6229 $$.invert = false;
6230 $$.term = new_reference(constant_of(constant_index(ZERO)));
6231 $$.term->all = true;
6232 }
6233 | expr {
6234 $$.invert = false;
6235 $$.term = $1;
6236 }
6237 | NOT rel_term {
6238 $$ = $2;
6239 $$.invert = true;
6240 }
6241 ;
6242
6243 expr: expr_term
6244 ;
6245 expr_term: expr_term '+' num_term
6246 {
6247 if( ($$ = ast_op($1, '+', $3)) == NULL ) YYERROR;
6248 }
6249 | expr_term '-' num_term
6250 {
6251 if( ($$ = ast_op($1, '-', $3)) == NULL ) YYERROR;
6252 }
6253 | num_term
6254 ;
6255
6256 num_term: num_term '*' value
6257 {
6258 if( ($$ = ast_op($1, '*', $3)) == NULL ) YYERROR;
6259 }
6260 | num_term '/' value
6261 {
6262 if( ($$ = ast_op($1, '/', $3)) == NULL ) YYERROR;
6263 }
6264 | value
6265 ;
6266
6267 value: value POW factor
6268 {
6269 if( ($$ = ast_op($1, '^', $3)) == NULL ) YYERROR;
6270 }
6271 | '-' value %prec NEG { $$ = negate( $2 );}
6272 | '+' factor %prec NEG { $$ = $2;}
6273 | factor[rhs]
6274 ;
6275
6276 factor: '(' expr ')' { $$ = $2; }
6277 | num_value { $$ = $num_value; }
6278 ;
6279
6280 if_stmt: if_impl end_if
6281 ;
6282
6283 if_impl: if_verb if_test if_body
6284 {
6285 parser_fi();
6286 }
6287 ;
6288 if_verb: IF { statement_begin(@1, IF); }
6289 ;
6290 if_test: bool_expr then
6291 {
6292 if( ! is_conditional($bool_expr) ) {
6293 error_msg(@1, "%s is not a Boolean expression",
6294 name_of($bool_expr->field) );
6295 YYERROR;
6296 }
6297 parser_if( $bool_expr->cond() );
6298 }
6299 ;
6300
6301 if_body: next_statements
6302 {
6303 parser_else();
6304 }
6305 | next_statements ELSE {
6306 location_set(@2);
6307 parser_else();
6308 } next_statements
6309 ;
6310
6311 next_statements: statements %prec ADD
6312 | NEXT SENTENCE %prec ADD
6313 {
6314 next_sentence = label_add(LblNone, "next_sentence", 0);
6315 parser_label_goto(next_sentence);
6316 }
6317 ;
6318
6319 end_if: %empty %prec ADD
6320 | END_IF
6321 ;
6322
6323 evaluate: eval_verb eval_subjects eval_switch end_evaluate {
6324 auto& ev( eval_stack.current() );
6325 parser_label_label(ev.when());
6326 parser_label_label(ev.done());
6327 eval_stack.free();
6328 }
6329 ;
6330 eval_verb: EVALUATE {
6331 statement_begin(@1, EVALUATE);
6332 eval_stack.alloc();
6333 }
6334 ;
6335
6336 eval_subjects: eval_subject
6337 | eval_subjects ALSO eval_subject
6338 ;
6339 eval_subject: eval_subject1 {
6340 auto& ev( eval_stack.current() );
6341 ev.append(*$1);
6342 }
6343 ;
6344 eval_subject1: bool_expr
6345 | expr
6346 | true_false
6347 {
6348 static cbl_field_t *zero = constant_of(constant_index(ZERO));
6349 enum relop_t op = $1 == TRUE_kw? eq_op : ne_op;
6350 $$ = new cbl_refer_t( new_temporary(FldConditional) );
6351 parser_relop($$->field, zero, op, zero);
6352 }
6353 ;
6354
6355 eval_switch: eval_cases
6356 | eval_cases WHEN OTHER {
6357 auto& ev( eval_stack.current() );
6358 ev.write_when_label();
6359 }
6360 statements %prec ADD
6361 ;
6362
6363 eval_cases: eval_case
6364 | eval_cases eval_case
6365 ;
6366
6367 eval_case: eval_objects statements %prec ADD {
6368 auto& ev( eval_stack.current() );
6369 parser_label_goto( ev.done() );
6370 ev.rewind();
6371 }
6372 | eval_objects NEXT SENTENCE %prec ADD
6373 {
6374 auto& ev( eval_stack.current() );
6375 ev.write_when_label();
6376 next_sentence = label_add(LblNone, "next_sentence", 0);
6377 parser_label_goto(next_sentence);
6378 }
6379 ;
6380
6381 eval_objects: eval_whens {
6382 auto& ev( eval_stack.current() );
6383 // Place the object's Yeah label before the statements.
6384 ev.write_yeah_label();
6385 }
6386 ;
6387 eval_whens: eval_when
6388 | eval_whens eval_when
6389 ;
6390
6391 eval_when: WHEN {
6392 auto& ev( eval_stack.current() );
6393 ev.write_when_label();
6394 }
6395 eval_obj_cols %prec ADD { // all TRUE, go to statements
6396 auto& ev( eval_stack.current() );
6397 parser_label_goto(ev.yeah());
6398 auto subj( ev.subject() );
6399 if( subj ) {
6400 error_msg(@2, "WHEN clause incomplete, %zu of %zu evaluated",
6401 ev.object_count(), ev.subject_count());
6402 }
6403 ev.rewind();
6404 }
6405 | WHEN error
6406 ;
6407
6408 eval_obj_cols: eval_obj_col
6409 | eval_obj_cols ALSO eval_obj_col
6410 ;
6411
6412 eval_obj_col: ANY {
6413 auto& ev( eval_stack.current() );
6414 if( ! ev.decide(ANY) ) {
6415 error_msg(@1, "WHEN 'ANY' phrase exceeds subject set count of %zu",
6416 ev.subject_count());
6417 YYERROR;
6418 }
6419 }
6420 | true_false {
6421 auto& ev( eval_stack.current() );
6422 auto subj( ev.subject() );
6423 if( !subj ) {
6424 error_msg(@$, "WHEN '%s' phrase exceeds subject set count of %zu",
6425 keyword_str($1), ev.subject_count());
6426 YYERROR;
6427 }
6428 if( ! is_conditional( subj ) ) {
6429 error_msg(@1, "subject %s, type %s, "
6430 "cannot be compared to TRUE/FALSE",
6431 subj->name, 3 + cbl_field_type_str(subj->type) );
6432 }
6433 ev.decide($1);
6434 }
6435 | eval_posneg[op] {
6436 relop_t op = static_cast<relop_t>($op);
6437 cbl_field_t *zero = constant_of(constant_index(ZERO));
6438 auto& ev( eval_stack.current() );
6439 auto subj( ev.subject() );
6440 if( !subj ) {
6441 error_msg(@1, "WHEN '%s' phrase exceeds subject set count of %zu",
6442 relop_str(op), ev.subject_count());
6443 YYERROR;
6444 }
6445 ev.decide(op, zero, false);
6446 }
6447 | bool_expr {
6448 auto& ev( eval_stack.current() );
6449 auto subj( ev.subject() );
6450 if( !subj ) {
6451 error_msg(@1, "WHEN CONDITIONAL phrase exceeds "
6452 "subject set count of %zu",
6453 ev.subject_count());
6454 YYERROR;
6455 }
6456 if( ! is_conditional( subj ) ) {
6457 error_msg(@1, "subject %s, type %s, "
6458 "cannot be compared to conditional expression",
6459 subj->name, 3 + cbl_field_type_str(subj->type) );
6460 }
6461 ev.decide(*$1, false);
6462 }
6463 | eval_abbrs {
6464 auto& ev( eval_stack.current() );
6465 ev.decided( $1->resolve() );
6466 }
6467 | rel_term[a] THRU rel_term[b] %prec THRU {
6468 auto& ev( eval_stack.current() );
6469 auto subj( ev.subject() );
6470 if( !subj ) {
6471 error_msg(@a, "WHEN %s THRU %s phrase exceeds "
6472 "subject set count of %zu",
6473 $a.term->name(), $b.term->name(), ev.subject_count());
6474 YYERROR;
6475 }
6476 if( is_conditional($a.term) || is_conditional($b.term) ) {
6477 error_msg(@a, "THRU with boolean operand");
6478 }
6479 if( $b.invert ) {
6480 error_msg(@b, "NOT %s is invalid with THRU",
6481 name_of($b.term->field));
6482 }
6483 ev.decide(*$a.term, *$b.term, $a.invert);
6484 }
6485 | rel_term[a] ELSE
6486 {
6487 error_msg(@ELSE, "ELSE not valid in WHEN");
6488 YYERROR;
6489 }
6490 ;
6491 eval_posneg: POSITIVE { $$ = $1 == NOT? le_op : gt_op; }
6492 | NEGATIVE { $$ = $1 == NOT? ge_op : lt_op; }
6493 ;
6494
6495 eval_abbrs: rel_term[a] {
6496 auto& ev( eval_stack.current() );
6497 auto subj( ev.subject() );
6498 if( !subj ) {
6499 error_msg(@1, "WHEN %qs phrase exceeds "
6500 "subject set count of %zu",
6501 nice_name_of($a.term->field), ev.subject_count());
6502 YYERROR;
6503 }
6504 if( ! ev.compatible($a.term->field) ) {
6505 auto obj($a.term->field);
6506 error_msg(@1, "subject %s, type %s, "
6507 "cannot be compared %s, type %s",
6508 subj->name, 3 + cbl_field_type_str(subj->type),
6509 obj->name, 3 + cbl_field_type_str(obj->type) );
6510 }
6511 auto result = ev.compare(*$a.term);
6512 if( ! result ) YYERROR;
6513 if( $a.invert ) {
6514 parser_logop(result, nullptr, not_op, result);
6515 }
6516 $$ = new log_expr_t(result);
6517 }
6518 | relop rel_term[a] {
6519 auto& ev( eval_stack.current() );
6520 relop_t relop(relop_of($relop));
6521 ev.object_relop(relop);
6522 auto subj( ev.subject() );
6523 if( !subj ) {
6524 error_msg(@1, "WHEN %s %s phrase exceeds "
6525 "subject set count of %zu",
6526 relop_str(relop_of($relop)), $a.term->name(), ev.subject_count());
6527 YYERROR;
6528 }
6529 if( ! ev.compatible($a.term->field) ) {
6530 auto obj($a.term->field);
6531 error_msg(@1, "subject %s, type %s, "
6532 "cannot be compared %s, type %s",
6533 subj->name, 3 + cbl_field_type_str(subj->type),
6534 obj->name, 3 + cbl_field_type_str(obj->type) );
6535 }
6536 if( is_conditional(ev.subject()) ) {
6537 auto obj($a.term->field);
6538 error_msg(@1, "subject %s, type %s, "
6539 "cannot be %s %s, type %s",
6540 subj->name, 3 + cbl_field_type_str(subj->type),
6541 relop_str(relop_of($relop)),
6542 obj->name, 3 + cbl_field_type_str(obj->type) );
6543 }
6544 auto result = ev.compare(relop, *$a.term);
6545 if( ! result ) YYERROR;
6546 if( $a.invert ) {
6547 parser_logop(result, nullptr, not_op, result);
6548 }
6549 $$ = new log_expr_t(result);
6550 }
6551 | '(' eval_abbrs ')' {
6552 $$ = $2;
6553 $$->resolve();
6554 }
6555 | eval_abbrs OR eval_abbr[rhs] {
6556 $$ = $1;
6557 $$->or_term($rhs);
6558 }
6559 | eval_abbrs OR '(' eval_abbr[rhs] ')' {
6560 $$ = $1;
6561 $$->or_term($rhs);
6562 }
6563 | eval_abbrs AND eval_abbr[rhs] {
6564 $$ = $1;
6565 $$->and_term($rhs);
6566 }
6567 | eval_abbrs AND '(' eval_abbr[rhs] ')' {
6568 $$ = $1;
6569 $$->and_term($rhs);
6570 }
6571 ;
6572
6573 eval_abbr: rel_term[a] {
6574 auto& ev( eval_stack.current() );
6575 relop_t relop(ev.object_relop());
6576 auto subj( ev.subject() );
6577 assert( subj );
6578 $$ = ev.compare(relop, *$a.term);
6579 if( $a.invert ) {
6580 parser_logop($$, nullptr, not_op, $$);
6581 }
6582 }
6583 | relop rel_term[a] {
6584 auto& ev( eval_stack.current() );
6585 relop_t relop(relop_of($relop));
6586 ev.object_relop(relop);
6587 $$ = ev.compare(relop, *$a.term);
6588 if( $a.invert ) {
6589 parser_logop($$, nullptr, not_op, $$);
6590 }
6591 }
6592 ;
6593
6594 end_evaluate: %empty %prec EVALUATE
6595 | END_EVALUATE
6596 ;
6597
6598 true_false: TRUE_kw { $$ = TRUE_kw; }
6599 | FALSE_kw { $$ = FALSE_kw; }
6600 ;
6601
6602 scalar: tableref {
6603 // Check for missing subscript; others already checked.
6604 if( $1->nsubscript() == 0 && 0 < dimensions($1->field) ) {
6605 subscript_dimension_error(@1, 0, $$);
6606 }
6607 }
6608 ;
6609
6610 tableref: tableish {
6611 // tableref is used by SORT. It may name a table without subscripts.
6612 $$ = $1;
6613 $$->loc = @1;
6614 if( $$->is_table_reference() ) {
6615 if( $$->nsubscript() != dimensions($$->field) ) {
6616 subscript_dimension_error(@1, $$->nsubscript(), $$);
6617 YYERROR;
6618 }
6619 }
6620 }
6621 tableish: name subscripts[subs] refmod[ref] %prec NAME
6622 {
6623 assert(yychar != LPAREN);
6624 $$ = new cbl_refer_t($name);
6625 $$->subscripts_set($subs->refers);
6626 literal_subscripts_valid( @subs, *$$ );
6627 $$->refmod = cbl_span_t( $ref.from,
6628 $ref.len );
6629 literal_refmod_valid( @ref, *$$ );
6630 }
6631 | name refmod[ref] %prec NAME
6632 {
6633 $$ = new cbl_refer_t($name);
6634 $$->refmod = cbl_span_t( $ref.from,
6635 $ref.len );
6636 literal_refmod_valid( @ref, *$$ );
6637 }
6638 | name subscripts[subs] %prec NAME
6639 {
6640 $$ = new cbl_refer_t($name);
6641 $$->subscripts_set($subs->refers);
6642 literal_subscripts_valid( @subs, *$$ );
6643 }
6644 | name %prec NAME
6645 {
6646 $$ = new cbl_refer_t($name);
6647 }
6648 ;
6649
6650 refmod: LPAREN expr[from] ':' expr[len] ')' %prec NAME
6651 {
6652 if( ! require_integer(@from, *$from) ) YYERROR;
6653 if( ! require_integer(@len, *$len) ) YYERROR;
6654 $$.from = $from;
6655 $$.len = $len;
6656 }
6657 | LPAREN expr[from] ':' ')' %prec NAME
6658 {
6659 if( ! require_integer(@from, *$from) ) YYERROR;
6660 $$.from = $from;
6661 $$.len = nullptr;
6662 }
6663 ;
6664
6665 typename: NAME
6666 {
6667 auto e = symbol_typedef(PROGRAM, $NAME);
6668 if( ! e ) {
6669 error_msg(@1, "DATA-ITEM '%s' not found", $NAME );
6670 YYERROR;
6671 }
6672 $$ = cbl_field_of(e);
6673 }
6674 ;
6675
6676 name: qname
6677 {
6678 build_symbol_map();
6679 auto namelocs( name_queue.pop() );
6680 auto names( name_queue.namelist_of(namelocs) );
6681 auto inner = namelocs.back();
6682 if( ($$ = field_find(names)) == NULL ) {
6683 if( procedure_div_e == current_division ) {
6684 error_msg(inner.loc,
6685 "DATA-ITEM '%s' not found", inner.name );
6686 YYERROR;
6687 }
6688 /*
6689 * Insert forward references, starting outermost.
6690 */
6691 size_t parent = 0;
6692 while( ! names.empty() ) {
6693 auto name = names.front();
6694 names.pop_front();
6695 auto e = symbol_field_forward_add(PROGRAM, parent,
6696 name, @1.first_line);
6697 if( !e ) YYERROR;
6698 symbol_field_location( symbol_index(e), @qname );
6699 parent = symbol_index(e);
6700 $$ = cbl_field_of(e);
6701 }
6702 }
6703 gcc_assert($$);
6704 }
6705 ;
6706
6707 qname: ctx_name
6708 {
6709 name_queue.qualify(@1, $1);
6710 }
6711 | qname inof ctx_name
6712 {
6713 name_queue.qualify(@3, $3);
6714 }
6715 ;
6716 inof: IN
6717 | OF
6718 ;
6719
6720 ctx_name: NAME
6721 | context_word
6722 ;
6723
6724 context_word: APPLY { static char s[] ="APPLY";
6725 $$ = s; } // screen description entry
6726 | ARGUMENT_NUMBER { static char s[] ="ARGUMENT-NUMBER";
6727 $$ = s; } // Display Upon / Accept From
6728 | ARGUMENT_VALUE { static char s[] ="ARGUMENT-VALUE";
6729 $$ = s; } // Accept From
6730 | ARITHMETIC { static char s[] ="ARITHMETIC";
6731 $$ = s; } // OPTIONS paragraph
6732 | ATTRIBUTE { static char s[] ="ATTRIBUTE";
6733 $$ = s; } // SET statement
6734 | AUTO { static char s[] ="AUTO";
6735 $$ = s; } // screen description entry
6736 | AUTOMATIC { static char s[] ="AUTOMATIC";
6737 $$ = s; } // LOCK MODE clause
6738 | AWAY_FROM_ZERO { static char s[] ="AWAY-FROM-ZERO";
6739 $$ = s; } // ROUNDED phrase
6740 | BACKGROUND_COLOR { static char s[] ="BACKGROUND-COLOR";
6741 $$ = s; } // screen description entry
6742 | BELL { static char s[] ="BELL";
6743 $$ = s; } // screen description entry and SET attribute statement
6744 | BINARY_ENCODING { static char s[] ="BINARY-ENCODING";
6745 $$ = s; } // USAGE clause and FLOAT-DECIMAL clause
6746 | BLINK { static char s[] ="BLINK";
6747 $$ = s; } // screen description entry and SET attribute statement
6748 | BYTE_LENGTH { static char s[] ="BYTE-LENGTH";
6749 $$ = s; } // constant entry
6750 | CAPACITY { static char s[] ="CAPACITY";
6751 $$ = s; } // OCCURS clause
6752 | CENTER { static char s[] ="CENTER";
6753 $$ = s; } // COLUMN clause
6754 | CLASSIFICATION { static char s[] ="CLASSIFICATION";
6755 $$ = s; } // OBJECT-COMPUTER paragraph
6756 | CYCLE { static char s[] ="CYCLE";
6757 $$ = s; } // EXIT statement
6758 | DECIMAL_ENCODING { static char s[] ="DECIMAL-ENCODING";
6759 $$ = s; } // USAGE clause and FLOAT-DECIMAL clause
6760 | EOL { static char s[] ="EOL";
6761 $$ = s; } // ERASE clause in a screen description entry
6762 | EOS { static char s[] ="EOS";
6763 $$ = s; } // ERASE clause in a screen description entry
6764 | ENTRY_CONVENTION { static char s[] ="ENTRY-CONVENTION";
6765 $$ = s; } // OPTIONS paragraph
6766 | ENVIRONMENT_NAME { static char s[] ="ENVIRONMENT-NAME";
6767 $$ = s; } // Display Upon
6768 | ENVIRONMENT_VALUE { static char s[] ="ENVIRONMENT-VALUE";
6769 $$ = s; } // Display Upon / Accept From
6770 | ERASE { static char s[] ="ERASE";
6771 $$ = s; } // screen description entry
6772 | EXPANDS { static char s[] ="EXPANDS";
6773 $$ = s; } // class-specifier and interface-specifier of the REPOSITORY paragraph
6774 | FEATURE { static char s[] ="FEATURE";
6775 $$ = s; } // gcobol CDF token
6776 | FLOAT_BINARY { static char s[] ="FLOAT-BINARY";
6777 $$ = s; } // OPTIONS paragraph
6778 | FLOAT_DECIMAL { static char s[] ="FLOAT-DECIMAL";
6779 $$ = s; } // OPTIONS paragraph
6780 | FOREGROUND_COLOR { static char s[] ="FOREGROUND-COLOR";
6781 $$ = s; } // screen description entry
6782 | FOREVER { static char s[] ="FOREVER";
6783 $$ = s; } // RETRY phrase
6784 | FULL { static char s[] ="FULL";
6785 $$ = s; } // screen description entry
6786 | HIGH_ORDER_LEFT { static char s[] ="HIGH-ORDER-LEFT";
6787 $$ = s; } // FLOAT-BINARY clause, FLOAT-DECIMAL clause, and USAGE clause
6788 | HIGH_ORDER_RIGHT { static char s[] ="HIGH-ORDER-RIGHT";
6789 $$ = s; } // FLOAT-BINARY clause, FLOAT-DECIMAL clause, and USAGE clause
6790 | HIGHLIGHT { static char s[] ="HIGHLIGHT";
6791 $$ = s; } // screen description entry and SET attribute statement
6792 | IGNORING { static char s[] ="IGNORING";
6793 $$ = s; } // READ statement
6794 | IMPLEMENTS { static char s[] ="IMPLEMENTS";
6795 $$ = s; } // FACTORY paragraph and OBJECT paragraph
6796 | INITIALIZED { static char s[] ="INITIALIZED";
6797 $$ = s; } // ALLOCATE statement and OCCURS clause
6798 | INTERMEDIATE { static char s[] ="INTERMEDIATE";
6799 $$ = s; } // OPTIONS paragraph
6800 | INTRINSIC { static char s[] ="INTRINSIC";
6801 $$ = s; } // function-specifier of the REPOSITORY paragraph
6802 | LC_ALL_kw { static char s[] ="LC_ALL";
6803 $$ = s; } // SET statement
6804 | LC_COLLATE_kw { static char s[] ="LC_COLLATE";
6805 $$ = s; } // SET statement
6806 | LC_CTYPE_kw { static char s[] ="LC_CTYPE";
6807 $$ = s; } // SET statement
6808 | LC_MESSAGES_kw { static char s[] ="LC_MESSAGES";
6809 $$ = s; } // SET statement
6810 | LC_MONETARY_kw { static char s[] ="LC_MONETARY";
6811 $$ = s; } // SET statement
6812 | LC_NUMERIC_kw { static char s[] ="LC_NUMERIC";
6813 $$ = s; } // SET statement
6814 | LC_TIME_kw { static char s[] ="LC_TIME";
6815 $$ = s; } // SET statement
6816 | LOWLIGHT { static char s[] ="LOWLIGHT";
6817 $$ = s; } // screen description entry and SET attribute statement
6818 | MANUAL { static char s[] ="MANUAL";
6819 $$ = s; } // LOCK MODE clause
6820 | MULTIPLE { static char s[] ="MULTIPLE";
6821 $$ = s; } // LOCK ON phrase
6822 | NEAREST_AWAY_FROM_ZERO { static char s[] ="NEAREST-AWAY-FROM-ZERO";
6823 $$ = s; } // INTERMEDIATE ROUNDING clause and ROUNDED phrase
6824 | NEAREST_EVEN { static char s[] ="NEAREST-EVEN";
6825 $$ = s; } // INTERMEDIATE ROUNDING clause and ROUNDED phrase
6826 | NEAREST_TOWARD_ZERO { static char s[] ="NEAREST-TOWARD-ZERO";
6827 $$ = s; } // INTERMEDIATE ROUNDING clause and ROUNDED phrase
6828 | NONE { static char s[] ="NONE";
6829 $$ = s; } // DEFAULT clause
6830 | NORMAL { static char s[] ="NORMAL";
6831 $$ = s; } // STOP statement
6832 | NUMBERS { static char s[] ="NUMBERS";
6833 $$ = s; } // COLUMN clause and LINE clause
6834 | ONLY { static char s[] ="ONLY";
6835 $$ = s; } // Object-view, SHARING clause, SHARING phrase, and USAGE clause
6836 | PREFIXED { static char s[] ="PREFIXED";
6837 $$ = s; } // DYNAMIC LENGTH STRUCTURE clause
6838 | PREVIOUS { static char s[] ="PREVIOUS";
6839 $$ = s; } // READ statement
6840 | PROHIBITED { static char s[] ="PROHIBITED";
6841 $$ = s; } // INTERMEDIATE ROUNDING clause and ROUNDED phrase
6842 | RECURSIVE { static char s[] ="RECURSIVE";
6843 $$ = s; } // PROGRAM-ID paragraph
6844 | RELATION { static char s[] ="RELATION";
6845 $$ = s; } // VALIDATE-STATUS clause
6846 | REQUIRED { static char s[] ="REQUIRED";
6847 $$ = s; } // screen description entry
6848 | REVERSE_VIDEO { static char s[] ="REVERSE-VIDEO";
6849 $$ = s; } // screen description entry and SET attribute statement
6850 | ROUNDING { static char s[] ="ROUNDING";
6851 $$ = s; } // OPTIONS paragraph
6852 | SECONDS { static char s[] ="SECONDS";
6853 $$ = s; } // RETRY phrase
6854 | SECURE { static char s[] ="SECURE";
6855 $$ = s; } // screen description entry
6856 | SHORT { static char s[] ="SHORT";
6857 $$ = s; } // DYNAMIC LENGTH STRUCTURE clause
6858 | SIGNED_kw { static char s[] ="SIGNED";
6859 $$ = s; } // DYNAMIC LENGTH STRUCTURE clause and USAGE clause
6860 | STANDARD_BINARY { static char s[] ="STANDARD-BINARY";
6861 $$ = s; } // ARITHMETIC clause
6862 | STANDARD_DECIMAL { static char s[] ="STANDARD-DECIMAL";
6863 $$ = s; } // ARITHMETIC clause
6864 | STATEMENT { static char s[] ="STATEMENT";
6865 $$ = s; } // RESUME statement
6866 | STEP { static char s[] ="STEP";
6867 $$ = s; } // OCCURS clause
6868 | STRONG { static char s[] ="STRONG";
6869 $$ = s; } // TYPEDEF clause
6870 | STRUCTURE { static char s[] ="STRUCTURE";
6871 $$ = s; } // DYNAMIC LENGTH STRUCTURE clause
6872 | SYMBOL { static char s[] ="SYMBOL";
6873 $$ = s; } // CURRENCY clause
6874 | TOWARD_GREATER { static char s[] ="TOWARD-GREATER";
6875 $$ = s; } // ROUNDED phrase
6876 | TOWARD_LESSER { static char s[] ="TOWARD-LESSER";
6877 $$ = s; } // ROUNDED phrase
6878 | TRUNCATION { static char s[] ="TRUNCATION";
6879 $$ = s; } // INTERMEDIATE ROUNDING clause and ROUNDED phrase
6880 | UCS_4 { static char s[] ="UCS-4";
6881 $$ = s; } // ALPHABET clause
6882 | UNDERLINE { static char s[] ="UNDERLINE";
6883 $$ = s; } // screen description entry and SET attribute statement
6884 | UNSIGNED_kw { static char s[] ="UNSIGNED";
6885 $$ = s; } // USAGE clause
6886 | UTF_8 { static char s[] ="UTF-8";
6887 $$ = s; } // ALPHABET clause
6888 | UTF_16 { static char s[] ="UTF-16";
6889 $$ = s; } // ALPHABET clause
6890 | YYYYDDD { static char s[] ="YYYYDDD";
6891 $$ = s; } // ACCEPT statement
6892 | YYYYMMDD { static char s[] ="YYYYMMDD";
6893 $$ = s; } // ACCEPT statement
6894 ;
6895
6896 move: MOVE scalar TO move_tgts[tgts]
6897 {
6898 statement_begin(@1, MOVE);
6899 if( $scalar->field->type == FldIndex ) {
6900 error_msg(@1, "%qs cannot be MOVEd because it is an %<INDEX%>",
6901 name_of($scalar->field) );
6902 YYERROR;
6903 }
6904 if( !parser_move2($tgts, *$scalar) ) { YYERROR; }
6905 }
6906 | MOVE all literalism[input] TO move_tgts[tgts]
6907 {
6908 statement_begin(@1, MOVE);
6909 struct cbl_refer_t *src = new_reference(new_literal($input,
6910 quoted_e));
6911 src->all = $all;
6912 if( !parser_move2($tgts, *src) ) { YYERROR; }
6913 }
6914 | MOVE all spaces_etc[src] TO move_tgts[tgts]
6915 {
6916 statement_begin(@1, MOVE);
6917 cbl_field_t *field;
6918 auto p = std::find_if( $tgts->targets.begin(),
6919 $tgts->targets.end(),
6920 [&field]( const auto& num_result ) {
6921 const cbl_refer_t& tgt = num_result.refer;
6922 field = tgt.field;
6923 return is_numeric(tgt.field);
6924 } );
6925
6926 if( p != $tgts->targets.end() ) {
6927 error_msg(@src, "cannot MOVE %s "
6928 "to numeric receiving field %s",
6929 constant_of(constant_index($src))->name,
6930 field->name );
6931 YYERROR;
6932 }
6933
6934 struct cbl_field_t* src = constant_of(constant_index($src));
6935 if( !parser_move2($tgts, src) ) { YYERROR; }
6936 }
6937 | MOVE all signed_literal[lit] TO move_tgts[tgts]
6938 {
6939 statement_begin(@1, MOVE);
6940 cbl_refer_t src( $lit, $all);
6941 if( !parser_move2($tgts, src) ) { YYERROR; }
6942 }
6943
6944 | MOVE intrinsic_call TO move_tgts[tgts]
6945 {
6946 statement_begin(@1, MOVE);
6947 if( !parser_move2($tgts, *$2) ) { YYERROR; }
6948 }
6949
6950 | MOVE CORRESPONDING scalar[from] TO scalar[to]
6951 {
6952 statement_begin(@1, MOVE);
6953 if( $from->field->type != FldGroup ) {
6954 error_msg(@from, "%s does not name a group", $from->name());
6955 YYERROR;
6956 }
6957 if( $to->field->type != FldGroup ) {
6958 error_msg(@to, "%s does not name a group", $to->name());
6959 YYERROR;
6960 }
6961
6962 if( !move_corresponding(*$to, *$from) ) {
6963 yywarn( "%s and %s have no corresponding fields",
6964 $from->field->name, $to->field->name );
6965 }
6966 }
6967 ;
6968
6969 move_tgts: move_tgt[tgt] {
6970 $$ = new tgt_list_t;
6971 if( $tgt ) list_add($$->targets, *$tgt, current_rounded_mode());
6972 }
6973 | move_tgts move_tgt[tgt]
6974 {
6975 if( $tgt ) list_add($1->targets, *$tgt, current_rounded_mode());
6976 }
6977 ;
6978 move_tgt: scalar[tgt] {
6979 if( is_literal($tgt->field) ) {
6980 auto litcon = $tgt->field->name[0] == '_'? "literal" : "constant";
6981 error_msg(@1, "%s is a %s", name_of($tgt->field), litcon);
6982 }
6983 }
6984 | literal {
6985 const auto& field(*$1);
6986 static char buf[32];
6987 const char *value_str( name_of($literal) );
6988 if( is_numeric($1) )
6989 {
6990 REAL_VALUE_TYPE val = TREE_REAL_CST (field.data.value_of());
6991 int ival = (int)real_to_integer (&val);
6992 val = real_value_truncate (TYPE_MODE (float_type_node),
6993 val);
6994 REAL_VALUE_TYPE rival;
6995 real_from_integer (&rival, VOIDmode, ival, SIGNED);
6996 if( real_identical (&val, &rival) ) {
6997 sprintf(buf, "%d", ival);
6998 value_str = buf;
6999 }
7000 }
7001 auto litcon = field.name[0] == '_'? "literal" : "constant";
7002 error_msg(@literal, "%s is a %s", value_str, litcon);
7003 $$ = NULL;
7004 }
7005 | error
7006 {
7007 static const char * error_at;
7008 if( error_at != yytext ) { // avoid repeated message
7009 error_at = yytext;
7010 error_msg(first_line_of(@1), "invalid receiving operand");
7011 }
7012 $$ = NULL;
7013 }
7014 ;
7015
7016 multiply: multiply_impl end_multiply { ast_multiply($1); }
7017 | multiply_cond end_multiply { ast_multiply($1); }
7018 ;
7019 multiply_impl: MULTIPLY multiply_body
7020 {
7021 statement_begin(@1, MULTIPLY);
7022 $$ = $2;
7023 }
7024 ;
7025 multiply_cond: MULTIPLY multiply_body[body] arith_errs[err]
7026 {
7027 statement_begin(@1, MULTIPLY);
7028 $$ = $body;
7029 $$->on_error = $err.on_error;
7030 $$->not_error = $err.not_error;
7031 }
7032 ;
7033 end_multiply: %empty %prec MULTIPLY
7034 | END_MULTIPLY
7035 ;
7036
7037 multiply_body: num_operand BY rnames
7038 {
7039 $$ = new arith_t(no_giving_e);
7040 $$->A.push_back(*$num_operand);
7041 std::copy( rhs.begin(),
7042 rhs.end(), back_inserter($$->tgts) );
7043 rhs.clear();
7044 }
7045 | num_operand BY signed_literal[lit]
7046 {
7047 error_msg(@lit, "%s is not a receiving field", name_of($lit));
7048 YYERROR;
7049 }
7050 | num_operand[a] BY num_operand[b] GIVING rnames
7051 {
7052 $$ = new arith_t(giving_e);
7053 $$->A.push_back(*$a);
7054 $$->B.push_back(*$b);
7055 std::copy( rhs.begin(),
7056 rhs.end(), back_inserter($$->tgts) );
7057 rhs.clear();
7058 }
7059 | num_operand[a] BY num_operand[b] GIVING signed_literal[lit]
7060 {
7061 error_msg(@lit, "%s is not a receiving field", name_of($lit));
7062 YYERROR;
7063 }
7064 | LITERAL
7065 {
7066 error_msg(@1, "invalid string operand '%s'", $1.data);
7067 YYERROR;
7068 }
7069 ;
7070
7071 arith_errs: arith_err[a] statements %prec ADD
7072 {
7073 assert( $a.on_error || $a.not_error );
7074 assert( ! ($a.on_error && $a.not_error) );
7075 cbl_label_t *tgt = $a.on_error? $a.on_error : $a.not_error;
7076 parser_arith_error_end(tgt);
7077 }
7078 | arith_errs[a] arith_err[b] statements %prec ADD
7079 {
7080 if( $a.on_error && $a.not_error ) {
7081 error_msg(@1, "too many ON ERROR clauses");
7082 YYERROR;
7083 }
7084 // "ON" and "NOT ON" could be reversed, but not duplicated.
7085 if( $a.on_error && $b.on_error ) {
7086 error_msg(@1, "duplicate ON ERROR clauses");
7087 YYERROR;
7088 }
7089 if( $a.not_error && $b.not_error ) {
7090 error_msg(@1, "duplicate NOT ON ERROR clauses");
7091 YYERROR;
7092 }
7093 $$ = $a;
7094 if( $b.on_error ) {
7095 $$.on_error = $b.on_error;
7096 assert($a.not_error);
7097 } else {
7098 $$.not_error = $b.not_error;
7099 assert($a.on_error);
7100 }
7101 assert( $b.on_error || $b.not_error );
7102 assert( ! ($b.on_error && $b.not_error) );
7103 cbl_label_t *tgt = $b.on_error? $b.on_error : $b.not_error;
7104 parser_arith_error_end(tgt);
7105 }
7106 ;
7107
7108 arith_err: SIZE_ERROR
7109 {
7110 assert( $1 == ERROR || $1 == NOT );
7111 $$.on_error = NULL;
7112 $$.not_error = NULL;
7113 cbl_label_t **ptgt = $1 == NOT? &$$.not_error : &$$.on_error;
7114 if( current.in_compute() ) {
7115 *ptgt = $1 == NOT?
7116 current.compute_not_error() : current.compute_on_error();
7117 } else {
7118 *ptgt = label_add(LblArith, uniq_label("arith"), @1.first_line);
7119 }
7120 (*ptgt)->lain = @1.first_line;
7121 parser_arith_error( *ptgt );
7122 }
7123 ;
7124
7125 /*
7126 * Relational operator Can be written
7127 * IS GREATER THAN IS >
7128 * IS NOT GREATER THAN IS NOT >
7129 * IS LESS THAN IS <
7130 * IS NOT LESS THAN IS NOT <
7131 * IS EQUAL TO IS =
7132 * IS NOT EQUAL TO IS NOT =
7133 * IS GREATER THAN OR EQUAL TO IS >=
7134 * IS LESS THAN OR EQUAL TO IS <=
7135 *
7136 * The lexer returns simple tokens.
7137 */
7138
7139 relop: '<' { $$ = '<'; }
7140 | LE { $$ = LE; }
7141 | '=' { $$ = '='; }
7142 | NE { $$ = NE; }
7143 | GE { $$ = GE; }
7144 | '>' { $$ = '>'; }
7145 ;
7146
7147 rnames: scalar rounded
7148 {
7149 list_add( rhs, *$scalar, $rounded );
7150 }
7151 | rnames scalar rounded
7152 {
7153 cbl_num_result_t arg = { static_cast<cbl_round_t>($rounded),
7154 *$scalar };
7155 rhs.push_back(arg);
7156 }
7157 ;
7158
7159 sum: num_operand { $$ = new refer_list_t($num_operand); }
7160 | sum num_operand { $$->push_back($num_operand); }
7161 ;
7162
7163 num_operand: scalar
7164 | signed_literal { $$ = new_reference($1); }
7165 | intrinsic_call
7166 ;
7167
7168 num_value: scalar // might actually be a string
7169 | intrinsic_call
7170 | num_literal { $$ = new_reference($1); }
7171 | ADDRESS OF scalar {$$ = $scalar; $$->addr_of = true; }
7172 | DETAIL OF scalar {$$ = $scalar; }
7173 | LENGTH_OF binary_type[size] {
7174 location_set(@1);
7175 $$ = new cbl_refer_t( new_tempnumeric() );
7176 $$->field->clear_attr(signable_e);
7177 if( dialect_gcc() ) {
7178 dialect_error(@1, "LENGTH OF", "ibm");
7179 }
7180 parser_set_numeric($$->field, $size);
7181 }
7182 | LENGTH_OF name[val] {
7183 location_set(@1);
7184 $$ = new cbl_refer_t( new_tempnumeric() );
7185 $$->field->clear_attr(signable_e);
7186 if( dialect_gcc() ) {
7187 dialect_error(@1, "LENGTH OF", "ibm");
7188 }
7189 parser_set_numeric($$->field, $val->data.capacity);
7190 }
7191 | LENGTH_OF name[val] subscripts[subs] {
7192 location_set(@1);
7193 $$ = new cbl_refer_t( new_tempnumeric() );
7194 $$->field->clear_attr(signable_e);
7195 if( dialect_gcc() ) {
7196 dialect_error(@1, "LENGTH OF", "ibm");
7197 }
7198 if( 0 == dimensions($val) ) {
7199 cbl_refer_t r1($val);
7200 subscript_dimension_error( @subs, $subs->refers.size(), &r1 );
7201 }
7202 parser_set_numeric($$->field, $val->data.capacity);
7203 }
7204 ;
7205
7206
7207 /*
7208 * Constant Compile-time Expressions
7209 */
7210
7211 /* cce_cond_expr: cce_bool_expr { $$ = $1 == 0? false : true; } */
7212 /* ; */
7213 /* cce_bool_expr: cce_and */
7214 /* | cce_bool_expr OR cce_and { $$ = $1 || $3; } */
7215 /* ; */
7216 /* cce_and: cce_reloper */
7217 /* | cce_and AND cce_reloper { $$ = $1 && $3; } */
7218 /* ; */
7219 /* cce_reloper: cce_relexpr */
7220 /* | NOT cce_relexpr { $$ = $2 != 0; } */
7221 /* ; */
7222 /* cce_relexpr: cce_expr */
7223 /* | cce_relexpr '<' cce_expr { $$ = $1 < $3; } */
7224 /* | cce_relexpr LE cce_expr { $$ = $1 <= $3; } */
7225 /* | cce_relexpr '=' cce_expr { $$ = $1 == $3; } */
7226 /* | cce_relexpr NE cce_expr { $$ = $1 != $3; } */
7227 /* | cce_relexpr GE cce_expr { $$ = $1 >= $3; } */
7228 /* | cce_relexpr '>' cce_expr { $$ = $1 > $3; } */
7229 /* ; */
7230
7231 cce_expr: cce_factor
7232 | cce_expr '+' cce_expr {
7233 real_arithmetic (&$$, PLUS_EXPR, &$1, &$3);
7234 real_convert (&$$, TYPE_MODE (float128_type_node), &$$);
7235 }
7236 | cce_expr '-' cce_expr {
7237 real_arithmetic (&$$, MINUS_EXPR, &$1, &$3);
7238 real_convert (&$$, TYPE_MODE (float128_type_node), &$$);
7239 }
7240 | cce_expr '*' cce_expr {
7241 real_arithmetic (&$$, MULT_EXPR, &$1, &$3);
7242 real_convert (&$$, TYPE_MODE (float128_type_node), &$$);
7243 }
7244 | cce_expr '/' cce_expr {
7245 real_arithmetic (&$$, RDIV_EXPR, &$1, &$3);
7246 real_convert (&$$, TYPE_MODE (float128_type_node), &$$);
7247 }
7248 | '+' cce_expr %prec NEG { $$ = $2; }
7249 | '-' cce_expr %prec NEG { $$ = real_value_negate (&$2); }
7250 | '(' cce_expr ')' { $$ = $2; }
7251 ;
7252
7253 cce_factor: NUMSTR {
7254 /* real_from_string does not allow arbitrary radix. */
7255 // When DECIMAL IS COMMA, commas act as decimal points.
7256 gcc_assert($1.radix == decimal_e);
7257 auto p = $1.string, pend = p + strlen(p);
7258 std::replace(p, pend, ',', '.');
7259 real_from_string3( &$$, $1.string,
7260 TYPE_MODE (float128_type_node) );
7261 }
7262 ;
7263
7264 /*
7265 * End Constant Compile-time Expressions
7266 */
7267
7268 section_name: NAME section_kw '.'
7269 {
7270 statement_begin(@1, SECTION);
7271 $$ = label_add(@1, LblSection, $1);
7272 ast_enter_section($$);
7273 apply_declaratives();
7274 }
7275 | NAME section_kw // lexer swallows '.' before USE
7276 <label>{
7277 statement_begin(@1, SECTION);
7278 $$ = label_add(@1, LblSection, $1);
7279 ast_enter_section($$);
7280 apply_declaratives();
7281 } [label]
7282 cdf_use dot
7283 {
7284 $$ = $label;
7285 }
7286 ;
7287
7288 section_kw: SECTION
7289 {
7290 if( $1 ) {
7291 if( *$1 == '-' ) {
7292 error_msg(@1, "SECTION segment %qs is negative", $1);
7293 } else {
7294 if( dialect_ibm() ) {
7295 int sectno;
7296 sscanf($1, "%d", &sectno);
7297 if( ! (0 <= sectno && sectno <= 99) ) {
7298 error_msg(@1, "SECTION segment %qs must be 0-99", $1);
7299 } else {
7300 if(false) { // stand-in for warning, someday.
7301 yywarn("SECTION segment %qs was ignored", $1);
7302 }
7303 }
7304 } else {
7305 cbl_unimplemented("SECTION segment %qs is not ISO syntax", $1);
7306 }
7307 }
7308 }
7309 }
7310 | SECTION error
7311 {
7312 error_msg(@1, "unknown section qualifier");
7313 }
7314 ;
7315
7316 stop: STOP RUN exit_with
7317 {
7318 statement_begin(@1, STOP);
7319 parser_see_stop_run( *$exit_with, NULL );
7320 }
7321 | STOP NUMSTR[status] // IBM syntax
7322 {
7323 statement_begin(@1, STOP);
7324 if( ! dialect_ibm() ) {
7325 dialect_error(@2, "STOP <number> is not ISO syntax,", "ibm");
7326 YYERROR;
7327 }
7328 cbl_refer_t status( new_literal($status.string, $status.radix) );
7329 parser_see_stop_run( status, NULL );
7330 }
7331 | STOP LITERAL[name] // CCVS-85 && IBM syntax
7332 {
7333 statement_begin(@1, STOP);
7334 const char *name = string_of($name);
7335 if( ! name ) {
7336 error_msg(@name, "'%s' has embedded NUL", $name.data);
7337 YYERROR;
7338 }
7339 parser_see_stop_run( literally_zero, $name.data );
7340 }
7341 ;
7342 stop_status: status { $$ = NULL; }
7343 | status scalar { $$ = $2; }
7344 | status NUMSTR {
7345 $$ = new_reference(new_literal($2.string, $2.radix));
7346 }
7347 ;
7348
7349 subscripts: LPAREN subscript_exprs ')' {
7350 $$ = $2;
7351 const auto& exprs( $$->refers );
7352 bool ok = std::all_of( exprs.begin(), exprs.end(),
7353 []( const auto& refer ) {
7354 return is_numeric(refer.field);
7355 } );
7356 if( ! ok ) {
7357 int i=0;
7358 for( auto refer : exprs ) {
7359 if( ! is_numeric(refer.field) ) {
7360 error_msg(@1, "subscript %d, %s, is not numeric (%s)",
7361 ++i, name_of(refer.field),
7362 cbl_field_type_str(refer.field->type) + 3);
7363 }
7364 }
7365 YYERROR;
7366 }
7367 }
7368 ;
7369 subscript_exprs: expr
7370 {
7371 if( ! require_integer(@expr, *$expr) ) YYERROR;
7372 $$ = new refer_list_t($expr);
7373 }
7374 | subscript_exprs expr {
7375 if( $1->size() == MAXIMUM_TABLE_DIMENSIONS ) {
7376 error_msg(@1, "table dimensions limited to %d",
7377 MAXIMUM_TABLE_DIMENSIONS);
7378 YYERROR;
7379 }
7380 if( ! require_integer(@expr, *$expr) ) YYERROR;
7381 $1->push_back($2); $$ = $1;
7382 }
7383 | ALL {
7384 auto ref = new_reference(constant_of(constant_index(ZERO)));
7385 $$ = new refer_list_t(ref);
7386 }
7387 ;
7388
7389 arg_list: any_arg { $$ = new refer_list_t($1); }
7390 | arg_list any_arg { $1->push_back($2); $$ = $1; }
7391 ;
7392 any_arg: expr
7393 | LITERAL {$$ = new_reference(new_literal($1, quoted_e)); }
7394 ;
7395
7396 /*
7397 * Because num_literal includes ZERO, this grammar
7398 * allows -ZERO and +ZERO. FWIW.
7399 */
7400 signed_literal: num_literal
7401 | '+' num_literal { $$ = $2; }
7402 | '-' num_literal
7403 {
7404 $$ = new_tempnumeric();
7405 struct cbl_field_t *zero = constant_of(constant_index(ZERO));
7406 parser_subtract( $$, zero, $2, current_rounded_mode() );
7407 }
7408 | LENGTH_OF binary_type[size] {
7409 location_set(@1);
7410 $$ = new_tempnumeric();
7411 $$->clear_attr(signable_e);
7412 if( dialect_gcc() ) {
7413 dialect_error(@1, "LENGTH OF", "ibm");
7414 }
7415 parser_set_numeric($$, $size);
7416 }
7417 | LENGTH_OF name[val] {
7418 location_set(@1);
7419 $$ = new_tempnumeric();
7420 $$->clear_attr(signable_e);
7421 if( dialect_gcc() ) {
7422 dialect_error(@1, "LENGTH OF", "ibm");
7423 }
7424 parser_set_numeric($$, $val->data.capacity);
7425 }
7426 | LENGTH_OF name[val] subscripts[subs] {
7427 location_set(@1);
7428 $$ = new_tempnumeric();
7429 $$->clear_attr(signable_e);
7430 if( dialect_gcc() ) {
7431 dialect_error(@1, "LENGTH OF", "ibm");
7432 }
7433 if( 0 == dimensions($val) ) {
7434 cbl_refer_t r1($val);
7435 subscript_dimension_error( @subs, $subs->refers.size(), &r1 );
7436 }
7437 parser_set_numeric($$, $val->data.capacity);
7438 }
7439 ;
7440
7441 num_literal: NUMSTR { $$ = new_literal($1.string, $1.radix); }
7442 | ZERO { $$ = constant_of(constant_index(ZERO)); }
7443 ;
7444
7445 open: OPEN { statement_begin(@1, OPEN); } open_files
7446 ;
7447 open_files: open_file
7448 | open_files open_file
7449 ;
7450 open_file: open_io[mode] filenames {
7451 size_t n = $2->files.size();
7452 parser_file_open( n, use_list($2->files, false), $mode );
7453 current.declaratives_evaluate($2->files);
7454 $2->files.clear();
7455 }
7456 ;
7457 open_io: INPUT { $$ = 'r'; }
7458 | OUTPUT { $$ = 'w'; }
7459 | EXTEND { $$ = 'a'; }
7460 | IO { $$ = '+'; }
7461 ;
7462
7463 close: CLOSE { statement_begin(@1, CLOSE); } close_files
7464 ;
7465 close_files: close_file
7466 | close_files close_file
7467 ;
7468 close_file: NAME close_how
7469 {
7470 struct symbol_elem_t *e = symbol_file(PROGRAM, $1);
7471 if( !e ) {
7472 error_msg(@1, "invalid file name '%s'", $1);
7473 YYERROR;
7474 }
7475 auto how = static_cast<file_close_how_t>($close_how);
7476 bool reel_unit = (file_close_reel_unit_e & $close_how) > 0;
7477 auto file = cbl_file_of(e);
7478 switch( file->org ) {
7479 case file_disorganized_e:
7480 gcc_unreachable();
7481 break;
7482 case file_sequential_e:
7483 case file_line_sequential_e:
7484 break;
7485 case file_indexed_e:;
7486 case file_relative_e:
7487 if( $close_how & ~file_close_with_lock_e ) {
7488 error_msg(@1, "INDEXED or RELATIVE file "
7489 "closed with incompatible qualifier" );
7490 YYERROR;
7491 }
7492 break;
7493 }
7494 if(reel_unit)
7495 {
7496 how = file_close_reel_unit_e;
7497 }
7498 parser_file_close( file, how );
7499 current.declaratives_evaluate( file );
7500 }
7501 ;
7502 close_how: %empty { $$ = file_close_no_how_e; }
7503 | reel_unit { $$ = file_close_reel_unit_e; }
7504 | reel_unit for_kw REMOVAL {
7505 $$ = file_close_reel_unit_e | file_close_removal_e;
7506 }
7507 | reel_unit WITH NO REWIND {
7508 $$ = file_close_reel_unit_e | file_close_no_rewind_e;
7509 }
7510 | with NO REWIND { $$ = file_close_no_rewind_e; }
7511 | with LOCK { $$ = file_close_with_lock_e; }
7512 ;
7513 reel_unit: REEL
7514 | UNIT
7515 ;
7516 for_kw: %empty
7517 | FOR
7518 ;
7519
7520 perform: perform_verb perform_proc { perform_free(); }
7521 | perform_verb perform_stmts {
7522 perform_ec_cleanup();
7523 perform_free();
7524 }
7525 | perform_verb perform_except {
7526 perform_ec_cleanup();
7527 perform_free();
7528 }
7529 ;
7530
7531 perform_stmts: perform_until perform_inline[in]
7532 {
7533 std::vector <cbl_perform_vary_t> varys($in->varys.size());
7534 std::copy( $in->varys.begin(), $in->varys.end(), varys.begin() );
7535
7536 parser_perform_until(&$in->tgt, $in->before,
7537 varys.size(), varys.data());
7538 }
7539 | perform_vary perform_inline[in]
7540 {
7541 struct perform_t *p = $in;
7542 std::vector <cbl_perform_vary_t> varys(p->varys.size());
7543 std::copy( p->varys.begin(), p->varys.end(), varys.begin() );
7544
7545 parser_perform_until(&$in->tgt, $in->before,
7546 varys.size(), varys.data());
7547 }
7548 | perform_times perform_inline[in]
7549 {
7550 parser_perform_inline_times(&$in->tgt, *$perform_times);
7551 }
7552 | perform_inline[in]
7553 {
7554 parser_perform_inline_times(&$in->tgt, literally_one);
7555 }
7556 ;
7557
7558 perform_proc: perform_names %prec NAME
7559 {
7560 struct perform_t *p = perform_current();
7561 if( yydebug ) p->tgt.dump();
7562 parser_perform(&p->tgt, NULL);
7563 }
7564 | perform_names num_operand TIMES
7565 {
7566 struct perform_t *p = perform_current();
7567 if( yydebug ) p->tgt.dump();
7568 parser_perform(&p->tgt, *$2);
7569 }
7570 | perform_names perform_until
7571 {
7572 struct perform_t *p = perform_current();
7573 if( yydebug ) p->tgt.dump();
7574 assert(1 == p->varys.size());
7575 parser_perform_until( &p->tgt, p->before, 1, &p->varys.front() );
7576 }
7577 | perform_names perform_vary
7578 {
7579 struct perform_t *p = perform_current();
7580 if( yydebug ) p->tgt.dump();
7581
7582 std::vector <cbl_perform_vary_t> varys(p->varys.size());
7583 std::copy( p->varys.begin(), p->varys.end(), varys.begin() );
7584
7585 parser_perform_until( &p->tgt, p->before, varys.size(), varys.data() );
7586 }
7587 ;
7588
7589 perform_names: label_1[para]
7590 {
7591 perform_tgt_set($para);
7592 }
7593 | label_1[para1] THRU label_1[para2]
7594 {
7595 perform_tgt_set($para1, $para2);
7596 }
7597 ;
7598
7599 perform_times: num_operand TIMES
7600 {
7601 $$ = $1;
7602 }
7603 ;
7604
7605 perform_vary: test_before varying vary_afters
7606 {
7607 perform_current()->before = $1 == BEFORE;
7608 }
7609 | varying vary_afters
7610 | test_before varying
7611 {
7612 perform_current()->before = $1 == BEFORE;
7613 }
7614 | varying
7615 ;
7616
7617 perform_verb: PERFORM {
7618 statement_begin(@1, PERFORM);
7619 $$ = perform_alloc();
7620 }
7621 ;
7622
7623 perform_until: test_before perform_cond
7624 {
7625 struct perform_t *p = perform_current();
7626 struct cbl_perform_vary_t vary;
7627
7628 p->before = $1 == BEFORE;
7629 vary.until = $2;
7630 p->varys.push_back(vary);
7631 }
7632 | perform_cond
7633 {
7634 struct perform_t *p = perform_current();
7635 struct cbl_perform_vary_t vary;
7636
7637 vary.until = $1;
7638 p->varys.push_back(vary);
7639 }
7640 ;
7641 perform_cond: UNTIL { parser_perform_conditional( &perform_current()->tgt); }
7642 bool_expr
7643 {
7644 parser_perform_conditional_end( &perform_current()->tgt);
7645 if( !is_conditional($bool_expr) ) {
7646 error_msg(@1, "%s is not a condition expression",
7647 name_of($bool_expr->field));
7648 YYERROR;
7649 }
7650 $$ = $bool_expr->cond();
7651 }
7652 ;
7653
7654 perform_inline: perform_start statements END_PERFORM
7655 {
7656 location_set(@END_PERFORM);
7657 parser_sleep(*cbl_refer_t::empty());
7658 $$ = perform_current();
7659 if( $perform_start == LOCATION ) {
7660 error_msg(@1, "LOCATION not valid with PERFORM Format 2");
7661 }
7662 }
7663 | perform_start END_PERFORM
7664 {
7665 location_set(@END_PERFORM);
7666 parser_sleep(*cbl_refer_t::empty());
7667 $$ = perform_current();
7668 if( $perform_start == LOCATION ) {
7669 error_msg(@1, "LOCATION not valid with PERFORM Format 2");
7670 }
7671 }
7672 ;
7673
7674 perform_start: %empty %prec LOCATION {
7675 perform_ec_setup();
7676 $$ = 0;
7677 }
7678 | with LOCATION {
7679 perform_ec_setup();
7680 $$ = LOCATION;
7681 }
7682 ;
7683
7684 perform_except: perform_start
7685 statements
7686 {
7687 auto perf = perform_current();
7688 parser_perform_inline_times(&perf->tgt, literally_one);
7689 }
7690 perform_when // paragraphs
7691 perform_ec_other // paragraph
7692 perform_ec_common // paragraph
7693 {
7694 auto perf = perform_current();
7695 parser_label_goto(perf->ec_labels.finally);
7696 }
7697 perform_ec_finally
7698 END_PERFORM
7699 {
7700 cbl_unimplemented("PERFORM Format 3");
7701 }
7702 ;
7703
7704 perform_when: perform_when1
7705 | perform_when perform_when1
7706 ;
7707 perform_when1: WHEN perform_ec {
7708 // accumulate handlers and their paragraphs
7709 auto perf = perform_current();
7710 auto when = perf->ec_labels.new_label(LblParagraph, "when");
7711 for( auto& dcl : $perform_ec->elems ) {
7712 // use section to hold paragraph
7713 dcl->section = symbol_index(symbol_elem_of(when));
7714 }
7715 std::transform( $perform_ec->elems.begin(),
7716 $perform_ec->elems.end(),
7717 std::back_inserter(perf->dcls),
7718 []( const cbl_declarative_t *p ) {
7719 return *p;
7720 } );
7721 ast_enter_paragraph(when);
7722 }
7723 statements {
7724 parser_exit_paragraph();
7725 }
7726 ;
7727
7728 perform_ec: EXCEPTION filenames {
7729 auto dcls = new declarative_list_t;
7730 auto p = $filenames->files.begin();
7731 auto pend = p;
7732 while( pend != $filenames->files.end() ) {
7733 for( size_t i=0; i < COUNT_OF(cbl_declarative_t::files); i++ ) {
7734 if( ++pend == $filenames->files.end() ) break;
7735 }
7736 std::list<size_t> files;
7737 std::transform( p, pend, std::back_inserter(files),
7738 []( const cbl_file_t* f ) {
7739 return symbol_index(symbol_elem_of(f)); } );
7740
7741 auto dcl = new cbl_declarative_t(0, ec_io_e, files, file_mode_none_e);
7742 dcls->elems.push_back(dcl);
7743 }
7744 $$ = dcls;
7745 }
7746 | EXCEPTION io_mode {
7747 auto dcl = new cbl_declarative_t($io_mode);
7748 $$ = new declarative_list_t(dcl);
7749 }
7750 | except_names {
7751 auto dcls = new declarative_list_t;
7752 const ec_list_t * ecs($except_names);
7753 // one cbl_declarative_t per EC
7754 std::transform( ecs->elems.begin(), ecs->elems.end(),
7755 std::back_inserter(dcls->elems),
7756 []( ec_type_t ec )
7757 {
7758 return new cbl_declarative_t(ec);
7759 } );
7760 $$ = dcls;
7761 }
7762 | except_files {
7763 // one cbl_declarative_t per 16 files
7764 auto dcls = new declarative_list_t;
7765 for( auto p = $except_files->elems.begin();
7766 p != $except_files->elems.end(); ) {
7767 auto dcl = new cbl_declarative_t;
7768 for( auto file = dcl->files;
7769 file < dcl->files + COUNT_OF(dcl->files); file++ ) {
7770 if( p != $except_files->elems.end() ) break;
7771 *file = *p++;
7772 }
7773 dcls->elems.push_back(dcl);
7774 }
7775 $$ = dcls;
7776 }
7777 ;
7778
7779 except_names: except_name { $$ = new ec_list_t($1); }
7780 | except_names except_name {
7781 $$ = $1->push_back($2);
7782 }
7783 ;
7784 except_name: EXCEPTION_NAME[ec] {
7785 assert($ec != ec_none_e);
7786 $$ = $1;
7787 }
7788 ;
7789
7790 except_files: except_name[ec] FILE_KW filenames {
7791 assert($ec != ec_none_e);
7792 if( ec_io_e != (ec_io_e & $ec) ) {
7793 error_msg(@1, "%s is not of type EC-I-O",
7794 ec_type_str($ec));
7795 }
7796 $$ = new isym_list_t;
7797 std::list<size_t>& files( $$->elems );
7798 std::transform( $filenames->files.begin(),
7799 $filenames->files.end(),
7800 std::back_inserter(files),
7801 []( const cbl_file_t* f ) {
7802 return symbol_index(symbol_elem_of(f)); } );
7803 }
7804 ;
7805
7806 perform_ec_other:
7807 %empty %prec WHEN {
7808 const auto& ec_labels( perform_current()->ec_labels );
7809 ast_enter_paragraph(ec_labels.other);
7810 parser_exit_paragraph();
7811 }
7812 | WHEN OTHER {
7813 const auto& ec_labels( perform_current()->ec_labels );
7814 ast_enter_paragraph(ec_labels.other);
7815 }
7816 exception statements %prec WHEN {
7817 parser_exit_paragraph();
7818 }
7819 ;
7820 perform_ec_common:
7821 %empty {
7822 const auto& ec_labels( perform_current()->ec_labels );
7823 ast_enter_paragraph(ec_labels.common);
7824 parser_exit_paragraph();
7825 }
7826 | WHEN COMMON {
7827 const auto& ec_labels( perform_current()->ec_labels );
7828 ast_enter_paragraph(ec_labels.common);
7829 }
7830 exception statements {
7831 parser_exit_paragraph();
7832 }
7833 ;
7834 perform_ec_finally:
7835 %empty {
7836 const auto& ec_labels( perform_current()->ec_labels );
7837 ast_enter_paragraph(ec_labels.finally);
7838 parser_exit_paragraph();
7839 parser_label_goto(ec_labels.fini);
7840 }
7841 | FINALLY {
7842 const auto& ec_labels( perform_current()->ec_labels );
7843 ast_enter_paragraph(ec_labels.finally);
7844 }
7845 exception statements {
7846 parser_exit_paragraph();
7847 const auto& ec_labels( perform_current()->ec_labels );
7848 parser_label_goto(ec_labels.fini);
7849 }
7850 ;
7851
7852 test_before: with TEST BEFORE { $$ = BEFORE; }
7853 | with TEST AFTER { $$ = AFTER; }
7854 ;
7855
7856 varying: VARYING num_operand[tgt] FROM num_operand[from] vary_by[by]
7857 perform_cond[until]
7858 {
7859 struct cbl_perform_vary_t vary(*$tgt, *$from, *$by, $until);
7860 perform_current()->varys.push_back(vary);
7861 }
7862 ;
7863
7864 vary_afters: vary_after
7865 | vary_afters vary_after
7866 ;
7867 vary_after: AFTER num_operand[tgt] FROM num_operand[from] vary_by[by]
7868 perform_cond[until]
7869 {
7870 struct cbl_perform_vary_t vary(*$tgt, *$from, *$by, $until);
7871 perform_current()->varys.push_back(vary);
7872 }
7873 ;
7874 vary_by: %empty { $$ = new cbl_refer_t(literally_one); }
7875 | BY num_operand { $$ = $2; }
7876 ;
7877
7878 reserved_value: spaces_etc
7879 | ZERO { $$ = ZERO; }
7880 | NULLS { $$ = NULLS; }
7881 ;
7882 spaces_etc: SPACES { $$ = SPACES; }
7883 | HIGH_VALUES { $$ = HIGH_VALUES; }
7884 | LOW_VALUES { $$ = LOW_VALUES; }
7885 | QUOTES { $$ = QUOTES; }
7886 ;
7887
7888 variable_type: NUMERIC { $$ = NUMERIC; }
7889 | ALPHABETIC { $$ = ALPHABETIC; }
7890 | ALPHABETIC_LOWER { $$ = ALPHABETIC_LOWER; }
7891 | ALPHABETIC_UPPER { $$ = ALPHABETIC_UPPER; }
7892 | DBCS { $$ = DBCS; }
7893 | KANJI { $$ = KANJI; }
7894 ;
7895
7896 subtract: subtract_impl end_subtract { ast_subtract($1); }
7897 | subtract_cond end_subtract { ast_subtract($1); }
7898 ;
7899 subtract_impl: SUBTRACT subtract_body[body]
7900 {
7901 statement_begin(@1, SUBTRACT);
7902 $$ = $body;
7903 }
7904 ;
7905 subtract_cond: SUBTRACT subtract_body[body] arith_errs[err]
7906 {
7907 statement_begin(@1, SUBTRACT);
7908 $body->on_error = $err.on_error;
7909 $body->not_error = $err.not_error;
7910 $$ = $body;
7911 }
7912 ;
7913 end_subtract: %empty %prec SUBTRACT
7914 | END_SUBTRACT
7915 ;
7916
7917 subtract_body: sum FROM rnames
7918 {
7919 $$ = new arith_t(no_giving_e, $sum);
7920 std::copy( rhs.begin(),
7921 rhs.end(), back_inserter($$->tgts) );
7922 rhs.clear();
7923 }
7924 | sum FROM num_operand[input] GIVING rnames
7925 {
7926 $$ = new arith_t(giving_e, $sum);
7927 $$->B.push_back(*$input);
7928 std::copy( rhs.begin(),
7929 rhs.end(), back_inserter($$->tgts) );
7930 rhs.clear();
7931 }
7932 | CORRESPONDING sum FROM rnames
7933 {
7934 corresponding_fields_t pairs =
7935 corresponding_arith_fields( $sum->refers.front().field,
7936 rhs.front().refer.field );
7937 if( pairs.empty() ) {
7938 yywarn( "%s and %s have no corresponding fields",
7939 $sum->refers.front().field->name,
7940 rhs.front().refer.field->name );
7941 }
7942 // First src/tgt elements are templates.
7943 // Their subscripts apply to the correspondents.
7944 $$ = new arith_t(corresponding_e, $sum);
7945 $$->tgts.push_front(rhs.front());
7946 // use arith_t functor to populate A and tgts
7947 *$$ = std::for_each( pairs.begin(), pairs.end(), *$$ );
7948 $$->A.pop_front();
7949 $$->tgts.pop_front();
7950 rhs.clear();
7951 }
7952 ;
7953
7954 vargs: varg { $$ = new vargs_t($varg); }
7955 | vargs[args] varg { $args->push_back($varg); $$ = $args; }
7956 ;
7957
7958 varg: varg1
7959 | ALL varg1 { $$ = $2; $$->all = true; }
7960 ;
7961
7962 varg1: scalar
7963 | varg1a
7964 ;
7965 varg1a: ADDRESS OF scalar {
7966 $$ = $scalar;
7967 $$->addr_of = true;
7968 }
7969 | intrinsic_call
7970 | literal
7971 {
7972 $$ = new_reference($1);
7973 }
7974 | reserved_value
7975 {
7976 $$ = new_reference(constant_of(constant_index($1)));
7977 }
7978 | LENGTH_OF binary_type[size] {
7979 location_set(@1);
7980 $$ = new cbl_refer_t( new_tempnumeric() );
7981 $$->field->clear_attr(signable_e);
7982 if( dialect_gcc() ) {
7983 dialect_error(@1, "LENGTH OF", "ibm");
7984 }
7985 parser_set_numeric($$->field, $size);
7986 }
7987 | LENGTH_OF name[val] {
7988 location_set(@1);
7989 $$ = new cbl_refer_t( new_tempnumeric() );
7990 $$->field->clear_attr(signable_e);
7991 if( dialect_gcc() ) {
7992 dialect_error(@1, "LENGTH OF", "ibm");
7993 }
7994 parser_set_numeric($$->field, $val->size());
7995 }
7996 | LENGTH_OF name[val] subscripts[subs] {
7997 location_set(@1);
7998 $$ = new cbl_refer_t( new_tempnumeric() );
7999 $$->field->clear_attr(signable_e);
8000 if( dialect_gcc() ) {
8001 dialect_error(@1, "LENGTH OF", "ibm");
8002 }
8003 if( 0 == dimensions($val) ) {
8004 cbl_refer_t r1($val);
8005 subscript_dimension_error( @subs, $subs->refers.size(), &r1 );
8006 }
8007 parser_set_numeric($$->field, $val->data.capacity);
8008 }
8009 ;
8010
8011 binary_type: BINARY_INTEGER { $$ = $1.capacity; }
8012 | COMPUTATIONAL { $$ = $1.capacity; }
8013 ;
8014
8015 literal: literalism
8016 {
8017 $$ = $1.isymbol()?
8018 cbl_field_of(symbol_at($1.isymbol()))
8019 :
8020 new_literal($1, quoted_e);
8021 }
8022 | NUMSTR
8023 {
8024 $$ = new_literal($1.string, $1.radix);
8025 }
8026 | DATETIME_FMT
8027 {
8028 $$ = new_literal(strlen($1), $1, quoted_e);
8029 }
8030 | DATE_FMT
8031 {
8032 $$ = new_literal(strlen($1), $1, quoted_e);
8033 }
8034 | TIME_FMT
8035 {
8036 $$ = new_literal(strlen($1), $1, quoted_e);
8037 }
8038 ;
8039
8040 raise: RAISE EXCEPTION NAME
8041 {
8042 auto ec = ec_type_of($NAME);
8043 if( ec == ec_none_e ) {
8044 error_msg(@NAME, "not an EXCEPTION CONDITION: %s", $NAME);
8045 YYERROR;
8046 }
8047 statement_begin(@$, RAISE);
8048 parser_exception_raise(ec);
8049 }
8050 | RAISE NAME
8051 {
8052 auto ec = ec_type_of($NAME);
8053 if( ec != ec_none_e ) {
8054 error_msg(@NAME, "RAISE EXCEPTION required for "
8055 "EXCEPTION CONDITION: %s", $NAME);
8056 YYERROR;
8057 }
8058 cbl_unimplemented("RAISE %<EXCEPTION OBJECT%>");
8059 YYERROR;
8060 }
8061 ;
8062
8063 read: read_file
8064 {
8065 current.declaratives_evaluate($1.file);
8066 }
8067 ;
8068
8069 read_file: READ read_body {
8070 file_read_args.call_parser_file_read();
8071 $$.file = $2; $$.handled = FsSuccess;
8072 }
8073 | READ read_body END_READ {
8074 file_read_args.call_parser_file_read();
8075 $$.file = $2; $$.handled = FsSuccess;
8076 }
8077 | READ read_body read_eofs[err] {
8078 bool handled = $err.nclause == 2 || !$err.tf;
8079 $$.file = $2; $$.handled = handled? FsEofSeq : FsSuccess;
8080 if( $$.file->access == file_access_rnd_e ) {
8081 // None of ADVANCING, AT END, NEXT, NOT AT END, or PREVIOUS
8082 // shall be specified if ACCESS MODE RANDOM
8083 error_msg(@err, "%s: AT END invalid for ACCESS MODE RANDOM", $$.file->name);
8084 YYERROR;
8085 }
8086 parser_fi();
8087 }
8088 | READ read_body read_eofs[err] END_READ {
8089 bool handled = $err.nclause == 2 || !$err.tf;
8090 $$.file = $2; $$.handled = handled? FsEofSeq : FsSuccess;
8091 if( $$.file->access == file_access_rnd_e ) {
8092 error_msg(@err, "%s: AT END invalid for ACCESS MODE RANDOM", $$.file->name);
8093 YYERROR;
8094 }
8095 parser_fi();
8096 }
8097 | READ read_body io_invalids[err] {
8098 bool handled = $err.nclause == 2 || !$err.tf;
8099 $$.file = $2; $$.handled = handled? FsNotFound : FsSuccess;
8100 parser_fi();
8101 }
8102 | READ read_body io_invalids[err] END_READ {
8103 bool handled = $err.nclause == 2 || !$err.tf;
8104 $$.file = $2; $$.handled = handled? FsNotFound : FsSuccess;
8105 parser_fi();
8106 }
8107 ;
8108
8109 read_body: NAME read_next read_into read_key
8110 {
8111 statement_begin(@$, READ);
8112 struct symbol_elem_t *e = symbol_file(PROGRAM, $NAME);
8113 if( !e ) {
8114 error_msg(@1, "invalid file name '%s'", $NAME);
8115 YYERROR;
8116 }
8117
8118 $$ = cbl_file_of(e);
8119
8120 struct cbl_field_t *record = symbol_file_record($$);
8121 if( !record ) {
8122 error_msg(@1, "syntax error? invalid file record name");
8123 YYERROR;
8124 }
8125 if( $read_key->field && is_sequential($$) ) {
8126 error_msg(@1, "SEQUENTIAL file %s has no KEY", $$->name);
8127 YYERROR;
8128 }
8129 if( $$->org == file_line_sequential_e && $read_next == -2 ) {
8130 error_msg(@1, "LINE SEQUENTIAL file %s cannot READ PREVIOUS",
8131 $$->name);
8132 YYERROR;
8133 }
8134 if( $read_key->field && $read_next < 0 ) {
8135 error_msg(@1, "cannot read NEXT with KEY %qs", $$->name);
8136 YYERROR;
8137 }
8138
8139 int ikey = $read_next;
8140 if( $read_key->field ) {
8141 ikey = $$->key_one($read_key->field);
8142 }
8143
8144 file_read_args.init( $$, record, $read_into, ikey );
8145 }
8146 ;
8147
8148 read_next: %empty { $$ = 0; }
8149 | PREVIOUS RECORD { $$ = -2; }
8150 | PREVIOUS { $$ = -2; }
8151 | NEXT RECORD { $$ = -1; }
8152 | NEXT { $$ = -1; }
8153 | RECORD { $$ = 0; }
8154 ;
8155
8156 read_into: %empty { $$ = NULL; }
8157 | INTO scalar { $$ = $scalar; }
8158 ;
8159
8160 /*
8161 * read_eofs may have 1 or 2 clauses, plus a boolean that
8162 * represents whether the last one is a NOT clause. That is,
8163 * there's an AT END clause if there are 2 clauses, or if
8164 * there's one clause that is an AT END clause (tf is false).
8165 */
8166 read_eofs: read_eof { $$.nclause = 1; $$.tf = $1; }
8167 | read_eofs read_eof
8168 {
8169 $$ = $1;
8170 if( ++$$.nclause > 2 ) {
8171 error_msg(@2, "too many AT END conditions");
8172 YYERROR;
8173 }
8174 if( $$.tf == $read_eof ) {
8175 error_msg(@2, "duplicate AT END conditions");
8176 YYERROR;
8177 }
8178 parser_fi();
8179 }
8180 ;
8181
8182 read_eof: END
8183 {
8184 if( file_read_args.ready() ) {
8185 file_read_args.default_march(true);
8186 file_read_args.call_parser_file_read();
8187 }
8188
8189 static const struct status_t { file_status_t L, U; }
8190 at_end = { FsEofSeq, FsKeySeq },
8191 not_at_end = { FsSuccess, FsEofSeq };
8192 assert( $1 == END || $1 == NOT );
8193 status_t st = $1 == END? at_end : not_at_end;
8194 // L <= ec < U
8195 cbl_field_t *cond = ast_file_status_between(st.L, st.U);
8196
8197 parser_if(cond);
8198 parser_exception_clear();
8199 } statements {
8200 parser_else();
8201 $$ = $1 == NOT;
8202 }
8203 ;
8204
8205 write_eops: write_eop { $$.nclause = 1; $$.tf = $1; }
8206 | write_eops write_eop
8207 {
8208 $$ = $1;
8209 if( ++$$.nclause > 2 ) {
8210 error_msg(@2, "too many AT EOP conditions");
8211 YYERROR;
8212 }
8213 if( $$.tf == $write_eop ) {
8214 error_msg(@2, "duplicate AT EOP conditions");
8215 YYERROR;
8216 }
8217 }
8218 ;
8219
8220 write_eop: EOP
8221 {
8222 // cond represents the _FILE_STATUS of the last WRITE.
8223 static cbl_field_t *cond = constant_of(constant_index(ZERO));
8224
8225 if( file_write_args.ready() ) {
8226 file_write_args.call_parser_file_write(true);
8227 cond = ast_file_status_between(FsEofSeq, FsKeySeq);
8228 }
8229 assert( $1 == EOP || $1 == NOT );
8230 if( $1 == NOT ) {
8231 parser_logop(cond, NULL, not_op, cond);
8232 }
8233 parser_if(cond);
8234 parser_exception_clear();
8235 } statements {
8236 parser_else();
8237 parser_fi();
8238 $$ = $1 == NOT;
8239 }
8240 ;
8241
8242 read_key: %empty { $$ = new cbl_refer_t(); }
8243 | KEY is name { $$ = new cbl_refer_t($name); }
8244 ;
8245
8246 write: write_file
8247 {
8248 current.declaratives_evaluate($1.file );
8249 }
8250 ;
8251
8252 write_file: WRITE write_body
8253 {
8254 $$.file = $2; $$.handled = FsSuccess;
8255 bool sequentially = $$.file->access == file_access_seq_e;
8256 file_write_args.call_parser_file_write(sequentially);
8257 }
8258 | WRITE write_body END_WRITE
8259 {
8260 $$.file = $2; $$.handled = FsSuccess;
8261 bool sequentially = $$.file->access == file_access_seq_e;
8262 file_write_args.call_parser_file_write(sequentially);
8263 }
8264 | WRITE write_body write_eops[err] {
8265 bool handled = $err.nclause == 2 || !$err.tf;
8266 $$.file = $2; $$.handled = handled? FsEofSeq : FsSuccess;
8267 }
8268 | WRITE write_body write_eops[err] END_WRITE {
8269 bool handled = $err.nclause == 2 || !$err.tf;
8270 $$.file = $2; $$.handled = handled? FsEofSeq : FsSuccess;
8271 }
8272 | WRITE write_body io_invalids[err] {
8273 bool handled = $err.nclause == 2 || !$err.tf;
8274 $$.file = $2; $$.handled = handled? FsEofSeq : FsSuccess;
8275 parser_fi();
8276 }
8277 | WRITE write_body io_invalids[err] END_WRITE {
8278 bool handled = $err.nclause == 2 || !$err.tf;
8279 $$.file = $2; $$.handled = handled? FsEofSeq : FsSuccess;
8280 parser_fi();
8281 }
8282 ;
8283
8284 write_body: write_what[field] advance_when[when] advancing
8285 {
8286 statement_begin(@$, WRITE);
8287 cbl_file_t *file = symbol_record_file($field);
8288 if( !file ) {
8289 error_msg(@1, "no FD record found for %s", $field->name);
8290 YYERROR;
8291 }
8292 $$ = file_write_args.init( file, $field, $when==AFTER, $advancing );
8293 current.declaratives_evaluate( file );
8294 }
8295 | write_what[field]
8296 {
8297 statement_begin(@$, WRITE);
8298 cbl_file_t *file = symbol_record_file($field);
8299 if( !file ) {
8300 error_msg(@1, "no FD record found for %s", $field->name);
8301 YYERROR;
8302 }
8303 cbl_refer_t lines;
8304 switch(file->org) {
8305 case file_sequential_e:
8306 break;
8307 case file_line_sequential_e:
8308 lines.field = literally_one;
8309 break;
8310 case file_disorganized_e:
8311 case file_indexed_e:
8312 case file_relative_e:
8313 break;
8314 }
8315 $$ = file_write_args.init( file, $field, false, &lines );
8316 }
8317 ;
8318 write_what: file_record FROM alpha_val[input]
8319 {
8320 $$ = $1;
8321 parser_move($$, *$input);
8322 }
8323 | file_record
8324 ;
8325 file_record: NAME
8326 {
8327 name_queue.qualify(@1, $1);
8328 auto namelocs( name_queue.pop() );
8329 auto names( name_queue.namelist_of(namelocs) );
8330 auto inner = namelocs.back();
8331 if( ($$ = field_find(names)) == NULL ) {
8332 error_msg(inner.loc, "no record name '%s'", inner.name);
8333 YYERROR;
8334 }
8335 }
8336 | NAME inof filename
8337 {
8338 std::list<const char *> names = {$filename->name, $NAME};
8339 auto record = symbol_find(names);
8340 if( !record ) {
8341 error_msg(@$, "%s IN %s not found",
8342 $NAME, $filename->name);
8343 YYERROR;
8344 }
8345 $$ = cbl_field_of(record);
8346 }
8347 | FILE_KW filename
8348 {
8349 $$ = cbl_field_of(symbol_at($filename->default_record));
8350 }
8351 ;
8352 advance_when: BEFORE { $$ = BEFORE; }
8353 | AFTER { $$ = AFTER; }
8354 ;
8355
8356 advancing: advance_by
8357 | ADVANCING advance_by { $$ = $2; }
8358 ;
8359 advance_by: scalar lines { $$ = $1; } /* BUG: should accept reference */
8360 | signed_literal lines { $$ = new_reference($1); }
8361 | PAGE
8362 {
8363 /*
8364 * The standard says behavior is undefined when the
8365 * number of lines is negative. So, we use the
8366 * negative Number Of The Beast as a PAGE flag.
8367 */
8368 $$ = new_reference( new_literal("-666") );
8369 }
8370 | device_name { $$ = new_reference(literally_one); }
8371 ;
8372
8373 io_invalids: io_invalid { $$.nclause = 1; $$.tf = $io_invalid; }
8374 | io_invalids io_invalid
8375 {
8376 $$ = $1;
8377 if( ++$$.nclause > 2 ) {
8378 error_msg(@2, "too many INVALID clauses");
8379 YYERROR;
8380 }
8381 if( $$.tf == $io_invalid ) {
8382 error_msg(@2, "duplicate INVALID conditions");
8383 YYERROR;
8384 }
8385 parser_fi();
8386 }
8387 ;
8388
8389 io_invalid: INVALID key {
8390 if( file_delete_args.ready() ) {
8391 file_delete_args.call_parser_file_delete(false);
8392 }
8393 if( file_read_args.ready() ) {
8394 file_read_args.default_march(false);
8395 file_read_args.call_parser_file_read();
8396 }
8397 if( file_rewrite_args.ready() ) {
8398 file_rewrite_args.call_parser_file_rewrite(false);
8399 }
8400 if( file_start_args.ready() ) {
8401 file_start_args.call_parser_file_start();
8402 }
8403 if( file_write_args.ready() ) {
8404 file_write_args.call_parser_file_write(false);
8405 }
8406
8407 static const struct status_t { file_status_t L, U; }
8408 invalid = { FsKeySeq, FsOsError },
8409 not_invalid = { FsSuccess, FsEofSeq };
8410 assert( $1 == INVALID || $1 == NOT );
8411 status_t st = $1 == INVALID? invalid : not_invalid;
8412 // L <= ec < U
8413 cbl_field_t *cond = ast_file_status_between(st.L, st.U);
8414
8415 parser_if(cond);
8416 parser_exception_clear();
8417 } statements {
8418 parser_else();
8419 $$ = $1 == NOT;
8420 }
8421 ;
8422
8423 delete: delete_impl end_delete
8424 | delete_cond end_delete
8425 ;
8426 delete_impl: DELETE delete_body[file]
8427 {
8428 file_delete_args.call_parser_file_delete(true);
8429 current.declaratives_evaluate( $file );
8430 }
8431 ;
8432 delete_cond: DELETE delete_body[file] io_invalids
8433 {
8434 if( is_sequential($file) ) {
8435 error_msg(@2, "INVALID KEY phrase invalid for sequential file '%s'",
8436 $file->name);
8437 YYERROR;
8438 }
8439 if( $file->access == file_access_seq_e ) {
8440 error_msg(@2, "INVALID KEY phrase invalid for "
8441 "sequential access mode on '%s'",
8442 $file->name);
8443 YYERROR;
8444 }
8445 parser_fi();
8446 // call happens in io_invalid
8447 current.declaratives_evaluate( $file );
8448 }
8449 ;
8450
8451 delete_body: filename[file] record
8452 {
8453 statement_begin(@1, DELETE);
8454 file_delete_args.init( $file );
8455 $$ = $file;
8456 }
8457 ;
8458 end_delete: %empty %prec DELETE
8459 | END_DELETE
8460 ;
8461
8462 rewrite: rewrite1
8463 {
8464 current.declaratives_evaluate($1.file);
8465 }
8466 ;
8467
8468 rewrite1: REWRITE rewrite_body end_rewrite {
8469 $$.file = $2.file; $$.handled = FsSuccess;
8470 file_rewrite_args.call_parser_file_rewrite( true );
8471 }
8472 | REWRITE rewrite_body io_invalids[err] end_rewrite {
8473 bool handled = $err.nclause == 2 || !$err.tf;
8474 $$.file = $2.file; $$.handled = handled? FsNotFound : FsSuccess;
8475
8476 if( is_sequential($$.file) ) {
8477 error_msg(@2, "INVALID KEY for sequential file '%s'",
8478 $$.file->name);
8479 YYERROR;
8480 }
8481 if( $$.file->relative_sequential() ) {
8482 error_msg(@2, "%s: INVALID KEY may not be specified for "
8483 "RELATIVE file and SEQUENTIAL access",
8484 $$.file->name);
8485 YYERROR;
8486 }
8487 parser_fi();
8488 }
8489 ;
8490
8491 rewrite_body: write_what record
8492 {
8493 statement_begin(@$, REWRITE);
8494 symbol_elem_t *e = symbol_file(PROGRAM, $1->name);
8495 file_rewrite_args.init(cbl_file_of(e), $1);
8496 $$.file = cbl_file_of(e);
8497 $$.buffer = $1;
8498 }
8499 ;
8500 end_rewrite: %empty %prec REWRITE
8501 | END_REWRITE
8502 ;
8503
8504 start: start_impl end_start
8505 {
8506 current.declaratives_evaluate($1);
8507 }
8508 | start_cond end_start
8509 {
8510 current.declaratives_evaluate($1);
8511 }
8512 ;
8513 start_impl: START start_body {
8514 $$ = $2;
8515 }
8516 ;
8517 start_cond: START start_body io_invalids {
8518 parser_fi();
8519 $$ = $2;
8520 }
8521 ;
8522 end_start: %empty %prec START
8523 | END_START
8524 ;
8525
8526 start_body: filename[file]
8527 {
8528 statement_begin(@$, START);
8529 $$ = file_start_args.init(@file, $file);
8530 parser_file_start( $file, lt_op, 0 );
8531 }
8532 | filename[file] KEY relop name[key]
8533 { // lexer swallows IS, although relop allows it.
8534 statement_begin(@$, START);
8535 int key = $file->key_one($key);
8536 int size = key == 0 ? 0 : $file->keys[key - 1].size();
8537 auto ksize = new_tempnumeric();
8538 parser_set_numeric(ksize, size);
8539 if( yydebug ) {
8540 yywarn("START: key #%d '%s' has size %d",
8541 key, $key->name, size);
8542 }
8543 $$ = file_start_args.init(@file, $file);
8544 parser_file_start( $file, relop_of($relop), key, ksize );
8545 }
8546 | filename[file] KEY relop name[key] with LENGTH expr
8547 { // lexer swallows IS, although relop allows it.
8548 statement_begin(@$, START);
8549 int key = $file->key_one($key);
8550 $$ = file_start_args.init(@file, $file);
8551 parser_file_start( $file, relop_of($relop), key, *$expr );
8552 }
8553 | filename[file] FIRST
8554 {
8555 statement_begin(@$, START);
8556 $$ = file_start_args.init(@file, $file);
8557 parser_file_start( $file, lt_op, -1 );
8558 }
8559 | filename[file] LAST
8560 {
8561 statement_begin(@$, START);
8562 $$ = file_start_args.init(@file, $file);
8563 parser_file_start( $file, gt_op, -2 );
8564 }
8565 ;
8566
8567 merge: MERGE { statement_begin(@1, MERGE); }
8568 filename[file] sort_keys sort_seq
8569 USING filenames[inputs] sort_output
8570 {
8571 std::vector <cbl_key_t> keys($sort_keys->key_list.size());
8572 std::copy( $sort_keys->key_list.begin(),
8573 $sort_keys->key_list.end(), keys.begin() );
8574
8575 size_t ninput = $inputs->files.size();
8576 size_t noutput = $sort_output->nfile();
8577 cbl_file_t **inputs = NULL, **outputs = NULL;
8578 cbl_perform_tgt_t *out_proc = NULL;
8579
8580 inputs = new cbl_file_t * [ ninput ];
8581 std::copy($inputs->files.begin(),
8582 $inputs->files.end(), inputs);
8583
8584 if( noutput > 0 ) {
8585 outputs = new cbl_file_t * [ noutput ];
8586 std::copy($sort_output->file_list.files.begin(),
8587 $sort_output->file_list.files.end(), outputs);
8588 } else {
8589 out_proc = &$sort_output->tgt;
8590 }
8591
8592 parser_file_merge( $file, $sort_seq, keys,
8593 ninput, inputs,
8594 noutput, outputs,
8595 out_proc );
8596 }
8597 ;
8598
8599 set_tgts: set_tgt {
8600 $$ = new tgt_list_t;
8601 list_add($$->targets, *$set_tgt, current_rounded_mode());
8602 }
8603 | set_tgts set_tgt
8604 {
8605 list_add($1->targets, *$set_tgt, current_rounded_mode());
8606 }
8607 ;
8608 set_operand: set_tgt
8609 | signed_literal { $$ = new_reference($1); }
8610 | ADDRESS of FUNCTION ctx_name[name]
8611 {
8612 $$ = NULL;
8613 auto e = symbol_function(0, $name);
8614 if( e ) {
8615 $$ = new cbl_refer_t(cbl_label_of(e));
8616 } else {
8617 e = symbol_find(@name, $name);
8618 if( !e ) {
8619 error_msg(@name, "%s not found", $name);
8620 YYERROR;
8621 }
8622 $$ = new cbl_refer_t(cbl_field_of(e));
8623 }
8624 assert($$);
8625 }
8626 | ADDRESS of PROGRAM_kw ctx_name[name]
8627 {
8628 $$ = NULL;
8629 auto label = symbol_program(0, $name);
8630 if( label ) {
8631 $$ = new cbl_refer_t(label);
8632 } else {
8633 auto e = symbol_find(@name, $name);
8634 if( !e ) {
8635 error_msg(@name, "%s not found", $name);
8636 YYERROR;
8637 }
8638 $$ = new cbl_refer_t(cbl_field_of(e));
8639 }
8640 assert($$);
8641 }
8642 | ADDRESS of PROGRAM_kw LITERAL[lit]
8643 {
8644 auto label = symbol_program(0, $lit.data);
8645 $$ = new cbl_refer_t( label );
8646 }
8647 ;
8648 set_tgt: scalar
8649 | ADDRESS of scalar { $$ = $scalar; $$->addr_of = true; }
8650 ;
8651
8652 set: SET set_tgts[tgts] TO set_operand[src]
8653 {
8654 statement_begin(@1, SET);
8655
8656 switch( set_operand_type(*$src) ) {
8657 case FldInvalid:
8658 if( ! ($src->prog_func && $src->addr_of) ) {
8659 error_msg(@src, "SET source operand '%s' is invalid", $src->name());
8660 YYERROR;
8661 break;
8662 }
8663 __attribute__((fallthrough));
8664 case FldPointer:
8665 if( !valid_set_targets(*$tgts, true) ) {
8666 YYERROR;
8667 }
8668 ast_set_pointers($tgts->targets, *$src);
8669 break;
8670
8671 case FldIndex:
8672 case FldPacked:
8673 case FldNumericDisplay:
8674 case FldNumericBinary:
8675 case FldFloat:
8676 case FldNumericBin5:
8677 case FldLiteralN:
8678 if( !valid_set_targets(*$tgts, $src->is_pointer()) ) {
8679 YYERROR;
8680 }
8681 parser_index($tgts, *$src);
8682 break;
8683 default:
8684 if( strcmp($src->field->name, "ZEROS") != 0 ) {
8685 error_msg(@src, "%s must be numeric or POINTER type",
8686 $src->field->name);
8687 YYERROR;
8688 }
8689 }
8690 }
8691 | SET set_tgts[tgts] TO NULLS[src]
8692 {
8693 statement_begin(@1, SET);
8694 if( !valid_set_targets(*$tgts, true) ) {
8695 YYERROR;
8696 }
8697 ast_set_pointers($tgts->targets, constant_of(constant_index(NULLS)));
8698 }
8699 | SET set_tgts TO spaces_etc[error]
8700 {
8701 error_msg(@2, "invalid value for SET TO");
8702 }
8703 | SET set_tgts[tgts] TO ENTRY scalar[src]
8704 {
8705 ast_set_pointers($tgts->targets, *$src);
8706 }
8707 | SET set_tgts[tgts] TO ENTRY LITERAL[src]
8708 {
8709 auto literal = $src.isymbol()?
8710 cbl_field_of(symbol_at($src.isymbol()))
8711 :
8712 new_literal($src, quoted_e);
8713 ast_set_pointers($tgts->targets, literal);
8714 }
8715 | SET set_tgts[tgts] UP BY num_operand[src]
8716 {
8717 statement_begin(@1, SET);
8718 list<cbl_num_result_t>& tgts = $tgts->targets;
8719
8720 for( auto p = tgts.begin(); p != tgts.end(); p++ ) {
8721 parser_add2( *p, *$src );
8722 }
8723 delete $tgts;
8724 }
8725 | SET set_tgts[tgts] DOWN BY num_operand[src]
8726 {
8727 statement_begin(@1, SET);
8728 list<cbl_num_result_t>& tgts = $tgts->targets;
8729
8730 for( auto p = tgts.begin(); p != tgts.end(); p++ ) {
8731 parser_subtract2( *p, *$src );
8732 }
8733 delete $tgts;
8734 }
8735 | SET ENVIRONMENT envar TO alpha_val[scalar]
8736 {
8737 statement_begin(@1, SET);
8738 parser_set_envar(*$envar, *$scalar);
8739 }
8740 | SET LAST EXCEPTION TO OFF
8741 {
8742 statement_begin(@1, SET);
8743 // send the signal to clear the stashed exception values
8744 parser_exception_raise(ec_none_e);
8745 }
8746 | SET LENGTH_OF scalar TO scalar
8747 {
8748 statement_begin(@1, SET);
8749 cbl_unimplemented("SET LENGTH OF");
8750 YYERROR;
8751 }
8752 | SET scalar88s[names] TO true_false[yn]
8753 {
8754 statement_begin(@1, SET);
8755 class set_conditional {
8756 bool tf;
8757 public:
8758 explicit set_conditional( int token ) : tf(token == TRUE_kw) {}
8759 void operator()(cbl_refer_t& refer) {
8760 if( refer.field->data.false_value_of() == NULL && !tf ) {
8761 auto loc = symbol_field_location(field_index(refer.field));
8762 error_msg(loc, "%s has no WHEN SET TO FALSE",
8763 refer.field->name);
8764 return;
8765 }
8766 parser_set_conditional88(refer, tf);
8767 }
8768 };
8769 std::for_each($names->refers.begin(), $names->refers.end(),
8770 set_conditional($yn));
8771 }
8772 | SET { statement_begin(@1, SET); } many_switches
8773 ;
8774
8775 many_switches: set_switches
8776 | many_switches set_switches
8777 ;
8778
8779 set_switches: switches TO on_off
8780 {
8781 struct switcheroo {
8782 bitop_t op;
8783 explicit switcheroo( bool tf ) : op( tf? bit_set_op : bit_clear_op ) {}
8784 switcheroo& operator()(cbl_field_t* sw) {
8785 assert(sw->type == FldSwitch);
8786 assert(sw->data.initial); // not a switch condition
8787 parser_bitop(NULL, parent_of(sw),
8788 op, sw->data.upsi_mask_derive());
8789 return *this;
8790 }
8791 };
8792 std::for_each( $switches->fields.begin(), $switches->fields.end(),
8793 switcheroo($on_off) );
8794 }
8795 ;
8796
8797 switches: one_switch { $$ = new field_list_t($1); }
8798 | switches one_switch[sw] { $$->fields.push_back($sw); }
8799 ;
8800 one_switch: SWITCH {
8801 $$ = cbl_field_of(symbol_find(@1, $1));
8802 }
8803 ;
8804
8805 on_off: ON { $$ = true; }
8806 | OFF { $$ = false; }
8807 ;
8808
8809 search: search_linear end_search
8810 | search_binary end_search
8811 ;
8812
8813 search_linear: SEARCH search_1_place search_1_cases
8814 {
8815 parser_lsearch_end(search_current());
8816 search_free();
8817 }
8818 ;
8819 end_search: %empty %prec SEARCH
8820 | END_SEARCH
8821 ;
8822
8823 search_1_place: search_1_body
8824 | search_1_body at END statements
8825 ;
8826
8827 search_1_body: name[table] search_varying[varying]
8828 {
8829 statement_begin(@$, SEARCH);
8830 cbl_field_t *index = table_primary_index($table);
8831 if( !index ) {
8832 error_msg(@1, "%s has no defined index", $table->name);
8833 YYERROR;
8834 }
8835
8836 cbl_name_t label_name;
8837 auto len = snprintf(label_name, sizeof(label_name),
8838 "linear_search_%d", @1.first_line);
8839 if( ! (0 < len && len < int(sizeof(label_name))) ) {
8840 gcc_unreachable();
8841 }
8842 cbl_label_t *name = label_add( LblSearch,
8843 label_name, @1.first_line );
8844 auto varying($varying);
8845 if( index == varying ) varying = NULL;
8846 parser_lsearch_start( name, $table, index, varying );
8847 search_alloc(name);
8848 }
8849 ;
8850
8851 search_varying: %empty { $$ = NULL; }
8852 | VARYING name { $$ = $2; }
8853 ;
8854
8855 search_1_cases: search_1_case
8856 {
8857 if( yydebug ) {
8858 const char *lookahead = "?";
8859 switch( yychar ) {
8860 case 0: lookahead = "YYEOF"; break;
8861 case -2: lookahead = "YYEMPTY"; break;
8862 default:
8863 if( yychar > 0 ) {
8864 lookahead = keyword_str(yychar);
8865 }
8866 }
8867 yywarn("Just one case, lookahead is '%s'", lookahead);
8868 }
8869 }
8870 | search_1_cases search_1_case
8871 ;
8872 search_1_case: search_1_when search_1_test search_stmts
8873 ;
8874 search_1_when: WHEN { parser_lsearch_conditional(search_current()); }
8875 ;
8876 search_1_test: bool_expr {
8877 parser_lsearch_when( search_current(), $bool_expr->cond() );
8878 }
8879 ;
8880
8881 search_binary: SEARCH ALL search_2_body search_2_cases
8882 {
8883 parser_bsearch_end(search_current());
8884 search_free();
8885 }
8886 | SEARCH ALL search_2_body at END statements search_2_cases
8887 {
8888 parser_bsearch_end(search_current());
8889 search_free();
8890 }
8891 ;
8892
8893 search_2_body: name[table]
8894 {
8895 statement_begin(@$, SEARCH);
8896 char *label_name = xasprintf("binary_search_%d", @1.first_line);
8897 cbl_label_t *name = label_add( LblSearch,
8898 label_name, @1.first_line );
8899 parser_bsearch_start( name, $table );
8900 search_alloc(name);
8901 }
8902 ;
8903
8904 search_2_cases: search_2_case
8905 | search_2_cases search_2_case
8906 ;
8907 search_2_case: WHEN { parser_bsearch_conditional(search_current()); }
8908 search_terms search_stmts
8909 ;
8910
8911 search_stmts: statements %prec ADD
8912 | NEXT SENTENCE %prec ADD {
8913 next_sentence = label_add(LblNone, "next_sentence", 0);
8914 parser_label_goto(next_sentence);
8915 }
8916 ;
8917
8918 search_terms: search_term
8919 | search_terms AND search_term
8920 ;
8921 search_term: scalar[key] '=' search_expr[sarg]
8922 {
8923 if( $key->nsubscript() == 0 ) {
8924 error_msg(@1, "no index for key");
8925 YYERROR;
8926 }
8927 if( dimensions($key->field) < $key->nsubscript() ) {
8928 error_msg(@1, "too many subscripts: "
8929 "%u for table of %zu dimensions",
8930 $key->nsubscript(), dimensions($key->field) );
8931 YYERROR;
8932 }
8933
8934 parser_bsearch_when( search_current(),
8935 *$key,
8936 *$sarg,
8937 is_ascending_key(*$key) );
8938 }
8939 | scalar88[sarg] {
8940 cbl_field_t *key = field_at($sarg->field->parent);
8941 parser_bsearch_when( search_current(), key, *$sarg,
8942 is_ascending_key(key) );
8943 }
8944 ;
8945 search_expr: expr
8946 | LITERAL { $$ = new_reference(new_literal($1, quoted_e)); }
8947 ;
8948
8949 sort: sort_table
8950 | sort_file
8951 ;
8952
8953 sort_table: SORT tableref[table] sort_keys sort_dup sort_seq {
8954 statement_begin(@1, SORT);
8955 std::vector <cbl_key_t> keys($sort_keys->key_list.size());
8956 if( ! is_table($table->field) ) {
8957 error_msg(@1, "%s has no OCCURS clause", $table->field->name);
8958 }
8959 // 23) If data-name-1 is omitted, the data item referenced by
8960 // data-name-2 is the key data item.
8961 int i = 0;
8962 for( auto k : $sort_keys->key_list ) {
8963 if( k.fields.empty() ) {
8964 k.fields.push_back($table->field);
8965 }
8966 keys.at(i++) = cbl_key_t(k);
8967 }
8968
8969 parser_sort( *$table, $sort_dup, $sort_seq, keys );
8970 }
8971 | SORT tableref[table] sort_dup sort_seq {
8972 statement_begin(@1, SORT);
8973 if( ! is_table($table->field) ) {
8974 error_msg(@1, "%s has no OCCURS clause", $table->field->name);
8975 }
8976 cbl_key_t
8977 key = cbl_key_t($table->field->occurs.keys[0]),
8978 guess(1, &$table->field);
8979
8980 if( key.fields.empty() ) key = guess;
8981 std::vector<cbl_key_t> keys(1, key);
8982 parser_sort( *$table, $sort_dup, $sort_seq, keys );
8983 }
8984 ;
8985
8986 sort_file: SORT FILENAME[file] sort_keys sort_dup sort_seq
8987 sort_input sort_output
8988 {
8989 statement_begin(@1, SORT);
8990 struct symbol_elem_t *e = symbol_file(PROGRAM, $file);
8991 if( !(e && e->type == SymFile) ) {
8992 error_msg(@file, "invalid file name");
8993 YYERROR;
8994 }
8995 cbl_file_t *file = cbl_file_of(e);
8996 std::vector <cbl_key_t> keys($sort_keys->key_list.size());
8997 std::copy( $sort_keys->key_list.begin(),
8998 $sort_keys->key_list.end(), keys.begin() );
8999
9000 size_t ninput = $sort_input->nfile();
9001 size_t noutput = $sort_output->nfile();
9002 cbl_file_t **inputs = NULL, **outputs = NULL;
9003 cbl_perform_tgt_t *in_proc = NULL, *out_proc = NULL;
9004
9005 if( ninput > 0 ) {
9006 inputs = new cbl_file_t * [ ninput ];
9007 std::copy($sort_input->file_list.files.begin(),
9008 $sort_input->file_list.files.end(), inputs);
9009 } else {
9010 in_proc = &$sort_input->tgt;
9011 }
9012 if( noutput > 0 ) {
9013 outputs = new cbl_file_t * [ noutput ];
9014 std::copy($sort_output->file_list.files.begin(),
9015 $sort_output->file_list.files.end(), outputs);
9016 } else {
9017 out_proc = &$sort_output->tgt;
9018 }
9019
9020 parser_file_sort( file,
9021 $sort_dup,
9022 $sort_seq,
9023 keys,
9024 ninput, inputs,
9025 noutput, outputs,
9026 in_proc, out_proc );
9027 }
9028 | SORT FILENAME[file] sort_keys sort_dup sort_seq error
9029 {
9030 error_msg(@file, "SORT missing INPUT or OUTPUT phrase");
9031 }
9032
9033
9034 sort_keys: sort_key {
9035 $$ = new sort_keys_t();
9036 $$->key_list.push_back(*$sort_key);
9037 }
9038 | sort_keys sort_key { $$->key_list.push_back(*$sort_key); }
9039 ;
9040
9041 sort_key: on forward_order key field_list %prec NAME
9042 {
9043 $$ = new sort_key_t( $forward_order, *$field_list );
9044 }
9045 | on forward_order key %prec NAME
9046 {
9047 field_list_t flist;
9048 $$ = new sort_key_t( $forward_order, flist );
9049 }
9050 ;
9051
9052 forward_order: ASCENDING { $$ = true; }
9053 | DESCENDING { $$ = false; }
9054 ;
9055 field_list: name { $$ = new field_list_t($1); }
9056 | field_list name { $1->fields.push_back($name); }
9057 ;
9058
9059 sort_dup: %empty { $$ = false; }
9060 | with DUPLICATES in order { $$ = true; }
9061 ;
9062 sort_seq: %empty { $$ = NULL; }
9063 | collating SEQUENCE is ctx_name[name]
9064 {
9065 symbol_elem_t *e = symbol_alphabet(PROGRAM, $name);
9066 if( !e ) {
9067 error_msg(@name, "not an alphabet: '%s'", $name);
9068 $$ = NULL;
9069 }
9070 $$ = cbl_alphabet_of(e);
9071 }
9072 ;
9073
9074 sort_input: USING filenames
9075 {
9076 $$ = new file_sort_io_t(*$2);
9077 delete $2;
9078 }
9079 | INPUT PROCEDURE is sort_target
9080 {
9081 $$ = new file_sort_io_t(*$sort_target);
9082 delete $sort_target;
9083 }
9084 ;
9085 sort_output: GIVING filenames
9086 {
9087 $$ = new file_sort_io_t(*$2);
9088 }
9089 | OUTPUT PROCEDURE is sort_target
9090 {
9091 $$ = new file_sort_io_t(*$sort_target);
9092 }
9093 ;
9094
9095 sort_target: label_name
9096 {
9097 $$ = new cbl_perform_tgt_t($1);
9098 }
9099 | label_name THRU label_name
9100 {
9101 $$ = new cbl_perform_tgt_t($1, $3);
9102 }
9103 ;
9104
9105 release: RELEASE NAME[record] FROM scalar[name]
9106 {
9107 statement_begin(@1, RELEASE);
9108 symbol_elem_t *record = symbol_find(@record, $record);
9109 parser_move(cbl_field_of(record), *$name);
9110 parser_release(cbl_field_of(record));
9111 }
9112 | RELEASE NAME[record]
9113 {
9114 statement_begin(@1, RELEASE);
9115 symbol_elem_t *record = symbol_find(@record, $record);
9116 parser_release(cbl_field_of(record));
9117 }
9118 ;
9119
9120 return_stmt: return_impl return_end
9121 | return_cond return_end
9122 ;
9123
9124 return_impl: RETURN return_body[body]
9125 {
9126 cbl_file_t *file = cbl_file_of(symbol_at(current_sort_file));
9127 parser_return_finish(file);
9128 current_sort_file = $body;
9129 }
9130 ;
9131
9132 return_cond: RETURN return_body[body] return_outputs
9133 {
9134 cbl_file_t *file = cbl_file_of(symbol_at(current_sort_file));
9135 parser_return_finish(file);
9136 current_sort_file = $body;
9137 }
9138 ;
9139 return_end: %empty %prec RETURN
9140 | END_RETURN
9141 ;
9142
9143 return_body: return_file
9144 {
9145 file_return_args.call_parser_return_start();
9146 }
9147 | return_file INTO scalar
9148 {
9149 file_return_args.call_parser_return_start(*$scalar);
9150 }
9151 ;
9152
9153 return_file: filename
9154 {
9155 statement_begin(@$, RETURN);
9156 $$ = current_sort_file; // preserve current sort file
9157 current_sort_file = symbol_index(symbol_elem_of($filename));
9158 file_return_args.init($filename);
9159 }
9160 | filename RECORD
9161 {
9162 statement_begin(@$, RETURN);
9163 $$ = current_sort_file; // preserve current sort file
9164 current_sort_file = symbol_index(symbol_elem_of($filename));
9165 file_return_args.init($filename);
9166 }
9167 ;
9168 return_outputs: return_output
9169 | return_outputs return_output // TODO: only 2, AT END and/or NOT AT END
9170 ;
9171 return_output: output_atend statements %prec RETURN
9172 ;
9173
9174 output_atend: END {
9175 assert($1 == END || $1 == NOT);
9176 auto func = $1 == END?
9177 parser_return_atend : parser_return_notatend ;
9178 func(cbl_file_of(symbol_at(current_sort_file)));
9179 }
9180 ;
9181 filenames: filename { $$ = new file_list_t($1); }
9182 | filenames filename { $1->files.push_back($2); }
9183 ;
9184 filename: NAME
9185 {
9186 struct symbol_elem_t *e = symbol_file(PROGRAM, $1);
9187 if( !(e && e->type == SymFile) ) {
9188 error_msg(@NAME, "invalid file name");
9189 YYERROR;
9190 }
9191 $$ = cbl_file_of(e);
9192 }
9193 ;
9194
9195 label_name: NAME
9196 {
9197 struct cbl_label_t *label = symbol_label(PROGRAM,
9198 LblNone, 0, $1);
9199 if( !label ) { // no line number for forward declaraion
9200 label = label_add(@NAME, LblNone, $1);
9201 }
9202 $$ = label;
9203 }
9204 ;
9205
9206 inspected: scalar
9207 | intrinsic_call
9208 ;
9209 backward: %empty { $$ = false; }
9210 | BACKWARD { $$ = true; }
9211 ;
9212 inspect: INSPECT backward inspected TALLYING tallies
9213 {
9214 statement_begin(@1, INSPECT);
9215 ast_inspect( @$, *$inspected, $backward, *$tallies );
9216 }
9217 | INSPECT backward inspected TALLYING tallies REPLACING replacements
9218 {
9219 if( is_constant($inspected->field) ) {
9220 auto name = nice_name_of($inspected->field);
9221 if( !name[0] ) name = "its argument";
9222 error_msg(@inspected, "INSPECT cannot write to %s", name);
9223 YYERROR;
9224 }
9225 statement_begin(@1, INSPECT);
9226 // All tallying is done before any replacing
9227 ast_inspect( @$, *$inspected, $backward, *$tallies );
9228 ast_inspect( @$, *$inspected, $backward, *$replacements );
9229 }
9230 | INSPECT backward inspected REPLACING replacements
9231 {
9232 if( is_constant($inspected->field) ) {
9233 auto name = nice_name_of($inspected->field);
9234 if( !name[0] ) name = "its argument";
9235 error_msg(@inspected, "INSPECT cannot write to %s", name);
9236 YYERROR;
9237 }
9238 statement_begin(@1, INSPECT);
9239 ast_inspect( @$, *$inspected, $backward, *$replacements );
9240 }
9241 | INSPECT backward inspected CONVERTING alpha_val[match]
9242 TO all alpha_val[replace_oper]
9243 insp_mtqual[qual]
9244 {
9245 if( $all ) {
9246 $replace_oper->all = true;
9247 if( is_literal($replace_oper->field) ) {
9248 if( $replace_oper->field->data.capacity != 1 ) {
9249 error_msg(@all, "ALL %s must be a single character",
9250 $replace_oper->field->data.initial);
9251 YYERROR;
9252 }
9253 } else {
9254 error_msg(@all, "ALL must be part of a figurative constant");
9255 YYERROR;
9256 }
9257 } else {
9258 cbl_field_t *match = $match->field,
9259 *replace = $replace_oper->field;
9260 if( is_literal(match) && is_literal(replace) ) {
9261 if( !$match->all && !$replace_oper->all) {
9262 if( match->data.capacity != replace->data.capacity ) {
9263 error_msg(@match, "%qs, size %u NOT EQUAL %qs, size %u",
9264 nice_name_of(match), match->data.capacity,
9265 nice_name_of(replace), replace->data.capacity);
9266 YYERROR;
9267 }
9268 }
9269 }
9270 }
9271 if( is_constant($inspected->field) ) {
9272 auto name = nice_name_of($inspected->field);
9273 if( !name[0] ) name = "its argument";
9274 error_msg(@inspected, "INSPECT cannot write to %s", name);
9275 YYERROR;
9276 }
9277 statement_begin(@1, INSPECT);
9278 // IBM Format 4 does not show the qualifiers as optional, but
9279 // they don't appear in Listing-15-1.
9280 parser_inspect_conv( *$inspected, $backward,
9281 *$match,
9282 *$replace_oper,
9283 $qual->before, $qual->after );
9284 }
9285 ;
9286
9287 tallies: { need_nume_set(); } tally
9288 {
9289 $$ = new cbl_inspect_opers_t( 1, *$tally );
9290 }
9291 | tallies { need_nume_set(); } tally
9292 {
9293 $$ = $1;
9294 cbl_inspect_t& next(*$tally);
9295
9296 if( !next.tally.field ) {
9297 // prior tally swallowed one too many
9298 cbl_inspect_t& prior = $$->back();
9299 assert(prior.nbound() > 0);
9300 cbl_inspect_oper_t& prior_op = prior.back();
9301 assert(! prior_op.matches.empty() );
9302 assert(prior_op.n_identifier_3() > 0 );
9303 cbl_inspect_match_t wrong_match = prior_op.matches.back();
9304 dbgmsg("moving overeager tally to next clause");
9305 dump_inspect_match(wrong_match);
9306 next.tally = wrong_match.premature_tally();
9307 if( wrong_match.empty() ) {
9308 prior_op.matches.pop_back();
9309 }
9310 }
9311 if( !next.tally.field ) {
9312 error_msg(@$, "missing summation field before FOR");
9313 YYERROR;
9314 }
9315 $$->push_back(next);
9316 }
9317 ;
9318
9319 /*
9320 * numref might be "empty" only because it was consumed by a
9321 * prior insp_mtqual, which can end in a scalar. If that
9322 * happens, the tallies target, above, takes back the borrowed
9323 * scalar and assigns it to be the tally total, as the user
9324 * intended.
9325 */
9326 tally: numeref[total] FOR tally_fors[fors]
9327 {
9328 if( yydebug && !$total ) {
9329 dbgmsg("tally: caution: missing summation field before FOR");
9330 }
9331 $$ = $fors;
9332 if( $total ) $$->tally = *$total;
9333 }
9334 ;
9335
9336 tally_fors: tally_forth { $$ = new cbl_inspect_t(1, *$1); }
9337 | tally_fors tally_forth { $$->push_back(*$2); $$ = $1; }
9338 ;
9339
9340 tally_forth: CHARACTERS insp_mtqual[q] scalar[next_tally]
9341 {
9342 // Add ensuing scalar as if it were an argument to CHARACTERS.
9343 // It will be moved to the succeeding FOR as its tally.
9344 dbgmsg("saving overeager tally for next clause");
9345 $q->save_premature_tally(*$next_tally);
9346 $$ = new cbl_inspect_oper_t(*$q);
9347 dump_inspect_match($$->matches.back());
9348 }
9349 | CHARACTERS insp_mtqual[q]
9350 {
9351 $$ = new cbl_inspect_oper_t(*$q);
9352 }
9353 | ALL tally_matches[q]
9354 { $q->bound = bound_all_e;
9355 $$ = $q;
9356 }
9357 | LEADING tally_matches[q]
9358 { $q->bound = bound_leading_e;
9359 $$ = $q;
9360 }
9361 | TRAILING tally_matches[q]
9362 { $q->bound = bound_trailing_e;
9363 $$ = $q;
9364 if( ! dialect_mf() ) {
9365 dialect_error(@1, "TRAILING", "mf");
9366 }
9367 }
9368 ;
9369
9370 tally_matches: tally_match { $$ = new cbl_inspect_oper_t(*$1); }
9371 | tally_matches tally_match
9372 { // add to the list of matches for an operand
9373 $1->matches.push_back(*$2);
9374 }
9375 ;
9376 tally_match: alpha_val[matching] insp_mtqual[q]
9377 { // include the matching field with the qualifiers
9378 $$ = $q;
9379 $$->matching(*$matching);
9380 }
9381 ;
9382
9383 numeref: %empty { $$ = NULL; need_nume_set(false); }
9384 | nume[name] subscripts[subs]
9385 {
9386 $$ = new cbl_refer_t($name, $subs->vectorize());
9387 }
9388 | nume { $$ = new cbl_refer_t($nume); }
9389 ;
9390
9391 nume: qnume {
9392 $$ = NULL;
9393 struct symbol_elem_t *e = NULL;
9394 size_t index = 0;
9395 auto names( name_queue.pop() );
9396
9397 for( ; !names.empty(); names.pop_front() ) {
9398 auto nameloc = names.front();
9399 if( (e = symbol_field(PROGRAM,
9400 index, nameloc.name)) == NULL ) {
9401 error_msg(nameloc.loc, "DATA-ITEM '%s' not found", nameloc.name );
9402 YYERROR;
9403 }
9404 $$ = cbl_field_of(e);
9405 index = symbol_index(e);
9406 }
9407 }
9408 ;
9409
9410 qnume: NUME { name_queue.qualify(@1, $1); }
9411 | qnume inof NUME { name_queue.qualify(@3, $3); }
9412 ;
9413
9414 replacements: replacement
9415 {
9416 cbl_inspect_t inspect( cbl_refer_t(), *$1 );
9417 $$ = new cbl_inspect_opers_t(1, inspect);
9418 }
9419 ;
9420 replacement: replace_oper
9421 {
9422 $$ = new cbl_inspect_t;
9423 $$->push_back( cbl_inspect_oper_t($1->bound, $1->replaces) );
9424 }
9425 | replacement replace_oper
9426 {
9427 $$->push_back( cbl_inspect_oper_t($2->bound, $2->replaces) );
9428 }
9429 ;
9430 replace_oper: CHARACTERS BY alpha_val[replace] insp_mtqual[q]
9431 {
9432 $$ = new cbl_inspect_oper_t( cbl_inspect_replace_t(NULL,
9433 *$replace,
9434 $q->before,
9435 $q->after) );
9436 }
9437 | first_leading x_by_ys %prec NAME
9438 {
9439 $$ = $2;
9440 $$->bound = static_cast<cbl_inspect_bound_t>($1);
9441 }
9442 ;
9443
9444 x_by_ys: x_by_y
9445 {
9446 $$ = new cbl_inspect_oper_t(*$1);
9447 }
9448 | x_by_ys x_by_y
9449 {
9450 $$->replaces.push_back(*$2);
9451 }
9452 ;
9453 x_by_y: alpha_val[matching] BY alpha_val[replace] insp_mtqual[q]
9454 {
9455 $$ = new cbl_inspect_replace_t(*$matching, *$replace,
9456 $q->before, $q->after);
9457 }
9458 ;
9459
9460 /* mt may be "empty": match may have no qualifiers */
9461 insp_mtqual: %empty { $$ = new cbl_inspect_match_t; }
9462 | insp_quals
9463 ;
9464 insp_quals: insp_qual {
9465 $$ = new cbl_inspect_match_t;
9466 if( $insp_qual.before ) {
9467 $$->before = *$insp_qual.qual;
9468 } else {
9469 $$->after = *$insp_qual.qual;
9470 }
9471 dump_inspect_match(*$$);
9472 }
9473 | insp_quals insp_qual
9474 {
9475 if( ($$->before.active() && $insp_qual.before) ||
9476 ($$->after.active() && !$insp_qual.before) ) {
9477 error_msg(@2, "duplicate BEFORE/AFTER phrase");
9478 YYERROR;
9479 }
9480 auto p = $insp_qual.before? &$$->before : &$$->after;
9481 *p = *$insp_qual.qual;
9482 }
9483 ;
9484 insp_qual: befter initial alpha_val
9485 {
9486 // NIST NC115A: INITIAL has no effect (GnuCOBOL & ISO say same).
9487 bool initial = $initial == INITIAL_kw;
9488 $$.before = $befter == BEFORE;
9489 $$.qual = new cbl_inspect_qual_t(initial, *$3);
9490 }
9491 ;
9492
9493 first_leading: FIRST { $$ = bound_first_e; }
9494 | ALL { $$ = bound_all_e; }
9495 | LEADING { $$ = bound_leading_e; }
9496 | TRAILING { $$ = bound_trailing_e;
9497 if( ! dialect_mf() ) {
9498 dialect_error(@1, "TRAILING", "mf");
9499 }
9500 }
9501 ;
9502
9503 alphaval: LITERAL { $$ = new_reference(new_literal($1, quoted_e)); }
9504 | reserved_value
9505 {
9506 $$ = new_reference( constant_of(constant_index($1)) );
9507 }
9508 | intrinsic_call
9509 ;
9510
9511 befter: BEFORE { $$ = BEFORE; }
9512 | AFTER { $$ = AFTER; }
9513 ;
9514
9515 initialize: INITIALIZE move_tgts[tgts]
9516 {
9517 statement_begin(@1, INITIALIZE);
9518 initialize_statement( $tgts->targets, false, data_category_none );
9519 }
9520 | INITIALIZE move_tgts[tgts] with FILLER_kw
9521 {
9522 statement_begin(@1, INITIALIZE);
9523 initialize_statement( $tgts->targets, true, data_category_none );
9524 }
9525 | INITIALIZE move_tgts[tgts] init_clause[ini]
9526 {
9527 statement_begin(@1, INITIALIZE);
9528 initialize_statement( $tgts->targets, false, $ini->category,
9529 $ini->replacement);
9530 }
9531 | INITIALIZE move_tgts[tgts] init_clause[ini] with FILLER_kw
9532 {
9533 statement_begin(@1, INITIALIZE);
9534 initialize_statement( $tgts->targets, true, $ini->category,
9535 $ini->replacement);
9536 }
9537 | INITIALIZE move_tgts[tgts] with FILLER_kw init_clause[ini]
9538 {
9539 statement_begin(@1, INITIALIZE);
9540 initialize_statement( $tgts->targets, true, $ini->category,
9541 $ini->replacement );
9542 }
9543 ;
9544
9545 init_clause: init_value
9546 | init_categora
9547 {
9548 $$ = new init_statement_t(false);
9549 $$->category = $1;
9550 }
9551 | init_categora to VALUE
9552 {
9553 $$ = new init_statement_t(true);
9554 $$->category = $1;
9555 }
9556 | init_categora to VALUE init_value
9557 {
9558 $$ = $init_value;
9559 $$->category = $1;
9560 }
9561 ;
9562
9563 init_value: init_replace then to DEFAULT
9564 {
9565 $$ = new init_statement_t( *$init_replace);
9566 }
9567 | init_replace
9568 {
9569 $$ = new init_statement_t( *$init_replace);
9570 }
9571 | then to DEFAULT
9572 {
9573 $$ = new init_statement_t( false );
9574 }
9575 ;
9576
9577 init_categora: init_category
9578 | ALL { $$ = data_category_all; }
9579 ;
9580 init_category: ALPHABETIC { $$ = data_alphabetic_e; }
9581 | ALPHANUMERIC { $$ = data_alphanumeric_e; }
9582 | ALPHANUMERIC_EDITED { $$ = data_alphanumeric_edited_e; }
9583 | DBCS { $$ = data_dbcs_e; }
9584 | EGCS { $$ = data_egcs_e; }
9585 | NATIONAL { $$ = data_national_e; }
9586 | NATIONAL_EDITED { $$ = data_national_edited_e; }
9587 | NUMERIC { $$ = data_numeric_e; }
9588 | NUMERIC_EDITED { $$ = data_numeric_edited_e; }
9589 ;
9590
9591 init_replace: then REPLACING init_bys { $$ = $init_bys; }
9592 ;
9593 init_bys: init_by
9594 {
9595 $$ = new category_map_t;
9596 category_map_t& replacements = *$$;
9597 replacements[$init_by.category] = $init_by.replacement;
9598 }
9599 | init_bys init_by
9600 {
9601 $$ = $1;
9602 category_map_t& replacements = *$$;
9603 replacements[$init_by.category] = $init_by.replacement;
9604 }
9605 ;
9606 init_by: init_category data BY init_data
9607 {
9608 $$.category = $init_category;
9609 $$.replacement = $init_data;
9610 }
9611 ;
9612 init_data: alpha_val
9613 | NUMSTR {
9614 $$ = new_reference(new_literal($1.string, $1.radix));
9615 }
9616 ;
9617
9618 call: call_impl end_call
9619 | call_cond end_call
9620 ;
9621
9622 call_impl: CALL call_body[body]
9623 {
9624 ffi_args_t *params = $body.using_params;
9625 if( yydebug && params ) params->dump();
9626 size_t narg = params? params->elems.size() : 0;
9627 std::vector <cbl_ffi_arg_t> args(narg);
9628 cbl_ffi_arg_t *pargs = NULL;
9629 if( narg > 0 ) {
9630 std::copy( params->elems.begin(),
9631 params->elems.end(), args.begin() );
9632 pargs = args.data();
9633 }
9634 ast_call( $body.loc, *$body.ffi_name,
9635 *$body.ffi_returning, narg, pargs, NULL, NULL, false );
9636 current.declaratives_evaluate();
9637 }
9638 ;
9639 call_cond: CALL call_body[body] call_excepts[except]
9640 {
9641 ffi_args_t *params = $body.using_params;
9642 if( yydebug && params ) params->dump();
9643 size_t narg = params? params->elems.size() : 0;
9644 std::vector <cbl_ffi_arg_t> args(narg);
9645 cbl_ffi_arg_t *pargs = NULL;
9646 if( narg > 0 ) {
9647 std::copy( params->elems.begin(),
9648 params->elems.end(), args.begin() );
9649 pargs = args.data();
9650 }
9651 ast_call( $body.loc, *$body.ffi_name,
9652 *$body.ffi_returning, narg, pargs,
9653 $except.on_error, $except.not_error, false );
9654 current.declaratives_evaluate();
9655 }
9656 ;
9657 end_call: %empty %prec CALL
9658 | END_CALL
9659 ;
9660
9661 call_body: ffi_name
9662 { statement_begin(@1, CALL);
9663 $$.ffi_name = $ffi_name;
9664 $$.using_params = NULL;
9665 $$.ffi_returning = cbl_refer_t::empty();
9666 }
9667
9668 | ffi_name USING parameters
9669 { statement_begin(@1, CALL);
9670 $$.ffi_name = $ffi_name;
9671 $$.using_params = $parameters;
9672 $$.ffi_returning = cbl_refer_t::empty();
9673 }
9674 | ffi_name call_returning scalar[ret]
9675 { statement_begin(@1, CALL);
9676 $$.ffi_name = $ffi_name;
9677 $$.using_params = NULL;
9678 $$.ffi_returning = $ret;
9679 }
9680 | ffi_name USING parameters call_returning scalar[ret]
9681 { statement_begin(@1, CALL);
9682 $$.ffi_name = $ffi_name;
9683 $$.using_params = $parameters;
9684 $$.ffi_returning = $ret;
9685 }
9686 ;
9687 call_returning: RETURNING
9688 | GIVING {
9689 if( !dialect_mf() ) {
9690 dialect_error(@1, "CALL ... GIVING", "mf");
9691 }
9692 }
9693 ;
9694
9695 entry: ENTRY LITERAL
9696 { statement_begin(@1, ENTRY);
9697 auto name = new_literal($2, quoted_e);
9698 parser_entry( name );
9699 }
9700 | ENTRY LITERAL USING parameters
9701 { statement_begin(@1, ENTRY);
9702 auto name = new_literal($2, quoted_e);
9703 ffi_args_t *params = $parameters;
9704 size_t narg = params? params->elems.size() : 0;
9705 cbl_ffi_arg_t *pargs = NULL;
9706 std::vector <cbl_ffi_arg_t> args(narg);
9707 if( narg > 0 ) {
9708 std::copy( params->elems.begin(),
9709 params->elems.end(), args.begin() );
9710 pargs = args.data();
9711 }
9712 parser_entry( name, narg, pargs );
9713 }
9714 ;
9715
9716 ffi_name: scalar
9717 {
9718 $$ = $1;
9719 if( ! is_callable($1->field) ) {
9720 error_msg(@1, "CALL requires %s to be "
9721 "PROGRAM-POINTER or alphanumeric", $1->name());
9722 YYERROR;
9723 }
9724 if( $1->field->type == FldLiteralA ) {
9725 // Replace repository literal with aliased program's name.
9726 assert($1->field->parent > 0);
9727 auto& L = *cbl_label_of(symbol_at($1->field->parent));
9728 $$->field = new_literal(strlen(L.name), L.name, quoted_e);
9729 }
9730 }
9731 | LITERAL { $$ = new_reference(new_literal($1, quoted_e)); }
9732 ;
9733
9734 parameters: parameter { $$ = new ffi_args_t($1); }
9735 | parameters parameter
9736 {
9737 $1->push_back($2);
9738 $$ = $1;
9739 }
9740 ;
9741 parameter: ffi_by_ref { $$ = $1; $$->crv = by_default_e; }
9742 | by REFERENCE ffi_by_ref { $$ = $3; }
9743 | by CONTENT ffi_by_con { $$ = $3; }
9744 | by VALUE ffi_by_val { $$ = $3; }
9745 ;
9746 ffi_by_ref: scalar_arg[refer]
9747 {
9748 $$ = new cbl_ffi_arg_t(by_reference_e, $refer);
9749 }
9750 | ADDRESS OF scalar_arg[refer]
9751 {
9752 $$ = new cbl_ffi_arg_t(by_reference_e, $refer, address_of_e);
9753 }
9754 | OMITTED
9755 {
9756 cbl_refer_t *r = new cbl_refer_t();
9757 $$ = new cbl_ffi_arg_t(by_reference_e, r);
9758 }
9759 ;
9760
9761 ffi_by_con: expr
9762 {
9763 cbl_refer_t *r = new cbl_refer_t(*$1);
9764 $$ = new cbl_ffi_arg_t(by_content_e, r);
9765 }
9766 | LITERAL
9767 {
9768 cbl_refer_t *r = new_reference(new_literal($1, quoted_e));
9769 $$ = new cbl_ffi_arg_t(by_content_e, r);
9770 }
9771 | OMITTED
9772 {
9773 cbl_refer_t *r = new cbl_refer_t();
9774 $$ = new cbl_ffi_arg_t(by_content_e, r);
9775 }
9776 ;
9777
9778 ffi_by_val: by_value_arg
9779 {
9780 $$ = new cbl_ffi_arg_t(by_value_e, $1);
9781 }
9782 | cce_expr %prec NAME
9783 {
9784 auto r = new_reference(new_literal(string_of($1)));
9785 $$ = new cbl_ffi_arg_t(by_value_e, r);
9786 }
9787 | ADDRESS OF scalar
9788 {
9789 $$ = new cbl_ffi_arg_t(by_value_e, $scalar, address_of_e);
9790 }
9791 | LENGTH_OF scalar
9792 {
9793 $$ = new cbl_ffi_arg_t(by_value_e, $scalar, length_of_e);
9794 }
9795 ;
9796
9797 scalar_arg: scalar
9798 | scalar AS FIXED LENGTH %prec NAME
9799 ;
9800
9801 call_excepts: call_excepts[a] call_except[b] statements %prec CALL
9802 {
9803 if( $a.on_error && $a.not_error ) {
9804 error_msg(@b, "too many ON EXCEPTION clauses");
9805 YYERROR;
9806 }
9807 // "ON" and "NOT ON" could be reversed, but not duplicated.
9808 if( $a.on_error && $b.on_error ) {
9809 error_msg(@b, "duplicate ON EXCEPTION clauses");
9810 YYERROR;
9811 }
9812 if( $a.not_error && $b.not_error ) {
9813 error_msg(@b, "duplicate NOT ON EXCEPTION clauses");
9814 YYERROR;
9815 }
9816 $$ = $a;
9817 if( $b.on_error ) {
9818 $$.on_error = $b.on_error;
9819 assert($a.not_error);
9820 } else {
9821 $$.not_error = $b.not_error;
9822 assert($a.on_error);
9823 }
9824 assert( $b.on_error || $b.not_error );
9825 assert( ! ($b.on_error && $b.not_error) );
9826 cbl_label_t *tgt = $a.on_error? $a.on_error : $a.not_error;
9827 parser_call_exception_end(tgt);
9828 }
9829 | call_except[a] statements %prec CALL
9830 {
9831 $$ = $a;
9832 assert( $a.on_error || $a.not_error );
9833 assert( ! ($a.on_error && $a.not_error) );
9834 cbl_label_t *tgt = $a.on_error? $a.on_error : $a.not_error;
9835 parser_call_exception_end(tgt);
9836 }
9837 ;
9838
9839 call_except: EXCEPTION
9840 {
9841 $$.not_error = NULL;
9842 $$.on_error = label_add(LblArith,
9843 uniq_label("call"), @1.first_line);
9844 if( !$$.on_error ) YYERROR;
9845 parser_call_exception( $$.on_error );
9846
9847 assert( $1 == EXCEPTION || $1 == NOT );
9848 if( $1 == NOT ) {
9849 std::swap($$.on_error, $$.not_error);
9850 }
9851 }
9852 | OVERFLOW_kw
9853 {
9854 $$.not_error = NULL;
9855 $$.on_error = label_add(LblArith,
9856 uniq_label("call"), @1.first_line);
9857 if( !$$.on_error ) YYERROR;
9858 parser_call_exception( $$.on_error );
9859
9860 assert( $1 == OVERFLOW_kw || $1 == NOT );
9861 if( $1 == NOT ) {
9862 std::swap($$.on_error, $$.not_error);
9863 }
9864 }
9865 ;
9866
9867 cancel: CANCEL ffi_names
9868 {
9869 statement_begin(@1, CANCEL);
9870 std::vector <cbl_refer_t> progs($ffi_names->refers.size());
9871 std::copy( $ffi_names->refers.begin(),
9872 $ffi_names->refers.end(), progs.begin() );
9873 parser_initialize_programs( progs.size(), progs.empty()? NULL : progs.data() );
9874 }
9875 ;
9876 ffi_names: ffi_name { $$ = new refer_list_t($1); }
9877 | ffi_names ffi_name { $$ = $1->push_back($2); }
9878 ;
9879
9880 alter: ALTER { statement_begin(@1, ALTER); } alter_tgts
9881 ;
9882
9883 alter_tgts: alter_tgt
9884 | alter_tgts alter_tgt
9885 ;
9886 alter_tgt: label_1[old] alter_to label_1[new]
9887 {
9888 cbl_perform_tgt_t tgt( $old, $new );
9889 parser_alter(&tgt);
9890
9891 const auto prog = cbl_label_of( symbol_at(symbol_elem_of($old)->program));
9892 if( prog->initial ) {
9893 cbl_unimplemented("ALTER %s", $old->name);
9894 }
9895 }
9896 ;
9897
9898 alter_to: TO
9899 | TO PROCEED TO
9900 ;
9901
9902 go_to: GOTO labels[args]
9903 {
9904 statement_begin(@1, GOTO);
9905 size_t narg = $args->elems.size();
9906 if( 1 != narg ) {
9907 error_msg(@args, "more than one GO TO label requires DEPENDING");
9908 YYERROR;
9909 }
9910
9911 for( auto& label : $args->elems ) {
9912 label->used = @2.first_line;
9913 }
9914 cbl_label_t *arg = $args->elems.front();
9915 parser_goto( cbl_refer_t(), 1, &arg );
9916 }
9917 | GOTO labels[args] DEPENDING on scalar[value]
9918 {
9919 statement_begin(@1, GOTO);
9920 assert(! $args->elems.empty());
9921 std::vector <cbl_label_t *> args($args->elems.size());
9922 std::copy($args->elems.begin(), $args->elems.end(), args.begin());
9923 for( auto& label : $args->elems ) {
9924 label->used = @2.first_line;
9925 }
9926 parser_goto( *$value, args.size(), args.data() );
9927 }
9928 | GOTO
9929 {
9930 cbl_unimplemented("altered GO TO syntax (format 3)");
9931 YYERROR;
9932 }
9933 ;
9934
9935 resume: RESUME NEXT STATEMENT
9936 {
9937 statement_begin(@1, RESUME);
9938 parser_clear_exception();
9939 }
9940 | RESUME label_1[tgt]
9941 {
9942 statement_begin(@1, RESUME);
9943 parser_clear_exception();
9944 $tgt->used = @1.first_line;
9945 parser_goto( cbl_refer_t(), 1, &$tgt );
9946 }
9947 ;
9948
9949 labels: label_1 { $$ = new Label_list_t($1); }
9950 | labels label_1 { $$ = $1->push_back($2); }
9951 ;
9952 label_1: qname
9953 { // Add a forward label with no line number, or get an existing.
9954 assert(!name_queue.empty());
9955 auto namelocs( name_queue.pop() );
9956
9957 auto nameloc = namelocs.back();
9958 if( namelocs.size() > 2 ) {
9959 error_msg(nameloc.loc,
9960 "too many qualifications for %s", nameloc.name);
9961 YYERROR;
9962 }
9963 const char *para = nameloc.name;
9964 size_t isect = 0;
9965
9966 if( namelocs.size() == 2 ) {
9967 auto nameloc = namelocs.front();
9968 cbl_label_t *sect = label_add(nameloc.loc, LblSection, nameloc.name);
9969 isect = symbol_index(symbol_elem_of(sect));
9970 }
9971
9972 $$ = paragraph_reference(para, isect);
9973 assert($$);
9974 if( yydebug ) dbgmsg( "using procedure %s of line %d",
9975 $$->name, $$->line );
9976 }
9977 | NUMSTR
9978 {
9979 // Add a forward label with no line number, or get an existing.
9980 $$ = label_add(@1, LblNone, $1.string);
9981 assert($$ != NULL);
9982 }
9983 ;
9984
9985 /* string & unstring */
9986
9987
9988 string: string_impl end_string
9989 | string_cond end_string
9990 ;
9991 string_impl: STRING_kw string_body[body]
9992 {
9993 stringify($body.inputs, *$body.into.first, *$body.into.second);
9994 current.declaratives_evaluate();
9995 }
9996 ;
9997 string_cond: STRING_kw string_body[body] on_overflows[over]
9998 {
9999 stringify($body.inputs, *$body.into.first, *$body.into.second,
10000 $over.on_error, $over.not_error);
10001 current.declaratives_evaluate();
10002 }
10003 ;
10004 end_string: %empty %prec LITERAL
10005 | END_STRING
10006 ;
10007
10008 string_body: str_delimiteds[inputs] str_into[into]
10009 {
10010 statement_begin(@$, STRING_kw);
10011 $$.inputs = $inputs;
10012 $$.into = $into;
10013 }
10014 ;
10015
10016 str_delimiteds: str_delimited
10017 {
10018 refer_marked_list_t marked($1.delimiter, $1.input);
10019 $$ = new refer_collection_t(marked);
10020 }
10021 | str_delimiteds str_delimited[input]
10022 {
10023 // matching delimiters (or none) adds to the list
10024 refer_marked_list_t& marked = $1->lists.back();
10025 if( !marked.marker ) {
10026 marked.push_on($input.delimiter, $input.input);
10027 } else { // start a new list
10028 $1->push_back( refer_marked_list_t($input.delimiter,
10029 $input.input) );
10030 }
10031 }
10032 ;
10033
10034 str_delimited: str_input DELIMITED by str_size
10035 {
10036 $$.input = $str_input;
10037 $$.delimiter = $str_size;
10038 }
10039 | str_input
10040 {
10041 $$.input = $str_input;
10042 $$.delimiter = NULL;
10043 }
10044 ;
10045
10046 str_input: scalar
10047 | LITERAL { $$ = new_reference(new_literal($1, quoted_e)); }
10048 | reserved_value
10049 {
10050 $$ = new_reference(constant_of(constant_index($1)));
10051 }
10052 | intrinsic_call
10053 ;
10054
10055 str_size: SIZE { $$ = new_reference(NULL); }
10056 | LITERAL { $$ = new_reference(new_literal($1, quoted_e)); }
10057 | scalar
10058 | reserved_value
10059 {
10060 $$ = new_reference(constant_of(constant_index($1)));
10061 }
10062 ;
10063
10064 str_into: INTO scalar
10065 {
10066 $$.first = $2;
10067 $$.second = new_reference(NULL);
10068 }
10069 | INTO scalar with POINTER scalar[from]
10070 {
10071 $$.first = $2;
10072 $$.second = $from;
10073 }
10074 ;
10075
10076 on_overflows: on_overflow[over] statements %prec ADD
10077 {
10078 assert( $over.on_error || $over.not_error );
10079 assert( ! ($over.on_error && $over.not_error) );
10080 cbl_label_t *tgt = $over.on_error?
10081 $over.on_error : $over.not_error;
10082 parser_string_overflow_end(tgt);
10083 }
10084 | on_overflows[a] on_overflow[b] statements %prec ADD
10085 {
10086 if( $a.on_error && $a.not_error ) {
10087 error_msg(@b, "too many ON OVERFLOW clauses");
10088 YYERROR;
10089 }
10090 // "ON" and "NOT ON" could be reversed, but not duplicated.
10091 if( $a.on_error && $b.on_error ) {
10092 error_msg(@b, "duplicate ON OVERFLOW clauses");
10093 YYERROR;
10094 }
10095 if( $a.not_error && $b.not_error ) {
10096 error_msg(@b, "duplicate NOT ON OVERFLOW clauses");
10097 YYERROR;
10098 }
10099 $$ = $a;
10100 if( $b.on_error ) {
10101 $$.on_error = $b.on_error;
10102 assert($a.not_error);
10103 } else {
10104 $$.not_error = $b.not_error;
10105 assert($a.on_error);
10106 }
10107 assert( $b.on_error || $b.not_error );
10108 assert( ! ($b.on_error && $b.not_error) );
10109 cbl_label_t *tgt = $b.on_error?
10110 $b.on_error : $b.not_error;
10111 parser_string_overflow_end(tgt);
10112 }
10113 ;
10114
10115 on_overflow: OVERFLOW_kw
10116 {
10117 $$.not_error = NULL;
10118 $$.on_error = label_add(LblString,
10119 uniq_label("string"), @1.first_line);
10120 if( !$$.on_error ) YYERROR;
10121 parser_string_overflow( $$.on_error );
10122
10123 assert( $1 == OVERFLOW_kw || $1 == NOT );
10124 if( $1 == NOT ) {
10125 std::swap($$.on_error, $$.not_error);
10126 }
10127 }
10128 ;
10129
10130 unstring: unstring_impl end_unstring
10131 | unstring_cond end_unstring
10132 ;
10133 end_unstring: %empty %prec UNSTRING
10134 | END_UNSTRING
10135 ;
10136
10137 unstring_impl: UNSTRING unstring_body[body]
10138 {
10139 unstringify( *$body.input, $body.delimited, $body.into );
10140 current.declaratives_evaluate();
10141 }
10142 ;
10143 unstring_cond: UNSTRING unstring_body[body] on_overflows[over]
10144 {
10145 unstringify( *$body.input, $body.delimited, $body.into,
10146 $over.on_error, $over.not_error );
10147 current.declaratives_evaluate();
10148 }
10149 ;
10150
10151 unstring_body: unstring_src[src] uns_delimited INTO uns_into[into]
10152 {
10153 statement_begin(@$, UNSTRING);
10154 $$.input = $src;
10155 $$.delimited = $uns_delimited;
10156 $$.into = $into;
10157 }
10158 unstring_src: scalar
10159 | intrinsic_call
10160 | LITERAL
10161 {
10162 $$ = new_reference(new_literal($1, quoted_e));
10163 }
10164 ;
10165
10166 uns_delimited: %empty { $$ = NULL; }
10167 | DELIMITED by uns_delimiters { $$ = $3; }
10168 ;
10169
10170 uns_delimiters: uns_delimiter { $$ = new refer_list_t($1); }
10171 | uns_delimiters OR uns_delimiter
10172 {
10173 $$ = $1;
10174 $$->push_back($3);
10175 }
10176 ;
10177 uns_delimiter: all str_input
10178 {
10179 $$ = $2;
10180 $$->all = $all;
10181 }
10182 ;
10183
10184 uns_into: uns_tgts %prec NAME
10185 {
10186 $$ = new unstring_into_t($1);
10187 }
10188 | uns_tgts with POINTER scalar[ptr]
10189 {
10190 $$ = new unstring_into_t($1, $ptr);
10191 }
10192 | uns_tgts TALLYING in scalar[tally]
10193 {
10194 $$ = new unstring_into_t($1, NULL, $tally);
10195 }
10196 | uns_tgts with POINTER scalar[ptr] TALLYING in scalar[tally]
10197 {
10198 $$ = new unstring_into_t($1, $ptr, $tally);
10199 }
10200 ;
10201
10202 uns_tgts: uns_tgt { $$ = new unstring_tgt_list_t($1); }
10203 | uns_tgts uns_tgt { $$ = $1; $$->push_back($2); }
10204 ;
10205 uns_tgt: scalar[tgt]
10206 {
10207 $$ = new unstring_tgt_t($tgt);
10208 }
10209 | scalar[tgt] DELIMITER in scalar[delim]
10210 {
10211 $$ = new unstring_tgt_t($tgt, $delim);
10212 }
10213 | scalar[tgt] COUNT in scalar[count]
10214 {
10215 if( ! $count->field->is_integer() ) {
10216 error_msg(@count, "COUNT %s must be integer type",
10217 $count->field->name);
10218 }
10219 if( $count->field->has_attr(scaled_e) ) {
10220 error_msg(@count, "COUNT %s may not be P scaled",
10221 $count->field->name);
10222 }
10223 $$ = new unstring_tgt_t($tgt, NULL, $count);
10224 }
10225 | scalar[tgt] DELIMITER in scalar[delim] COUNT in scalar[count]
10226 {
10227 if( ! $count->field->is_integer() ) {
10228 error_msg(@count, "COUNT %s must be integer type",
10229 $count->field->name);
10230 }
10231 if( $count->field->has_attr(scaled_e) ) {
10232 error_msg(@count, "COUNT %s may not be P scaled",
10233 $count->field->name);
10234 }
10235 $$ = new unstring_tgt_t($tgt, $delim, $count);
10236 }
10237 ;
10238
10239 /* intrinsics */
10240 intrinsic_call: function intrinsic { // "intrinsic" includes UDFs.
10241 $$ = new_reference($intrinsic);
10242 $$->field->attr |= constant_e;
10243 }
10244 | function intrinsic refmod[ref]
10245 {
10246 if( $ref.from->is_reference() || $ref.len->is_reference() ) {
10247 error_msg(@ref, "subscripts on start:len refmod "
10248 "parameters are unsupported");
10249 YYERROR;
10250 }
10251 if( $intrinsic->type != FldAlphanumeric ) {
10252 error_msg(@ref, "'%s' only AlphaNumeric fields accept refmods",
10253 $intrinsic->name);
10254 YYERROR;
10255 }
10256 cbl_span_t span( $ref.from, $ref.len );
10257 $$ = new cbl_refer_t($intrinsic, span);
10258 $$->field->attr |= constant_e;
10259 }
10260 | function NAME {
10261 error_msg(@NAME, "no such function: %s", $NAME);
10262 YYERROR;
10263 }
10264
10265 ;
10266 function: %empty %prec FUNCTION
10267 {
10268 statement_begin(@$, FUNCTION);
10269 }
10270 | FUNCTION
10271 {
10272 statement_begin(@1, FUNCTION);
10273 }
10274 ;
10275
10276 function_udf: FUNCTION_UDF '(' arg_list[args] ')' {
10277 std::vector<function_descr_arg_t> params;
10278 auto L = cbl_label_of(symbol_at($1));
10279 if( ! current.udf_args_valid(L, $args->refers, params) ) {
10280 YYERROR;
10281 }
10282 const auto returning = cbl_field_of(symbol_at(L->returning));
10283 $$ = new_temporary_clone(returning);
10284 $$->data.initial = returning->name; // user's name for the field
10285 std::vector <cbl_ffi_arg_t> args($args->refers.size());
10286 size_t i = 0;
10287 // Pass parameters as defined by the function.
10288 std::transform( $args->refers.begin(), $args->refers.end(), args.begin(),
10289 [params, &i]( const cbl_refer_t& arg ) {
10290 function_descr_arg_t param = params.at(i++);
10291 auto ar = new cbl_refer_t(arg);
10292 cbl_ffi_arg_t actual(param.crv, ar);
10293 return actual;
10294 } );
10295 auto name = new_literal(strlen(L->name), L->name, quoted_e);
10296 ast_call( @1, name, $$, args.size(), args.data(), NULL, NULL, true );
10297 }
10298 | FUNCTION_UDF_0 {
10299 static const size_t narg = 0;
10300 static cbl_ffi_arg_t *args = NULL;
10301
10302 auto L = cbl_label_of(symbol_at($1));
10303 const auto returning = cbl_field_of(symbol_at(L->returning));
10304 $$ = new_temporary_clone(returning);
10305 $$->data.initial = returning->name; // user's name for the field
10306
10307 auto name = new_literal(strlen(L->name), L->name, quoted_e);
10308 ast_call( @1, name, $$, narg, args, NULL, NULL, true );
10309 }
10310 ;
10311
10312 /*
10313 * The scanner returns a function-token (e.g. NUMVAL) if it was
10314 * preceded by FUNCTION, or if the name is in the program's
10315 * function repository. Else it returns NAME, because it looks
10316 * like a user-defined name (possibly a data item). If the user
10317 * attempts to use an intrinsic function without using
10318 * REPOSITORY or FUNCTION, the NAME results in a syntax error.
10319 *
10320 * Function arguments may be variables or literals or
10321 * functions, and string-valued functions accept a refmod. In
10322 * addition to "scalar", we have this inconsistent set:
10323 * var: [ALL] LITERAL, NUMSTR, instrinsic, or scalar
10324 * num_operand: signed NUMSTR/ZERO, instrinsic, or scalar
10325 * alpahaval: LITERAL, reserved_value, instrinsic, or scalar
10326 */
10327 intrinsic: function_udf
10328 | intrinsic0
10329 | intrinsic_v '(' arg_list[args] ')' {
10330 location_set(@1);
10331 std::vector <cbl_refer_t> args($args->size());
10332 assert(! args.empty());
10333 std::copy( $args->begin(), $args->end(), args.begin() );
10334 cbl_refer_t *p = intrinsic_inconsistent_parameter(args.size(),
10335 args.data());
10336 if( p != NULL ) {
10337 auto loc = symbol_field_location(field_index(p->field));
10338 error_msg(loc, "FUNCTION %qs has "
10339 "inconsistent parameter type %ld (%qs)",
10340 keyword_str($1), (long)(p - args.data()), name_of(p->field) );
10341 YYERROR;
10342 }
10343 $$ = is_numeric(args[0].field)?
10344 new_tempnumeric_float() :
10345 new_alphanumeric();
10346 $$->data.initial = keyword_str($1);
10347 parser_intrinsic_callv( $$, intrinsic_cname($1),
10348 args.size(), args.data() );
10349 }
10350
10351 | PRESENT_VALUE '(' arg_list[args] ')'
10352 {
10353 static char s[] = "__gg__present_value";
10354 location_set(@1);
10355 $$ = new_tempnumeric_float("PRESENT-VALUE");
10356 size_t n = $args->size();
10357 assert(n > 0);
10358 if( n < 2 ) {
10359 error_msg(@args, "PRESENT-VALUE requires 2 parameters");
10360 YYERROR;
10361 }
10362 std::vector <cbl_refer_t> args(n);
10363 std::copy( $args->begin(), $args->end(), args.begin() );
10364 bool ok = std::all_of( args.begin(),
10365 args.end(), [loc = @1]( auto r ) {
10366 return require_numeric(loc, r); } );
10367 if( ! ok ) YYERROR;
10368 parser_intrinsic_callv( $$, s, args.size(), args.data() );
10369 }
10370
10371 | BASECONVERT '(' varg[r1] varg[r2] varg[r3] ')' {
10372 location_set(@1);
10373 $$ = new_tempnumeric("BASECONVERT");
10374 cbl_unimplemented("BASECONVERT");
10375 if( ! intrinsic_call_3($$, BASECONVERT, $r1, $r2, $r3 )) YYERROR;
10376 }
10377 | BIT_OF '(' expr[r1] ')' {
10378 location_set(@1);
10379 $$ = new_alphanumeric("BIT-OF");
10380 if( ! intrinsic_call_1($$, BIT_OF, $r1, @r1)) YYERROR;
10381 }
10382 | CHAR '(' expr[r1] ')' {
10383 location_set(@1);
10384 $$ = new_alphanumeric(1,"CHAR");
10385 if( ! intrinsic_call_1($$, CHAR, $r1, @r1)) YYERROR;
10386 }
10387
10388 | CONVERT '(' varg[r1] convert_src[src] convert_dst[dst] ')' {
10389 location_set(@1);
10390 $$ = new_alphanumeric(1,"CONVERT");
10391 cbl_unimplemented("CONVERT");
10392 /* if( ! intrinsic_call_3($$, CONVERT, $r1, $src, $dst) ) YYERROR; */
10393 }
10394
10395 | DISPLAY_OF '(' varg[r1] ')' {
10396 location_set(@1);
10397 $$ = new_alphanumeric("DISPLAY-OF");
10398 if( ! intrinsic_call_2($$, DISPLAY_OF, $r1, NULL) ) YYERROR;
10399 }
10400 | DISPLAY_OF '(' varg[r1] varg[r2] ')' {
10401 location_set(@1);
10402 $$ = new_alphanumeric("DISPLAY-OF");
10403 if( ! intrinsic_call_2($$, DISPLAY_OF, $r1, $r2) ) YYERROR;
10404 }
10405
10406 | EXCEPTION_FILE filename {
10407 location_set(@1);
10408 $$ = new_alphanumeric("EXCEPTION-FILE");
10409 parser_exception_file( $$, $filename );
10410 }
10411
10412 | FIND_STRING '(' varg[r1] last start_after anycase ')' {
10413 location_set(@1);
10414 $$ = new_alphanumeric("FIND-STRING");
10415 /* auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e)); */
10416 cbl_unimplemented("%<FIND_STRING%>");
10417 /* if( ! intrinsic_call_4($$, FIND_STRING, r1, $r2) ) YYERROR; */
10418 }
10419
10420 | FORMATTED_DATE '(' DATE_FMT[r1] expr[r2] ')' {
10421 location_set(@1);
10422 $$ = new_alphanumeric(MAXLENGTH_FORMATTED_DATE, "FORMATTED-DATE");
10423 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10424 if( ! intrinsic_call_2($$, FORMATTED_DATE, r1, $r2) ) YYERROR;
10425 }
10426
10427
10428 | FORMATTED_DATETIME '(' DATETIME_FMT[r1] expr[r2]
10429 expr[r3] ')' {
10430 location_set(@1);
10431 $$ = new_alphanumeric(MAXLENGTH_FORMATTED_DATETIME, "FORMATTED-DATETIME");
10432 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10433 static cbl_refer_t r3(literally_zero);
10434 if( ! intrinsic_call_4($$, FORMATTED_DATETIME,
10435 r1, $r2, $r3, &r3) ) YYERROR;
10436 }
10437 | FORMATTED_DATETIME '(' DATETIME_FMT[r1] expr[r2]
10438 expr[r3] expr[r4] ')' {
10439 location_set(@1);
10440 $$ = new_alphanumeric(MAXLENGTH_FORMATTED_DATETIME, "FORMATTED-DATETIME");
10441 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10442 if( ! intrinsic_call_4($$, FORMATTED_DATETIME,
10443 r1, $r2, $r3, $r4) ) YYERROR;
10444 }
10445 | FORMATTED_DATETIME '(' error ')' {
10446 YYERROR;
10447 }
10448 | FORMATTED_TIME '(' TIME_FMT[r1] expr[r2]
10449 expr[r3] ')' {
10450 location_set(@1);
10451 $$ = new_alphanumeric(MAXLENGTH_FORMATTED_TIME, "FORMATTED-DATETIME");
10452 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10453 if( ! intrinsic_call_3($$, FORMATTED_TIME,
10454 r1, $r2, $r3) ) YYERROR;
10455 }
10456 | FORMATTED_TIME '(' TIME_FMT[r1] expr[r2] ')' {
10457 location_set(@1);
10458 $$ = new_alphanumeric(MAXLENGTH_FORMATTED_TIME, "FORMATTED-TIME");
10459 auto r3 = new_reference(new_literal("0"));
10460 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10461 if( ! intrinsic_call_3($$, FORMATTED_TIME,
10462 r1, $r2, r3) ) YYERROR;
10463 }
10464 | FORMATTED_CURRENT_DATE '(' DATETIME_FMT[r1] ')' {
10465 location_set(@1);
10466 $$ = new_alphanumeric(MAXLENGTH_FORMATTED_DATETIME, "FORMATTED-CURRENT_DATE");
10467 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10468 if( ! intrinsic_call_1($$, FORMATTED_CURRENT_DATE, r1, @r1) )
10469 YYERROR;
10470 }
10471 | TEST_FORMATTED_DATETIME '(' DATE_FMT[r1] varg[r2] ')' {
10472 location_set(@1);
10473 $$ = new_tempnumeric("TEST-FORMATTED-DATETIME");
10474 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10475 if( ! intrinsic_call_2($$, TEST_FORMATTED_DATETIME,
10476 r1, $r2) ) YYERROR;
10477 }
10478 | TEST_FORMATTED_DATETIME '(' TIME_FMT[r1] varg[r2] ')' {
10479 location_set(@1);
10480 $$ = new_tempnumeric("TEST-FORMATTED-DATETIME");
10481 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10482 if( ! intrinsic_call_2($$, TEST_FORMATTED_DATETIME,
10483 r1, $r2) ) YYERROR;
10484 }
10485 | TEST_FORMATTED_DATETIME '(' DATETIME_FMT[r1] varg[r2] ')'
10486 {
10487 location_set(@1);
10488 $$ = new_tempnumeric("TEST-FORMATTED-DATETIME");
10489 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10490 if( ! intrinsic_call_2($$, TEST_FORMATTED_DATETIME,
10491 r1, $r2) ) YYERROR;
10492 }
10493 | INTEGER_OF_FORMATTED_DATE '(' DATE_FMT[r1] varg[r2] ')' {
10494 location_set(@1);
10495 $$ = new_tempnumeric("INTEGER-OF-FORMATTED-DATE");
10496 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10497 if( ! intrinsic_call_2($$, INTEGER_OF_FORMATTED_DATE,
10498 r1, $r2) ) YYERROR;
10499 }
10500 | INTEGER_OF_FORMATTED_DATE '(' DATETIME_FMT[r1] varg[r2] ')'
10501 {
10502 location_set(@1);
10503 $$ = new_tempnumeric("INTEGER-OF-FORMATTED-DATE");
10504 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10505 if( ! intrinsic_call_2($$, INTEGER_OF_FORMATTED_DATE,
10506 r1, $r2) ) YYERROR;
10507 }
10508 | SECONDS_FROM_FORMATTED_TIME '(' TIME_FMT[r1] varg[r2] ')' {
10509 location_set(@1);
10510 $$ = new_tempnumeric("SECONDS-FROM-FORMATTED-TIME");
10511 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10512 if( ! intrinsic_call_2($$, SECONDS_FROM_FORMATTED_TIME,
10513 r1, $r2) ) YYERROR;
10514 }
10515 | SECONDS_FROM_FORMATTED_TIME '(' DATETIME_FMT[r1] varg[r2] ')'
10516 {
10517 location_set(@1);
10518 $$ = new_tempnumeric("SECONDS-FROM-FORMATTED-TIME");
10519 auto r1 = new_reference(new_literal(strlen($r1), $r1, quoted_e));
10520 if( ! intrinsic_call_2($$, SECONDS_FROM_FORMATTED_TIME,
10521 r1, $r2) ) YYERROR;
10522 }
10523
10524 | HEX_OF '(' varg[r1] ')' {
10525 location_set(@1);
10526 $$ = new_alphanumeric("HEX-OF");
10527 if( ! intrinsic_call_1($$, HEX_OF, $r1, @r1)) YYERROR;
10528 }
10529 | LENGTH '(' tableish[val] ')' {
10530 location_set(@1);
10531 $$ = new_tempnumeric("LENGTH");
10532 $$->clear_attr(signable_e);
10533 parser_set_numeric($$, $val->field->size());
10534 if( ! intrinsic_call_1($$, LENGTH, $val, @val)) YYERROR;
10535 }
10536 | LENGTH '(' varg1a[val] ')' {
10537 location_set(@1);
10538 $$ = new_tempnumeric("LENGTH");
10539 $$->clear_attr(signable_e);
10540 parser_set_numeric($$, $val->field->data.capacity);
10541 if( ! intrinsic_call_1($$, LENGTH, $val, @val)) YYERROR;
10542 }
10543 | lopper_case[func] '(' alpha_val[r1] ')' {
10544 location_set(@1);
10545 $$ = new_alphanumeric($r1->field->data.capacity, "lopper_case[func]");
10546 if( ! intrinsic_call_1($$, $func, $r1, @r1)) YYERROR;
10547 }
10548
10549 | MODULE_NAME '(' module_type[type] ')'
10550 {
10551 $$ = new_alphanumeric(sizeof(cbl_name_t), "MODULE-NAME");
10552 parser_module_name( $$, $type );
10553 }
10554
10555 | NUMVAL_C '(' varg[r1] numval_locale[r2] anycase ')' {
10556 location_set(@1);
10557 $$ = new_tempnumeric("NUMVAL-C");
10558 parser_intrinsic_numval_c( $$, *$r1, $r2.is_locale,
10559 *$r2.arg2, $anycase );
10560 }
10561 | ORD '(' alpha_val[r1] ')'
10562 {
10563 location_set(@1);
10564 $$ = new_tempnumeric("ORD");
10565 if( ! intrinsic_call_1($$, ORD, $r1, @r1)) YYERROR;
10566 }
10567 | RANDOM
10568 {
10569 location_set(@1);
10570 $$ = new_tempnumeric_float("RANDOM");
10571 parser_intrinsic_call_0( $$, intrinsic_cname(RANDOM) );
10572 }
10573 | RANDOM_SEED expr[r1] ')'
10574 { // left parenthesis consumed by lexer
10575 location_set(@1);
10576 $$ = new_tempnumeric_float("RANDOM-SEED");
10577 if( ! intrinsic_call_1($$, RANDOM, $r1, @r1)) YYERROR;
10578 }
10579
10580 | STANDARD_COMPARE '(' varg[r1] varg[r2] varg[r3] varg[r4] ')'
10581 {
10582 location_set(@1);
10583 $$ = new_tempnumeric("STANDARD-COMPARE");
10584 cbl_unimplemented("STANDARD-COMPARE");
10585 /* if( ! intrinsic_call_4($$, STANDARD_COMPARE, $r1) ) YYERROR; */
10586 }
10587 | STANDARD_COMPARE '(' varg[r1] varg[r2] varg[r3] ')'
10588 {
10589 location_set(@1);
10590 $$ = new_tempnumeric("STANDARD-COMPARE");
10591 cbl_unimplemented("STANDARD-COMPARE");
10592 /* if( ! intrinsic_call_4($$, STANDARD_COMPARE, $r1) ) YYERROR; */
10593 }
10594 | STANDARD_COMPARE '(' varg[r1] varg[r2] ')'
10595 {
10596 location_set(@1);
10597 $$ = new_tempnumeric("STANDARD-COMPARE");
10598 cbl_unimplemented("STANDARD-COMPARE");
10599 /* if( ! intrinsic_call_4($$, STANDARD_COMPARE, $r1) ) YYERROR; */
10600 }
10601
10602 | SUBSTITUTE '(' varg[r1] subst_inputs[inputs] ')' {
10603 location_set(@1);
10604 $$ = new_alphanumeric("SUBSTITUTE");
10605 std::vector <cbl_substitute_t> args($inputs->size());
10606 std::transform( $inputs->begin(), $inputs->end(), args.begin(),
10607 []( const substitution_t& arg ) {
10608 cbl_substitute_t output( arg.anycase,
10609 char(arg.first_last),
10610 arg.orig,
10611 arg.replacement );
10612 return output; } );
10613
10614 parser_intrinsic_subst($$, *$r1, args.size(), args.data());
10615 }
10616
10617
10618 | TEST_NUMVAL_C '(' varg[r1] numval_locale[r2] anycase ')' {
10619 location_set(@1);
10620 $$ = new_tempnumeric("parser_intrinsic_subst($$,");
10621 parser_intrinsic_numval_c( $$, *$r1, $r2.is_locale,
10622 *$r2.arg2, $anycase, true );
10623 }
10624 | TRIM '(' error ')' {
10625 error_msg(@error, "invalid TRIM argument");
10626 YYERROR;
10627 }
10628 | TRIM '(' expr[r1] trim_trailing ')'
10629 {
10630 location_set(@1);
10631 switch( $r1->field->type ) {
10632 case FldGroup:
10633 case FldAlphanumeric:
10634 case FldLiteralA:
10635 case FldAlphaEdited:
10636 case FldNumericEdited:
10637 break; // alphanumeric OK
10638 default:
10639 // BLANK WHEN ZERO implies numeric-edited, so OK
10640 if( $r1->field->has_attr(blank_zero_e) ) {
10641 break;
10642 }
10643 error_msg(@r1, "TRIM argument must be alphanumeric");
10644 YYERROR;
10645 break;
10646 }
10647 $$ = new_alphanumeric("TRIM");
10648 cbl_refer_t * how = new_reference($trim_trailing);
10649 if( ! intrinsic_call_2($$, TRIM, $r1, how) ) YYERROR;
10650 }
10651
10652 | USUBSTR '(' alpha_val[r1] expr[r2] expr[r3] ')' {
10653 location_set(@1);
10654 $$ = new_alphanumeric("USUBSTR");
10655 if( ! intrinsic_call_3($$, FORMATTED_DATETIME,
10656 $r1, $r2, $r3) ) YYERROR;
10657 }
10658
10659 | intrinsic_I '(' expr[r1] ')'
10660 {
10661 location_set(@1);
10662 $$ = new_tempnumeric(keyword_str($1));
10663 if( ! intrinsic_call_1($$, $1, $r1, @r1)) YYERROR;
10664 }
10665
10666 | intrinsic_N '(' expr[r1] ')'
10667 {
10668 location_set(@1);
10669 $$ = new_tempnumeric_float(keyword_str($1));
10670 if( ! intrinsic_call_1($$, $1, $r1, @r1)) YYERROR;
10671 }
10672
10673 | intrinsic_X '(' varg[r1] ')'
10674 {
10675 location_set(@1);
10676 auto type = intrinsic_return_type($1);
10677 switch(type) {
10678 case FldAlphanumeric:
10679 $$ = new_alphanumeric(keyword_str($1));
10680 break;
10681 default:
10682 if( $1 == NUMVAL || $1 == NUMVAL_F ) {
10683 $$ = new_temporary(FldFloat, keyword_str($1));
10684 } else {
10685 $$ = new_temporary(type, keyword_str($1));
10686 }
10687 }
10688 if( $1 == NUMVAL_F ) {
10689 if( is_literal($r1->field) && ! is_numeric($r1->field->type) ) {
10690 // The parameter might be literal, but could be "hello".
10691 auto input = $r1->field->data.initial;
10692 error_msg(@r1, "'%s' is not a numeric literal", input);
10693 }
10694 }
10695 if( ! intrinsic_call_1($$, $1, $r1, @r1)) YYERROR;
10696 }
10697
10698 | intrinsic_I2 '(' expr[r1] expr[r2] ')'
10699 {
10700 location_set(@1);
10701 $$ = new_tempnumeric("intrinsic_I2");
10702 if( ! intrinsic_call_2($$, $1, $r1, $r2) ) YYERROR;
10703 }
10704
10705 | DATE_TO_YYYYMMDD '(' expr[r1] ')'
10706 {
10707 location_set(@1);
10708 static auto r2 = new_reference(FldNumericDisplay, "50");
10709 static auto one = new cbl_refer_t( new_literal("1") );
10710 static auto four = new cbl_refer_t( new_literal("4") );
10711 cbl_span_t year(one, four);
10712 auto r3 = new_reference(new_alphanumeric(MAXLENGTH_CALENDAR_DATE));
10713 r3->refmod = year;
10714
10715 parser_intrinsic_call_0( r3->field, "__gg__current_date" );
10716
10717 $$ = new_tempnumeric("DATE_TO_YYYYMMDD");
10718 if( ! intrinsic_call_3($$, DATE_TO_YYYYMMDD,
10719 $r1, r2, r3) ) YYERROR;
10720 }
10721
10722 | DATE_TO_YYYYMMDD '(' expr[r1] expr[r2] ')'
10723 {
10724 location_set(@1);
10725 static auto one = new cbl_refer_t( new_literal("1") );
10726 static auto four = new cbl_refer_t( new_literal("4") );
10727 cbl_span_t year(one, four);
10728 auto r3 = new_reference(new_alphanumeric(MAXLENGTH_CALENDAR_DATE));
10729 r3->refmod = year;
10730
10731 parser_intrinsic_call_0( r3->field, "__gg__current_date" );
10732
10733 $$ = new_tempnumeric("DATE_TO_YYYYMMDD");
10734 if( ! intrinsic_call_3($$, DATE_TO_YYYYMMDD,
10735 $r1, $r2, r3) ) YYERROR;
10736 }
10737
10738 | DATE_TO_YYYYMMDD '(' expr[r1]
10739 expr[r2] expr[r3] ')'
10740 {
10741 location_set(@1);
10742 $$ = new_tempnumeric("DATE_TO_YYYYMMDD");
10743 if( ! intrinsic_call_3($$, DATE_TO_YYYYMMDD,
10744 $r1, $r2, $r3) ) YYERROR;
10745 }
10746
10747 | DAY_TO_YYYYDDD '(' expr[r1] ')'
10748 {
10749 location_set(@1);
10750 static auto r2 = new_reference(FldNumericDisplay, "50");
10751 static auto one = new cbl_refer_t( new_literal("1") );
10752 static auto four = new cbl_refer_t( new_literal("4") );
10753 cbl_span_t year(one, four);
10754 auto r3 = new_reference(new_alphanumeric(MAXLENGTH_CALENDAR_DATE));
10755 r3->refmod = year;
10756
10757 parser_intrinsic_call_0( r3->field, "__gg__current_date" );
10758
10759 $$ = new_tempnumeric("DAY_TO_YYYYDDD");
10760 if( ! intrinsic_call_3($$, DAY_TO_YYYYDDD,
10761 $r1, r2, r3) ) YYERROR;
10762 }
10763
10764 | DAY_TO_YYYYDDD '(' expr[r1] expr[r2] ')'
10765 {
10766 location_set(@1);
10767 static auto one = new cbl_refer_t( new_literal("1") );
10768 static auto four = new cbl_refer_t( new_literal("4") );
10769 cbl_span_t year(one, four);
10770 auto r3 = new_reference(new_alphanumeric(MAXLENGTH_CALENDAR_DATE));
10771 r3->refmod = year;
10772
10773 parser_intrinsic_call_0( r3->field, "__gg__current_date" );
10774
10775 $$ = new_tempnumeric("DAY_TO_YYYYDDD");
10776 if( ! intrinsic_call_3($$, DAY_TO_YYYYDDD,
10777 $r1, $r2, r3) ) YYERROR;
10778 }
10779
10780 | DAY_TO_YYYYDDD '(' expr[r1]
10781 expr[r2] expr[r3] ')'
10782 {
10783 location_set(@1);
10784 $$ = new_tempnumeric("DAY_TO_YYYYDDD");
10785 if( ! intrinsic_call_3($$, DAY_TO_YYYYDDD,
10786 $r1, $r2, $r3) ) YYERROR;
10787 }
10788
10789 | YEAR_TO_YYYY '(' expr[r1] ')'
10790 {
10791 location_set(@1);
10792 static auto r2 = new_reference(new_literal("50", decimal_e));
10793 static auto one = new cbl_refer_t( new_literal("1") );
10794 static auto four = new cbl_refer_t( new_literal("4") );
10795 cbl_span_t year(one, four);
10796 auto r3 = new_reference(new_alphanumeric(MAXLENGTH_CALENDAR_DATE));
10797 r3->refmod = year;
10798
10799 parser_intrinsic_call_0( r3->field, "__gg__current_date" );
10800
10801 $$ = new_tempnumeric("YEAR_TO_YYYY");
10802 if( ! intrinsic_call_3($$, YEAR_TO_YYYY,
10803 $r1, r2, r3) ) YYERROR;
10804 }
10805
10806 | YEAR_TO_YYYY '(' expr[r1] expr[r2] ')'
10807 {
10808 location_set(@1);
10809 static auto one = new cbl_refer_t( new_literal("1") );
10810 static auto four = new cbl_refer_t( new_literal("4") );
10811 cbl_span_t year(one, four);
10812 auto r3 = new_reference(new_alphanumeric(MAXLENGTH_CALENDAR_DATE));
10813 r3->refmod = year;
10814
10815 parser_intrinsic_call_0( r3->field, "__gg__current_date" );
10816
10817 $$ = new_tempnumeric("YEAR_TO_YYYY");
10818 if( ! intrinsic_call_3($$, YEAR_TO_YYYY,
10819 $r1, $r2, r3) ) YYERROR;
10820 }
10821
10822 | YEAR_TO_YYYY '(' expr[r1]
10823 expr[r2] expr[r3] ')'
10824 {
10825 location_set(@1);
10826 $$ = new_tempnumeric("YEAR_TO_YYYY");
10827 if( ! intrinsic_call_3($$, YEAR_TO_YYYY,
10828 $r1, $r2, $r3) ) YYERROR;
10829 }
10830
10831 | intrinsic_N2 '(' expr[r1] expr[r2] ')'
10832 {
10833 location_set(@1);
10834 switch($1) {
10835 case ANNUITY:
10836 $$ = new_tempnumeric_float();
10837 break;
10838 case COMBINED_DATETIME:
10839 $$ = new_tempnumeric();
10840 break;
10841 case REM:
10842 $$ = new_tempnumeric_float();
10843 break;
10844 }
10845 $$->data.initial = keyword_str($1); // function name
10846 if( ! intrinsic_call_2($$, $1, $r1, $r2) ) YYERROR;
10847 }
10848
10849 | intrinsic_X2 '(' varg[r1] varg[r2] ')'
10850 {
10851 location_set(@1);
10852 $$ = new_alphanumeric(keyword_str($1));
10853 if( ! intrinsic_call_2($$, $1, $r1, $r2) ) YYERROR;
10854 }
10855 | intrinsic_locale
10856 ;
10857
10858 module_type: ACTIVATING { $$ = module_activating_e; }
10859 | CURRENT { $$ = module_current_e; }
10860 | NESTED { $$ = module_nested_e; }
10861 | STACK { $$ = module_stack_e; }
10862 | TOP_LEVEL { $$ = module_toplevel_e; }
10863 ;
10864
10865 convert_src: ANY
10866 | HEX
10867 | convert_fmt
10868 ;
10869 convert_dst: convert_fmt HEX
10870 | BYTE
10871 ;
10872 convert_fmt: ALPHANUMERIC
10873 | ANUM
10874 | NAT
10875 | NATIONAL
10876 ;
10877
10878 numval_locale: %empty {
10879 $$.is_locale = false;
10880 $$.arg2 = cbl_refer_t::empty();
10881 }
10882 | LOCALE NAME { $$.is_locale = true; $$.arg2 = NULL;
10883 cbl_unimplemented("%<NUMVAL_C LOCALE%>"); YYERROR;
10884 }
10885 | varg { $$.is_locale = false; $$.arg2 = $1; }
10886 ;
10887
10888 subst_inputs: subst_input { $$ = new substitutions_t; $$->push_back($1); }
10889 | subst_inputs subst_input { $$ = $1; $$->push_back($2); }
10890 ;
10891 subst_input: anycase first_last varg[v1] varg[v2] {
10892 $$.init( $anycase, $first_last, $v1, $v2 );
10893 }
10894 ;
10895
10896 intrinsic_locale:
10897 LOCALE_COMPARE '(' varg[r1] varg[r2] ')'
10898 {
10899 location_set(@1);
10900 $$ = new_alphanumeric();
10901 cbl_refer_t dummy = {};
10902 if( ! intrinsic_call_3($$, LOCALE_COMPARE, $r1, $r2, &dummy) ) YYERROR;
10903 }
10904 | LOCALE_COMPARE '(' varg[r1] varg[r2] varg[r3] ')'
10905 {
10906 location_set(@1);
10907 $$ = new_alphanumeric();
10908 if( ! intrinsic_call_3($$, LOCALE_COMPARE, $r1, $r2, $r3) ) YYERROR;
10909 }
10910
10911 | LOCALE_DATE '(' varg[r1] ')'
10912 {
10913 location_set(@1);
10914 $$ = new_alphanumeric();
10915 cbl_refer_t dummy = {};
10916 if( ! intrinsic_call_2($$, LOCALE_DATE, $r1, &dummy) ) YYERROR;
10917 }
10918 | LOCALE_DATE '(' varg[r1] varg[r2] ')'
10919 {
10920 location_set(@1);
10921 $$ = new_alphanumeric();
10922 if( ! intrinsic_call_2($$, LOCALE_DATE, $r1, $r2) ) YYERROR;
10923 }
10924 | LOCALE_TIME '(' varg[r1] ')'
10925 {
10926 location_set(@1);
10927 $$ = new_alphanumeric();
10928 cbl_refer_t dummy = {};
10929 if( ! intrinsic_call_2($$, LOCALE_TIME, $r1, &dummy) ) YYERROR;
10930 }
10931 | LOCALE_TIME '(' varg[r1] varg[r2] ')'
10932 {
10933 location_set(@1);
10934 $$ = new_alphanumeric();
10935 if( ! intrinsic_call_2($$, LOCALE_TIME, $r1, $r2) ) YYERROR;
10936 }
10937 | LOCALE_TIME_FROM_SECONDS '(' varg[r1] ')'
10938 {
10939 location_set(@1);
10940 $$ = new_alphanumeric();
10941 cbl_refer_t dummy = {};
10942 if( ! intrinsic_call_2($$, LOCALE_TIME_FROM_SECONDS, $r1, &dummy) ) YYERROR;
10943 }
10944 | LOCALE_TIME_FROM_SECONDS '(' varg[r1] varg[r2] ')'
10945 {
10946 location_set(@1);
10947 $$ = new_alphanumeric();
10948 if( ! intrinsic_call_2($$, LOCALE_TIME_FROM_SECONDS, $r1, $r2) ) YYERROR;
10949 }
10950 ;
10951
10952 lopper_case: LOWER_CASE { $$ = LOWER_CASE; }
10953 | UPPER_CASE { $$ = UPPER_CASE; }
10954 ;
10955
10956 trim_trailing: %empty { $$ = new_literal("0"); } // Remove both
10957 | LEADING { $$ = new_literal("1"); } // Remove leading spaces
10958 | TRAILING { $$ = new_literal("2"); } // Remove trailing spaces
10959 ;
10960
10961 intrinsic0: CURRENT_DATE {
10962 location_set(@1);
10963 $$ = new_alphanumeric(MAXLENGTH_CALENDAR_DATE, "CURRENT-DATE");
10964 parser_intrinsic_call_0( $$, "__gg__current_date" );
10965 }
10966 | E {
10967 location_set(@1);
10968 $$ = new_tempnumeric("E");
10969 parser_intrinsic_call_0( $$, "__gg__e" );
10970 }
10971
10972 | EXCEPTION_FILE_N {
10973 location_set(@1);
10974 $$ = new_alphanumeric("EXCEPTION-FILE-N");
10975 intrinsic_call_0( $$, EXCEPTION_FILE_N );
10976 }
10977
10978 | EXCEPTION_FILE {
10979 location_set(@1);
10980 $$ = new_alphanumeric("EXCEPTION-FILE");
10981 parser_exception_file( $$ );
10982 }
10983 | EXCEPTION_LOCATION_N {
10984 location_set(@1);
10985 $$ = new_alphanumeric("EXCEPTION-LOCATION-N");
10986 intrinsic_call_0( $$, EXCEPTION_LOCATION_N );
10987 }
10988 | EXCEPTION_LOCATION {
10989 location_set(@1);
10990 $$ = new_alphanumeric("EXCEPTION-LOCATION");
10991 intrinsic_call_0( $$, EXCEPTION_LOCATION );
10992 }
10993 | EXCEPTION_STATEMENT {
10994 location_set(@1);
10995 $$ = new_alphanumeric("EXCEPTION-STATEMENT");
10996 intrinsic_call_0( $$, EXCEPTION_STATEMENT );
10997 }
10998 | EXCEPTION_STATUS {
10999 location_set(@1);
11000 $$ = new_alphanumeric("EXCEPTION-STATUS");
11001 intrinsic_call_0( $$, EXCEPTION_STATUS );
11002 }
11003
11004 | PI {
11005 location_set(@1);
11006 $$ = new_tempnumeric_float("PI");
11007 parser_intrinsic_call_0( $$, "__gg__pi" );
11008 }
11009 | SECONDS_PAST_MIDNIGHT {
11010 location_set(@1);
11011 $$ = new_tempnumeric("SECONDS-PAST-MIDNIGHT");
11012 intrinsic_call_0( $$, SECONDS_PAST_MIDNIGHT );
11013 }
11014 | UUID4 {
11015 location_set(@1);
11016 $$ = new_alphanumeric("UUID4");
11017 parser_intrinsic_call_0( $$, "__gg__uuid4" );
11018 }
11019 | WHEN_COMPILED {
11020 location_set(@1);
11021 // Returns YYYYMMDDhhmmssss-0500)
11022 $$ = new_alphanumeric(MAXLENGTH_CALENDAR_DATE, "WHEN-COMPILED");
11023 parser_intrinsic_call_0( $$, "__gg__when_compiled" );
11024 }
11025 ;
11026
11027 intrinsic_I: BOOLEAN_OF_INTEGER { $$ = BOOLEAN_OF_INTEGER;
11028 cbl_unimplemented("BOOLEAN-OF-INTEGER");
11029 }
11030 | CHAR_NATIONAL { $$ = CHAR_NATIONAL;
11031 cbl_unimplemented("CHAR-NATIONAL");
11032 }
11033 | DATE_OF_INTEGER { $$ = DATE_OF_INTEGER; }
11034 | DAY_OF_INTEGER { $$ = DAY_OF_INTEGER; }
11035 | FACTORIAL { $$ = FACTORIAL; }
11036 | FRACTION_PART { $$ = FRACTION_PART; }
11037 | HIGHEST_ALGEBRAIC { $$ = HIGHEST_ALGEBRAIC; }
11038 | INTEGER { $$ = INTEGER; }
11039 | INTEGER_OF_BOOLEAN { $$ = INTEGER_OF_BOOLEAN;
11040 cbl_unimplemented("INTEGER-OF-BOOLEAN");
11041 }
11042 | INTEGER_OF_DATE { $$ = INTEGER_OF_DATE; }
11043 | INTEGER_OF_DAY { $$ = INTEGER_OF_DAY; }
11044 | INTEGER_PART { $$ = INTEGER_PART; }
11045 | LOWEST_ALGEBRAIC { $$ = LOWEST_ALGEBRAIC; }
11046 | SIGN { $$ = SIGN; }
11047 | TEST_DATE_YYYYMMDD { $$ = TEST_DATE_YYYYMMDD; }
11048 | TEST_DAY_YYYYDDD { $$ = TEST_DAY_YYYYDDD; }
11049 | ULENGTH { $$ = ULENGTH; }
11050 | UPOS { $$ = UPOS; }
11051 | USUPPLEMENTARY { $$ = USUPPLEMENTARY; }
11052 | UVALID { $$ = UVALID; }
11053 | UWIDTH { $$ = UWIDTH; }
11054 ;
11055
11056 intrinsic_I2: MOD { $$ = MOD; }
11057 ;
11058
11059 intrinsic_N: ABS { $$ = ABS; }
11060 | ACOS { $$ = ACOS; }
11061 | ASIN { $$ = ASIN; }
11062 | ATAN { $$ = ATAN; }
11063 | COS { $$ = COS; }
11064 | EXP { $$ = EXP; }
11065 | EXP10 { $$ = EXP10; }
11066 | LOG { $$ = LOG; }
11067 | LOG10 { $$ = LOG10; }
11068 | SIN { $$ = SIN; }
11069 | SMALLEST_ALGEBRAIC { $$ = SMALLEST_ALGEBRAIC;
11070 cbl_unimplemented("SMALLEST-ALGEBRAIC");
11071 }
11072 | SQRT { $$ = SQRT; }
11073 | TAN { $$ = TAN; }
11074 ;
11075
11076 intrinsic_N2: ANNUITY { $$ = ANNUITY; }
11077 | COMBINED_DATETIME { $$ = COMBINED_DATETIME; }
11078 | REM { $$ = REM; }
11079 ;
11080
11081 intrinsic_X: BIT_TO_CHAR { $$ = BIT_TO_CHAR; }
11082 | BYTE_LENGTH { $$ = BYTE_LENGTH; }
11083 | HEX_TO_CHAR { $$ = HEX_TO_CHAR; }
11084 | NUMVAL { $$ = NUMVAL; }
11085 | NUMVAL_F { $$ = NUMVAL_F; }
11086 | REVERSE { $$ = REVERSE; }
11087 | TEST_NUMVAL { $$ = TEST_NUMVAL; }
11088 | TEST_NUMVAL_F { $$ = TEST_NUMVAL_F; }
11089 ;
11090
11091 intrinsic_X2: NATIONAL_OF { $$ = NATIONAL_OF; }
11092 ;
11093
11094 intrinsic_v: CONCAT { $$ = CONCAT; }
11095 | MAXX { $$ = MAXX; }
11096 | MEAN { $$ = MEAN; }
11097 | MEDIAN { $$ = MEDIAN; }
11098 | MIDRANGE { $$ = MIDRANGE; }
11099 | MINN { $$ = MINN; }
11100 | ORD_MAX { $$ = ORD_MAX; }
11101 | ORD_MIN { $$ = ORD_MIN; }
11102 | RANGE { $$ = RANGE; }
11103 | STANDARD_DEVIATION { $$ = STANDARD_DEVIATION; }
11104 | SUM { $$ = SUM; }
11105 | VARIANCE { $$ = VARIANCE; }
11106 ;
11107
11108 all: %empty { $$ = false; }
11109 | ALL { $$ = true; }
11110 ;
11111
11112 anycase: %empty { $$ = false; }
11113 | ANYCASE { $$ = true; }
11114 ;
11115
11116 as: %empty
11117 | AS
11118 ;
11119
11120 at: %empty
11121 | AT
11122 ;
11123
11124 by: %empty
11125 | BY
11126 ;
11127
11128 characters: %empty
11129 | CHARACTERS
11130 ;
11131
11132 collating: %empty
11133 | COLLATING
11134 ;
11135
11136 contains: %empty
11137 | CONTAINS
11138 ;
11139
11140 in: %empty
11141 | IN
11142 ;
11143
11144 data: %empty
11145 | DATA
11146 ;
11147
11148 exception: %empty
11149 | EXCEPTION
11150 ;
11151
11152 file: %empty
11153 | FILE_KW
11154 ;
11155
11156 first_last: %empty { $$ = 0; }
11157 | FIRST { $$ = 'F'; }
11158 | LAST { $$ = 'L'; }
11159 ;
11160
11161 is_global: %empty %prec GLOBAL { $$ = false; }
11162 | is GLOBAL { $$ = true; }
11163 ;
11164
11165 global: %empty %prec GLOBAL { $$ = false; }
11166 | GLOBAL { $$ = true; }
11167 ;
11168
11169 initial: %empty { $$ = 0; }
11170 | INITIAL_kw { $$ = INITIAL_kw; }
11171 ;
11172
11173 is: %empty
11174 | IS
11175 ;
11176
11177 key: %empty
11178 | KEY
11179 ;
11180
11181 last: %empty %prec LAST
11182 | LAST
11183 ;
11184
11185 lines: %empty
11186 | LINE
11187 | LINES
11188 ;
11189
11190 mode: %empty
11191 | MODE
11192 ;
11193
11194 native: %empty
11195 | NATIVE
11196 ;
11197
11198 of: %empty
11199 | OF
11200 ;
11201
11202 on: %empty
11203 | ON
11204 ;
11205
11206 optional: %empty { $$ = false; }
11207 | OPTIONAL { $$ = true; }
11208 ;
11209
11210 program_kw: %empty
11211 | PROGRAM_kw
11212 ;
11213
11214 order: %empty
11215 | ORDER
11216 ;
11217
11218 record: %empty
11219 | RECORD
11220 ;
11221
11222 sign: %empty
11223 | SIGN
11224 ;
11225
11226 is_signed: %empty { $$ = true; }
11227 | SIGNED_kw { $$ = true; }
11228 | UNSIGNED_kw { $$ = false; }
11229 ;
11230
11231 start_after: %empty %prec AFTER
11232 | START AFTER varg
11233 ;
11234
11235 status: %empty
11236 | STATUS
11237 ;
11238 strong: %empty { $$ = true; }
11239 | STRONG { $$ = false; }
11240 ;
11241
11242 times: %empty
11243 | TIMES
11244 ;
11245 then: %empty
11246 | THEN
11247 ;
11248
11249 to: %empty
11250 | TO
11251 ;
11252
11253 usage: %empty
11254 | USAGE
11255 | USAGE IS
11256 ;
11257
11258 with: %empty
11259 | WITH
11260 ;
11261
11262 /*
11263 * CDF: Compiler-directing Facility
11264 */
11265 cdf: cdf_none
11266 | cdf_library
11267 | cdf_listing
11268 | cdf_option
11269 ;
11270
11271 cdf_library: cdf_basis
11272 /* | DELETE */
11273 | INSERTT
11274 ;
11275 cdf_basis: BASIS NAME /* BASIS is never passed to the parser. */
11276 | BASIS LITERAL
11277 ;
11278
11279 cdf_use: cdf_use_when {
11280 statement_cleanup = false;
11281 }
11282 ;
11283
11284 cdf_use_when: USE DEBUGGING on labels
11285 {
11286 if( ! current.declarative_section_name() ) {
11287 error_msg(@1, "USE valid only in DECLARATIVES");
11288 YYERROR;
11289 }
11290 std::for_each($labels->elems.begin(), $labels->elems.end(),
11291 add_debugging_declarative);
11292
11293 }
11294 | USE DEBUGGING on ALL PROCEDURES
11295 {
11296 if( ! current.declarative_section_name() ) {
11297 error_msg(@1, "USE valid only in DECLARATIVES");
11298 YYERROR;
11299 }
11300 static const cbl_label_t all = {
11301 LblNone, 0, 0,0,0, false, false, false, 0,0, ":all:" };
11302 add_debugging_declarative(&all);
11303 }
11304
11305 | USE globally mistake procedure on filenames
11306 { // Format 1
11307 if( ! current.declarative_section_name() ) {
11308 error_msg(@1, "USE valid only in DECLARATIVES");
11309 YYERROR;
11310 }
11311 bool global = $globally == GLOBAL;
11312 std::list<size_t> files;
11313 auto& culprits = $filenames->files;
11314 std::transform( culprits.begin(), culprits.end(),
11315 std::back_inserter(files),
11316 file_list_t::symbol_index );
11317 cbl_declarative_t declarative(current.declarative_section(),
11318 ec_io_e, files,
11319 file_mode_any_e, global);
11320 current.declaratives.add(declarative);
11321 }
11322
11323 | USE globally mistake procedure on io_mode
11324 { // Format 1
11325 if( ! current.declarative_section_name() ) {
11326 error_msg(@1, "USE valid only in DECLARATIVES");
11327 YYERROR;
11328 }
11329 bool global = $globally == GLOBAL;
11330 std::list<size_t> files;
11331 cbl_declarative_t declarative(current.declarative_section(),
11332 ec_io_e, files,
11333 $io_mode, global);
11334 current.declaratives.add(declarative);
11335 }
11336 | USE cdf_use_excepts
11337 { // Format 3 (AFTER swallowed by lexer)
11338 if( ! current.declarative_section_name() ) {
11339 error_msg(@1, "USE valid only in DECLARATIVES");
11340 YYERROR;
11341 }
11342 }
11343 ;
11344
11345 cdf_use_excepts:
11346 cdf_use_except
11347 | cdf_use_excepts cdf_use_except
11348 ;
11349 cdf_use_except: EC NAME cdf_use_files[files]
11350 {
11351 auto ec = ec_type_of($NAME);
11352 if( ec == ec_none_e ) {
11353 error_msg(@NAME, "not an EXCEPTION CONDITION: %s", $NAME);
11354 YYERROR;
11355 }
11356 std::list<size_t> files;
11357 if( $files ) {
11358 if( ec_io_e != (ec_io_e & ec) ) {
11359 error_msg(@NAME, "not an I-O EXCEPTION CONDITION: %s", $NAME);
11360 YYERROR;
11361 }
11362 auto& culprits = $files->files;
11363 std::transform( culprits.begin(), culprits.end(),
11364 std::back_inserter(files),
11365 file_list_t::symbol_index );
11366 }
11367
11368 cbl_declarative_t declarative(current.declarative_section(),
11369 ec, files, file_mode_none_e);
11370 // Check for duplicates, but keep going.
11371 current.declaratives.add(declarative);
11372 }
11373 ;
11374 cdf_use_files: %empty { $$ = NULL; }
11375 | FILE_KW filenames { $$ = $2; }
11376 ;
11377
11378 io_mode: INPUT { $$ = file_mode_input_e; }
11379 | OUTPUT { $$ = file_mode_output_e; }
11380 | IO { $$ = file_mode_io_e; }
11381 | EXTEND { $$ = file_mode_extend_e; }
11382 ;
11383
11384 globally: global { $$ = $1? GLOBAL : 0; }
11385 | global STANDARD { $$ = $1? GLOBAL : STANDARD; }
11386 | global AFTER { $$ = $1? GLOBAL : 0; }
11387 | global AFTER STANDARD { $$ = $1? GLOBAL : STANDARD; }
11388 ;
11389 mistake: EXCEPTION { $$ = EXCEPTION; }
11390 | ERROR { $$ = ERROR; }
11391 ;
11392 procedure: %empty
11393 | PROCEDURE
11394 ;
11395
11396 cdf_listing: STAR_CBL star_cbl_opts
11397 ;
11398 star_cbl_opts: star_cbl_opt
11399 | star_cbl_opts star_cbl_opt
11400 ;
11401 star_cbl_opt: LIST { $$ = $LIST[0] == 'N'? NOLIST : LIST; }
11402 | MAP { $$ = $MAP[0] == 'N'? NOMAP : MAP; }
11403 /* | SOURCE { $$ = $SOURCE[0] == 'N'? NOSOURCE : SOURCE; } */
11404 ;
11405
11406 cdf_option: CBL cbl_options
11407 ;
11408 cbl_options: cbl_option
11409 | cbl_options cbl_option
11410 ;
11411 cbl_option: LITERAL
11412 ; /* Ignore all options. */
11413
11414 /* The following compiler directing statements have no effect */
11415 cdf_none: ENTER
11416 | READY
11417 | RESET
11418 | TRACE
11419 | SERVICE_RELOAD
11420 ;
11421
11422
11423 %%
11424
11425 static YYLTYPE
11426 first_line_of( YYLTYPE loc ) {
11427 if( loc.first_line < loc.last_line ) loc.last_line = loc.first_line;
11428 if( loc.last_column < loc.first_column ) loc.last_column = loc.first_column;
11429 return loc;
11430 }
11431
11432 void ast_call( const YYLTYPE& loc, cbl_refer_t name, const cbl_refer_t& returning,
11433 size_t narg, cbl_ffi_arg_t args[],
11434 cbl_label_t *except,
11435 cbl_label_t *not_except,
11436 bool is_function)
11437 {
11438 if( is_literal(name.field) ) {
11439 cbl_field_t called = { 0, FldLiteralA, FldInvalid, quoted_e | constant_e,
11440 0, 0, 77, nonarray, 0, "",
11441 0, cbl_field_t::linkage_t(), {}, NULL };
11442 snprintf(called.name, sizeof(called.name), "_%s", name.field->data.initial);
11443 called.data = name.field->data;
11444 name.field = cbl_field_of(symbol_field_add(PROGRAM, &called));
11445 symbol_field_location(field_index(name.field), loc);
11446 parser_symbol_add(name.field);
11447 }
11448
11449 parser_call( name, returning, narg, args, except, not_except, is_function );
11450 }
11451
11452 /*
11453 * Check if any EC *could* be raised that would be handled by a declarative. If
11454 * so, the generated statement epilog will ask the runtime library to attempt
11455 * to match any raised EC with a declarative. If not, the statement epilog
11456 * will be limited to calling the default EC handler, which logs unhandled ECs
11457 * [todo] and calls abort(3) for fatal ECs.
11458 */
11459 static bool
11460 possible_ec() {
11461 cbl_enabled_exceptions_t& enabled_exceptions( cdf_enabled_exceptions() );
11462 bool format_1 = current.declaratives.has_format_1();
11463
11464 bool enabled = 0xFF < (current.declaratives.status()
11465 &
11466 enabled_exceptions.status());
11467 bool epilog = enabled || format_1;
11468
11469 dbgmsg("%sEC handling for DCL %08x && EC %08x with %s Format 1",
11470 epilog? "" : "no ",
11471 current.declaratives.status(),
11472 enabled_exceptions.status(), format_1? "a" : "no");
11473
11474 return epilog;
11475 }
11476
11477 /*
11478 * If there's potential overlap between enabled ECs and Declaratives, generate
11479 * a PERFORM of the _DECLARATIVES_EVAL "ladder" that matches a section number
11480 * to its name, and executes the Declarative.
11481 */
11482 static void
11483 statement_epilog( int token ) {
11484 cbl_enabled_exceptions_t& enabled_exceptions( cdf_enabled_exceptions() );
11485 if( possible_ec() && token != CONTINUE ) {
11486 if( enabled_exceptions.size() ) {
11487 current.declaratives_evaluate();
11488 }
11489 }
11490 parser_check_fatal_exception();
11491 }
11492
11493 static inline void
11494 statement_prolog( int token ) {
11495 parser_statement_begin( keyword_str(token),
11496 current.declaratives.runtime.ena,
11497 current.declaratives.runtime.dcl );
11498 }
11499
11500 /*
11501 * We check the EC against the Declarative status prior to parsing the
11502 * statement because a TURN directive can be embedded in the statement. An
11503 * embedded directive applies to the following statement, not the one being
11504 * parsed.
11505 */
11506 static void
11507 statement_begin( const YYLTYPE& loc, int token ) {
11508 static int prior_token = 0;
11509
11510 if( statement_cleanup ) {
11511 statement_epilog(prior_token);
11512 } else {
11513 statement_cleanup = true;
11514 }
11515 location_set(loc);
11516 statement_prolog(token);
11517
11518 prior_token = token;
11519 }
11520
11521 #include "parse_util.h"
11522 #include <sys/types.h>
11523
11524 struct string_match {
11525 const char *name;
11526 explicit string_match( const char name[] ) : name(name) {}
11527 bool operator()( const char input[] ) const {
11528 return strlen(name) == strlen(input) && 0 == strcasecmp(name, input);
11529 }
11530 };
11531
11532 const char *
11533 keyword_str( int token ) {
11534 switch( token ) {
11535 case YYEOF: return "YYEOF";
11536 case YYEMPTY: return "YYEMPTY";
11537 case 256: return "YYerror";
11538 case 257: return "invalid token"; // YYUNDEF
11539 }
11540
11541 if( token < 256 ) {
11542 static char ascii[2];
11543 ascii[0] = token;
11544 return ascii;
11545 }
11546
11547 return cdf_tokens.name_of(token);
11548 }
11549
11550 bool iso_cobol_word( const std::string& name, bool include_context );
11551
11552 /*
11553 * Return the token for the Cobol name, unless it is a function name. The
11554 * lexer uses keyword_tok to determine if what appears to be a NAME is in fact
11555 * a token defined by the parser. For functions, the situation is unambiguous:
11556 * a function name appears only after FUNCTION or in the REPOSITORY paragraph.
11557 * All function names are rejected here; the lexer uses typed_name to check
11558 * REPOSITORY names.
11559 */
11560
11561 // tokens.h is generated as needed from parse.h with tokens.h.gen
11562 current_tokens_t::tokenset_t::tokenset_t() {
11563 #include "token_names.h"
11564 }
11565
11566
11567 // Look up the lowercase form of a keyword, excluding some CDF names.
11568 int
11569 current_tokens_t::tokenset_t::find( const cbl_name_t name, bool include_intrinsics ) {
11570 static const cbl_name_t non_names[] = { // including CDF NAMES, and "SWITCH"
11571 "CHECKING", "LIST", "LOCATION", "MAP", "SWITCH",
11572 }, * const eonames = non_names + COUNT_OF(non_names);
11573
11574 if( std::any_of(non_names, eonames,
11575 [candidate=name](const cbl_name_t non_name) {
11576 return 0 == strcasecmp(non_name, candidate)
11577 && strlen(non_name) == strlen(candidate);
11578 } ) ) {
11579 return 0; // CDF names are never ordinary tokens
11580 }
11581
11582 if( dialect_ibm() ) {
11583 static const cbl_name_t ibm_non_names[] = {
11584 "RESUME",
11585 }, * const eoibm = ibm_non_names + COUNT_OF(ibm_non_names);
11586
11587 if( std::any_of(ibm_non_names, eoibm,
11588 [candidate=name](const cbl_name_t non_name) {
11589 return 0 == strcasecmp(non_name, candidate)
11590 && strlen(non_name) == strlen(candidate);
11591 } ) ) {
11592 return 0; // Names not reserved by IBM are never ordinary IBM tokens
11593 }
11594 }
11595
11596 /*
11597 * The input name may be one of:
11598 * 1. an intrinsic function name (OK if include_intrinsics)
11599 * 2. an ISO/GCC reserved word or context-sensitive word (OK)
11600 * 3. a token in our token list for convenience, such as BINARY_INTEGER (bzzt)
11601 */
11602
11603 cbl_name_t lname;
11604 std::transform(name, name + strlen(name) + 1, lname, ftolower);
11605 auto p = tokens.find(lname);
11606 if( p == tokens.end() ) return 0;
11607 int token = p->second;
11608
11609 if( token == SECTION ) yylval.number = 0;
11610
11611 if( include_intrinsics && intrinsic_cname(token) ) return token;
11612 if( iso_cobol_word(uppercase(name), true) ) return token;
11613
11614 return 0;
11615 }
11616
11617 int
11618 keyword_tok( const char * text, bool include_intrinsics ) {
11619 return cdf_tokens.find(text, include_intrinsics);
11620 }
11621
11622 static inline size_t
11623 verify_figconst( enum cbl_figconst_t figconst , size_t pos ) {
11624 const cbl_field_t *f = cbl_field_of(symbol_at(pos));
11625 assert((f->attr & FIGCONST_MASK) == figconst);
11626 return pos;
11627 }
11628
11629 static size_t
11630 constant_index( int token ) {
11631 switch(token) {
11632 case SPACES : return 0;
11633 case LOW_VALUES : return verify_figconst(low_value_e, 2);
11634 case ZERO : return verify_figconst(zero_value_e, 3);
11635 case HIGH_VALUES : return verify_figconst(high_value_e, 4);
11636 case QUOTES : return 5;
11637 case NULLS : return 6;
11638 }
11639 cbl_errx( "%s:%d: no such constant %d", __func__, __LINE__, token);
11640 return (size_t)-1;
11641 }
11642
11643
11644 static enum relop_t
11645 relop_of(int token) {
11646 switch(token) {
11647 case '<': return lt_op;
11648 case LE: return le_op;
11649 case '=': return eq_op;
11650 case NE: return ne_op;
11651 case GE: return ge_op;
11652 case '>': return gt_op;
11653 }
11654 cbl_internal_error( "%s:%d: invalid relop token %d",
11655 __func__, __LINE__, token);
11656
11657 return lt_op; // not reached
11658 }
11659
11660 static relop_t
11661 relop_invert(relop_t op) {
11662 switch(op) {
11663 case lt_op: return ge_op;
11664 case le_op: return gt_op;
11665 case eq_op: return ne_op;
11666 case ne_op: return eq_op;
11667 case ge_op: return lt_op;
11668 case gt_op: return le_op;
11669 }
11670 cbl_internal_error("%s:%d: invalid %<relop_t%> %d", __func__, __LINE__, op);
11671
11672 return relop_t(0); // not reached
11673 }
11674
11675 #if needed
11676 static const char *
11677 relop_debug_str(int token) {
11678 switch(token) {
11679 case 0: return "zilch";
11680 case '<': return "<";
11681 case LE: return "LE";
11682 case '=': return "=";
11683 case NE: return "NE";
11684 case GE: return "GE";
11685 case '>': return ">";
11686 }
11687 dbgmsg("%s:%d: invalid relop token %d", __func__, __LINE__, token);
11688 return "???";
11689 }
11690
11691 static int
11692 token_of(enum relop_t op) {
11693 switch(op) {
11694 case lt_op: return '<';
11695 case le_op: return LE;
11696 case eq_op: return '=';
11697 case ne_op: return NE;
11698 case ge_op: return GE;
11699 case gt_op: return '>';
11700 }
11701 cbl_errx( "%s:%d: invalid relop_t %d", __func__, __LINE__, op);
11702
11703 return 0; // not reached
11704 }
11705 #endif
11706
11707 static enum classify_t
11708 classify_of( int token ) {
11709 switch(token) {
11710 case NUMERIC: return ClassNumericType;
11711 case ALPHABETIC: return ClassAlphabeticType;
11712 case ALPHABETIC_LOWER: return ClassLowerType;
11713 case ALPHABETIC_UPPER: return ClassUpperType;
11714 case DBCS: return ClassDbcsType;
11715 case KANJI: return ClassKanjiType;
11716 }
11717 return (enum classify_t)-1;
11718 }
11719
11720 static cbl_round_t
11721 rounded_of( int token ) {
11722 cbl_round_t mode = current_rounded_mode();
11723 if( 0 <= token && token <= int(truncation_e) ) {
11724 return cbl_round_t(token);
11725 }
11726 switch(token) {
11727 case ROUNDED:
11728 mode = current.rounded_mode();
11729 break;
11730 case AWAY_FROM_ZERO:
11731 mode = away_from_zero_e;
11732 break;
11733 case NEAREST_TOWARD_ZERO:
11734 mode = nearest_toward_zero_e;
11735 break;
11736 case TOWARD_GREATER:
11737 mode = toward_greater_e;
11738 break;
11739 case TOWARD_LESSER:
11740 mode = toward_lesser_e;
11741 break;
11742 case NEAREST_AWAY_FROM_ZERO:
11743 mode = nearest_away_from_zero_e;
11744 break;
11745 case NEAREST_EVEN:
11746 mode = nearest_even_e;
11747 break;
11748 case PROHIBITED:
11749 mode = prohibited_e;
11750 break;
11751 case TRUNCATION:
11752 mode = truncation_e;
11753 break;
11754 default:
11755 dbgmsg("%s: logic error: unrecognized rounding value %d", __func__, token);
11756 }
11757 return mode;
11758 }
11759
11760 static cbl_round_t
11761 current_rounded_mode( int token ) {
11762 cbl_round_t mode = rounded_of(token);
11763 return current.rounded_mode(mode);
11764 }
11765
11766 template <cbl_label_type_t T>
11767 class label_named {
11768 size_t program;
11769 const char *name;
11770 public:
11771 label_named( size_t program, const char name[] )
11772 : program(program), name(name) {}
11773 bool operator()( const symbol_elem_t& sym ) const {
11774 if( sym.program == program && sym.type == SymLabel ) {
11775 auto p = cbl_label_of(&sym);
11776 return p->type == T && 0 == strcasecmp(p->name, name);
11777 }
11778 return false;
11779 }
11780 };
11781
11782 typedef label_named<LblSection> section_named;
11783 typedef label_named<LblParagraph> paragraph_named;
11784
11785 static struct cbl_label_t *
11786 label_add( const YYLTYPE& loc,
11787 enum cbl_label_type_t type, const char name[] ) {
11788 size_t parent = 0;
11789
11790 // Verify the new paragraph doesn't conflict with a section
11791 if( type == LblParagraph ) {
11792 parent = current.program_section();
11793 auto p = std::find_if(symbols_begin(PROGRAM), symbols_end(),
11794 section_named(PROGRAM, name));
11795 if( p != symbols_end() ) {
11796 error_msg(loc, "paragraph %s conflicts with section %s on line %d",
11797 name, cbl_label_of(p)->name, cbl_label_of(p)->line);
11798 }
11799 }
11800
11801 // Verify the new section doesn't conflict with a paragraph
11802 if( type == LblSection ) {
11803 // line is zero if the forward reference is to PARA OF SECT
11804 auto p = std::find_if(symbols_begin(PROGRAM), symbols_end(),
11805 paragraph_named(PROGRAM, name));
11806 if( p != symbols_end() ) {
11807 error_msg(loc, "section %s conflicts with paragraph %s on line %d",
11808 name, cbl_label_of(p)->name, cbl_label_of(p)->line);
11809 }
11810 }
11811 struct cbl_label_t label = { type, parent, loc.first_line };
11812
11813 if( !namcpy(loc, label.name, name) ) return NULL;
11814 auto p = symbol_label_add(PROGRAM, &label);
11815
11816 if( type == LblParagraph || type == LblSection ) {
11817 procedure_definition_add(PROGRAM, p);
11818 }
11819
11820 assert( !(p->type == LblSection && p->parent > 0) );
11821
11822 return p;
11823 }
11824
11825 /*
11826 * Many label names are defined statically and so are guaranteed to be in
11827 * bounds. Often they are created far away from the yacc metavariables, so
11828 * there's no location to access.
11829 */
11830 static struct cbl_label_t *
11831 label_add( enum cbl_label_type_t type, const char name[], int line ) {
11832 YYLTYPE loc { line, 1, line, 1 };
11833 return label_add(loc, type, name);
11834 }
11835
11836 cbl_label_t *
11837 perform_t::ec_labels_t::new_label( cbl_label_type_t type,
11838 const cbl_name_t role )
11839 {
11840 size_t n = 1 + symbols_end() - symbols_begin();
11841 cbl_name_t name;
11842 sprintf(name, "_perf_%s_" HOST_SIZE_T_PRINT_UNSIGNED, role, (fmt_size_t)n);
11843 return label_add( type, name, yylineno );
11844 }
11845
11846 /*
11847 * An unqualified procedure reference occurs within a section may refer to a:
11848 * 1. section
11849 * 2. paragraph, perhaps in a section, perhaps the current section.
11850 *
11851 * The named procedure need only be unique, either within the current section
11852 * or globally. A paragraph within one section may be referenced without
11853 * qualification in another section if its name is unique.
11854 *
11855 * An otherwise globally unique name is shadowed by the same name in the
11856 * current section, and the section-local name may be referenced before being
11857 * defined. That is, given:
11858 *
11859 * S1 SECTION.
11860 * PROC.
11861 * ...
11862 * S2 SECTION.
11863 * PERFORM PROC.
11864 * PROC. ...
11865 *
11866 * the procedure performed is PROC OF S2.
11867 *
11868 * That creates a challenge for the compiler, because PROC appears to have been
11869 * defined when PERFORM is encountered. When PROC OF S2 is defined, the parser
11870 * detects and corrects its misstep.
11871 */
11872 static struct cbl_label_t *
11873 paragraph_reference( const char name[], size_t section )
11874 {
11875 // A reference has line == 0. It is LblParagraph if the section is
11876 // explicitly named, else LblNone (because we don't know).
11877 struct cbl_label_t *p, label = { section? LblParagraph : LblNone, section };
11878 assert(strlen(name) < sizeof(label.name)); // caller ensures
11879 strcpy(label.name, name);
11880 if( label.type == LblNone ) assert(label.parent == 0);
11881
11882 p = symbol_label_add(PROGRAM, &label);
11883 assert(p);
11884
11885 const char *sect_name = section? cbl_label_of(symbol_at(section))->name : NULL;
11886 procedure_reference_add(sect_name, p->name, yylineno, current.program_section());
11887
11888 return p;
11889 }
11890
11891 void
11892 current_t::repository_add_all() {
11893 assert( !programs.empty() );
11894 auto& repository = programs.top().function_repository;
11895 std::copy( function_descrs, function_descrs_end,
11896 std::inserter(repository, repository.begin()) );
11897 }
11898
11899 /*
11900 * A function is added to the symbol table when first named, in Identification
11901 * Division. It's also added to the current list of UDFs in current_t::udfs.
11902 * Its return type and parameters, if any, are defined later, in Procedure
11903 * Division. When they are parsed, we call udf_update to finalize the
11904 * functions's descriptor, giving us enough information to validate the
11905 * arguments at point of invocation.
11906 */
11907 void
11908 current_t::udf_update( const ffi_args_t *ffi_args ) {
11909 auto L = cbl_label_of(symbol_at(program_index()));
11910 assert(L);
11911 assert(L->type == LblFunction);
11912 assert(L->returning);
11913 if( ! ffi_args ) return;
11914 assert(ffi_args->elems.size() < sizeof(function_descr_t::types));
11915
11916 const auto returning = cbl_field_of(symbol_at(L->returning));
11917 auto key = function_descr_t::init(L->name);
11918 auto func = udfs.find(key);
11919 assert(func != udfs.end());
11920
11921 function_descr_t udf = *func;
11922
11923 udf.ret_type = returning->type;
11924 udf.token = ffi_args->elems.empty()? FUNCTION_UDF_0 : FUNCTION_UDF;
11925 auto types = ffi_args->parameter_types();
11926 strcpy(udf.types, types);
11927
11928 std::transform( ffi_args->elems.begin(), ffi_args->elems.end(),
11929 std::back_inserter(udf.linkage_fields),
11930 []( const cbl_ffi_arg_t& arg ) {
11931 return function_descr_arg_t( field_index( arg.refer.field ),
11932 arg.crv, arg.optional );
11933 } );
11934
11935 udfs.erase(func);
11936 auto result = udfs.insert(udf);
11937 assert(result.second);
11938 }
11939
11940 bool
11941 current_t::udf_args_valid( const cbl_label_t *L,
11942 const std::list<cbl_refer_t>& args,
11943 std::vector<function_descr_arg_t>& params /*out*/ )
11944 {
11945 auto key = function_descr_t::init(L->name);
11946 auto func = udfs.find(key);
11947 assert(func != udfs.end());
11948 function_descr_t udf = *func;
11949 params = udf.linkage_fields;
11950
11951 if( udf.linkage_fields.size() < args.size() ) {
11952 auto loc = symbol_field_location(field_index(args.back().field));
11953 error_msg(loc, "too many parameters for UDF %s", L->name);
11954 return false;
11955 }
11956
11957 size_t i = 0;
11958 for( const cbl_refer_t& arg : args ) {
11959 if( arg.field ) { // else omitted
11960 auto tgt = cbl_field_of(symbol_at(udf.linkage_fields.at(i).isym));
11961 if( ! valid_move(tgt, arg.field) ) {
11962 auto loc = symbol_field_location(field_index(arg.field));
11963 error_msg(loc, "FUNCTION %s argument %zu, '%s' cannot be passed to %s, type %s",
11964 L->name, i, arg.field->pretty_name(),
11965 tgt->pretty_name(), 3 + cbl_field_type_str(tgt->type) );
11966 return false;
11967 }
11968 }
11969 i++;
11970 }
11971 return true;
11972 }
11973
11974 bool
11975 current_t::repository_add( const char name[]) {
11976 assert( !programs.empty() );
11977 function_descr_t arg = function_descr_t::init(name);
11978 auto parg = std::find( function_descrs, function_descrs_end, arg );
11979 if( parg == function_descrs_end ) {
11980 dbgmsg("%s:%d: no intrinsic %s found", __func__, __LINE__, name);
11981 return false;
11982 }
11983 auto p = programs.top().function_repository.insert(*parg);
11984 if( yydebug ) {
11985 for( auto descr : programs.top().function_repository ) {
11986 dbgmsg("%s:%d: %-20s %-20s %-20s", __func__, __LINE__,
11987 keyword_str(descr.token), descr.name, descr.cname);
11988 }
11989 }
11990 return p.second;
11991 }
11992
11993 int
11994 current_t::repository_in( const char name[]) {
11995 assert( !programs.empty() );
11996 auto isym = programs.top().program_index;
11997 // possible to call self
11998 auto self = cbl_label_of(symbol_at(isym));
11999 if( self->type == LblFunction ) {
12000 if( 0 == strcasecmp(self->name, name) ) {
12001 return FUNCTION_UDF;
12002 }
12003 }
12004 function_descr_t arg = function_descr_t::init(name);
12005 auto repository = programs.top().function_repository;
12006 auto p = repository.find(arg);
12007 return p != repository.end()? p->token : 0;
12008 }
12009
12010 int repository_function_tok( const char name[] ) {
12011 return current.repository_in(name);
12012 }
12013
12014 function_descr_t
12015 function_descr_t::init( int isym ) {
12016 function_descr_t descr = { FUNCTION_UDF_0 };
12017 descr.ret_type = FldInvalid;
12018 const auto L = cbl_label_of(symbol_at(isym));
12019 bool ok = namcpy(YYLTYPE(), descr.name, L->name);
12020 gcc_assert(ok);
12021 return descr;
12022 }
12023
12024 arith_t::arith_t( cbl_arith_format_t format, refer_list_t * refers )
12025 : format(format), on_error(NULL), not_error(NULL)
12026 {
12027 std::copy( refers->refers.begin(), refers->refers.end(), back_inserter(A) );
12028 refers->refers.clear();
12029 delete refers;
12030 }
12031
12032 cbl_key_t::cbl_key_t( sort_key_t that )
12033 : ascending(that.ascending)
12034 , fields( that.fields.begin(), that.fields.end() )
12035 {}
12036
12037 cbl_key_t&
12038 cbl_key_t::operator=( const sort_key_t& that ) {
12039 ascending = that.ascending;
12040 fields = that.as_vector();
12041 return *this;
12042 }
12043
12044 static cbl_refer_t *
12045 ast_op( cbl_refer_t *lhs, char op, cbl_refer_t *rhs ) {
12046 assert(lhs);
12047 assert(rhs);
12048 if( ! (is_numeric(lhs->field) && is_numeric(rhs->field)) ) {
12049 // If one of the fields isn't numeric, allow for index addition.
12050 switch(op) {
12051 case '+':
12052 case '-':
12053 // Simple addition OK for table indexes.
12054 if( lhs->field->type == FldIndex || rhs->field->type == FldIndex ) {
12055 goto ok;
12056 }
12057 }
12058
12059 auto f = !is_numeric(lhs->field)? lhs->field : rhs->field;
12060 auto loc = symbol_field_location(field_index(f));
12061 error_msg(loc, "'%s' is not numeric", f->name);
12062 return NULL;
12063 }
12064 ok:
12065 cbl_field_t skel = determine_intermediate_type( *lhs, op, *rhs );
12066 cbl_refer_t *tgt = new_reference_like(skel);
12067 if( !mode_syntax_only() ) {
12068 parser_op( *tgt, *lhs, op, *rhs, current.compute_label() );
12069 }
12070 return tgt;
12071 }
12072
12073 static void
12074 ast_add( arith_t *arith ) {
12075 size_t nC = arith->tgts.size(), nA = arith->A.size();
12076 std::vector <cbl_num_result_t> C(nC);
12077 cbl_num_result_t *pC;
12078 std::vector <cbl_refer_t> A(nA);
12079 cbl_refer_t *pA;
12080
12081 pC = use_any(arith->tgts, C);
12082 pA = use_any(arith->A, A);
12083
12084 parser_add( nC, pC, nA, pA, arith->format, arith->on_error, arith->not_error );
12085
12086 current.declaratives_evaluate();
12087 }
12088
12089 static bool
12090 ast_subtract( arith_t *arith ) {
12091 size_t nC = arith->tgts.size(), nA = arith->A.size(), nB = arith->B.size();
12092 std::vector <cbl_refer_t> A(nA);
12093 std::vector <cbl_refer_t> B(nB);
12094 std::vector <cbl_num_result_t> C(nC);
12095
12096 cbl_refer_t *pA = use_any(arith->A, A);
12097 cbl_refer_t *pB = use_any(arith->B, B);
12098 cbl_num_result_t *pC = use_any(arith->tgts, C);
12099
12100 parser_subtract( nC, pC, nA, pA, nB, pB, arith->format, arith->on_error, arith->not_error );
12101
12102 current.declaratives_evaluate();
12103 return true;
12104 }
12105
12106 static bool
12107 ast_multiply( arith_t *arith ) {
12108 size_t nC = arith->tgts.size(), nA = arith->A.size(), nB = arith->B.size();
12109 std::vector <cbl_refer_t> A(nA);
12110 std::vector <cbl_refer_t> B(nB);
12111 std::vector <cbl_num_result_t> C(nC);
12112
12113 cbl_refer_t *pA = use_any(arith->A, A);
12114 cbl_refer_t *pB = use_any(arith->B, B);
12115 cbl_num_result_t *pC = use_any(arith->tgts, C);
12116
12117 parser_multiply( nC, pC, nA, pA, nB, pB, arith->on_error, arith->not_error );
12118
12119 current.declaratives_evaluate();
12120 return true;
12121 }
12122
12123 static bool
12124 ast_divide( arith_t *arith ) {
12125 size_t nC = arith->tgts.size(), nA = arith->A.size(), nB = arith->B.size();
12126 std::vector <cbl_refer_t> A(nA);
12127 std::vector <cbl_refer_t> B(nB);
12128 std::vector <cbl_num_result_t> C(nC);
12129
12130 cbl_refer_t *pA = use_any(arith->A, A);
12131 cbl_refer_t *pB = use_any(arith->B, B);
12132 cbl_num_result_t *pC = use_any(arith->tgts, C);
12133
12134 parser_divide( nC, pC, nA, pA, nB, pB,
12135 arith->remainder, arith->on_error, arith->not_error );
12136
12137 current.declaratives_evaluate();
12138 return true;
12139 }
12140
12141 /*
12142 * Populate a parser API struct from lists built up by the parser.
12143 * The API doesn't use STL containers or classes that exist only for
12144 * the convenience of the parser.
12145 */
12146 struct stringify_src_t : public cbl_string_src_t {
12147 stringify_src_t( const refer_marked_list_t& marked = refer_marked_list_t() )
12148 : cbl_string_src_t( marked.marker? *marked.marker : null_reference,
12149 marked.refers.size(),
12150 new cbl_refer_t[marked.refers.size()] )
12151 {
12152 std::copy( marked.refers.begin(), marked.refers.end(), inputs );
12153 }
12154
12155 static void dump( const cbl_string_src_t& src ) {
12156 dbgmsg( "%s:%d:, " HOST_SIZE_T_PRINT_UNSIGNED " inputs delimited by %s:",
12157 __func__, __LINE__, (fmt_size_t)src.ninput,
12158 src.delimited_by.field? field_str(src.delimited_by.field) : "SIZE" );
12159 std::for_each(src.inputs, src.inputs + src.ninput, dump_input);
12160 }
12161
12162 protected:
12163 static void dump_input( const cbl_refer_t& refer ) {
12164 yywarn( "%s: %s", __func__, field_str(refer.field) );
12165 }
12166 };
12167
12168 void
12169 stringify( refer_collection_t *inputs,
12170 const cbl_refer_t& into, const cbl_refer_t& pointer,
12171 cbl_label_t *on_error,
12172 cbl_label_t *not_error )
12173 {
12174 std::vector <stringify_src_t> sources(inputs->lists.size());
12175
12176 if( inputs->lists.back().marker == NULL ) {
12177 inputs->lists.back().marker = cbl_refer_t::empty();
12178 }
12179 assert( inputs->lists.back().marker );
12180 std::copy( inputs->lists.begin(), inputs->lists.end(), sources.begin() );
12181 parser_string( into, pointer, sources.size(), sources.data(), on_error, not_error );
12182 }
12183
12184 void
12185 unstringify( const cbl_refer_t& src,
12186 refer_list_t *delimited,
12187 unstring_into_t * into,
12188 cbl_label_t *on_error,
12189 cbl_label_t *not_error )
12190 {
12191 size_t ndelimited = delimited? delimited->size() : 0;
12192 cbl_refer_t *pdelimited = NULL;
12193 // cppcheck-suppress [variableScope] pdelimited points to delimiteds.data()
12194 std::vector <cbl_refer_t> delimiteds(ndelimited);
12195 if( ndelimited > 0 ) {
12196 pdelimited = use_any( delimited->refers, delimiteds );
12197 }
12198
12199 std::vector <cbl_refer_t> outputs(into->size());
12200 into->use_list( outputs, unstring_tgt_t::tgt_of );
12201
12202 std::vector <cbl_refer_t> delimiters(into->size());
12203 into->use_list( delimiters, unstring_tgt_t::delimiter_of );
12204
12205 std::vector <cbl_refer_t> counts(into->size());
12206 into->use_list( counts, unstring_tgt_t::count_of );
12207
12208 parser_unstring( src,
12209 ndelimited, pdelimited,
12210 // into
12211 outputs.size(),
12212 outputs.data(), delimiters.data(), counts.data(),
12213 into->pointer, into->tally,
12214 on_error, not_error );
12215 delete into;
12216 }
12217
12218 static const char *
12219 data_section_str( data_section_t section ) {
12220 switch(section) {
12221 case not_data_datasect_e:
12222 return "NONE";
12223 case local_storage_datasect_e:
12224 return "LOCAL";
12225 case file_datasect_e:
12226 return "FILE";
12227 case working_storage_datasect_e:
12228 return "WORKING";
12229 case linkage_datasect_e:
12230 return "LINKAGE";
12231 }
12232 gcc_unreachable();
12233 return NULL;
12234 }
12235
12236 static bool
12237 current_data_section_set(const YYLTYPE& loc, data_section_t data_section ) {
12238 // order is mandatory
12239 if( data_section < current_data_section ) {
12240 error_msg(loc, "%s SECTION must precede %s SECTION",
12241 data_section_str(data_section),
12242 data_section_str(current_data_section));
12243 return false;
12244 }
12245
12246 cbl_section_type_t type = file_sect_e;
12247
12248 switch(data_section) {
12249 case not_data_datasect_e:
12250 gcc_unreachable();
12251 break;
12252 case file_datasect_e:
12253 type = file_sect_e;
12254 break;
12255 case working_storage_datasect_e:
12256 type = working_sect_e;
12257 break;
12258 case local_storage_datasect_e:
12259 type = local_sect_e;
12260 break;
12261 case linkage_datasect_e:
12262 type = linkage_sect_e;
12263 break;
12264 }
12265
12266 cbl_section_t section = { type, yylineno, NULL };
12267
12268 if( ! symbol_section_add(PROGRAM, &section) ) {
12269 error_msg(loc, "could not add section %s to program %s, exists line %d",
12270 section.name(), current.program()->name,
12271 symbol_section(PROGRAM, &section)->line );
12272 return false;
12273 }
12274
12275 current_data_section = data_section ;
12276 return true;
12277 }
12278
12279 void apply_declaratives() {
12280 // look for declaratives for this procedure, and all procedures
12281 bool tf[2] = { false, true };
12282 for( bool *yn = tf; yn < tf + COUNT_OF(tf); yn++ ) {
12283 auto declaratives = current.debugging_declaratives(*yn);
12284 for( auto p = declaratives.begin() ;
12285 p != declaratives.end(); p++ ) {
12286 // TODO: delarative for PARA OF SECTION
12287 cbl_label_t *label = symbol_label(PROGRAM, LblNone, 0, p->c_str());
12288 assert(label);
12289 parser_perform(label);
12290 }
12291 }
12292 }
12293 #define FIG_CONST(X) constant_of(constant_index((X)))
12294
12295 #pragma GCC diagnostic push
12296 #pragma GCC diagnostic ignored "-Wunused-parameter"
12297
12298 int warn_abi_version = -1;
12299 int cp_unevaluated_operand;
12300 void
12301 lang_check_failed (const char* file, int line, const char* function) {}
12302
12303 #pragma GCC diagnostic pop
12304
12305 void
12306 ast_inspect( YYLTYPE loc, cbl_refer_t& input, bool backward,
12307 cbl_inspect_opers_t& inspects )
12308 {
12309 if( yydebug ) {
12310 dbgmsg("%s:%d: INSPECT " HOST_SIZE_T_PRINT_UNSIGNED " operations on %s, "
12311 "lines %d:%d - %d:%d",
12312 __func__, __LINE__,
12313 (fmt_size_t)inspects.size(), input.field->name,
12314 loc.first_line, loc.first_column, loc.last_line, loc.last_column );
12315 }
12316 std::for_each(inspects.begin(), inspects.end(), dump_inspect);
12317 parser_inspect( input, backward, inspects );
12318 }
12319
12320 static const char *
12321 cbl_refer_str( char output[], const cbl_refer_t& R ) {
12322 sprintf( output, "refer = %s %s %s",
12323 R.field? field_str(R.field) : "(none)",
12324 R.is_table_reference()? "(table)" : "",
12325 R.is_refmod_reference()? "(refmod)" : "" );
12326 return output;
12327 }
12328
12329 void
12330 dump_inspect_match( const cbl_inspect_match_t& M ) {
12331 static char fields[4][4 * 64];
12332 cbl_refer_str(fields[0], M.match);
12333 cbl_refer_str(fields[1], M.tally);
12334 cbl_refer_str(fields[2], M.before.identifier_4);
12335 cbl_refer_str(fields[3], M.after.identifier_4);
12336
12337 dbgmsg( "matching %s [tally %s]\n\t\tbefore %s%s \n\t\tafter %s%s",
12338 fields[0], fields[1],
12339 M.before.initial? "initial " : "", fields[2],
12340 M.after.initial? "initial " : "", fields[3] );
12341 }
12342
12343 static void
12344 dump_inspect_replace( const cbl_inspect_replace_t& R ) {
12345 static char fields[4][4 * 64];
12346 cbl_refer_str(fields[0], R.matching());
12347 cbl_refer_str(fields[1], R.before.identifier_4);
12348 cbl_refer_str(fields[2], R.after.identifier_4);
12349 cbl_refer_str(fields[3], R.replacement);
12350
12351 dbgmsg( "matching %s \n\treplacement %s\n\t\tbefore %s%s \n\t\tafter %s%s",
12352 fields[0], fields[3],
12353 R.before.initial? "initial " : "", fields[1],
12354 R.after.initial? "initial " : "", fields[2] );
12355 }
12356
12357 static const char *
12358 bound_str( cbl_inspect_bound_t bound ) {
12359 switch(bound) {
12360 case bound_characters_e: return "characters";
12361 case bound_all_e: return "all";
12362 case bound_first_e: return "first";
12363 case bound_leading_e: return "leading";
12364 case bound_trailing_e: return "trailing";
12365 }
12366 return "bound?";
12367 }
12368
12369 /*
12370 * INITIALIZE
12371 */
12372 static data_category_t
12373 data_category_of( const cbl_refer_t& refer ) {
12374 assert(refer.field);
12375 switch( refer.field->type ) {
12376 case FldInvalid:
12377 assert(refer.field->type != FldInvalid);
12378 return data_category_none;
12379
12380 case FldGroup:
12381 return data_category_none;
12382
12383 case FldLiteralA:
12384 case FldAlphanumeric:
12385 return refer.field->has_attr(all_alpha_e)?
12386 data_alphabetic_e : data_alphanumeric_e;
12387
12388 case FldNumericBinary:
12389 case FldFloat:
12390 case FldNumericBin5:
12391 case FldPacked:
12392 case FldNumericDisplay:
12393 case FldLiteralN:
12394 return data_numeric_e;
12395
12396 case FldNumericEdited:
12397 return data_numeric_edited_e;
12398 case FldAlphaEdited:
12399 return data_alphanumeric_edited_e;
12400
12401 case FldPointer:
12402 return data_data_pointer_e;
12403
12404 case FldClass:
12405 case FldConditional:
12406 case FldForward:
12407 case FldIndex:
12408 case FldSwitch:
12409 case FldDisplay:
12410 case FldBlob:
12411 return data_category_none;
12412 }
12413 gcc_unreachable();
12414 return data_category_none;
12415 }
12416
12417 static bool
12418 valid_target( const cbl_refer_t& refer ) {
12419 assert(refer.field);
12420 switch( refer.field->type ) {
12421 case FldInvalid:
12422 assert(refer.field->type != FldInvalid);
12423 return false;
12424 case FldGroup:
12425 case FldAlphanumeric:
12426 case FldNumericBinary:
12427 case FldFloat:
12428 case FldNumericBin5:
12429 case FldPacked:
12430 case FldNumericDisplay:
12431 case FldNumericEdited:
12432 case FldAlphaEdited:
12433 case FldPointer:
12434 return true;
12435 case FldLiteralA:
12436 case FldLiteralN:
12437 case FldClass:
12438 case FldConditional:
12439 case FldForward:
12440 case FldIndex:
12441 case FldSwitch:
12442 case FldDisplay:
12443 case FldBlob:
12444 return false;
12445 }
12446 gcc_unreachable();
12447 return false;
12448 }
12449
12450 static REAL_VALUE_TYPE
12451 numstr2i( const char input[], radix_t radix ) {
12452 REAL_VALUE_TYPE output;
12453 size_t integer = 0;
12454 fmt_size_t integerf = 0;
12455 int erc=0;
12456
12457 switch( radix ) {
12458 case decimal_e: { // Use decimal point for comma, just in case.
12459 auto local = xstrdup(input);
12460 if( !local ) { erc = -1; break; }
12461 std::replace(local, local + strlen(local), ',', '.');
12462 real_from_string3 (&output, local, TYPE_MODE (float128_type_node));
12463 }
12464 break;
12465 case hexadecimal_e:
12466 erc = sscanf(input, "%" GCC_PRISZ "x", &integerf);
12467 integer = integerf;
12468 real_from_integer (&output, VOIDmode, integer, UNSIGNED);
12469 break;
12470 case boolean_e:
12471 for( const char *p = input; *p != '\0'; p++ ) {
12472 if( ssize_t(8 * sizeof(integer) - 1) < p - input ) {
12473 yywarn("'%s' was accepted as %zu", input, integer);
12474 break;
12475 }
12476 switch(*p) {
12477 case '0':
12478 case '1':
12479 integer = (integer << (p - input));
12480 integer |= ((*p) == '0' ? 0 : 1);
12481 break;
12482 default:
12483 yywarn("'%s' was accepted as %zu", input, integer);
12484 break;
12485 }
12486 }
12487 real_from_integer (&output, VOIDmode, integer, UNSIGNED);
12488 return output;
12489 }
12490 if( erc == -1 ) {
12491 yywarn("'%s' was accepted as %zu", input, integer);
12492 }
12493 return output;
12494 }
12495
12496 static inline cbl_field_t *
12497 new_literal( const char initial[], enum radix_t radix ) {
12498 auto attr = constant_e;
12499
12500 switch( radix ) {
12501 case decimal_e:
12502 break;
12503 case hexadecimal_e:
12504 attr = hex_encoded_e;
12505 break;
12506 case boolean_e:
12507 attr = bool_encoded_e;
12508 break;
12509 }
12510 return new_literal(strlen(initial), initial,
12511 cbl_field_attr_t(constant_e | attr));
12512 }
12513
12514 class is_elementary_type { // for INITIALIZE purposes
12515 bool with_filler;
12516 public:
12517 explicit is_elementary_type( bool with_filler ) : with_filler(with_filler) {}
12518
12519 bool operator()( const symbol_elem_t& elem ) const {
12520 if( elem.type != SymField ) return false;
12521 const cbl_field_t *f = cbl_field_of(&elem);
12522 if( symbol_redefines(f) ) return false;
12523 return ( f->has_attr(filler_e) && with_filler )
12524 || ::is_elementary(f->type);
12525 }
12526 };
12527
12528 size_t end_of_group( size_t igroup );
12529
12530 static std::list<cbl_refer_t>
12531 symbol_group_data_members( const cbl_refer_t& refer, bool with_filler ) {
12532 std::list<cbl_refer_t> refers;
12533 refers.push_front( refer );
12534
12535 if( refer.field->type != FldGroup ) return refers;
12536
12537 class refer_of : public cbl_refer_t {
12538 public:
12539 explicit refer_of( const cbl_refer_t& refer ) : cbl_refer_t(refer) {}
12540 cbl_refer_t operator()( symbol_elem_t& elem ) {
12541 this->field = cbl_field_of(&elem); // preserve subscript/refmod
12542 return *this;
12543 }
12544 };
12545
12546 size_t igroup = field_index(refer.field), eogroup = end_of_group(igroup);
12547 std::list<symbol_elem_t> elems;
12548 is_elementary_type is_elem(with_filler);
12549
12550 std::copy_if( symbols_begin(igroup), symbols_begin(eogroup),
12551 std::back_inserter(elems), [is_elem]( const symbol_elem_t& elem ) {
12552 return is_elem(elem) || cbl_field_of(&elem)->occurs.ntimes() > 0; } );
12553 std::transform( elems.begin(), elems.end(),
12554 std::back_inserter(refers), refer_of(refer) );
12555 return refers;
12556 }
12557
12558 struct expand_group : public std::list<cbl_refer_t> {
12559 static cbl_refer_t referize( cbl_field_t *field ) {
12560 return cbl_refer_t(field);
12561 }
12562 bool with_filler;
12563 explicit expand_group( bool with_filler ) : with_filler(with_filler) {}
12564
12565 void operator()( const cbl_refer_t& refer ) {
12566 assert(refer.field);
12567 if( refer.field->type != FldGroup ) {
12568 push_back(refer);
12569 return;
12570 }
12571 std::list<cbl_refer_t> members = symbol_group_data_members( refer,
12572 with_filler );
12573 std::copy( members.begin(), members.end(), back_inserter(*this) );
12574 }
12575 };
12576
12577
12578 static const char * initial_default_value;
12579 const char * wsclear() { return initial_default_value; }
12580
12581 void
12582 wsclear( char ch ) {
12583 static char byte = ch;
12584 initial_default_value = &byte;
12585 current.program_needs_initial();
12586 }
12587
12588 static void
12589 initialize_allocated( const cbl_refer_t& input ) {
12590 cbl_num_result_t result = { truncation_e, input };
12591 std::list<cbl_num_result_t> results;
12592 results.push_back(result);
12593 initialize_statement(results, true,
12594 data_category_all, category_map_t());
12595 }
12596
12597 static int
12598 initialize_with( const cbl_refer_t& tgt ) {
12599 if( tgt.field->type == FldPointer ) return ZERO;
12600 if( tgt.is_refmod_reference() ) return SPACES;
12601 return is_numeric(tgt.field)? ZERO : SPACES;
12602 }
12603
12604 static bool
12605 // cppcheck-suppress [passedByValue] target.refer.field is modified
12606 initialize_one( cbl_num_result_t target, bool with_filler,
12607 data_category_t value_category,
12608 const category_map_t& replacements,
12609 bool explicitly )
12610 {
12611 cbl_refer_t& tgt( target.refer );
12612 if( ! valid_target(tgt) ) return false;
12613
12614 // Rule 1 c: is valid for VALUE, REPLACING, or DEFAULT
12615 // If no VALUE (category none), set to blank/zero.
12616 if( value_category == data_category_none && replacements.empty() ) {
12617 auto token = initialize_with(tgt);
12618 auto src = constant_of(constant_index(token));
12619 cbl_refer_t source(src);
12620 auto s = wsclear();
12621 if( s ) {
12622 char ach[5];
12623 int v = *s;
12624 sprintf(ach, "%d", v);
12625 source.field = new_literal(ach);
12626 source.addr_of = true;
12627 }
12628
12629 if( tgt.field->type == FldPointer ) {
12630 parser_set_pointers(1, &tgt, source);
12631 } else {
12632 parser_move(tgt, src, current_rounded_mode());
12633 }
12634 return true;
12635 }
12636
12637 /*
12638 * Either VALUE or REPLACING specified.
12639 */
12640
12641 if( value_category == data_category_all ||
12642 value_category == data_category_of(tgt) ) {
12643 // apply any applicable VALUE
12644 if( explicitly || tgt.field->data.initial ) {
12645 assert( with_filler || !tgt.field->has_attr(filler_e) );
12646 if( tgt.field->data.initial ) {
12647 parser_initialize(tgt);
12648 }
12649 }
12650 }
12651
12652 // apply REPLACING, possibly overwriting VALUE
12653 // N.B., may be wrong:
12654 /*
12655 * "If the data item does not qualify as a receiving-operand because of the
12656 * VALUE phrase, but does qualify because of the REPLACING phrase ..."
12657 */
12658 auto r = replacements.find(data_category_of(tgt));
12659 if( r != replacements.end() ) {
12660 parser_move( tgt, *r->second );
12661
12662 return true;
12663 }
12664
12665 return true;
12666 }
12667
12668 typedef std::pair<cbl_field_t*,cbl_field_t*> field_span_t;
12669 typedef std::pair<size_t, size_t> cbl_bytespan_t;
12670
12671 /*
12672 * After the 1st record is initialized, copy it to the others.
12673 */
12674 static bool
12675 initialize_table( const cbl_num_result_t& target,
12676 size_t nspan, const cbl_bytespan_t spans[],
12677 const std::list<cbl_subtable_t>& subtables )
12678 {
12679 assert( target.refer.nsubscript() == dimensions(target.refer.field) );
12680 const cbl_refer_t& src( target.refer );
12681 size_t n( src.field->occurs.ntimes());
12682 assert( 0 < n );
12683
12684 size_t isym( field_index(src.field) );
12685 std::vector <cbl_subtable_t> tbls(subtables.size());
12686 std::copy( subtables.begin(), subtables.end(), tbls.begin() );
12687 parser_initialize_table( n, src, nspan, spans, isym, tbls.size(), tbls.data() );
12688 return true;
12689 }
12690
12691 static cbl_refer_t
12692 synthesize_table_refer( cbl_refer_t tgt ) {
12693 // For a table, use supplied subscripts or start with 1.
12694 auto ndim( dimensions(tgt.field) );
12695 if( tgt.nsubscript() < ndim ) { // it's an incomplete table
12696 std::vector <cbl_refer_t> subscripts(ndim);
12697 for( size_t i=0; i < ndim; i++ ) {
12698 if( i < tgt.nsubscript() ) {
12699 subscripts[i] = tgt.subscripts[i];
12700 continue;
12701 }
12702 subscripts[i].field = new_tempnumeric();
12703 parser_set_numeric(subscripts[i].field, 1);
12704 }
12705 return cbl_refer_t( tgt.field, subscripts );
12706 }
12707 return tgt;
12708 }
12709
12710 static size_t
12711 group_offset( const cbl_field_t *field ) {
12712 if( field->parent ) {
12713 auto e = symbol_at(field->parent);
12714 if( e->type == SymField ) {
12715 const auto parent = cbl_field_of(e);
12716 return field->offset - parent->offset;
12717 }
12718 }
12719 return field->offset;
12720 }
12721
12722 static bool
12723 initialize_statement( const cbl_num_result_t& target, bool with_filler,
12724 data_category_t value_category,
12725 const category_map_t& replacements,
12726 size_t depth = 0 )
12727 {
12728 const cbl_refer_t& tgt( target.refer );
12729 assert(dimensions(tgt.field) == tgt.nsubscript() || 0 < depth);
12730 assert(!is_literal(tgt.field));
12731
12732 if( tgt.field->type == FldGroup ) {
12733 if( tgt.field->data.initial ) goto initialize_this;
12734 if( tgt.is_refmod_reference() ) goto initialize_this;
12735 // iterate over group memebers
12736 auto imember = field_index(tgt.field);
12737 auto eogroup = end_of_group(imember);
12738 bool fOK = true;
12739 std::list<cbl_field_t*> members;
12740 std::list<cbl_subtable_t> subtables;
12741
12742 while( ++imember < eogroup ) {
12743 auto e = symbol_at(imember);
12744 if( e->type != SymField ) continue;
12745 auto f = cbl_field_of(e);
12746 if( ! (f->type == FldGroup || is_elementary(f->type)) ) continue;
12747 if( ! symbol_redefines(f) ) {
12748 members.push_back(f);
12749 if( is_table(f) ) {
12750 size_t offset = group_offset(f);
12751 subtables.push_back( cbl_subtable_t { offset, imember } );
12752 }
12753 cbl_num_result_t next_target(target);
12754 next_target.refer.field = f;
12755 // recurse on each member, which might be a table or group
12756 fOK = fOK && initialize_statement( next_target, with_filler, value_category,
12757 replacements, 1 + depth );
12758 }
12759 if( f->type == FldGroup ) {
12760 imember = end_of_group(imember) - 1;
12761 }
12762 }
12763
12764 if( fOK && is_table(tgt.field) ) {
12765 cbl_num_result_t output = { target.rounded, synthesize_table_refer(tgt) };
12766 if( tgt.nsubscript() < output.refer.nsubscript() ) { // tgt is whole table
12767 std::list<field_span_t> field_spans;
12768 static const field_span_t empty_span = { NULL, NULL };
12769 field_span_t span = empty_span;
12770 bool honor_filler = false;
12771 // construct non-filler field spans
12772 for( auto member : members ) {
12773 if( !with_filler && member->has_attr(filler_e) ) {
12774 if( span.first ) { // conclude the span and begin to skip filler
12775 field_spans.push_back(span);
12776 span = empty_span;
12777 honor_filler = true;
12778 }
12779 continue;
12780 }
12781 if( span.first ) {
12782 span.second = member; // extend the span
12783 } else {
12784 span.first = member; // start a new span
12785 }
12786 }
12787 if( span.first ) {
12788 field_spans.push_back(span);
12789 }
12790 // convert field spans to byte ranges
12791 std::vector <cbl_bytespan_t> ranges( field_spans.size() );
12792 size_t nrange = 0;
12793 if( honor_filler ) {
12794 nrange = ranges.size();
12795 std::transform( field_spans.begin(), field_spans.end(), ranges.begin(),
12796 []( const auto& span ) {
12797 size_t first, second;
12798 first = second = group_offset(span.first);
12799 if( ! span.second ) {
12800 second += std::max(span.first->data.capacity,
12801 span.first->data.memsize);
12802 } else {
12803 second = group_offset(span.second)
12804 - group_offset(span.first);
12805 second += std::max(span.second->data.capacity,
12806 span.second->data.memsize);
12807 }
12808 return std::make_pair(first, second);
12809 } );
12810 }
12811 return initialize_table( output, nrange, ranges.data(), subtables );
12812 }
12813 }
12814 return fOK;
12815 }
12816
12817 if( !is_elementary(tgt.field->type) ) return false;
12818
12819 assert(is_elementary(tgt.field->type));
12820 assert(data_category_of(tgt) != data_category_none);
12821
12822 /*
12823 * Initialize elementary field.
12824 */
12825
12826 initialize_this:
12827 // Cannot initialize constants
12828 if( is_constant(tgt.field) ) {
12829 auto loc = symbol_field_location(field_index(tgt.field));
12830 error_msg(loc, "%s is constant", name_of(tgt.field));
12831 return false;
12832 }
12833 // Ignore filler unless instructed otherwise.
12834 if( !with_filler && tgt.field->has_attr(filler_e) ) return true;
12835
12836 cbl_num_result_t output = { target.rounded, synthesize_table_refer(tgt) };
12837
12838 bool fOK = initialize_one( output, with_filler, value_category,
12839 replacements, depth == 0 );
12840
12841 if( fOK && is_table(tgt.field) ) {
12842 return initialize_table( output,
12843 0, NULL, std::list<cbl_subtable_t>() );
12844 }
12845
12846 return fOK;
12847 }
12848
12849 const char *
12850 data_category_str( data_category_t category ) {
12851 switch(category) {
12852 case data_category_none: return "category_none";
12853 case data_category_all: return "category_all";
12854 case data_alphabetic_e: return "alphabetic";
12855 case data_alphanumeric_e: return "alphanumeric";
12856 case data_alphanumeric_edited_e: return "alphanumeric_edited";
12857 case data_boolean_e: return "data_boolean";
12858 case data_data_pointer_e: return "data_data_pointer";
12859 case data_function_pointer_e: return "data_function_pointer";
12860 case data_msg_tag_e: return "data_msg_tag";
12861 case data_dbcs_e: return "dbcs";
12862 case data_egcs_e: return "egcs";
12863 case data_national_e: return "national";
12864 case data_national_edited_e: return "national_edited";
12865 case data_numeric_e: return "numeric";
12866 case data_numeric_edited_e: return "numeric_edited";
12867 case data_object_referenc_e: return "data_object_referenc";
12868 case data_program_pointer_e: return "data_program_pointer";
12869 }
12870 return "???";
12871 }
12872
12873 static void
12874 initialize_statement( std::list<cbl_num_result_t>& tgts, bool with_filler,
12875 data_category_t value_category,
12876 const category_map_t& replacements) {
12877 for( const auto& tgt : tgts ) {
12878 initialize_statement( tgt, with_filler, value_category,
12879 replacements );
12880 }
12881 tgts.clear();
12882 }
12883
12884 static void
12885 dump_inspect_oper( const cbl_inspect_oper_t& op ) {
12886 dbgmsg("\t%s: " HOST_SIZE_T_PRINT_UNSIGNED
12887 " \"matches\", " HOST_SIZE_T_PRINT_UNSIGNED " \"replaces\"",
12888 bound_str(op.bound),
12889 (fmt_size_t)op.matches.size(),
12890 (fmt_size_t)op.replaces.size());
12891 std::for_each(op.matches.begin(), op.matches.end(), dump_inspect_match);
12892 std::for_each(op.replaces.begin(), op.replaces.end(), dump_inspect_replace);
12893 }
12894
12895 #pragma GCC diagnostic push
12896 #pragma GCC diagnostic ignored "-Wunused-function"
12897
12898 static void
12899 dump_inspect( const cbl_inspect_t& I ) {
12900 if( !yydebug ) return;
12901 if( I.tally.field ) {
12902 fprintf( stderr, "\tTALLYING to %s %s %s:\n",
12903 field_str(I.tally.field),
12904 I.tally.is_table_reference()? "(table)" : "",
12905 I.tally.is_refmod_reference()? "(refmod)" : "" );
12906 } else {
12907 fprintf( stderr, "\tREPLACING:\n" );
12908 }
12909 std::for_each( I.begin(), I.end(), dump_inspect_oper );
12910 }
12911 #pragma GCC diagnostic pop
12912
12913 #include <iterator>
12914
12915 struct declarative_file_list_t : protected cbl_declarative_t {
12916 explicit declarative_file_list_t( const cbl_declarative_t& d )
12917 : cbl_declarative_t(d)
12918 {
12919 if( nfile > 0 )
12920 assert(d.files[0] == this->files[0]);
12921 }
12922 static std::ostream&
12923 splat( std::ostream& os, const declarative_file_list_t& dcl ) {
12924 static int i=0;
12925
12926 os << "static size_t dcl_file_list_" << i++
12927 << "[" << dcl.nfile << "] = { ";
12928 std::ostream_iterator<size_t> out(os, ", ");
12929 std::copy( dcl.files, dcl.files + dcl.nfile, out );
12930 return os << "};";
12931 }
12932 };
12933
12934 std::ostream&
12935 operator<<( std::ostream& os, const declarative_file_list_t& dcl ) {
12936 return dcl.splat( os, dcl );
12937 }
12938
12939 static declarative_file_list_t
12940 file_list_of( const cbl_declarative_t& dcl ) {
12941 return declarative_file_list_t(dcl);
12942 }
12943
12944 std::ostream&
12945 operator<<( std::ostream& os, const cbl_declarative_t& dcl ) {
12946 static int i=0;
12947
12948 return os <<
12949 "\t{ " << dcl.section << ", "
12950 << std::boolalpha << dcl.global << ", "
12951 << ec_type_str(dcl.type) << ", "
12952 << dcl.nfile << ", "
12953 << "dcl_file_list_" << i++ << ", "
12954 << cbl_file_mode_str(dcl.mode) << " }"
12955 << std::flush;
12956 }
12957
12958 void parser_add_declaratives( size_t n, cbl_declarative_t *declaratives) {
12959 const char *prog = cbl_label_of(symbol_at(PROGRAM))->name;
12960 char *filename = xasprintf("declaratives.%s.h", prog);
12961 std::ofstream os(filename);
12962 {
12963 std::ostream_iterator<declarative_file_list_t> out(os, "\n");
12964 std::transform( declaratives, declaratives + n, out, file_list_of );
12965 }
12966 os << "\nstatic cbl_declarative_base_t declaratives[] = {\n";
12967 std::ostream_iterator<cbl_declarative_t> out(os, ", \n");
12968 std::copy( declaratives, declaratives + n, out );
12969 os << "};\n" << std::endl;
12970 }
12971
12972 cbl_field_t *
12973 new_literal( const literal_t& lit, enum cbl_field_attr_t attr ) {
12974 bool zstring = lit.prefix[0] == 'Z';
12975 if( !zstring && lit.data[lit.len] != '\0' ) {
12976 dbgmsg("%s:%d: line %d, no NUL terminator '%-*.*s'{"
12977 HOST_SIZE_T_PRINT_UNSIGNED "/" HOST_SIZE_T_PRINT_UNSIGNED "}",
12978 __func__, __LINE__, yylineno,
12979 int(lit.len), int(lit.len),
12980 lit.data, (fmt_size_t)strlen(lit.data), (fmt_size_t)lit.len);
12981 }
12982 assert(zstring || lit.data[lit.len] == '\0');
12983
12984 size_t attrs(attr);
12985 attrs |= constant_e;
12986 attrs |= literal_attr(lit.prefix);
12987
12988 return new_literal(lit.len, lit.data, cbl_field_attr_t(attrs));
12989 }
12990
12991 bool
12992 cbl_file_t::validate_forward( size_t isym ) const {
12993 if( isym > 0 && FldForward == symbol_field_forward(isym)->type ) {
12994 auto loc = symbol_field_location(isym);
12995 error_msg(loc, "line %d: %s of %s is not defined",
12996 this->line, cbl_field_of(symbol_at(isym))->name,
12997 this->name );
12998 return false;
12999 }
13000 return true;
13001 }
13002
13003 bool
13004 cbl_file_t::validate_key( const cbl_file_key_t& key ) const {
13005 for( auto f = key.fields; f < key.fields + key.nfield; f++ ) {
13006 if( ! validate_forward(*f) ) return false;
13007 }
13008 return true;
13009 }
13010
13011 bool
13012 cbl_file_t::validate() const {
13013 const size_t members[] = { user_status, vsam_status, record_length };
13014 bool tf = true;
13015
13016 for( auto isym : members ) {
13017 if( ! validate_forward(isym) ) tf = false;
13018 }
13019
13020 for( auto p = keys; p < keys + nkey; p++ ) {
13021 if( ! validate_key(*p) ) tf = false;
13022 }
13023
13024 return tf;
13025 }
13026
13027 bool
13028 cbl_file_lock_t::mode_set( int token ) {
13029 switch( token ) {
13030 case MANUAL: mode = manual_e; break;
13031 case RECORD: mode = record_e; break;
13032 case AUTOMATIC: mode = automatic_e; break;
13033 default:
13034 return false;
13035 }
13036 return true;
13037 }
13038
13039 enum cbl_figconst_t
13040 cbl_figconst_of( const char *value ) {
13041 struct values_t {
13042 const char *value; cbl_figconst_t type;
13043 } static const values[] = {
13044 { constant_of(constant_index(ZERO))->data.initial, zero_value_e },
13045 { constant_of(constant_index(SPACES))->data.initial, space_value_e },
13046 { constant_of(constant_index(HIGH_VALUES))->data.initial, high_value_e },
13047 { constant_of(constant_index(LOW_VALUES))->data.initial, low_value_e },
13048 { constant_of(constant_index(QUOTES))->data.initial, quote_value_e },
13049 { constant_of(constant_index(NULLS))->data.initial, null_value_e },
13050 }, *eovalues = values + COUNT_OF(values);
13051
13052 auto p = std::find_if( values, eovalues,
13053 [value]( const values_t& elem ) {
13054 return elem.value == value;
13055 } );
13056
13057 return p == eovalues? normal_value_e : p->type;
13058 }
13059
13060 int
13061 cbl_figconst_tok( const char *value ) {
13062 struct values_t {
13063 const char *value; int token;
13064 } static const values[] = {
13065 { constant_of(constant_index(ZERO))->data.initial, ZERO },
13066 { constant_of(constant_index(SPACES))->data.initial, SPACES },
13067 { constant_of(constant_index(HIGH_VALUES))->data.initial, HIGH_VALUES },
13068 { constant_of(constant_index(LOW_VALUES))->data.initial, LOW_VALUES },
13069 { constant_of(constant_index(QUOTES))->data.initial, QUOTES },
13070 { constant_of(constant_index(NULLS))->data.initial, NULLS },
13071 }, *eovalues = values + COUNT_OF(values);
13072
13073 auto p = std::find_if( values, eovalues,
13074 [value]( const values_t& elem ) {
13075 return elem.value == value;
13076 } );
13077
13078 return p == eovalues? 0 : p->token;
13079 }
13080
13081 const cbl_field_t *
13082 cbl_figconst_field_of( const char *value ) {
13083 int token = cbl_figconst_tok(value);
13084 return token == 0 ? nullptr : constant_of(constant_index(token));
13085 }
13086
13087
13088 cbl_field_attr_t
13089 literal_attr( const char prefix[] ) {
13090 switch(strlen(prefix)) {
13091 case 0: return none_e;
13092
13093 case 1:
13094 switch(prefix[0]) {
13095 case 'B': return bool_encoded_e;
13096 case 'N': cbl_unimplemented("National"); return none_e;
13097 case 'X': return hex_encoded_e;
13098 case 'Z': return quoted_e;
13099 }
13100 break;
13101
13102 case 2:
13103 switch(prefix[1]) {
13104 case 'X':
13105 switch(prefix[0]) {
13106 case 'B': return cbl_field_attr_t(hex_encoded_e | bool_encoded_e);
13107 case 'N': cbl_unimplemented("National"); return none_e;
13108 }
13109 break;
13110 }
13111 }
13112
13113 // must be [BN]X
13114 cbl_internal_error("invalid literal prefix: %qs", prefix);
13115 gcc_unreachable();
13116 return none_e;
13117 }
13118
13119 bool
13120 cbl_field_t::has_subordinate( const cbl_field_t *that ) const {
13121 while( (that = parent_of(that)) != NULL ) {
13122 if( field_index(this) == field_index(that) ) return true;
13123 }
13124 return false;
13125 }
13126
13127 const char *
13128 cbl_field_t::value_str() const {
13129 if( data.etc_type == cbl_field_data_t::value_e )
13130 return string_of( data.value_of() );
13131 return "???";
13132 }
13133
13134 static const cbl_division_t not_syntax_only = cbl_division_t(-1);
13135 cbl_division_t cbl_syntax_only = not_syntax_only;
13136
13137 void
13138 mode_syntax_only( cbl_division_t division ) {
13139 cbl_syntax_only = division;
13140 }
13141
13142 // Parser moves to syntax-only mode if data-division errors preclude compilation.
13143 bool
13144 mode_syntax_only() {
13145 return cbl_syntax_only != not_syntax_only
13146 && cbl_syntax_only <= current_division;
13147 }
13148
13149 void
13150 cobol_dialect_set( cbl_dialect_t dialect ) {
13151 switch(dialect) {
13152 case dialect_gcc_e:
13153 break;
13154 case dialect_ibm_e:
13155 cobol_gcobol_feature_set(feature_embiggen_e);
13156 break;
13157 case dialect_mf_e:
13158 break;
13159 case dialect_gnu_e:
13160 if( 0 == (cbl_dialects & dialect) ) { // first time
13161 cdf_tokens.equate(YYLTYPE(), "BINARY-DOUBLE", "BINARY-C-LONG");
13162 }
13163 break;
13164 }
13165 cbl_dialects |= dialect;
13166 }
13167
13168 static bool internal_ebcdic_locked = false;
13169
13170 void internal_ebcdic_lock() {
13171 internal_ebcdic_locked = true;
13172 }
13173 void internal_ebcdic_unlock() {
13174 internal_ebcdic_locked = false;
13175 }
13176
13177 bool
13178 cobol_gcobol_feature_set( cbl_gcobol_feature_t gcobol_feature, bool on ) {
13179 if( gcobol_feature == feature_internal_ebcdic_e ) {
13180 if( internal_ebcdic_locked ) return false;
13181 }
13182 if( on ) {
13183 cbl_gcobol_features |= gcobol_feature;
13184 } else {
13185 cbl_gcobol_features &= ~gcobol_feature;
13186 }
13187 return true;
13188 }
13189
13190 static bool
13191 literal_refmod_valid( YYLTYPE loc, const cbl_refer_t& r ) {
13192 if( r.field->has_attr(any_length_e) ) return true;
13193
13194 const cbl_span_t& refmod(r.refmod);
13195
13196 if( ! is_literal(refmod.from->field) ) {
13197 if( ! refmod.len ) return true;
13198 if( ! is_literal(refmod.len->field) ) return true;
13199 auto edge = refmod.len->field->as_integer();
13200 if( 0 < edge ) {
13201 if( edge-1 < r.field->data.capacity ) return true;
13202 }
13203 // len < 0 or not: 0 < from + len <= capacity
13204 error_msg(loc, "%s(%s:%zu) out of bounds, "
13205 "size is %u",
13206 r.field->name,
13207 refmod.from->name(),
13208 size_t(edge),
13209 static_cast<unsigned int>(r.field->data.capacity) );
13210 return false;
13211 }
13212
13213 auto edge = refmod.from->field->as_integer();
13214 if( edge > 0 ) {
13215 if( --edge < r.field->data.capacity ) {
13216 if( ! refmod.len ) return true;
13217 if( ! is_literal(refmod.len->field) ) return true;
13218 auto len = refmod.len->field->as_integer();
13219 if( len > 0 ) {
13220 edge += len;
13221 if( --edge < r.field->data.capacity ) return true;
13222 }
13223 // len < 0 or not: 0 < from + len <= capacity
13224 loc = symbol_field_location(field_index(r.field));
13225 error_msg(loc, "%s(%zu:%zu) out of bounds, "
13226 "size is %u",
13227 r.field->name,
13228 size_t(refmod.from->field->as_integer()),
13229 size_t(len),
13230 static_cast<unsigned int>(r.field->data.capacity) );
13231 return false;
13232 }
13233 }
13234 // not: 0 < from <= capacity
13235 error_msg(loc,"%s(%zu) out of bounds, size is %u",
13236 r.field->name,
13237 size_t(refmod.from->field->as_integer()),
13238 static_cast<unsigned int>(r.field->data.capacity) );
13239 return false;
13240 }
13241
13242 const cbl_field_t *
13243 literal_subscript_oob( const cbl_refer_t& r, size_t& isub );
13244
13245 static bool
13246 literal_subscripts_valid( YYLTYPE loc, const cbl_refer_t& name ) {
13247 size_t isub;
13248
13249 // Report any out-of-bound subscript.
13250 const cbl_field_t *oob = literal_subscript_oob(name, isub);
13251 if( oob ) {
13252 std::string sep("");
13253 std::string subscript_names =
13254 std::accumulate( name.subscripts.begin(),
13255 name.subscripts.end(),
13256 std::string(),
13257 [&sep]( std::string acc, const auto& sub ) {
13258 acc += sep;
13259 sep = " ";
13260 return acc + nice_name_of(sub.field);
13261 } );
13262
13263 const char *upper_phrase = "";
13264 if( ! oob->occurs.bounds.fixed_size() ) {
13265 static char ub[32] = "boo";
13266 sprintf(ub, " to " HOST_SIZE_T_PRINT_UNSIGNED,
13267 (fmt_size_t)oob->occurs.bounds.upper);
13268 upper_phrase = ub;
13269 }
13270
13271 // X(0): subscript 1 of for out of range for 02 X OCCURS 4 to 6
13272 error_msg(loc, "%s(%s): subscript %zu out of range "
13273 "for %s %s OCCURS %zu%s",
13274 oob->name, subscript_names.c_str(), 1 + isub,
13275 oob->level_str(), oob->name,
13276 oob->occurs.bounds.lower, upper_phrase );
13277 return false;
13278 }
13279 return true;
13280 }
13281
13282 static void
13283 subscript_dimension_error( YYLTYPE loc, size_t nsub, const cbl_refer_t *scalar ) {
13284 if( 0 == dimensions(scalar->field) ) {
13285 error_msg(loc, "%zu subscripts provided for %s, "
13286 "which has no dimensions",
13287 nsub, scalar->name() );
13288 } else {
13289 error_msg(loc, "%zu subscripts provided for %s, "
13290 "which requires %zu dimensions",
13291 nsub, scalar->name(), dimensions(scalar->field) );
13292 }
13293 }
13294
13295 static void
13296 reject_refmod( YYLTYPE loc, const cbl_refer_t& scalar ) {
13297 if( scalar.is_refmod_reference() ) {
13298 error_msg(loc, "%s cannot be reference-modified here", scalar.name());
13299 }
13300 }
13301
13302 static bool
13303 require_pointer( YYLTYPE loc, const cbl_refer_t& scalar ) {
13304 if( scalar.field->type != FldPointer ) {
13305 error_msg(loc, "%s must have USAGE POINTER", scalar.name());
13306 return false;
13307 }
13308 return true;
13309 }
13310
13311 static bool
13312 require_numeric( YYLTYPE loc, const cbl_refer_t& scalar ) {
13313 if( ! is_numeric(scalar.field) ) {
13314 error_msg(loc, "%s must have numeric USAGE", scalar.name());
13315 return false;
13316 }
13317 return true;
13318 }
13319
13320 static bool
13321 require_integer( YYLTYPE loc, const cbl_refer_t& scalar ) {
13322 if( is_literal(scalar.field) ) {
13323 if( ! is_integer_literal(scalar.field) ) {
13324 error_msg(loc, "numeric literal '%s' must be an integer",
13325 scalar.field->pretty_name());
13326 return false;
13327 }
13328 }
13329 return require_numeric(loc, scalar);
13330 }
13331 /* eval methods */
13332
13333 eval_subject_t::eval_subject_t()
13334 : result( new_temporary(FldConditional) )
13335 {
13336 labels.when = label("when");
13337 labels.yeah = label("yeah");
13338 labels.done = label("done");
13339 pcol = columns.begin();
13340 }
13341
13342 cbl_label_t *
13343 eval_subject_t::label( const char skel[] ) {
13344 static const cbl_label_t protolabel = { LblEvaluate };
13345 cbl_label_t label = protolabel;
13346 label.line = yylineno;
13347 size_t n = 1 + symbols_end() - symbols_begin();
13348 snprintf(label.name, sizeof(label.name),
13349 "_eval_%s_" HOST_SIZE_T_PRINT_UNSIGNED, skel, (fmt_size_t)n);
13350 auto output = symbol_label_add( PROGRAM, &label );
13351 return output;
13352 }
13353
13354 bool
13355 eval_subject_t::compatible( const cbl_field_t *object ) const {
13356 assert(pcol != columns.end());
13357 assert(pcol->field);
13358 auto subject(pcol->field);
13359 if( subject->type != object->type ) {
13360 if( is_conditional(subject) ) {
13361 return is_conditional(object);
13362 }
13363 return ! is_conditional(object);
13364 }
13365 return true;
13366 }
13367
13368
13369 cbl_field_t *
13370 eval_subject_t::compare( int token ) {
13371 size_t tf( very_false_register() );
13372
13373 switch( token ) {
13374 case ANY:
13375 parser_logop(result,
13376 field_at(very_true_register()), and_op,
13377 field_at(very_true_register()));
13378 break;
13379 case TRUE_kw:
13380 tf = very_true_register();
13381 __attribute__((fallthrough));
13382 case FALSE_kw:
13383 assert( is_conditional(pcol->field) );
13384 parser_logop(this->result, pcol->field, xnor_op, field_at(tf));
13385 break;
13386 default:
13387 assert(token == -1 && false );
13388 break;
13389 }
13390 return result;
13391 }
13392
13393 cbl_field_t *
13394 eval_subject_t::compare( relop_t op, const cbl_refer_t& object, bool deciding ) {
13395 auto subject(*pcol);
13396 if( compatible(object.field) ) {
13397 if( ! is_conditional(subject.field) ) {
13398 auto result = deciding? this->result : new_temporary(FldConditional);
13399 parser_relop(result, subject, op, object);
13400 return result;
13401 }
13402 }
13403 if( yydebug ) {
13404 dbgmsg("%s:%d: failed for %s %s %s",
13405 __func__, __LINE__,
13406 name_of(subject.field), relop_str(op), name_of(object.field));
13407 }
13408 return nullptr;
13409 }
13410
13411 cbl_field_t *
13412 eval_subject_t::compare( const cbl_refer_t& object,
13413 const cbl_refer_t& object2 ) {
13414 auto subject(*pcol);
13415
13416 if( ! compatible( object.field ) ) {
13417 if( yydebug ) {
13418 dbgmsg("%s:%d: failed for %s %s",
13419 __func__, __LINE__,
13420 name_of(subject.field), name_of(object.field));
13421 }
13422 return nullptr;
13423 }
13424 if( object2.field ) {
13425 if( ! compatible( object2.field ) ) {
13426 if( yydebug ) {
13427 dbgmsg("%s:%d: failed for %s %s",
13428 __func__, __LINE__,
13429 name_of(subject.field), name_of(object2.field));
13430 }
13431 return nullptr;
13432 }
13433 }
13434
13435 if( is_conditional(subject.field) ) {
13436 assert( object2.field == nullptr );
13437 parser_logop(result, subject.field, xnor_op, object.field);
13438 return result;
13439 }
13440
13441 if( object2.field ) {
13442 assert( ! is_conditional(object.field) );
13443 assert( ! is_conditional(object2.field) );
13444
13445 cbl_field_t * gte = new_temporary(FldConditional);
13446 cbl_field_t * lte = new_temporary(FldConditional);
13447
13448 parser_relop( gte, object, le_op, subject );
13449 parser_relop( lte, subject, le_op, object2 );
13450
13451 parser_logop(result, gte, and_op, lte);
13452 return result;
13453 }
13454
13455 parser_relop(result, subject, eq_op, object);
13456 return result;
13457 }