summaryrefslogtreecommitdiff
path: root/ghc/compiler/yaccParser/hsparser-DPH.y
blob: 55749cd24c0dbb4c448193c9c551f4a3a5f6892e (plain)
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
/**************************************************************************
*   File:               hsparser.y                                        *
*                                                                         *
*                       Author:                 Maria M. Gutierrez        *
*                       Modified by:            Kevin Hammond             *
*                       Last date revised:      December 13 1991. KH.     *
*                       Modification:           o Haskell 1.1 Syntax.     *
*						o Data Parallel Syntax.   *
*                                                                         *
*                                                                         *
*   Description:  This file contains the LALR(1) grammar for Haskell.     *
*                                                                         *
*   Entry Point:  module                                                  *
*                                                                         *
*   Problems:     None known.                                             *
*                                                                         *
*                                                                         *
*                 LALR(1) Syntax for Haskell 1.2 + Data Parallelism       *
*                                                                         *
**************************************************************************/


%{
#ifdef DEBUG
# define YYDEBUG 1
#endif

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "hspincl.h"
#include "constants.h"
#include "utils.h"



/**********************************************************************
*                                                                     *
*                                                                     *
*     Imported Variables and Functions                                *
*                                                                     *
*                                                                     *
**********************************************************************/

extern BOOLEAN nonstandardFlag;
extern BOOLEAN expect_ccurly;
extern BOOLEAN etags;

extern BOOLEAN  ispatt PROTO((tree, BOOLEAN));
extern tree 	function PROTO((tree));

static char modname[MODNAME_SIZE];
static char *the_module_name;
static char iface_name[MODNAME_SIZE];
static char interface_filename[FILENAME_SIZE];

static list module_exports;		/* Exported entities */
static list prelude_imports;		/* Entities imported from the Prelude */

extern list all;			/* All valid deriving classes */

extern tree niltree;
extern list Lnil;

extern tree root;

/* For FN, PREVPATT and SAMEFN macros */
extern tree fns[];
extern short samefn[];
extern tree prevpatt[];
extern short icontexts;


/* Line Numbers */
extern int hsplineno;
extern int startlineno;

/**********************************************************************
*                                                                     *
*                                                                     *
*      Fixity and Precedence Declarations                             *
*                                                                     *
*                                                                     *
**********************************************************************/

list fixlist;
static int Fixity = 0, Precedence = 0;
struct infix;

char *ineg();

static BOOLEAN hidden = FALSE;		/*  Set when HIDING used        */

extern BOOLEAN inpat;			/*  True when parsing a pattern */
extern BOOLEAN implicitPrelude;		/*  True when we should read the Prelude if not given */

%}

%union {
	tree utree;
	list ulist;
	ttype uttype;
	atype uatype;
	binding ubinding;
	pbinding upbinding;
	finfot ufinfo;
	impidt uimpid;
	entidt uentid;
	id uid;
	int uint;
	float ufloat;
	char *ustring;
	hpragma uhpragma;
}


/**********************************************************************
*                                                                     *
*                                                                     *
*     These are lexemes.                                              *
*                                                                     *
*                                                                     *
**********************************************************************/


%token 	VARID 		CONID		
	VARSYM		CONSYM		MINUS

%token 	INTEGER 	FLOAT 		CHAR 		STRING
	CHARPRIM	INTPRIM		FLOATPRIM	DOUBLEPRIM
	CLITLIT		VOIDPRIM



/**********************************************************************
*                                                                     *
*                                                                     *
*      Special Symbols                                                *
*                                                                     *
*                                                                     *
**********************************************************************/

%token	OCURLY		CCURLY		VCCURLY		SEMI
%token	OBRACK		CBRACK		OPAREN		CPAREN
%token	COMMA		BQUOTE
%token  OPOD		CPOD 		OPROC 		CPROC


/**********************************************************************
*                                                                     *
*                                                                     *
*     Reserved Operators                                              *
*                                                                     *
*                                                                     *
**********************************************************************/

%token	RARROW
%token	VBAR		EQUAL		DARROW		DOTDOT
%token	DCOLON		LARROW
%token	WILDCARD	AT		LAZY		LAMBDA
%token 	DRAWNFROM	INDEXFROM


/**********************************************************************
*                                                                     *
*                                                                     *
*     Reserved Identifiers                                            *
*                                                                     *
*                                                                     *
**********************************************************************/

%token  LET		IN
%token	WHERE		CASE		OF		
%token	TYPE		DATA		CLASS		INSTANCE	DEFAULT
%token	INFIX		INFIXL		INFIXR		
%token	MODULE		IMPORT		INTERFACE	HIDING
%token	CCALL		CCALL_DANGEROUS	CASM		CASM_DANGEROUS	SCC

%token	IF		THEN		ELSE
%token	RENAMING	DERIVING	TO

/**********************************************************************
*                                                                     *
*                                                                     *
*     Special Symbols for the Lexer                                   *
*                                                                     *
*                                                                     *
**********************************************************************/

%token	LEOF
%token	ARITY_PRAGMA SPECIALIZE_PRAGMA STRICTNESS_PRAGMA UPDATE_PRAGMA
%token  END_PRAGMA

/**********************************************************************
*                                                                     *
*                                                                     *
*     Precedences of the various tokens                               *
*                                                                     *
*                                                                     *
**********************************************************************/


%left	CASE		LET		IN		LAMBDA		
  	IF		ELSE		CCALL		CCALL_DANGEROUS
	CASM		CASM_DANGEROUS	SCC		AT

%left	VARSYM		CONSYM		PLUS		MINUS		BQUOTE

%left	DCOLON

%left	SEMI		COMMA

%left	OCURLY		OBRACK		OPAREN

%left	OPOD		OPROC

%left 	EQUAL

%right	DARROW
%right	RARROW



/**********************************************************************
*                                                                     *
*                                                                     *
*      Type Declarations                                              *
*                                                                     *
*                                                                     *
**********************************************************************/


%type <ulist>   alt alts altrest quals vars varsrest cons
  		tyvars constrs dtypes types atypes
  		exps pats context context_list tyvar_list
		maybeexports export_list
  		impspec maybeimpspec import_list
 		impdecls maybeimpdecls impdecl
  		renaming renamings renaming_list
		tyclses tycls_list
  		gdrhs gdpat valrhs valrhs1
  		lampats
  		upto
		cexp
		tyvar_pids
		parquals
  		pragmas


