1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
|
%{
#define import_spp
#define import_libc
#define import_stdio
#define import_ctype
#include <iraf.h>
#include "config.h"
#include "mem.h"
#include "operand.h"
#include "param.h"
#include "grammar.h"
#include "opcodes.h"
#include "clmodes.h"
#include "task.h"
#include "construct.h"
#include "errs.h"
/* CL parser, written as a yacc grammar:
* build up an (rpn) instruction sequence begining at the base of the
* operand stack as the grammar is recognized.
*
* The parser may be called during parameter initialization (initiated by
* the CALL meta-code instruction), and to parse the executable portion
* (from the EXEC instruction).
*
* CONSTANT's are put on the dictionary by addconst() rather than the operand
* stack to avoid conflict with the code being created. They are accessed
* by using the yylval of IDENT and CONSTANT as dictionary indices that
* point to struct operands. This is facilitated with the stkop() macro.
* Make sure that topd and topcs are restored on return to discard these
* temporaries.
* When building offsets for branches, such as BIFF and GOTO, allow
* for the advancement of the pc by the size of the instruction (in ints).
* See opcodes.c for the code executed by the branch instructions.
*/
extern int cldebug;
#define lint /* turns off sccsid in Yacc parser */
/* shorthand way to get at operands in dictionary. x will be values returned
* from addconst() by way of $n's from CONSTANT and IDENT tokens; see gram.c
* and its uses in grammar.l. also see pushop() for a description of the stack.
*/
#define stkop(x) (reference (operand, (x)))
int dobkg = 0; /* set when want to do code in bkground */
int npipes = 0; /* number of pipes in a command */
XINT pipe_pc = 0; /* pc of last ADDPIPE instruction */
int posit = 0; /* positional argument count */
int inarglist = 0; /* set when in argument list */
int parenlevel = 0; /* level of paren nesting in command */
int in_iferr = 0; /* in an iferr block */
int cl_level = 0; /* CL calling level */
int index_cnt; /* Index counter in array ref's */
char curr_param[SZ_FNAME]; /* Parameter name of ref's */
char curr_task[SZ_FNAME]; /* ltaskname of command */
XINT stmt_pc; /* PC at beginning of current statement */
int varlist; /* Declaration is list directed. */
int vartype; /* Type of declaration. */
int do_params; /* Are param definitions legal here? */
int errcnt; /* Syntax error count. */
int inited; /* Was variable already initialized. */
struct param *pp; /* Pointer to param being compiled. */
int n_aval; /* Number of array init values. */
int lastref; /* Was last ref an array? */
int for_expr; /* Was there an expression in FOR? */
char *ifseen; /* Have we just processed an IF? */
char *errmsg; /* Syntax error message. */
/* context-sensitive switches. technique is ok, but beware of nesting!
*/
static int absmode = 0; /* set by first absolute mode arg in cmd*/
static int newstdout = 0; /* set if stdout redirected in arg */
static int bracelevel = 0; /* set while in s_list to inhibit & */
static int tbrace = 0; /* fake braces for declarations */
static int dobrace = 0; /* handling braces. */
static int sawnl = 0; /* set when EOST was \n, else 0 */
static int printstmt = 0; /* set when parsing FPRINT statement */
static int scanstmt = 0; /* set when parsing SCAN statement */
static int iferr_tok = 0; /* iferr/ifnoerr token type seen */
/* printf-format error messages.
*/
char *arrdeferr = "Error in array initialization for `%s'.";
char *badparm = "Parameter definition of `%s' is illegal here.";
char *inval_arr = "Invalid array type for `%s'.";
char *inv_index = "Invalid index definition for `%s'.";
char *twoinits = "Two initializations for parameter `%s'.";
char *exlimits = "Explicit range required for loop in external param.";
char *illegalvar = "Illegal variable declarations.";
char *locallist = "Local list variables are not permitted.";
char *nestediferr = "Nested iferr not allowed in test or handler block.";
char *posfirst = "All positional arguments must be first";
extern char cmdblk[SZ_CMDBLK+1]; /* Command buffer in history.c */
extern char *ip_cmdblk; /* Pointer to current char in command.*/
extern char *err_cmdblk; /* ip_cmdblk when error detected. */
char *index();
struct param *initparam();
struct label *getlabel(), *setlabel();
/* arbitrary large number for bracelevel in a procedure script
*/
#define MAX_ERR 10
#define EYYERROR { err_cmdblk = ip_cmdblk; YYERROR; }
%}
%token Y_SCAN Y_SCANF Y_FSCAN Y_FSCANF Y_OSESC
%token Y_APPEND Y_ALLAPPEND Y_ALLREDIR Y_GSREDIR Y_ALLPIPE
%token D_D D_PEEK
%token Y_NEWLINE Y_CONSTANT Y_IDENT
%token Y_WHILE Y_IF Y_ELSE
%token Y_FOR Y_BREAK Y_NEXT
%token Y_SWITCH Y_CASE Y_DEFAULT
%token Y_RETURN Y_GOTO
%token Y_PROCEDURE Y_BEGIN Y_END
%token Y_BOOL Y_INT Y_REAL Y_STRING Y_FILE Y_STRUCT
%token Y_GCUR Y_IMCUR Y_UKEY Y_PSET
%token Y_IFERR Y_IFNOERR Y_THEN
%right '=' YOP_AOADD YOP_AOSUB YOP_AOMUL YOP_AODIV YOP_AOCAT
%left YOP_OR
%left YOP_AND
%left YOP_EQ YOP_NE
%left '<' '>' YOP_LE YOP_GE
%left YOP_CONCAT
%left '+' '-'
%left '*' '/' '%'
%left YOP_NOT UMINUS /* supplies precedence for unary minus */
%left YOP_POW
%start block
%%
block : /* empty */ {
/* Done once on entry but after at least one call to
* yylex(). Good for initing parser flags.
* Note: this does not get called in procedure scripts.
*/
if (cldebug)
eprintf ("parse init (block)...\n");
errcnt = 0;
err_cmdblk = 0;
dobkg = 0;
inarglist = 0;
parenlevel = 0;
bracelevel = 0;
tbrace = 0;
dobrace = 0;
in_iferr = 0;
do_params = YES;
last_parm = NULL;
ifseen = NULL;
label1 = NULL;
errmsg = NULL;
parse_pfile= currentask->t_pfp;
}
| '.' NL {
/* Prepare to rerun whatever was compiled last.
* Does not work for the debug commands builtin here.
*/
if (parse_state != PARSE_FREE) {
errmsg = "Illegal parser state.";
EYYERROR;
}
rerun();
YYACCEPT;
}
| block {
if (parse_state == PARSE_PARAMS) {
errmsg = "Illegal parser state.";
EYYERROR;
}
}
debug xstmt {
if (sawnl && bracelevel == 0) {
if (!errcnt)
compile (END);
if (ifseen) {
/* Simulate an unput of what has been read
* from the current line.
*/
ip_cmdblk = ifseen;
}
YYACCEPT;
}
}
| script_params {
/* Parse the parameters in a script file. This will
* normally be done on a call by pfileread().
*/
if (parse_state != PARSE_PARAMS) {
eprintf ("Illegal parser state.\n");
errcnt++;
}
YYACCEPT;
}
| script_body {
/* Parse the executable statements in a script.
*/
if (parse_state != PARSE_BODY) {
eprintf ("Illegal parser state.\n");
errcnt++;
}
if (!errcnt)
compile (END);
YYACCEPT;
}
| error NL {
/* This catches errors that the two other error lines
* can't get, e.g. a missing `}' at the end of a script,
* or errors occuring in interactive input.
*/
yyerrok;
/* Discard everything and compile a null statement.
*/
if (!errcnt) {
do_params = YES;
pc = currentask->t_bascode;
if (parse_state != PARSE_PARAMS)
compile (END);
topd = currentask->t_topd;
topcs = currentask->t_topcs;
/* Unlink any added parms. Resetting of topd will
* already have reclaimed space.
*/
if (last_parm) {
last_parm->p_np = NULL;
currentask->t_pfp->pf_lastpp = last_parm;
last_parm = NULL;
}
}
/* Print cmdblk and show position of error.
*/
p_position();
if (currentask->t_flags & T_SCRIPT)
cl_error (E_UERR, "syntax error, line %d",
currentask->t_scriptln);
else
cl_error (E_UERR, "syntax error");
YYACCEPT;
}
;
debug : /* empty */
| D_XXX EOST {
/* debug are those debugging functions that
* should be run directly and not through a
* builtin task due to stack or other changes,
* ie, don't change what we are trying to show.
*/
printf ("\n");
} debug
;
D_XXX : D_D {
d_d(); /* show dictionary/stack pointers */
}
| D_PEEK Y_CONSTANT { /* show a dictionary location */
if (stkop($2)->o_type & OT_INT) {
int idx;
idx = stkop($2)->o_val.v_i;
eprintf ("%d:\t%d (0%o)\n", idx, stack[idx],
stack[idx]);
} else
eprintf ("usage: D_PEEK <d. index>\n");
}
| '~' {
d_stack (pc, 0, 0); /* show compiled code */
}
;
script_params : proc_stmt
var_decls
begin_stmt {
/* Check for required params.
*/
if (!errcnt)
proc_params(n_procpar);
}
;
script_body: begin_stmt {
/* Initialize parser for procedure body.
*/
if (cldebug)
eprintf ("parse init (script_body)...\n");
errcnt = 0;
err_cmdblk = 0;
dobkg = 0;
inarglist = 0;
parenlevel = 0;
in_iferr = 0;
dobrace = 0;
bracelevel = PBRACE; /* disable lexmodes; force "end" */
tbrace = 0;
do_params = NO;
last_parm = NULL;
ifseen = NULL;
label1 = NULL;
parse_pfile= currentask->t_pfp;
}
s_list
opnl
end_stmt
;
proc_stmt: Y_PROCEDURE {
/* Initialize parser for procedure parameters.
*/
if (cldebug)
eprintf ("parse init (proc_stmt)...\n");
errcnt = 0;
err_cmdblk = 0;
dobkg = 0;
inarglist = 0;
parenlevel = 0;
bracelevel = PBRACE;
tbrace = 0;
dobrace = 0;
in_iferr = 0;
do_params = YES;
last_parm = NULL;
label1 = NULL;
}
param bparam_list EOST
;
bparam_list: /* Nothing at all, not even parens. */
{
n_procpar = 0;
}
| LP param_list RP
;
/* The definition of the parameter list excludes lists of the
* form a,,b
*/
param_list: /* empty */ {
n_procpar = 0;
}
| xparam_list
;
xparam_list: param {
n_procpar = 1;
if (!errcnt)
push (stkop($1));
}
| xparam_list DELIM param {
n_procpar++;
if (!errcnt)
push (stkop($3));
}
;
var_decls: /* No params. */
| var_decl_block
;
var_decl_block: var_decl_line
| var_decl_block var_decl_line
;
var_decl_line: EOST /* Blank. */
| var_decl_stmt
| error NL {
/* This catches errors in the parameter declarations
* of a procedure script.
*/
yyerrok;
/* Discard everything and compile a null statement.
*/
if (!errcnt) {
do_params = YES;
pc = currentask->t_bascode;
if (parse_state != PARSE_PARAMS)
compile (END);
topd = currentask->t_topd;
topcs = currentask->t_topcs;
/* Unlink any added parms. Resetting of topd will
* already have reclaimed space.
*/
if (last_parm) {
last_parm->p_np = NULL;
currentask->t_pfp->pf_lastpp = last_parm;
last_parm = NULL;
}
}
/* Print cmdblk and show position of error. We know
* we're parsing a procedure script, so print the line
* number too.
*/
p_position();
cl_error (E_UERR, "syntax error, line %d",
currentask->t_scriptln);
}
;
var_decl_stmt: typedefs {
/* For in-line definitions we don't want
* to freeze stuff on the dictionary, so
* only allow additions if the dictionary
* is the same as at the beginning of the task.
*/
if (!errcnt) {
if (parse_state != PARSE_PARAMS) {
if (currentask->t_topd != topd)
cl_error (E_UERR, illegalvar);
last_parm = currentask->t_pfp->pf_lastpp;
}
}
/* Increment bracelevel temporarily to defeat command
* mode, in case this is an in-line declaration and
* lexmodes=yes.
*/
bracelevel += PBRACE;
tbrace++;
} var_decl_list EOST {
/* Update dictionary to include these definitions.
*/
if (!errcnt) {
if (parse_state != PARSE_PARAMS) {
currentask->t_topd = topd;
last_parm = 0;
}
}
/* Restore command mode */
bracelevel -= PBRACE;
tbrace--;
}
;
typedefs: Y_BOOL { vartype = V_BOOL; }
| Y_STRING { vartype = V_STRING; }
| Y_REAL { vartype = V_REAL; }
| Y_FILE { vartype = V_FILE; }
| Y_GCUR { vartype = V_GCUR; }
| Y_IMCUR { vartype = V_IMCUR; }
| Y_UKEY { vartype = V_UKEY; }
| Y_PSET { vartype = V_PSET; }
| Y_INT { vartype = V_INT; }
| Y_STRUCT { vartype = V_STRUCT; }
;
var_decl_list: var_decl_plus
| var_decl_plus DELIM var_decl_list
;
var_decl_plus: var_decl {
if (!errcnt) {
if (pp != NULL) {
if (n_aval > 1)
pp->p_type |= PT_ARRAY;
if (pp->p_type & PT_ARRAY)
do_arrayinit (pp, n_aval, index_cnt);
else
do_scalarinit (pp, inited);
}
}
}
/* Semi-colon in following rule is not input by user, but
* rather by lexical analyzer to help close compound
* statements.
*/
| var_decl '{' options_list ';' '}' {
if (!errcnt) {
if (pp != NULL) {
if (!do_params)
cl_error (E_UERR, badparm, pp->p_name);
if (n_aval > 1)
pp->p_type |= PT_ARRAY;
if (pp->p_type & PT_ARRAY)
do_arrayinit (pp, n_aval, index_cnt);
else
do_scalarinit (pp, n_aval);
}
}
}
;
var_decl: var_def {
inited = NO;
n_aval = 0;
}
| var_def '=' {
n_aval = 0;
}
init_list {
inited = YES;
}
;
var_def : var_name {
index_cnt = 0;
if (!errcnt)
pp = initparam (stkop($1), do_params, vartype, varlist);
}
| var_name {
int itemp;
if (!errcnt) {
pp = initparam (stkop($1), do_params, vartype, varlist);
if (pp != NULL) {
itemp = (pp->p_type & OT_BASIC) == pp->p_type;
itemp = itemp && !varlist;
if (itemp)
pp->p_type |= PT_ARRAY;
else
cl_error (E_UERR, inval_arr, pp->p_name);
}
}
}
'[' init_index_list ']'
;
var_name: param {
varlist = NO;
index_cnt = 0;
}
| '*' param {
if (!do_params) {
errmsg = locallist;
EYYERROR;
}
varlist = YES;
index_cnt = 0;
$$ = $2;
}
;
init_index_list:
/* A null index list means get the length of the array
* from the initializer.
*/
| init_index_range
| init_index_list DELIM init_index_range
;
init_index_range:
const {
if (!errcnt) {
if (pp != NULL) {
if (stkop($1)->o_type == OT_INT) {
push (stkop($1)->o_val.v_i);
push (1);
} else if (maybeindex) {
/* Confusion between sexagesimal and index
* range. Maybeindex is set only when operand
* is real.
*/
int i1,i2;
sexa_to_index (stkop($1)->o_val.v_r, &i1, &i2);
push (i2-i1+1);
push (i1);
} else {
eprintf (inv_index, pp->p_name);
EYYERROR;
}
index_cnt++;
}
}
}
| const ':' const {
if (!errcnt) {
if (pp != NULL) {
if (stkop($1)->o_type != OT_INT ||
stkop($3)->o_type != OT_INT)
cl_error (E_UERR, inv_index, pp->p_name);
else {
push (stkop($3)->o_val.v_i -
stkop($1)->o_val.v_i + 1);
push (stkop($1)->o_val.v_i);
}
index_cnt++;
}
}
}
;
init_list: init_elem
| init_list DELIM init_elem
;
init_elem: const {
if (!errcnt) {
if (pp != NULL) {
push (stkop($1) );
n_aval++;
}
}
}
| Y_CONSTANT LP const RP /* PL/I notation. */
{
int cnt;
if (!errcnt)
if (pp != NULL) {
if (stkop($1)->o_type != OT_INT)
cl_error (E_UERR, arrdeferr, pp->p_name);
cnt = stkop($1)->o_val.v_i;
if (cnt <= 0)
cl_error (E_UERR, arrdeferr, pp->p_name);
while (cnt-- > 0) {
push (stkop($3));
n_aval++;
}
}
}
;
const : Y_CONSTANT
| number
;
/* The parser and lexical analyzer don't see negative numbers as an
* entity. So we must join signs to their constants.
*/
number : sign Y_CONSTANT {
if (stkop($2)->o_type == OT_INT) {
stkop($2)->o_val.v_i *= $1;
$$ = $2;
} else if (stkop($2)->o_type == OT_REAL) {
stkop($2)->o_val.v_r *= $1;
$$ = $2;
} else {
errmsg = "Invalid constant in declaration.";
EYYERROR;
}
}
;
sign : '+' { $$ = 1; }
| '-' { $$ = -1; }
options_list: init_list DELIM options {
/* Check if we already had an initialization.
*/
if (!errcnt) {
if (inited && pp != NULL) {
eprintf (twoinits, pp->p_name);
EYYERROR;
}
}
}
| init_list {
if (!errcnt) {
if (inited && pp != NULL) {
eprintf (twoinits, pp->p_name);
EYYERROR;
}
}
}
| options
;
options : option
| options DELIM option
;
option : Y_IDENT '=' const {
if (!errcnt)
if (pp != NULL)
do_option (pp, stkop($1), stkop($3));
}
;
begin_stmt: Y_BEGIN NL
;
/* In normal expressions, a param means the name of a parameter, but in
* command line arguments, it may be a string constant. Pull out param
* from expr to let the arg rule deal with it specially.
*/
expr : expr0
| ref {
if (!errcnt)
compile (PUSHPARAM, stkop($1)->o_val.v_s);
}
;
/* EXPR0 is everything but a simple parameter. This is needed for argument
* lists so that a simple parameter may be treated as a special case of a
* string constant. EXPR1 also excludes constants. This is needed
* to eliminate ambiguities in the grammar which would arise from
* the handling of the lexical ambiguity of sexagesimal constants
* and array index ranges.
*/
expr0 : expr1
| Y_CONSTANT {
if (!errcnt)
compile (PUSHCONST, stkop($1));
}
| Y_GCUR {
/* "gcur" is both a keyword and a CL global parameter,
* and must be built into the grammar here to permit
* reference of the parameter in expressions.
*/
if (!errcnt)
compile (PUSHPARAM, "gcur");
}
| Y_IMCUR {
if (!errcnt)
compile (PUSHPARAM, "imcur");
}
| Y_UKEY {
if (!errcnt)
compile (PUSHPARAM, "ukey");
}
| Y_PSET {
if (!errcnt)
compile (PUSHPARAM, "pset");
}
;
expr1 : LP expr RP
| expr '+' opnl expr {
if (!errcnt)
compile (ADD);
}
| expr '-' opnl expr {
if (!errcnt)
compile (SUB);
}
| expr '*' opnl expr {
if (!errcnt)
compile (MUL);
}
| expr '/' opnl expr {
if (!errcnt)
compile (DIV);
}
| expr YOP_POW opnl expr {
if (!errcnt)
compile (POW);
}
| expr '%' opnl expr {
struct operand o;
if (!errcnt) {
o.o_type = OT_INT;
o.o_val.v_i = 2;
compile (PUSHCONST, &o);
compile (INTRINSIC, "mod");
}
}
| expr YOP_CONCAT opnl expr {
if (!errcnt)
compile (CONCAT);
}
| expr '<' opnl expr {
if (!errcnt)
compile (LT);
}
| expr '>' opnl expr {
if (!errcnt)
compile (GT);
}
| expr YOP_LE opnl expr {
if (!errcnt)
compile (LE);
}
| expr YOP_GE opnl expr {
if (!errcnt)
compile (GE);
}
| expr YOP_EQ opnl expr {
if (!errcnt)
compile (EQ);
}
| expr YOP_NE opnl expr {
if (!errcnt)
compile (NE);
}
| expr YOP_OR opnl expr {
if (!errcnt)
compile (OR);
}
| expr YOP_AND opnl expr {
if (!errcnt)
compile (AND);
}
| YOP_NOT expr {
if (!errcnt)
compile (NOT);
}
| '-' expr %prec UMINUS {
if (!errcnt)
compile (CHSIGN);
}
| Y_SCAN LP {
/* Free format scan. */
if (!errcnt)
push (0); /* use control stack to count args */
} scanarg RP {
if (!errcnt) {
struct operand o;
o.o_type = OT_INT;
o.o_val.v_i = pop(); /* get total number of args*/
compile (PUSHCONST, &o);
compile (SCAN);
}
}
| Y_SCANF LP {
/* Formatted scan. */
if (!errcnt)
push (0); /* use control stack to count args */
} scanfmt DELIM scanarg RP {
if (!errcnt) {
struct operand o;
/* Compile number of arguments. */
o.o_type = OT_INT;
o.o_val.v_i = pop();
compile (PUSHCONST, &o);
compile (SCANF);
}
}
| Y_FSCAN LP {
/* Free format scan from a parameter. */
if (!errcnt)
push (0); /* use control stack to count args */
} scanarg RP {
if (!errcnt) {
struct operand o;
o.o_type = OT_INT;
o.o_val.v_i = pop(); /* get total number of args*/
compile (PUSHCONST, &o);
compile (FSCAN);
}
}
| Y_FSCANF LP Y_IDENT DELIM {
/* Formatted scan from a parameter.
* fscanf (param, format, arg1, ...)
*/
if (!errcnt) {
compile (PUSHCONST, stkop ($3));
push (1); /* use control stack to count args */
}
} scanfmt DELIM scanarg RP {
if (!errcnt) {
struct operand o;
/* Compile number of arguments. */
o.o_type = OT_INT;
o.o_val.v_i = pop();
compile (PUSHCONST, &o);
compile (FSCANF);
}
}
| intrinsx LP {
if (!errcnt)
push (0); /* use control stack to count args */
} intrarg RP {
if (!errcnt) {
struct operand o;
o.o_type = OT_INT;
o.o_val.v_i = pop();
compile (PUSHCONST, &o);
compile (INTRINSIC, stkop($1)->o_val.v_s);
}
}
;
/* Variable types are keywords, so any types which are also intrinsic
* functions are added here.
*/
intrinsx: intrins
| Y_INT {
/* The YACC value of this must match normal intrinsics
* so we must generate an operand with the proper
* string.
*/
if (!errcnt)
$$ = addconst ("int", OT_STRING);
}
| Y_REAL {
if (!errcnt)
$$ = addconst ("real", OT_STRING);
}
;
scanfmt : expr {
if (!errcnt) {
push (pop() + 1); /* inc num args */
}
}
;
scanarg : /* empty. This is bad for scan but we don't want to
* generate a cryptic syntax error. See also intrarg.
* This rule reduces the strings from right to left.
* Note the lexical analyzer strips optional newlines
* after comma delimiters, so we don't need an opnl here.
*/
| Y_IDENT {
if (!errcnt) {
compile (PUSHCONST, stkop ($1));
push (pop() + 1); /* inc num args */
}
}
| Y_IDENT DELIM scanarg {
if (!errcnt) {
compile (PUSHCONST, stkop ($1));
push (pop() + 1); /* inc num args */
}
}
;
intrarg : /* empty. this is to allow () but it also allows
* (x,,x). may want to prune this out.
*/
| expr {
if (!errcnt)
push (pop() + 1); /* inc num args */
}
| intrarg DELIM expr {
if (!errcnt)
push (pop() + 1); /* inc num args */
}
;
/* Statements. */
stmt : c_stmt
| assign EOST
| cmdlist EOST
| immed EOST
| inspect EOST
| osesc EOST
| popstk EOST
| if
| ifelse
| iferr
| iferr_else
| while
| for
| switch
| case
| default
| next EOST
| break EOST
| goto EOST
| return EOST
| label_stmt
| nullstmt
;
/* A compound statement may be followed by zero or one
* newlines.
*/
c_stmt : c_blk
| c_blk NL
;
c_blk : '{' {
bracelevel++;
} s_list opnl {
--bracelevel;
} '}'
;
s_list : /* empty */
| s_list opnl xstmt
;
/* Put "implicit" parentheses around right hand side of assignments to
* permit easy arithmetic even with lexmodes=yes.
*/
assign : ref equals expr0 {
--parenlevel;
if (!errcnt)
compile (ASSIGN, stkop($1)->o_val.v_s);
}
| ref equals ref {
/* Old code pushed a constant rather than a param
* when not within braces. This doesn't seem
* to be what most people want.
*/
--parenlevel;
if (!errcnt) {
compile (PUSHPARAM, stkop($3)->o_val.v_s);
compile (ASSIGN, stkop($1)->o_val.v_s);
}
}
| ref {
parenlevel++;
}
assign_oper expr {
--parenlevel;
if (!errcnt)
compile ($3, stkop($1)->o_val.v_s);
}
;
/* Breaking out the '=' avoids grammar ambiguities.
*/
equals : '=' {
parenlevel++;
}
;
assign_oper: YOP_AOADD { $$ = ADDASSIGN; }
| YOP_AOSUB { $$ = SUBASSIGN; }
| YOP_AOMUL { $$ = MULASSIGN; }
| YOP_AODIV { $$ = DIVASSIGN; }
| YOP_AOCAT { $$ = CATASSIGN; }
;
cmdlist : command {
npipes = 0;
} cmdpipe {
if (!errcnt) {
compile (EXEC);
if (npipes > 0)
compile (RMPIPES, npipes);
}
}
;
cmdpipe : /* empty */
| cmdpipe pipe {
/* Pipefiles must be allocated at run time using a stack
* to permit pipe commands within loops, and to permit
* scripts called in a pipe to themselves contain pipe
* commands. ADDPIPE allocates a new pipefile on the
* pipe stack and pushes its name on the operand stack.
* GETPIPE pushes the pipefile at the top of the pipe
* stack onto the operand stack. RMPIPES removes N pipes
* from the pipe stack, and deletes the physical pipefiles.
*/
if (!newstdout) {
/* When the runtime code creates the pipe it needs to
* know the identity of the two tasks sharing the pipe
* to determine what type of pipe to create (text or
* binary). Save the pc of the ADDPIPE instruction
* so that we can backpatch it below with a pointer to
* the name of the second task in the pipe (ADDPIPE
* will be called during startup of the first task
* hence will know its name).
*/
pipe_pc = compile (ADDPIPE, NULL);
if ($2 == 1)
compile (REDIR);
else
compile (ALLREDIR);
compile (EXEC);
} else {
eprintf ("multiple redirection\n");
YYERROR;
}
} command {
/* Compile the GETPIPE instruction with the name of the
* second task in the current pipe, and backpatch the
* matching ADDPIPE instruction with the PC of the GETPIPE.
*/
(coderef(pipe_pc))->c_args = compile (GETPIPE, curr_task);
compile (REDIRIN);
npipes++; /* Overflow checking is in ADDPIPE */
}
;
pipe : '|' opnl {
$$ = 1;
}
| Y_ALLPIPE opnl {
$$ = 2;
}
;
command : tasknam {
char *ltname;
ltname = stkop($1)->o_val.v_s;
compile (CALL, ltname);
strcpy (curr_task, ltname);
/* The FPRINT task is special; the first arg
* is the destination and must be compiled as
* a string constant no matter what. Set flag
* so that 'arg' compiles PUSHCONST.
*/
printstmt = (strcmp (ltname, "fprint") == 0);
/* Ditto with SCAN; all the arguments are call by
* reference and must be compiled as string constants.
*/
scanstmt = (strcmp (ltname, "scan") == 0 ||
strcmp (ltname, "scanf") == 0);
absmode = 0;
posit = 0;
newstdout = 0;
parenlevel = 0;
} BARG {
inarglist = 1;
} args EARG {
extern char *onerr_handler;
inarglist = 0;
parenlevel = 0;
scanstmt = 0;
}
;
args : DELIM {
/* (,x) equates to nargs == 2. Call posargset with
* negative dummy argument to bump nargs.
*/
if (!errcnt) {
compile (POSARGSET, -1);
posit++;
printstmt = 0;
scanstmt = 0;
}
} arglist
| arglist
;
arglist : arg
| arglist DELIM arg
;
arg : /* nothing - compile a null posargset to bump nargs */
{
if (!errcnt) {
if (posit > 0) { /* not first time */
compile (POSARGSET, -posit);
printstmt = 0;
scanstmt = 0;
}
posit++;
}
}
| expr0 {
if (absmode) {
errmsg = posfirst;
EYYERROR;
} else
if (!errcnt)
compile (POSARGSET, posit++);
}
| ref {
if (absmode) {
errmsg = posfirst;
EYYERROR;
} else if (!errcnt) {
if (scanstmt) {
char pname[SZ_FNAME];
char *pk, *t, *p, *f;
struct pfile *pfp;
struct operand o;
/* If no task name specified check the pfile for
* the task containing the scan statement for the
* named parameter.
*/
breakout (stkop($1)->o_val.v_s, &pk, &t, &p, &f);
pfp = currentask->t_pfp;
if (*pk == NULL && *t == NULL &&
pfp && paramfind(pfp,p,0,1)) {
sprintf (pname, "%s.%s",
currentask->t_ltp->lt_lname, p);
if (*f) {
strcat (pname, ".");
strcat (pname, f);
}
} else
strcpy (pname, stkop($1)->o_val.v_s);
o = *(stkop($1));
o.o_val.v_s = pname;
compile (PUSHCONST, &o);
compile (INDIRPOSSET, posit++);
} else if (parenlevel == 0 || printstmt) {
compile (PUSHCONST, stkop($1));
compile (INDIRPOSSET, posit++);
/* only first arg of fprint stmt is special. */
printstmt = 0;
} else {
compile (PUSHPARAM, stkop($1)->o_val.v_s);
compile (POSARGSET, posit++);
}
}
}
| ref '=' expr0 {
absmode++;
if (!errcnt)
compile (ABSARGSET, stkop($1)->o_val.v_s);
}
| ref '=' ref {
absmode++;
if (!errcnt) {
if (parenlevel == 0) {
compile (PUSHCONST, stkop($3));
compile (INDIRABSSET, stkop($1)->o_val.v_s);
} else {
compile (PUSHPARAM, stkop($3)->o_val.v_s);
compile (ABSARGSET, stkop($1)->o_val.v_s);
}
}
}
| param '+' {
absmode++;
if (!errcnt)
compile (SWON, stkop($1)->o_val.v_s);
}
| param '-' {
absmode++;
if (!errcnt)
compile (SWOFF, stkop($1)->o_val.v_s);
}
| '<' file {
if (!errcnt)
compile (REDIRIN);
}
| '>' file {
newstdout++;
if (!errcnt)
compile (REDIR);
}
| Y_ALLREDIR file {
newstdout++;
if (!errcnt)
compile (ALLREDIR);
}
| Y_APPEND file {
newstdout++;
if (!errcnt)
compile (APPENDOUT);
}
| Y_ALLAPPEND file {
newstdout++;
if (!errcnt)
compile (ALLAPPEND);
}
| Y_GSREDIR file {
if (!errcnt)
compile (GSREDIR, stkop($1)->o_val.v_s);
}
;
file : expr0 {
absmode++;
/* constant already pushed by expr0.
*/
}
| param {
absmode++;
if (!errcnt) {
if (parenlevel == 0)
compile (PUSHCONST, stkop($1));
else
compile (PUSHPARAM, stkop($1)->o_val.v_s);
}
}
;
immed : equals expr0 {
--parenlevel;
if (!errcnt)
compile (IMMED);
}
| equals ref {
--parenlevel;
if (!errcnt)
compile (INSPECT, stkop($2)->o_val.v_s);
}
;
inspect : ref equals {
--parenlevel;
if (!errcnt)
compile (INSPECT, stkop($1)->o_val.v_s);
}
;
osesc : Y_OSESC {
if (!errcnt)
compile (OSESC, stkop($1)->o_val.v_s);
}
;
popstk : equals {
--parenlevel;
if (!errcnt)
compile (IMMED);
}
;
/* IFERR checking code.
*/
iferr: iferr_stat {
/* pop BIFF addr and set branch to just after statement */
if (!errcnt) {
XINT biffaddr = pop();
coderef (biffaddr)->c_args = pc - biffaddr - SZ_CE;
}
in_iferr = 0;
}
;
iferr_stat: iferr_tok {
if (++in_iferr > 1) {
errmsg = nestediferr;
EYYERROR;
}
compile (CALL, "_errpsh");
compile (EXEC);
} c_blk {
if (!errcnt) {
struct operand o;
o.o_type = OT_INT;
o.o_val.v_i = 0;
compile (PUSHCONST, &o); /* if (_errpop() != 0) */
compile (INTRINSIC, "_errpop");
compile (PUSHCONST, &o);
compile (((iferr_tok == 0) ? NE : EQ));
push (compile (BIFF, 0));
}
} op_then opnl xstmt {
in_iferr--;
}
;
iferr_else : iferr_stat Y_ELSE {
if (!errcnt) {
/* Pop and save BIFF address, compile and push addr
* of GOTO, and set BIFF branch to just after GOTO.
*/
XINT biffaddr = pop();
push (compile (GOTO, 0));
coderef (biffaddr)->c_args = pc - biffaddr - SZ_CE;
}
} opnl xstmt {
if (!errcnt) {
/* Pop GOTO addr and set branch to just after statement
*/
XINT gotoaddr = pop();
coderef (gotoaddr)->c_args = pc - gotoaddr - SZ_CE;
}
}
;
iferr_tok: Y_IFERR { iferr_tok = 0; }
| Y_IFNOERR { iferr_tok = 1; }
;
op_then: /* empty */
| Y_THEN
;
/* END IFERR checking rules.
*/
if : if_stat {
/* pop BIFF addr and set branch to just after statement
*/
XINT biffaddr;
if (!errcnt) {
biffaddr = pop();
coderef (biffaddr)->c_args = pc - biffaddr - SZ_CE;
}
}
;
if_stat : Y_IF LP expr RP {
/* save BIFF addr so branch can be filled in
*/
if (!errcnt)
push (compile (BIFF, 0));
} opnl xstmt {
/* The shift/reduce conflict in the IF-IF/ELSE
* construct can cause errors in compilation
* because the IF statement can also be a
* terminal symbol, i.e. it may be all that
* is parsed in one call to the parser.
* The parser must look ahead one token
* to find if there is an else statement
* following. If there is no following
* token an EOF may be detected prematurely.
* When the IF statement is being parsed not
* inside any braces, then when the next token
* is not an ELSE care must be taken that this
* token is seen on a subsequent invocation
* of the parser. The `ifseen' flag is
* used within the support for the lexical
* analyzer located in `history.c'.
*/
if (cldebug)
eprintf ("ytab: setting ifseen=yes\n");
if (currentask->t_flags & T_INTERACTIVE)
ifseen = ip_cmdblk;
else
ifseen = cmdblk;
}
;
ifelse : if_stat Y_ELSE {
XINT biffaddr;
ifseen = NULL;
if (!errcnt) {
/* Pop and save BIFF address, compile and push addr
* of GOTO, and set BIFF branch to just after GOTO.
*/
biffaddr = pop();
push (compile (GOTO, 0));
coderef (biffaddr)->c_args = pc - biffaddr - SZ_CE;
}
} opnl xstmt {
XINT gotoaddr;
if (!errcnt) {
/* Pop GOTO addr and set branch to just after statement
*/
gotoaddr = pop();
coderef (gotoaddr)->c_args = pc - gotoaddr - SZ_CE;
}
}
;
while : Y_WHILE LP {
/* Save starting addr of while expression.
*/
if (!errcnt) {
push (pc);
loopincr();
}
} expr RP {
/* Save BIFF addr so branch can be filled in.
*/
if (!errcnt)
push (compile (BIFF, 0));
} opnl xstmt {
XINT biffaddr;
if (!errcnt) {
/* Pop and save addr of BIFF instruction. */
biffaddr = pop();
/* Pop addr of expression and build a goto there. */
compile (GOTO, pop() - pc - SZ_CE);
/* Now can set BIFF branch to just after statement.*/
coderef (biffaddr)->c_args = pc - biffaddr - SZ_CE;
loopdecr();
}
}
;
/* The line of code:
*
* for (e1, e2, e3) stmt
*
* is compiled into:
*
* e1
* loop1: if (!e2) goto end
* goto loop3
* loop2: e3
* goto loop1
* loop3: stmt
* goto loop2
* end:
*
* Note that e1 and e3 are assignments while e2 is an expression.
*/
for : Y_FOR LP opnl xassign ';' opnl {
if (!errcnt)
push(pc); /* Loop1: */
}
xexpr ';' opnl {
if (!errcnt) {
if (for_expr)
ppush (compile(BIFF, 0)); /* if (!e2) */
/* Add SZ_CE to skip following GOTO.
*/
ppush (pc+SZ_CE); /* Loop2: */
ppush (compile(GOTO,0)); /* goto Loop3 */
/* Save current location as the destination
* for NEXT statements.
*/
loopincr();
}
}
xassign RP opnl {
XINT stmtaddr;
if (!errcnt) {
stmtaddr = pop();
compile (GOTO, stmtaddr-pc-SZ_CE); /* Goto loop1 */
stmtaddr = pop();
coderef(stmtaddr)->c_args = pc - stmtaddr - SZ_CE;
}
}
stmt {
XINT stmtaddr;
if (!errcnt) {
stmtaddr = pop();
compile (GOTO, stmtaddr-pc-SZ_CE); /* goto loop2 */
if (for_expr) {
stmtaddr = pop();
coderef(stmtaddr)->c_args = pc-stmtaddr-SZ_CE;
}
loopdecr();
}
}
;
/* The following allow skipping of fields in the FOR statement.
*/
xassign : assign
| /* empty */
;
xexpr : expr {
for_expr = YES;
}
| /* empty */ {
for_expr = NO;
}
;
/* The compiled code for the switch statement
* consists of a SWITCH, followed by a series of
* CASE and DEFAULT blocks, followed by a jump table.
* The first operand in each CASE and DEFAULT block
* is a CASE or DEFAULT operand which is never
* executed, but is used to store the values which
* will enter this block. Executable statements
* follow.
*
* The jump table consists of the addresses of the
* CASE and DEFAULT blocks. The DEFAULT block
* comes first, and is 0 if no default has
* been given. The list of addresses is terminated
* by a 0 address.
*
* The last statement of each CASE and DEFAULT
* statement is a branch back to a GOTO following
* the SWITCH. This GOTO points to after the jumptable.
*/
switch : Y_SWITCH opnl LP opnl expr opnl RP opnl
{
if (!errcnt) {
push (compile(SWITCH));
/* Compile GOTO which will branch past end of
* switch. This is needed if there is no DEFAULT.
*/
compile (GOTO, 0);
}
} xstmt {
/* Set up jumptable and pop space on stack.
*/
if (!errcnt)
setswitch();
}
;
case : Y_CASE {
if (!errcnt) {
ncaseval = 0;
if (!in_switch()) {
errmsg = "Improper CASE statement.";
EYYERROR;
}
}
} const_expr_list ':' opnl {
XINT pcase;
if (!errcnt) {
pcase = compile (CASE, ncaseval);
/* Fill in argument list.
*/
caseset (&(coderef(pcase)->c_args), ncaseval);
push (pcase);
}
} xstmt {
/* Branch to end of switch block
*/
if (!errcnt)
push (compile(GOTO, 0));
}
;
default : Y_DEFAULT ':' opnl {
/* Compile an operand to store the current PC.
*/
if (!errcnt) {
if (!in_switch()) {
errmsg = "Improper DEFAULT statement.";
EYYERROR;
}
push (compile(DEFAULT));
}
} xstmt {
/* Branch past jump table.
*/
if (!errcnt)
push (compile(GOTO, 0));
}
;
next : Y_NEXT {
/* All NEXT statements are backward references,
* so we simply store the addresses in an array.
*/
if (!errcnt) {
if (nestlevel)
compile (GOTO, nextdest[nestlevel-1]-pc-SZ_CE);
else {
errmsg = "NEXT outside of loop.";
EYYERROR;
}
}
}
;
break : Y_BREAK {
/* Each BREAK is a forward reference. For the
* first BREAK in each loop we compile a
* GOTO statement which will be the object of
* all BREAK statements within the loop. When
* the loop is terminated the target of this
* GOTO will be set.
*/
int dest;
if (!errcnt) {
if (!nestlevel) {
errmsg = "Break outside of loop.";
EYYERROR;
} else if ((dest = brkdest[nestlevel-1]) != 0)
compile (GOTO, dest-pc-SZ_CE);
else {
brkdest[nestlevel-1] = pc;
compile (GOTO, 0);
}
}
}
;
return : Y_RETURN {
if (!errcnt)
compile (END);
}
| Y_RETURN expr {
/* Return values currently not implemented.
*/
eprintf ("Warning: return value ignored.\n");
if (!errcnt)
compile (END);
}
;
/* Require end to terminate with a new-line, because
* it should be at the end of the file.
*/
end_stmt: Y_END NL {
bracelevel -= PBRACE;
if (bracelevel < 0) {
errmsg = "Too few left braces.";
EYYERROR;
} else if (bracelevel > 0) {
errmsg = "Too few right braces.";
EYYERROR;
}
}
;
label_stmt: Y_IDENT ':' opnl {
/* Put symbol in table in dictionary and
* process indirect references if present.
*/
struct label *l;
if (!errcnt) {
l = getlabel (stkop($1));
if (l == NULL) {
l = setlabel (stkop($1));
l->l_loc = pc;
} else if (l->l_defined) {
errmsg = "Identical labels.";
EYYERROR;
} else {
/* Get this GOTO out of the
* indirect list so we can use
* the argument as the destination
*/
XINT gotopc;
gotopc = l->l_loc;
unsetigoto (gotopc);
/* Fix the indirect reference.
*/
coderef(gotopc)->c_args = pc - gotopc - SZ_CE;
}
(l->l_defined)++;
}
}
xstmt
;
goto : Y_GOTO Y_IDENT {
/* Get the address corresponding to the label.
*/
struct label *l;
if (!errcnt) {
l = getlabel (stkop($2));
if (l != NULL)
compile (GOTO, l->l_loc - pc - SZ_CE);
else {
/* Ready for indirect GOTO
*/
l = setlabel (stkop($2));
l->l_loc = pc;
setigoto (compile(GOTO, 0));
l->l_defined = 0;
}
}
}
;
nullstmt: ';'
| ';' NL
;
/* xstmt is defined so that to handle implicit do loops created by
* open array references e.g. a[*,3]=a[3,*].
*/
xstmt : /* empty */ {
/* Save pc before compiling statement for loop back
*/
stmt_pc = pc;
n_oarr = 0;
i_oarr = 0;
ifseen = NULL;
}
stmt {
/* If there was an open reference compile the
* loop increment and goback.
*/
XINT push_pc;
if (!errcnt) {
if (n_oarr) {
compile (INDXINCR, stmt_pc-pc-4, 2*n_oarr+1);
/* We are going to store initialization
* info for the implicit loop here.
* It is loopincr's responsibility to
* branch around it. This data is what
* should be pointed to by the special
* PUSHINDEX compiled at the first open
* array reference.
*/
push_pc = pop(); /* Location of PUSHINDEX */
coderef(push_pc)->c_args = pc - push_pc - SZ_CE;
stack[pc++] = n_oarr;
for (i_oarr=0; i_oarr<n_oarr; i_oarr++) {
stack[pc++] = oarr_beg[i_oarr];
stack[pc++] = oarr_end[i_oarr];
}
/* Clear n_oarr. This must be done here
* because we may have the end of a compound
* statement following on the heels of the
* end of the simple statement with the
* implicit loop.
*/
n_oarr = 0;
i_oarr = 0;
}
}
}
| var_decl_stmt
| error NL {
/* This should get most errors in executable statements
* or in the local variable declarations in a script.
*/
yyerrok;
/* Get rid of any fake braces.
*/
bracelevel -= tbrace;
/* Discard everything and compile a null statement.
*/
if (!errcnt) {
do_params = YES;
pc = currentask->t_bascode;
if (parse_state != PARSE_PARAMS)
compile (END);
topd = currentask->t_topd;
topcs = currentask->t_topcs;
/* Unlink any added parms. Resetting of topd will
* already have reclaimed space.
*/
if (last_parm) {
last_parm->p_np = NULL;
currentask->t_pfp->pf_lastpp = last_parm;
last_parm = NULL;
}
}
/* Tell user about the syntax error, printing the
* offending line and position if possible.
*/
if (currentask->t_flags & T_SCRIPT) {
if (errmsg != NULL) {
eprintf ("** Syntax error, line %d: %s\n",
currentask->t_scriptln, errmsg);
} else {
eprintf ("** Syntax error, line %d\n",
currentask->t_scriptln);
}
} else
eprintf ("** Syntax error\n");
p_position();
if (!(currentask->t_flags & T_SCRIPT)) {
/* If interactive, we're finished if not within braces.
*/
if (!bracelevel)
YYACCEPT;
}
/* Note that we do not call cl_error() here to abort, but
* continue on parsing the script for more syntax errors.
*/
if (++errcnt > MAX_ERR)
cl_error (E_UERR, "Too many syntax errors.");
}
;
const_expr_list : const_expr
| const_expr DELIM const_expr_list
;
const_expr : const {
if (!errcnt) {
push(stkop($1)) ;
ncaseval++;
}
}
;
/* Use opnl when blank lines are permitted,
* or where a statement may be broken into more
* than one line. The lexical analyzer (actually
* get_command in history.c) ensures that all blank
* lines are deleted. So we don't have to use
* a recursive definition here.
*/
opnl : /* empty */
| NL
;
ref : param {
int dim, d, i1, i2, mode;
/* In command arguments, when not in parentheses
* we just pass the param as a string constant.
*/
if (!errcnt) {
lastref = NO;
if (!inarglist || parenlevel) {
i_oarr = 0;
index_cnt = 0;
strncpy (curr_param, stkop($1)->o_val.v_s,
SZ_FNAME);
/* If a '.' is found in the name we have a
* reference to an external task, or to a
* specific field. In these cases we don't
* want implicit looping.
*/
if (index (curr_param, '.') == NULL) {
if ((dim = get_dim (curr_param)) > 0) {
lastref = YES;
for (d = 0; d < dim; d++) {
getlimits (curr_param, d, &i1, &i2);
mode = make_imloop (i1, i2);
if (mode)
compile (PUSHINDEX, -1);
else
push (compile(PUSHINDEX, 0));
}
n_oarr = dim;
}
}
}
}
}
| param {
if (!errcnt) {
strncpy (curr_param, stkop($1)->o_val.v_s, SZ_FNAME);
index_cnt = 0;
}
}
'[' index_list ']'
{
if (i_oarr > 0 && n_oarr == 0)
n_oarr = i_oarr;
i_oarr = 0;
lastref = YES;
}
;
index_list: index {
index_cnt = 1;
}
| index {
index_cnt++;
}
DELIM index_list
;
index : expr1 {
if (!errcnt)
compile (PUSHINDEX, 0);
}
| ref /* This isn't included in expr1 */
{
if (!errcnt) {
compile (PUSHPARAM, stkop($1)->o_val.v_s);
compile (PUSHINDEX, 0);
}
}
| '*' {
int i1, i2, mode;
if (!errcnt) {
if (index(curr_param, '.') != NULL) {
errmsg = exlimits;
EYYERROR;
}
if (getlimits (curr_param, index_cnt, &i1, &i2)
== ERR) {
eprintf ("Implicit index error for %s.\n",
curr_param);
EYYERROR;
}
mode = make_imloop (i1, i2);
if (mode)
compile (PUSHINDEX, mode);
else
push (compile (PUSHINDEX, mode));
}
}
| Y_CONSTANT {
/* There is an ambiguity in the grammar between
* sexagesimal constants, and array range references.
* Since the sexagesimal constants are recognized
* in the lexical analyzer we can't just change the
* grammar. The kludge around this is to have
* makeop set a flag telling us that the last
* constant it compiled COULD have been an index
* range. We check the flag here and if it is
* set we convert back and compile an implicit loop
* otherwise we just push the constant.
*/
int i1, i2, mode;
if (!errcnt) {
if (maybeindex) {
sexa_to_index (stkop($1)->o_val.v_r, &i1, &i2);
mode = make_imloop (i1, i2);
if (mode)
compile (PUSHINDEX, mode);
else
push (compile (PUSHINDEX, mode));
} else {
compile (PUSHCONST, stkop($1));
compile (PUSHINDEX, 0);
}
}
}
;
/* these are just to make the grammar a bit easier to read.
* can yank them out to shrink parser a bit...
*/
intrins : Y_IDENT {
$$ = $1;
}
;
param : Y_IDENT {
$$ = $1;
}
;
tasknam : Y_IDENT {
$$ = $1;
}
;
EOST : NL
| ';' {
/* If statements are delimited by ';'s, do not execute
* until next newline EOST is received.
*/
sawnl = 0;
}
;
DELIM : ','
;
BARG : /* empty */
| LP
;
EARG : /* empty */
| RP
;
/* These eliminate several interior actions.
*/
LP : '(' { parenlevel++; }
;
RP : ')' { --parenlevel; }
;
NL : Y_NEWLINE { sawnl = 1; }
;
%%
#include "lexyy.c"
#include "lexicon.c"
|