%type <utree>	exp dexp fexp kexp oexp aexp literal
		tuple list sequence comprehension qual qualrest
  		gd
 		apat bpat pat apatc conpat dpat fpat opat aapat
 		dpatk fpatk opatk aapatk
  		texps
 		processor parqual

%type <uid>	MINUS VARID CONID VARSYM CONSYM
  		var vark con conk varop varop1 conop op op1
		varid conid varsym consym minus plus
		tycls tycon modid ccallid

%type <ubinding>  topdecl topdecls
		  typed datad classd instd defaultd
		  decl decls valdef valdefs sign
  		  iimport iimports maybeiimports
		  ityped idatad iclassd iinstd ivarsd
  		  itopdecl itopdecls
  		  maybe_where
  		  interface readinterface ibody
		  cbody rinst
  		  impdecl_rest

%type <uttype>  simple simple_long type atype btype ttype ntatype inst class
		tyvar	

%type <uatype>	constr

%type <ustring> STRING FLOAT INTEGER CHARPRIM INTPRIM FLOATPRIM DOUBLEPRIM CLITLIT VOIDPRIM
%type <uint>	CHAR
%type <uentid>	export import
%type <uhpragma>  pragma


/**********************************************************************
*                                                                     *
*                                                                     *
*      Start Symbol for the Parser                                    *
*                                                                     *
*                                                                     *
**********************************************************************/

%start pmodule


%%

pmodule	:  readprelude module
	;

module	:  MODULE modid maybeexports 
		{ the_module_name = $2; module_exports = $3; }
	   WHERE body
	|	{ the_module_name = install_literal("Main"); module_exports = Lnil; }
	   body
	;

body	:  ocurly maybeimpdecls maybefixes topdecls ccurly
	       {
		 root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,$4);
	       }
	|  vocurly maybeimpdecls maybefixes topdecls vccurly
	       {
		 root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,$4);
	       }

	|  vocurly impdecls vccurly
	       { 		 
		 root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,mknullbind());
	       }
	|  ocurly impdecls ccurly 
	       { 		 
		 root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,mknullbind());
	       }

/* Adds 1 S/R, 2 R/R conflicts, alternatives add 3 R/R conflicts */
	|  vocurly maybeimpdecls vccurly 
	       { 		 
		 root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,mknullbind());
	       }
	|  ocurly maybeimpdecls ccurly
	       { 		 
		 root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,mknullbind());
	       }
	;


maybeexports :	/* empty */			{ $$ = Lnil; }
	|  OPAREN export_list CPAREN		{ $$ = $2; }
	;

export_list:
	   export				{ $$ = lsing($1); }
  	|  export_list COMMA export		{ $$ = lapp($1,$3); }
	;

export	:
	   var					{ $$ = mkentid($1); }
	|  tycon				{ $$ = mkenttype($1); }
	|  tycon OPAREN DOTDOT CPAREN		{ $$ = mkenttypeall($1); }
	|  tycon OPAREN cons CPAREN		
		{ $$ = mkenttypecons($1,$3); 
		  /* should be a datatype with cons representing all constructors */
		}
	|  tycon OPAREN vars CPAREN		
		{ $$ = mkentclass($1,$3); 
		  /* should be a class with vars representing all Class operations */
		}
	|  tycon OPAREN CPAREN
    	    	{ $$ = mkentclass($1,Lnil);
    	    	  /* "tycon" should be a class with no operations */
		}
	|  tycon DOTDOT
		{ $$ = mkentmod($1);
		  /* "tycon" is a module id (but "modid" is bad for your identifier's health [KH])  */
		}
	;


impspec	:  OPAREN import_list CPAREN		{ $$ = $2; hidden = FALSE; }
	|  HIDING OPAREN import_list CPAREN	{ $$ = $3; hidden = TRUE; }
	|  OPAREN CPAREN			{ $$ = Lnil; hidden = FALSE; }
  	;

maybeimpspec :	/* empty */			{ $$ = Lnil; }
	|  impspec				{ $$ = $1; }
	;

import_list:
	   import				{ $$ = lsing($1); }
  	|  import_list COMMA import		{ $$ = lapp($1,$3); }
	;

import	:
	   var					{ $$ = mkentid($1); }
	|  tycon				{ $$ = mkenttype($1); }
	|  tycon OPAREN DOTDOT CPAREN		{ $$ = mkenttypeall($1); }
	|  tycon OPAREN cons CPAREN
		{ $$ = mkenttypecons($1,$3); 
		  /* should be a datatype with cons representing all constructors */
		}
	|  tycon OPAREN vars CPAREN		
		{ $$ = mkentclass($1,$3);
		  /* should be a class with vars representing all Class operations */
		}
	|  tycon OPAREN CPAREN
    	    	{ $$ = mkentclass($1,Lnil);
    	    	  /* "tycon" should be a class with no operations */
		}
	;


pragmas:
	   pragma { $$ = lsing($1); }
	|  pragmas pragma { $$ = lapp($1,$2); }
	|  /* empty */ { $$ = Lnil; }
	;

pragma:
	   ARITY_PRAGMA	     var EQUAL INTEGER END_PRAGMA
		{ $$ = mkarity_pragma($2,$4); }

	|  SPECIALIZE_PRAGMA var EQUAL ivarsd END_PRAGMA
		{ $$ = mkspecialize_pragma($2, $4); }

	|  STRICTNESS_PRAGMA var EQUAL STRING pragmas END_PRAGMA
		{ $$ = mkstrictness_pragma($2, $4, $5); }

	|  UPDATE_PRAGMA     var EQUAL INTEGER END_PRAGMA
		{ $$ = mkupdate_pragma($2, $4); }
	;


readprelude :   
	        {
		  if ( implicitPrelude ) {
		     find_module_on_imports_dirlist("Prelude",TRUE,interface_filename);
		  } else {
		     find_module_on_imports_dirlist("PreludeNull_",TRUE,interface_filename);
		  }
		  setyyin(interface_filename);
		  enteriscope();
		}
	   readinterface
		{
		  binding prelude = mkimport(installid(iface_name),Lnil,Lnil,$2,xstrdup(interface_filename),hsplineno);
		  prelude_imports = implicitPrelude? lsing(prelude): Lnil; 
		}
	;

maybeimpdecls : /* empty */			{ $$ = Lnil; }
	|  impdecls SEMI			{ $$ = $1; }
	;

impdecls:  impdecl				{ $$ = $1; }
	|  impdecls SEMI impdecl		{ $$ = lconc($1,$3); }
	;

impdecl	:  IMPORT modid
		{ /* filename returned in "interface_filename" */
		  char *module_name = id_to_string($2);
		  find_module_on_imports_dirlist(module_name,FALSE,interface_filename);
		  setyyin(interface_filename);
		  enteriscope();
		  if(strcmp(module_name,"Prelude")==0)
		    prelude_imports = Lnil;
		}
	   impdecl_rest
		{
		  if (hidden)
		    $4->tag = hiding;
		  $$ = lsing($4); 
		}

impdecl_rest:  
	   readinterface maybeimpspec
		{ $$ = mkimport(installid(iface_name),$2,Lnil,$1,xstrdup(interface_filename),hsplineno); }
		/* WDP: uncertain about those hsplinenos */
	|  readinterface maybeimpspec RENAMING renamings
		{ $$ = mkimport(installid(iface_name),$2,$4,$1,xstrdup(interface_filename),hsplineno); }
	;

readinterface:
	   interface LEOF
		{
		  exposeis(); /* partain: expose infix ops at level i+1 to level i */
		  $$ = $1;
		}
	;

renamings: OPAREN renaming_list CPAREN		{ $$ = $2; }
	;

renaming_list: renaming				{ $$ = lsing($1); }
	|  renaming_list COMMA renaming		{ $$ = lapp($1,$3); }
	;

renaming:  var TO var				{ $$ = ldub($1,$3); }
	|  con TO con				{ $$ = ldub($1,$3); }
	;

maybeiimports : /* empty */			{ $$ = mknullbind(); }
	|  iimports SEMI			{ $$ = $1; }
	;

iimports : iimports SEMI iimport		{ $$ = mkabind($1,$3); }
	| iimport				{ $$ = $1; }
	;

iimport :  importkey modid OPAREN import_list CPAREN
		{ $$ = mkmbind($2,$4,Lnil,startlineno); }
	|  importkey modid OPAREN import_list CPAREN RENAMING renamings  
		{ $$ = mkmbind($2,$4,$7,startlineno); }
	;


interface:
  	   INTERFACE modid
		{ fixlist = Lnil;
		  strcpy(iface_name, id_to_string($2));
		}
	   WHERE ibody
		{
		/* WDP: not only do we not check the module name
		   but we take the one in the interface to be what we really want
		   -- we need this for Prelude jiggery-pokery.  (Blech.  KH)
		   ToDo: possibly revert....
		   checkmodname(modname,id_to_string($2));
		*/
		  $$ = $5;
		}
	;


ibody	:  ocurly maybeiimports maybefixes itopdecls ccurly
		{
		  $$ = mkabind($2,$4);
		}
	|  ocurly iimports ccurly
		{
		  $$ = $2;
		}
	|  vocurly maybeiimports maybefixes itopdecls vccurly
		{
		  $$ = mkabind($2,$4);
		}
	|  vocurly iimports vccurly
		{
		  $$ = $2;
		}
  	;

maybefixes:  /* empty */
	|  fixes SEMI
	;


fixes	:  fixes SEMI fix		
	|  fix
	;

fix	:  INFIXL INTEGER
  		{ Precedence = checkfixity($2); Fixity = INFIXL; }
	   ops
	|  INFIXR INTEGER
  		{ Precedence = checkfixity($2); Fixity = INFIXR; }
	   ops
	|  INFIX  INTEGER
  		{ Precedence = checkfixity($2); Fixity = INFIX; }
	   ops
	|  INFIXL
  		{ Fixity = INFIXL; Precedence = 9; }
	   ops
	|  INFIXR
  		{ Fixity = INFIXR; Precedence = 9; }
	   ops
	|  INFIX
  		{ Fixity = INFIX; Precedence = 9; }
	   ops
	;

ops	:  op				{ makeinfix(id_to_string($1),Fixity,Precedence); }
	|  ops COMMA op			{ makeinfix(id_to_string($3),Fixity,Precedence); }
	;

topdecls:  topdecls SEMI topdecl
		{
		  if($1 != NULL)
		    if($3 != NULL)
		      if(SAMEFN)
			{
			  extendfn($1,$3);
			  $$ = $1;
			}
		      else
			$$ = mkabind($1,$3);
		    else
		      $$ = $1;
		  else
		    $$ = $3;
		  SAMEFN = 0;
		}
	|  topdecl
	;

topdecl	:  typed				{ $$ = $1; }
	|  datad 				{ $$ = $1; }
	|  classd 				{ $$ = $1; }
	|  instd 				{ $$ = $1; }
	|  defaultd 				{ $$ = $1; }
	|  decl 				{ $$ = $1; }
	;

typed	:  typekey simple EQUAL type		{ $$ = mknbind($2,$4,startlineno,mkno_pramga()); }
	;

	
datad	:  datakey context DARROW simple EQUAL constrs
		{ $$ = mktbind($2,$4,$6,all,startlineno,mkno_pragma()); }
	|  datakey simple EQUAL constrs
		{ $$ = mktbind(Lnil,$2,$4,all,startlineno,mkno_pragma()); }
	|  datakey context DARROW simple EQUAL constrs DERIVING tyclses
		{ $$ = mktbind($2,$4,$6,$8,startlineno,mkno_pragma()); }
	|  datakey simple EQUAL constrs DERIVING tyclses
		{ $$ = mktbind(Lnil,$2,$4,$6,startlineno,mkno_pragma()); }
	;

classd	:  classkey context DARROW class cbody	{ $$ = mkcbind($2,$4,$5,startlineno,Lnil); }
	|  classkey class cbody		 	{ $$ = mkcbind(Lnil,$2,$3,startlineno,Lnil); }
	;

cbody	:  /* empty */				{ $$ = mknullbind(); }
	|  WHERE ocurly decls ccurly		{ checkorder($3); $$ = $3; }
	|  WHERE vocurly decls vccurly		{ checkorder($3); $$ =$3; }
	;


instd	:  instkey context DARROW tycls inst rinst	{ $$ = mkibind($2,$4,$5,$6,startlineno,Lnil); }
	|  instkey tycls inst rinst		 	{ $$ = mkibind(Lnil,$2,$3,$4,startlineno,Lnil); }
	;

rinst	:  /* empty */				{ $$ = mknullbind(); }
	|  WHERE ocurly valdefs ccurly		{ $$ = $3; }
	|  WHERE vocurly valdefs vccurly	{ $$ = $3; }
	;

inst	:  tycon				{ $$ = mktname($1,Lnil); }
	|  OPAREN simple_long CPAREN		{ $$ = $2; }
    /* partain?: "simple" requires k >= 0, not k > 0 (hence "simple_long" hack) */
	|  OPAREN tyvar_list CPAREN		{ $$ = mkttuple($2); }
	|  OPAREN CPAREN			{ $$ = mkttuple(Lnil); }
	|  OBRACK tyvar CBRACK			{ $$ = mktllist($2); }
	|  OPAREN tyvar RARROW tyvar CPAREN	{ $$ = mktfun($2,$4); }
	|  OPOD tyvar CPOD			{ $$ = mktpod($2); }
	|  OPROC tyvar_pids SEMI tyvar CPROC	{ $$ = mktproc($2,$4); }
	|  OPOD tyvar_pids SEMI tyvar CPOD	{ $$ = mktpod(mktproc($2,$4));}
	|  OPOD OPROC tyvar_pids SEMI tyvar CPROC CPOD	
			{ $$ = mktpod(mktproc($3,$5)); }
	;

/* Note (hilly) : Similar to tyvar_list except k>=1 not k>=2 */

tyvar_pids 	: tyvar COMMA tyvar_pids	{ $$ = mklcons($1,$3); }
		|  tyvar 			{ $$ = lsing($1); }
		;

defaultd:  defaultkey dtypes
	 	{ 
		  $$ = mkdbind($2,startlineno); 
		}
	;

dtypes	:  OPAREN type COMMA types CPAREN	{ $$ = mklcons($2,$4); }
	|  ttype				{ $$ = lsing($1); }
/*	Omitting this forces () to be the *type* (), which never defaults.  This is a KLUDGE */
/*	|  OPAREN CPAREN			{ $$ = Lnil; }*/
	;

decls	:  decls SEMI decl
		{
		  if(SAMEFN)
		    {
		      extendfn($1,$3);
		      $$ = $1;
		    }
		  else
		    $$ = mkabind($1,$3);
		}
	|  decl
	;

/* partain: this "DCOLON context" vs "DCOLON type" is a problem,
    because you can't distinguish between

	foo :: (Baz a, Baz a)
	bar :: (Baz a, Baz a) => [a] -> [a] -> [a]

    with one token of lookahead.  The HACK is to have "DCOLON ttype"
    [tuple type] in the first case, then check that it has the right
    form C a, or (C1 a, C2 b, ... Cn z) and convert it into a
    context.  Blaach!
    (FIXED 90/06/06)
*/

decl	:  vars DCOLON type DARROW type iclasop_pragma
		{ /* type2context.c for code */
		  $$ = mksbind($1,mkcontext(type2context($3),$5),startlineno,$6);
		  PREVPATT = NULL;
		  FN = NULL;
		  SAMEFN = 0;
		}
	|  sign
	|  valdef
	|  /* empty */			{ $$ = mknullbind(); PREVPATT = NULL; FN = NULL; SAMEFN = 0; }
  	;

sign	:  vars DCOLON type iclasop_pragma
		{
		  $$ = mksbind($1,$3,startlineno,$4);
		  PREVPATT = NULL;
		  FN = NULL;
		  SAMEFN = 0;
		}
	;



itopdecls : itopdecls SEMI itopdecl		{ $$ = mkabind($1,$3); }
	| itopdecl				{ $$ = $1; }
  	;

itopdecl:  ityped	 			{ $$ = $1; }
	|  idatad 				{ $$ = $1; }
	|  iclassd 				{ $$ = $1; }
	|  iinstd 				{ $$ = $1; }
	|  ivarsd				{ $$ = $1; }
	|  /* empty */				{ $$ = mknullbind(); }
	;

   	/* partain: see comment elsewhere about why "type", not "context" */
ivarsd	:  vars DCOLON type DARROW type ival_pragma
		{ $$ = mksbind($1,mkcontext(type2context($3),$5),startlineno,$6); }
	|  vars DCOLON type ival_pragma
		{ $$ = mksbind($1,$3,startlineno,$4); }
	;

ityped	:  typekey simple EQUAL type itype_pragma { $$ = mknbind($2,$4,startlineno,$5); }
	;
	
idatad	:  datakey context DARROW simple idata_pragma			{ $$ = mktbind($2,$4,Lnil,Lnil,startlineno,$5); }
  	|  datakey simple idata_pragma					{ $$ = mktbind(Lnil,$2,Lnil,Lnil,startlineno,$3); }
	|  datakey context DARROW simple EQUAL constrs 			{ $$ = mktbind($2,$4,$6,Lnil,startlineno,mk_nopragma()); }
	|  datakey simple EQUAL constrs					{ $$ = mktbind(Lnil,$2,$4,Lnil,startlineno,mk_nopragma()); }
	|  datakey context DARROW simple EQUAL constrs DERIVING tyclses	{ $$ = mktbind($2,$4,$6,$8,startlineno,mk_nopragma()); }
	|  datakey simple EQUAL constrs DERIVING tyclses		{ $$ = mktbind(Lnil,$2,$4,$6,startlineno,mk_nopragma()); }
	;


iclassd	:  classkey context DARROW class cbody pragmas
		{ $$ = mkcbind($2,$4,$5,startlineno,$6); }
	|  classkey class cbody	pragmas
		{ $$ = mkcbind(Lnil,$2,$3,startlineno,$4); }
	;

iinstd	:  instkey context DARROW tycls inst pragmas
		{ $$ = mkibind($2,$4,$5,mknullbind(),startlineno,$6); }
	|  instkey tycls inst pragmas
		{ $$ = mkibind(Lnil,$2,$3,mknullbind(),startlineno,$4); }
	;


/* obsolete: "(C a, ...)" cause r/r conflict, resolved in favour of context rather than type */

class	:  tycon tyvar 				{ $$ = mktname($1,lsing($2)); }
    	    /* partain: changed "tycls" to "tycon" */
	;

types	:  types COMMA type			{ $$ = lapp($1,$3); }
	|  type					{ $$ = lsing($1); }
	;

type	:  btype				{ $$ = $1; }
	|  btype RARROW type			{ $$ = mktfun($1,$3); }

btype	:  atype				{ $$ = $1; }
	|  tycon atypes				{ $$ = mktname($1,$2); }
	;

atypes	:  atypes atype				{ $$ = lapp($1,$2); }
	|  atype				{ $$ = lsing($1); }
	;

/* The split with ntatype allows us to use the same syntax for defaults as for types */
ttype	:  ntatype				{ $$ = $1; }
	|  btype RARROW type			{ $$ = mktfun($1,$3); }
	|  tycon atypes				{ $$ = mktname($1,$2); }
	;

atype	:  ntatype
	|  OPAREN type COMMA types CPAREN	{ $$ = mkttuple(mklcons($2,$4)); }
	;

ntatype	:  tyvar				{ $$ = $1; }
  	|  tycon				{ $$ = mktname($1,Lnil); }
	|  OPAREN CPAREN			{ $$ = mkttuple(Lnil); }
	|  OPAREN type CPAREN			{ $$ = $2; }
	|  OBRACK type CBRACK			{ $$ = mktllist($2); }
	|  OPOD type CPOD			{ $$ = mktpod($2); }
	|  OPROC types SEMI type CPROC		{ $$ = mktproc($2,$4); }
	|  OPOD types SEMI type CPOD		{ $$ = mktpod(mktproc($2,$4));}
	;
	

simple	:  tycon		 		{ $$ = mktname($1,Lnil); }
	|  tycon tyvars		 		{ $$ = mktname($1,$2); }
	;


simple_long : tycon tyvars	 		{ $$ = mktname($1,$2); }
	; /* partain: see comment in "inst" */


constrs	:  constrs VBAR constr			{ $$ = lapp($1,$3); }
	|  constr				{ $$ = lsing($1); }
	;

/* Using tycon rather than con avoids 5 S/R errors */
constr	:  tycon atypes				{ $$ = mkatc($1,$2,hsplineno); }
	|  OPAREN consym CPAREN atypes		{ $$ = mkatc($2,$4,hsplineno); }
	|  tycon				{ $$ = mkatc($1,Lnil,hsplineno); }
	|  OPAREN consym CPAREN			{ $$ = mkatc($2,Lnil,hsplineno); }
	|  btype conop btype			{ $$ = mkatc($2, ldub($1,$3), hsplineno); }
	;

tyclses	:  OPAREN tycls_list CPAREN		{ $$ = $2; }
	|  OPAREN CPAREN			{ $$ = Lnil; }
	|  tycls				{ $$ = lsing($1); }
	;

tycls_list:  tycls COMMA tycls_list		{ $$ = mklcons($1,$3); }
	|  tycls				{ $$ = lsing($1); }
	;

context	:  OPAREN context_list CPAREN		{ $$ = $2; }
	|  class				{ $$ = lsing($1); }
	;

context_list:  class COMMA context_list		{ $$ = mklcons($1,$3); }
	|  class				{ $$ = lsing($1); }
	;

valdefs	:  valdefs SEMI valdef
		{
		  if(SAMEFN)
		    {
		      extendfn($1,$3);
		      $$ = $1;
		    }
		  else
		    $$ = mkabind($1,$3);
		}
	|  valdef				{ $$ = $1; }
	|  /* empty */				{ $$ = mknullbind(); }
	;


vars	:  vark COMMA varsrest			{ $$ = mklcons($1,$3); }
	|  vark					{ $$ = lsing($1); }
	;

varsrest:  varsrest COMMA var	 		{ $$ = lapp($1,$3); }
	|  var					{ $$ = lsing($1); }
	;

cons	:  cons COMMA con	 		{ $$ = lapp($1,$3); }
	|  con					{ $$ = lsing($1); }
	;


valdef	:  opatk
		{
		  tree fn = function($1);

		  PREVPATT = $1;

		  if(ttree(fn) == ident)
		    {
		      checksamefn(gident(fn));
		      FN = fn;
		    }

		  else if (ttree(fn) == tinfixop && ttree(ginfun((struct Sap *) fn)) == ident)
		    {
		      checksamefn(gident(ginfun((struct Sap *) fn)));
		      FN = ginfun((struct Sap *) fn);
		    }

		  else if(etags)
		    printf("%u\n",startlineno);
		}
	   valrhs
	  	{
		  if(ispatt($1,TRUE))
		    {
		      $$ = mkpbind($3, startlineno);
		      FN = NULL;
		      SAMEFN = 0;
		    }
		  else
		    $$ = mkfbind($3,startlineno);

		  PREVPATT = NULL;
		}
	;

valrhs	:  valrhs1 maybe_where			{ $$ = lsing(createpat($1, $2)); }
	;

valrhs1	:  gdrhs
	|  EQUAL exp				{ $$ = lsing(mktruecase($2)); }
	;

gdrhs	:  gd EQUAL exp				{ $$ = lsing(ldub($1,$3)); }
	|  gd EQUAL exp gdrhs			{ $$ = mklcons(ldub($1,$3),$4); }
	;

maybe_where:
	   WHERE ocurly decls ccurly		{ $$ = $3; }
	|  WHERE vocurly decls vccurly		{ $$ = $3; }
	|  /* empty */				{ $$ = mknullbind(); }
	;

gd	:  VBAR oexp				{ $$ = $2; }
	;


lampats	:  apat lampats				{ $$ = mklcons($1,$2); }
	|  apat					{ $$ = lsing($1); }
	;


/*
  	Changed as above to allow for contexts!
	KH@21/12/92
*/


exp	:  oexp DCOLON type DARROW type		{ $$ = mkrestr($1,mkcontext(type2context($3),$5)); }
	|  oexp DCOLON type	  		{ $$ = mkrestr($1,$3); }
	|  oexp
	;

/*
  Operators must be left-associative  at the same precedence 
  for prec. parsing to work.
*/

 	/* Infix operator application */
oexp	:  dexp
	|  oexp op oexp %prec PLUS
		{ $$ = mkinfixop($2,$1,$3); precparse($$); }
	;

/*
  This comes here because of the funny precedence rules concerning
  prefix minus.
*/


dexp	:  MINUS kexp				{ $$ = mknegate($2); }
	|  kexp
	;  

/*
  let/if/lambda/case have higher precedence than infix operators.
*/

kexp	:  LAMBDA
		{ /* enteriscope(); /? I don't understand this -- KH */
		  hsincindent();    /* added by partain; push new context for */
				    /* FN = NULL; not actually concerned about */
		  FN = NULL;	    /* indenting */
		  $<uint>$ = hsplineno; /* remember current line number */
		}
	   lampats
  		{ hsendindent();    /* added by partain */
		  /* exitiscope();	    /? Also not understood */
		}
	   RARROW exp	/* lambda abstraction */
		{
		  $$ = mklambda($3, $6, $<uint>2);
		}

	/* Let Expression */
	|  LET ocurly decls ccurly IN exp	{ $$ = mklet($3,$6); }	  
	|  LET vocurly decls vccurly IN exp	{ $$ = mklet($3,$6); }	  

	/* If Expression */
	|  IF exp THEN exp ELSE exp		{ $$ = mkife($2,$4,$6); }

	/* Case Expression */
	|  CASE exp OF ocurly alts ccurly	{ $$ = mkcasee($2,$5); }
	|  CASE exp OF vocurly alts vccurly	{ $$ = mkcasee($2,$5); }

	/* CCALL/CASM Expression */
	|  CCALL ccallid cexp			{ $$ = mkccall($2,installid("n"),$3); }
	|  CCALL ccallid			{ $$ = mkccall($2,installid("n"),Lnil); }
	|  CCALL_DANGEROUS ccallid cexp		{ $$ = mkccall($2,installid("p"),$3); }
	|  CCALL_DANGEROUS ccallid		{ $$ = mkccall($2,installid("p"),Lnil); }
	|  CASM CLITLIT cexp			{ $$ = mkccall($2,installid("N"),$3); }
	|  CASM CLITLIT				{ $$ = mkccall($2,installid("N"),Lnil); }
	|  CASM_DANGEROUS CLITLIT cexp		{ $$ = mkccall($2,installid("P"),$3); }
	|  CASM_DANGEROUS CLITLIT		{ $$ = mkccall($2,installid("P"),Lnil); }

	/* SCC Expression */
	|  SCC STRING exp
		{ extern BOOLEAN ignoreSCC;
		  extern BOOLEAN warnSCC;
		  extern char * input_filename;

		  if (ignoreSCC) {
		    if (warnSCC) 
			fprintf(stderr,
				"\"%s\", line %d: scc (`set [profiling] cost centre') ignored\n",
			    	input_filename, hsplineno);
		    $$ = $3;
		  } else {
		    $$ = mkscc($2, $3);
		  }
		}
	|  fexp
  	;



	/* Function application */
fexp	:  fexp aexp				{ $$ = mkap($1,$2); }
	|  aexp
	;

cexp	:  cexp aexp				{ $$ = lapp($1,$2); }
	|  aexp					{ $$ = lsing($1); }
	;


/*
   The mkpars are so that infix parsing doesn't get confused.

   KH.
*/

	/* Simple Expressions */
aexp	:  var					{ $$ = mkident($1); }
	|  con		 			{ $$ = mkident($1); }
	|  literal	
	|  OPAREN exp CPAREN			{ $$ = mkpar($2); }
	|  OPAREN oexp op CPAREN		{ checkprec($2,$3,FALSE); $$ = mklsection($2,$3); }
	|  OPAREN op1 oexp CPAREN		{ checkprec($3,$2,TRUE);  $$ = mkrsection($2,$3); }

	/* structures */
	|  tuple
	|  list					{ $$ = mkpar($1); }
	|  sequence				{ $$ = mkpar($1); }
	|  comprehension			{ $$ = mkpar($1); }
	|  OPOD exp VBAR parquals CPOD		{ $$ = mkparzf($2,$4); }
	|  OPOD exps CPOD			{ $$ = mkpod($2); }
	|  processor				{ $$ = mkpar($1); }

	/* These only occur in patterns */
	|  var AT aexp				{ checkinpat();  $$ = mkas($1,$3); } 
	|  WILDCARD				{ checkinpat();  $$ = mkwildp();   }
	|  LAZY aexp				{ checkinpat();  $$ = mklazyp($2); }
	;


processor :  OPROC exps SEMI exp CPROC		{ $$ = mkproc($2,$4); }
	  ;

parquals  :  parquals COMMA parqual		{ $$ = lapp($1,$3); }
	  |  parqual				{ $$ = lsing($1); }
	  ;

parqual  : exp					{ $$ = mkparfilt($1); }
	  | processor  DRAWNFROM exp	
		{	$$ = mkpardgen($1,$3); 
			checkpatt($1);
		}
	  | processor  INDEXFROM exp	
		{       $$ = mkparigen($1,$3); 
			checkpatt(gprocdata($1));
		}
	  ;


/*
	LHS patterns are parsed in a similar way to
	expressions.  This avoids the horrible non-LRness
	which occurs with the 1.1 syntax.

	The xpatk business is to do with accurately recording
	the starting line for definitions.
*/

/*TESTTEST
bind	:  opatk
	|  vark lampats
		{ $$ = mkap($1,$2); }
	|  opatk varop opat %prec PLUS
		{
		  $$ = mkinfixop($2,$1,$3);
		}
	;

opatk	:  dpatk
	|  opatk conop opat %prec PLUS
		{
		  $$ = mkinfixop($2,$1,$3);
		  precparse($$);
		}
	;

*/

opatk	:  dpatk
	|  opatk op opat %prec PLUS
		{
		  $$ = mkinfixop($2,$1,$3);

		  if(isconstr(id_to_string($2)))
		    precparse($$);
		  else 
		    {
		      checkprec($1,$2,FALSE);	/* Check the precedence of the left pattern */
		      checkprec($3,$2,TRUE);	/* then check the right pattern */
		    }
		}
	;

opat	:  dpat
	|  opat op opat %prec PLUS
		{
		  $$ = mkinfixop($2,$1,$3);

		  if(isconstr(id_to_string($2)))
		    precparse($$);
		  else 
		    {
		      checkprec($1,$2,FALSE);	/* Check the precedence of the left pattern */
		      checkprec($3,$2,TRUE);	/* then check the right pattern */
		    }
		}
	;

/*
  This comes here because of the funny precedence rules concerning
  prefix minus.
*/


dpat	:  MINUS fpat					{ $$ = mknegate($2); }
	|  fpat
	;  

	/* Function application */
fpat	:  fpat aapat					{ $$ = mkap($1,$2); }
	|  aapat
	;

dpatk	:  minuskey fpat				{ $$ = mknegate($2); }
	|  fpatk
	;  

	/* Function application */
fpatk	:  fpatk aapat					{ $$ = mkap($1,$2); }
	|  aapatk
	;

aapat	:  con		 				{ $$ = mkident($1); }
	|  var		 				{ $$ = mkident($1); }
	|  var AT apat			 		{ $$ = mkas($1,$3); }
	|  literal					{ $$ = $1; }
	|  WILDCARD					{ $$ = mkwildp(); }
	|  OPAREN CPAREN				{ $$ = mktuple(Lnil); }
	|  OPAREN var PLUS INTEGER CPAREN		{ $$ = mkplusp(mkident($2),mkinteger($4)); }
	|  OPAREN WILDCARD PLUS INTEGER CPAREN		{ $$ = mkplusp(mkwildp(),mkinteger($4)); }
	|  OPAREN opat CPAREN				{ $$ = mkpar($2); }
	|  OPAREN opat COMMA pats CPAREN 		{ $$ = mktuple(mklcons($2,$4)); }
	|  OBRACK pats CBRACK				{ $$ = mkllist($2); }
	|  OBRACK CBRACK				{ $$ = mkllist(Lnil); }
	|  LAZY apat					{ $$ = mklazyp($2); }
        |  OPROC pats SEMI apat CPROC			{ $$ = mkproc($2,$4); }
	;

aapatk	:  conk		 				{ $$ = mkident($1); }
	|  vark		 				{ $$ = mkident($1); }
	|  vark AT apat			 		{ $$ = mkas($1,$3); }
	|  literal					{ $$ = $1; setstartlineno(); }
	|  WILDCARD					{ $$ = mkwildp(); setstartlineno(); }
	|  oparenkey CPAREN				{ $$ = mktuple(Lnil); }
	|  oparenkey var PLUS INTEGER CPAREN		{ $$ = mkplusp(mkident($2),mkinteger($4)); }
	|  oparenkey WILDCARD PLUS INTEGER CPAREN	{ $$ = mkplusp(mkwildp(),mkinteger($4)); }
	|  oparenkey opat CPAREN			{ $$ = mkpar($2); }
	|  oparenkey opat COMMA pats CPAREN 		{ $$ = mktuple(mklcons($2,$4)); }
	|  obrackkey pats CBRACK			{ $$ = mkllist($2); }
	|  obrackkey CBRACK				{ $$ = mkllist(Lnil); }
	|  lazykey apat					{ $$ = mklazyp($2); }
        |  oprockey pats SEMI opat CPROC		{ $$ = mkproc($2,$4); }
	;


/*
   The mkpars are so that infix parsing doesn't get confused.

   KH.
*/

tuple	:  OPAREN exp COMMA texps CPAREN
  		{ if (ttree($4) == tuple)
		    $$ = mktuple(mklcons($2, gtuplelist($4)));
		else
		  $$ = mktuple(ldub($2, $4));
		}
	|  OPAREN CPAREN
		{ $$ = mktuple(Lnil); }
	;

texps	:  exp COMMA texps
		{ if (ttree($3) == tuple)
		    $$ = mktuple(mklcons($1, gtuplelist($3)));
		else
		  $$ = mktuple(ldub($1, $3));
		}
	|  exp					{ $$ = mkpar($1); }
	;


list	:  OBRACK CBRACK  			{ $$ = mkllist(Lnil); }
  	|  OBRACK exps CBRACK			{ $$ = mkllist($2); }
	;

exps	:  exp COMMA exps			{ $$ = mklcons($1,$3); }
	|  exp					{ $$ = lsing($1); }
	;


sequence:  OBRACK exp COMMA exp DOTDOT upto CBRACK	{$$ = mkeenum($2,lsing($4),$6);}
	|  OBRACK exp DOTDOT upto CBRACK	{ $$ = mkeenum($2,Lnil,$4); }
	;

comprehension:  OBRACK exp VBAR quals CBRACK	{ $$ = mkcomprh($2,$4); }
	;

quals	:  quals COMMA qual			{ $$ = lapp($1,$3); }
	|  qual					{ $$ = lsing($1); }
	;

qual	:  	{ inpat = TRUE; } exp { inpat = FALSE; } qualrest
		{ if ($4 == NULL)
		    $$ = mkguard($2);
		  else 
		    {
		      checkpatt($2);
		      if(ttree($4)==def)
			{
			  tree prevpatt_save = PREVPATT;
			  PREVPATT = $2;
			  $$ = mkdef(mkpbind(lsing(createpat(lsing(mktruecase((tree)(ggdef($4)))),mknullbind())),hsplineno));
			  PREVPATT = prevpatt_save;
			}
		      else
			$$ = mkqual($2,$4);
		    }
		}
	;

qualrest:  LARROW exp				{ $$ = $2; }
/* OLD:
	|  EQUAL exp
		{ if(nonstandardFlag)
		    $$ = mkdef($2); 
		  else
		    hsperror("Definitions in comprehensions are not standard Haskell");
		}
*/
        |  /* empty */				{ $$ = NULL; }
	;


alts	:  alts SEMI alt			{ $$ = lconc($1,$3); }
	|  alt					{ $$ = $1; }
	;

alt	:  pat
		{ PREVPATT = $1; }
	   altrest
		{ $$ = $3;
		  PREVPATT = NULL;
		}
	|  /* empty */				{ $$ = Lnil; }
	;

altrest	:  gdpat maybe_where	 		{ $$ = lsing(createpat($1,$2)); }
	|  RARROW exp maybe_where		{ $$ = lsing(createpat(lsing(mktruecase($2)),$3)); }
	;

gdpat	:  gd RARROW exp gdpat			{ $$ = mklcons(ldub($1,$3),$4);  }
	|  gd RARROW exp			{ $$ = lsing(ldub($1,$3)); }
	;

upto	:  /* empty */				{ $$ = Lnil; }
	|  exp					{ $$ = lsing($1); }
	;

pats	:  pat COMMA pats			{ $$ = mklcons($1,$3); }
	|  pat					{ $$ = lsing($1); }
	;

pat	:  bpat
	|  pat conop bpat			{ $$ = mkinfixop($2,$1,$3); precparse($$); }
	;

bpat	:  apatc
 	|  conpat
	|  MINUS INTEGER			{ $$ = mkinteger(ineg($2)); }
	|  MINUS FLOAT				{ $$ = mkfloatr(ineg($2)); }
	;

conpat	:  con					{ $$ = mkident($1); }
	|  conpat apat				{ $$ = mkap($1,$2); }
	;

apat	:  con		 			{ $$ = mkident($1); }
	|  apatc
	;

apatc	:  var		 			{ $$ = mkident($1); }
	|  var AT apat			 	{ $$ = mkas($1,$3); }
	|  literal				{ $$ = $1; }
	|  WILDCARD				{ $$ = mkwildp(); }
	|  OPAREN CPAREN			{ $$ = mktuple(Lnil); }
	|  OPAREN var PLUS INTEGER CPAREN	{ $$ = mkplusp(mkident($2),mkinteger($4)); }
	|  OPAREN WILDCARD PLUS INTEGER CPAREN	{ $$ = mkplusp(mkwildp(),mkinteger($4)); }
	|  OPAREN pat CPAREN			{ $$ = mkpar($2); }
	|  OPAREN pat COMMA pats CPAREN 	{ $$ = mktuple(mklcons($2,$4)); }
	|  OBRACK pats CBRACK			{ $$ = mkllist($2); }
	|  OBRACK CBRACK			{ $$ = mkllist(Lnil); }
	|  LAZY apat				{ $$ = mklazyp($2); }
        |  OPROC pats SEMI apat CPROC		{ $$ = mkproc($2,$4); }
	;

/*
patk	:  bpatk
	|  patk conop bpat			{ $$ = mkinfixop($2,$1,$3); precparse($$); }
	;

bpatk	:  apatck
 	|  conpatk
	|  minuskey INTEGER			{ $$ = mkinteger(ineg($2)); }
	|  minuskey FLOAT			{ $$ = mkfloatr(ineg($2)); }
	;

conpatk	:  conk					{ $$ = mkident($1); }
	|  conpatk apat				{ $$ = mkap($1,$2); }
	;

apatck	:  vark		 			{ $$ = mkident($1); }
	|  vark AT apat			 	{ $$ = mkas($1,$3); }
	|  literal				{ $$ = $1; setstartlineno(); }
	|  WILDCARD				{ $$ = mkwildp(); setstartlineno(); }
	|  oparenkey CPAREN			{ $$ = mktuple(Lnil); }
	|  oparenkey var PLUS INTEGER CPAREN	{ $$ = mkplusp(mkident($2),mkinteger($4)); }
	|  oparenkey WILDCARD PLUS INTEGER CPAREN	{ $$ = mkplusp(mkwildp(),mkinteger($4)); }
	|  oparenkey pat CPAREN			{ $$ = mkpar($2); }
	|  oparenkey pat COMMA pats CPAREN 	{ $$ = mktuple(mklcons($2,$4)); }
	|  obrackkey pats CBRACK			{ $$ = mkllist($2); }
	|  obrackkey CBRACK			{ $$ = mkllist(Lnil); }
	|  lazykey apat				{ $$ = mklazyp($2); }
        |  oprockey pats SEMI opat CPROC		{ $$ = mkproc($2,$4); }
	;
*/

literal	:  INTEGER				{ $$ = mkinteger($1); }
	|  FLOAT				{ $$ = mkfloatr($1); }
	|  CHAR					{ $$ = mkcharr($1); }
	|  STRING				{ $$ = mkstring($1); }
	|  CHARPRIM				{ $$ = mkcharprim($1); }
	|  INTPRIM				{ $$ = mkintprim($1); }
	|  FLOATPRIM				{ $$ = mkfloatprim($1); }
	|  DOUBLEPRIM				{ $$ = mkdoubleprim($1); }
	|  CLITLIT				{ $$ = mkclitlit($1); }
	|  VOIDPRIM				{ $$ = mkvoidprim(); }
	;


/* Keywords which record the line start */

importkey:  IMPORT	{ setstartlineno(); }
	;

datakey	:   DATA	{ setstartlineno();
			  if(etags)
			    printf("%u\n",startlineno);
			} 
	;

typekey	:   TYPE	{ setstartlineno();
			  if(etags)
			    printf("%u\n",startlineno);
			} 
	;

instkey	:   INSTANCE	{ setstartlineno();
			  if(etags)
			    printf("%u\n",startlineno);
			} 
	;

defaultkey: DEFAULT	{ setstartlineno(); }
	;

classkey:   CLASS	{ setstartlineno();
			  if(etags)
			    printf("%u\n",startlineno);
			}
	;

minuskey:   MINUS	{ setstartlineno(); }
	;

oparenkey:  OPAREN	{ setstartlineno(); }
	;

obrackkey:  OBRACK	{ setstartlineno(); }
	;

lazykey	:   LAZY	{ setstartlineno(); }
	;

oprockey:   OPROC	{ setstartlineno(); }
	;


/* Non "-" op, used in right sections -- KH */
op1	:  conop
	|  varop1
  	;

op	:  conop
	|  varop
  	;

varop	:  varsym
	|  BQUOTE varid BQUOTE		{ $$ = $2; }
	;

/*	Non-minus varop, used in right sections */
varop1	:  VARSYM
	|  plus
	|  BQUOTE varid BQUOTE		{ $$ = $2; }
	;

conop	:  consym
	|  BQUOTE conid BQUOTE		{ $$ = $2; }
	;

consym	:  CONSYM
	;

varsym	:  VARSYM
	|  plus
	|  minus
	;

minus	:  MINUS			{ $$ = install_literal("-"); }
	;

plus	:  PLUS 			{ $$ = install_literal("+"); }
	;

var	:  VARID
	|  OPAREN varsym CPAREN		{ $$ = $2; }
	;

vark	:  VARID			{ setstartlineno(); $$ = $1; }
	|  oparenkey varsym CPAREN	{ $$ = $2; }
	;

/* tycon used here to eliminate 11 spurious R/R errors -- KH */
con	:  tycon
	|  OPAREN consym CPAREN		{ $$ = $2; }
	;

conk	:  tycon			{ setstartlineno(); $$ = $1; }
	|  oparenkey consym CPAREN	{ $$ = $2; }
	;

varid	:  VARID
	;

conid	:  CONID
	;

ccallid	:  varid
	|  conid
	;

/* partain: "tyvar_list" must be at least 2 elements long (defn of "inst") */
tyvar_list: tyvar COMMA tyvar_list		{ $$ = mklcons($1,$3); }
	|  tyvar COMMA tyvar			{ $$ = mklcons($1,lsing($3)); }
	;

tyvars	:  tyvar tyvars				{ $$ = mklcons($1,$2); }
	|  tyvar				{ $$ = lsing($1); }
	;

tyvar	:  VARID				{ $$ = mknamedtvar($1); }
	;

tycls	:  tycon
		/* partain: "aconid"->"tycon" got rid of a r/r conflict
		    (and introduced >= 2 s/r's ...)
	         */
	;

tycon	:  conid
	;

modid	:  CONID
	;


ocurly	: layout OCURLY				{ hsincindent(); }

vocurly	: layout				{ hssetindent(); }
	;

layout	: 					{ hsindentoff(); }
	;

ccurly	:
	 CCURLY
		{
		  FN = NULL; SAMEFN = 0; PREVPATT = NULL;
		  hsendindent();
		}
	;

vccurly	:  { expect_ccurly = 1; }  vccurly1  { expect_ccurly = 0; }
	;

vccurly1:
	 VCCURLY
		{
		  FN = NULL; SAMEFN = 0; PREVPATT = NULL;
		  hsendindent();
		}
	| error
		{
		  yyerrok;
		  FN = NULL; SAMEFN = 0; PREVPATT = NULL;
		  hsendindent();
		}
	;
        
%%