[project @ 1996-03-22 09:24:22 by partain]
[ghc-hetmet.git] / ghc / compiler / yaccParser / hsparser-DPH.y
1 /**************************************************************************
2 *   File:               hsparser.y                                        *
3 *                                                                         *
4 *                       Author:                 Maria M. Gutierrez        *
5 *                       Modified by:            Kevin Hammond             *
6 *                       Last date revised:      December 13 1991. KH.     *
7 *                       Modification:           o Haskell 1.1 Syntax.     *
8 *                                               o Data Parallel Syntax.   *
9 *                                                                         *
10 *                                                                         *
11 *   Description:  This file contains the LALR(1) grammar for Haskell.     *
12 *                                                                         *
13 *   Entry Point:  module                                                  *
14 *                                                                         *
15 *   Problems:     None known.                                             *
16 *                                                                         *
17 *                                                                         *
18 *                 LALR(1) Syntax for Haskell 1.2 + Data Parallelism       *
19 *                                                                         *
20 **************************************************************************/
21
22
23 %{
24 #ifdef DEBUG
25 # define YYDEBUG 1
26 #endif
27
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include "hspincl.h"
32 #include "constants.h"
33 #include "utils.h"
34
35
36
37 /**********************************************************************
38 *                                                                     *
39 *                                                                     *
40 *     Imported Variables and Functions                                *
41 *                                                                     *
42 *                                                                     *
43 **********************************************************************/
44
45 extern BOOLEAN nonstandardFlag;
46 extern BOOLEAN expect_ccurly;
47 extern BOOLEAN etags;
48
49 extern BOOLEAN  ispatt PROTO((tree, BOOLEAN));
50 extern tree     function PROTO((tree));
51
52 static char modname[MODNAME_SIZE];
53 static char *the_module_name;
54 static char iface_name[MODNAME_SIZE];
55 static char interface_filename[FILENAME_SIZE];
56
57 static list module_exports;             /* Exported entities */
58 static list prelude_imports;            /* Entities imported from the Prelude */
59
60 extern list all;                        /* All valid deriving classes */
61
62 extern tree niltree;
63 extern list Lnil;
64
65 extern tree root;
66
67 /* For FN, PREVPATT and SAMEFN macros */
68 extern tree fns[];
69 extern short samefn[];
70 extern tree prevpatt[];
71 extern short icontexts;
72
73
74 /* Line Numbers */
75 extern int hsplineno;
76 extern int startlineno;
77
78 /**********************************************************************
79 *                                                                     *
80 *                                                                     *
81 *      Fixity and Precedence Declarations                             *
82 *                                                                     *
83 *                                                                     *
84 **********************************************************************/
85
86 list fixlist;
87 static int Fixity = 0, Precedence = 0;
88 struct infix;
89
90 char *ineg();
91
92 static BOOLEAN hidden = FALSE;          /*  Set when HIDING used        */
93
94 extern BOOLEAN inpat;                   /*  True when parsing a pattern */
95 extern BOOLEAN implicitPrelude;         /*  True when we should read the Prelude if not given */
96
97 %}
98
99 %union {
100         tree utree;
101         list ulist;
102         ttype uttype;
103         atype uatype;
104         binding ubinding;
105         pbinding upbinding;
106         finfot ufinfo;
107         impidt uimpid;
108         entidt uentid;
109         id uid;
110         int uint;
111         float ufloat;
112         char *ustring;
113         hpragma uhpragma;
114 }
115
116
117 /**********************************************************************
118 *                                                                     *
119 *                                                                     *
120 *     These are lexemes.                                              *
121 *                                                                     *
122 *                                                                     *
123 **********************************************************************/
124
125
126 %token  VARID           CONID           
127         VARSYM          CONSYM          MINUS
128
129 %token  INTEGER         FLOAT           CHAR            STRING
130         CHARPRIM        INTPRIM         FLOATPRIM       DOUBLEPRIM
131         CLITLIT         VOIDPRIM
132
133
134
135 /**********************************************************************
136 *                                                                     *
137 *                                                                     *
138 *      Special Symbols                                                *
139 *                                                                     *
140 *                                                                     *
141 **********************************************************************/
142
143 %token  OCURLY          CCURLY          VCCURLY         SEMI
144 %token  OBRACK          CBRACK          OPAREN          CPAREN
145 %token  COMMA           BQUOTE
146 %token  OPOD            CPOD            OPROC           CPROC
147
148
149 /**********************************************************************
150 *                                                                     *
151 *                                                                     *
152 *     Reserved Operators                                              *
153 *                                                                     *
154 *                                                                     *
155 **********************************************************************/
156
157 %token  RARROW
158 %token  VBAR            EQUAL           DARROW          DOTDOT
159 %token  DCOLON          LARROW
160 %token  WILDCARD        AT              LAZY            LAMBDA
161 %token  DRAWNFROM       INDEXFROM
162
163
164 /**********************************************************************
165 *                                                                     *
166 *                                                                     *
167 *     Reserved Identifiers                                            *
168 *                                                                     *
169 *                                                                     *
170 **********************************************************************/
171
172 %token  LET             IN
173 %token  WHERE           CASE            OF              
174 %token  TYPE            DATA            CLASS           INSTANCE        DEFAULT
175 %token  INFIX           INFIXL          INFIXR          
176 %token  MODULE          IMPORT          INTERFACE       HIDING
177 %token  CCALL           CCALL_DANGEROUS CASM            CASM_DANGEROUS  SCC
178
179 %token  IF              THEN            ELSE
180 %token  RENAMING        DERIVING        TO
181
182 /**********************************************************************
183 *                                                                     *
184 *                                                                     *
185 *     Special Symbols for the Lexer                                   *
186 *                                                                     *
187 *                                                                     *
188 **********************************************************************/
189
190 %token  LEOF
191 %token  ARITY_PRAGMA SPECIALIZE_PRAGMA STRICTNESS_PRAGMA UPDATE_PRAGMA
192 %token  END_PRAGMA
193
194 /**********************************************************************
195 *                                                                     *
196 *                                                                     *
197 *     Precedences of the various tokens                               *
198 *                                                                     *
199 *                                                                     *
200 **********************************************************************/
201
202
203 %left   CASE            LET             IN              LAMBDA          
204         IF              ELSE            CCALL           CCALL_DANGEROUS
205         CASM            CASM_DANGEROUS  SCC             AT
206
207 %left   VARSYM          CONSYM          PLUS            MINUS           BQUOTE
208
209 %left   DCOLON
210
211 %left   SEMI            COMMA
212
213 %left   OCURLY          OBRACK          OPAREN
214
215 %left   OPOD            OPROC
216
217 %left   EQUAL
218
219 %right  DARROW
220 %right  RARROW
221
222
223
224 /**********************************************************************
225 *                                                                     *
226 *                                                                     *
227 *      Type Declarations                                              *
228 *                                                                     *
229 *                                                                     *
230 **********************************************************************/
231
232
233 %type <ulist>   alt alts altrest quals vars varsrest cons
234                 tyvars constrs dtypes types atypes
235                 exps pats context context_list tyvar_list
236                 maybeexports export_list
237                 impspec maybeimpspec import_list
238                 impdecls maybeimpdecls impdecl
239                 renaming renamings renaming_list
240                 tyclses tycls_list
241                 gdrhs gdpat valrhs valrhs1
242                 lampats
243                 upto
244                 cexp
245                 tyvar_pids
246                 parquals
247                 pragmas
248
249
250 %type <utree>   exp dexp fexp kexp oexp aexp literal
251                 tuple list sequence comprehension qual qualrest
252                 gd
253                 apat bpat pat apatc conpat dpat fpat opat aapat
254                 dpatk fpatk opatk aapatk
255                 texps
256                 processor parqual
257
258 %type <uid>     MINUS VARID CONID VARSYM CONSYM
259                 var vark con conk varop varop1 conop op op1
260                 varid conid varsym consym minus plus
261                 tycls tycon modid ccallid
262
263 %type <ubinding>  topdecl topdecls
264                   typed datad classd instd defaultd
265                   decl decls valdef valdefs sign
266                   iimport iimports maybeiimports
267                   ityped idatad iclassd iinstd ivarsd
268                   itopdecl itopdecls
269                   maybe_where
270                   interface readinterface ibody
271                   cbody rinst
272                   impdecl_rest
273
274 %type <uttype>  simple simple_long type atype btype ttype ntatype inst class
275                 tyvar   
276
277 %type <uatype>  constr
278
279 %type <ustring> STRING FLOAT INTEGER CHARPRIM INTPRIM FLOATPRIM DOUBLEPRIM CLITLIT VOIDPRIM
280 %type <uint>    CHAR
281 %type <uentid>  export import
282 %type <uhpragma>  pragma
283
284
285 /**********************************************************************
286 *                                                                     *
287 *                                                                     *
288 *      Start Symbol for the Parser                                    *
289 *                                                                     *
290 *                                                                     *
291 **********************************************************************/
292
293 %start pmodule
294
295
296 %%
297
298 pmodule :  readprelude module
299         ;
300
301 module  :  MODULE modid maybeexports 
302                 { the_module_name = $2; module_exports = $3; }
303            WHERE body
304         |       { the_module_name = install_literal("Main"); module_exports = Lnil; }
305            body
306         ;
307
308 body    :  ocurly maybeimpdecls maybefixes topdecls ccurly
309                {
310                  root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,$4);
311                }
312         |  vocurly maybeimpdecls maybefixes topdecls vccurly
313                {
314                  root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,$4);
315                }
316
317         |  vocurly impdecls vccurly
318                {                 
319                  root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,mknullbind());
320                }
321         |  ocurly impdecls ccurly 
322                {                 
323                  root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,mknullbind());
324                }
325
326 /* Adds 1 S/R, 2 R/R conflicts, alternatives add 3 R/R conflicts */
327         |  vocurly maybeimpdecls vccurly 
328                {                 
329                  root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,mknullbind());
330                }
331         |  ocurly maybeimpdecls ccurly
332                {                 
333                  root = mkhmodule(the_module_name,lconc(prelude_imports,$2),module_exports,mknullbind());
334                }
335         ;
336
337
338 maybeexports :  /* empty */                     { $$ = Lnil; }
339         |  OPAREN export_list CPAREN            { $$ = $2; }
340         ;
341
342 export_list:
343            export                               { $$ = lsing($1); }
344         |  export_list COMMA export             { $$ = lapp($1,$3); }
345         ;
346
347 export  :
348            var                                  { $$ = mkentid($1); }
349         |  tycon                                { $$ = mkenttype($1); }
350         |  tycon OPAREN DOTDOT CPAREN           { $$ = mkenttypeall($1); }
351         |  tycon OPAREN cons CPAREN             
352                 { $$ = mkenttypecons($1,$3); 
353                   /* should be a datatype with cons representing all constructors */
354                 }
355         |  tycon OPAREN vars CPAREN             
356                 { $$ = mkentclass($1,$3); 
357                   /* should be a class with vars representing all Class operations */
358                 }
359         |  tycon OPAREN CPAREN
360                 { $$ = mkentclass($1,Lnil);
361                   /* "tycon" should be a class with no operations */
362                 }
363         |  tycon DOTDOT
364                 { $$ = mkentmod($1);
365                   /* "tycon" is a module id (but "modid" is bad for your identifier's health [KH])  */
366                 }
367         ;
368
369
370 impspec :  OPAREN import_list CPAREN            { $$ = $2; hidden = FALSE; }
371         |  HIDING OPAREN import_list CPAREN     { $$ = $3; hidden = TRUE; }
372         |  OPAREN CPAREN                        { $$ = Lnil; hidden = FALSE; }
373         ;
374
375 maybeimpspec :  /* empty */                     { $$ = Lnil; }
376         |  impspec                              { $$ = $1; }
377         ;
378
379 import_list:
380            import                               { $$ = lsing($1); }
381         |  import_list COMMA import             { $$ = lapp($1,$3); }
382         ;
383
384 import  :
385            var                                  { $$ = mkentid($1); }
386         |  tycon                                { $$ = mkenttype($1); }
387         |  tycon OPAREN DOTDOT CPAREN           { $$ = mkenttypeall($1); }
388         |  tycon OPAREN cons CPAREN
389                 { $$ = mkenttypecons($1,$3); 
390                   /* should be a datatype with cons representing all constructors */
391                 }
392         |  tycon OPAREN vars CPAREN             
393                 { $$ = mkentclass($1,$3);
394                   /* should be a class with vars representing all Class operations */
395                 }
396         |  tycon OPAREN CPAREN
397                 { $$ = mkentclass($1,Lnil);
398                   /* "tycon" should be a class with no operations */
399                 }
400         ;
401
402
403 pragmas:
404            pragma { $$ = lsing($1); }
405         |  pragmas pragma { $$ = lapp($1,$2); }
406         |  /* empty */ { $$ = Lnil; }
407         ;
408
409 pragma:
410            ARITY_PRAGMA      var EQUAL INTEGER END_PRAGMA
411                 { $$ = mkarity_pragma($2,$4); }
412
413         |  SPECIALIZE_PRAGMA var EQUAL ivarsd END_PRAGMA
414                 { $$ = mkspecialize_pragma($2, $4); }
415
416         |  STRICTNESS_PRAGMA var EQUAL STRING pragmas END_PRAGMA
417                 { $$ = mkstrictness_pragma($2, $4, $5); }
418
419         |  UPDATE_PRAGMA     var EQUAL INTEGER END_PRAGMA
420                 { $$ = mkupdate_pragma($2, $4); }
421         ;
422
423
424 readprelude :   
425                 {
426                   if ( implicitPrelude ) {
427                      find_module_on_imports_dirlist("Prelude",TRUE,interface_filename);
428                   } else {
429                      find_module_on_imports_dirlist("PreludeNull_",TRUE,interface_filename);
430                   }
431                   setyyin(interface_filename);
432                   enteriscope();
433                 }
434            readinterface
435                 {
436                   binding prelude = mkimport(installid(iface_name),Lnil,Lnil,$2,xstrdup(interface_filename),hsplineno);
437                   prelude_imports = implicitPrelude? lsing(prelude): Lnil; 
438                 }
439         ;
440
441 maybeimpdecls : /* empty */                     { $$ = Lnil; }
442         |  impdecls SEMI                        { $$ = $1; }
443         ;
444
445 impdecls:  impdecl                              { $$ = $1; }
446         |  impdecls SEMI impdecl                { $$ = lconc($1,$3); }
447         ;
448
449 impdecl :  IMPORT modid
450                 { /* filename returned in "interface_filename" */
451                   char *module_name = id_to_string($2);
452                   find_module_on_imports_dirlist(module_name,FALSE,interface_filename);
453                   setyyin(interface_filename);
454                   enteriscope();
455                   if(strcmp(module_name,"Prelude")==0)
456                     prelude_imports = Lnil;
457                 }
458            impdecl_rest
459                 {
460                   if (hidden)
461                     $4->tag = hiding;
462                   $$ = lsing($4); 
463                 }
464
465 impdecl_rest:  
466            readinterface maybeimpspec
467                 { $$ = mkimport(installid(iface_name),$2,Lnil,$1,xstrdup(interface_filename),hsplineno); }
468                 /* WDP: uncertain about those hsplinenos */
469         |  readinterface maybeimpspec RENAMING renamings
470                 { $$ = mkimport(installid(iface_name),$2,$4,$1,xstrdup(interface_filename),hsplineno); }
471         ;
472
473 readinterface:
474            interface LEOF
475                 {
476                   exposeis(); /* partain: expose infix ops at level i+1 to level i */
477                   $$ = $1;
478                 }
479         ;
480
481 renamings: OPAREN renaming_list CPAREN          { $$ = $2; }
482         ;
483
484 renaming_list: renaming                         { $$ = lsing($1); }
485         |  renaming_list COMMA renaming         { $$ = lapp($1,$3); }
486         ;
487
488 renaming:  var TO var                           { $$ = ldub($1,$3); }
489         |  con TO con                           { $$ = ldub($1,$3); }
490         ;
491
492 maybeiimports : /* empty */                     { $$ = mknullbind(); }
493         |  iimports SEMI                        { $$ = $1; }
494         ;
495
496 iimports : iimports SEMI iimport                { $$ = mkabind($1,$3); }
497         | iimport                               { $$ = $1; }
498         ;
499
500 iimport :  importkey modid OPAREN import_list CPAREN
501                 { $$ = mkmbind($2,$4,Lnil,startlineno); }
502         |  importkey modid OPAREN import_list CPAREN RENAMING renamings  
503                 { $$ = mkmbind($2,$4,$7,startlineno); }
504         ;
505
506
507 interface:
508            INTERFACE modid
509                 { fixlist = Lnil;
510                   strcpy(iface_name, id_to_string($2));
511                 }
512            WHERE ibody
513                 {
514                 /* WDP: not only do we not check the module name
515                    but we take the one in the interface to be what we really want
516                    -- we need this for Prelude jiggery-pokery.  (Blech.  KH)
517                    ToDo: possibly revert....
518                    checkmodname(modname,id_to_string($2));
519                 */
520                   $$ = $5;
521                 }
522         ;
523
524
525 ibody   :  ocurly maybeiimports maybefixes itopdecls ccurly
526                 {
527                   $$ = mkabind($2,$4);
528                 }
529         |  ocurly iimports ccurly
530                 {
531                   $$ = $2;
532                 }
533         |  vocurly maybeiimports maybefixes itopdecls vccurly
534                 {
535                   $$ = mkabind($2,$4);
536                 }
537         |  vocurly iimports vccurly
538                 {
539                   $$ = $2;
540                 }
541         ;
542
543 maybefixes:  /* empty */
544         |  fixes SEMI
545         ;
546
547
548 fixes   :  fixes SEMI fix               
549         |  fix
550         ;
551
552 fix     :  INFIXL INTEGER
553                 { Precedence = checkfixity($2); Fixity = INFIXL; }
554            ops
555         |  INFIXR INTEGER
556                 { Precedence = checkfixity($2); Fixity = INFIXR; }
557            ops
558         |  INFIX  INTEGER
559                 { Precedence = checkfixity($2); Fixity = INFIX; }
560            ops
561         |  INFIXL
562                 { Fixity = INFIXL; Precedence = 9; }
563            ops
564         |  INFIXR
565                 { Fixity = INFIXR; Precedence = 9; }
566            ops
567         |  INFIX
568                 { Fixity = INFIX; Precedence = 9; }
569            ops
570         ;
571
572 ops     :  op                           { makeinfix(id_to_string($1),Fixity,Precedence); }
573         |  ops COMMA op                 { makeinfix(id_to_string($3),Fixity,Precedence); }
574         ;
575
576 topdecls:  topdecls SEMI topdecl
577                 {
578                   if($1 != NULL)
579                     if($3 != NULL)
580                       if(SAMEFN)
581                         {
582                           extendfn($1,$3);
583                           $$ = $1;
584                         }
585                       else
586                         $$ = mkabind($1,$3);
587                     else
588                       $$ = $1;
589                   else
590                     $$ = $3;
591                   SAMEFN = 0;
592                 }
593         |  topdecl
594         ;
595
596 topdecl :  typed                                { $$ = $1; }
597         |  datad                                { $$ = $1; }
598         |  classd                               { $$ = $1; }
599         |  instd                                { $$ = $1; }
600         |  defaultd                             { $$ = $1; }
601         |  decl                                 { $$ = $1; }
602         ;
603
604 typed   :  typekey simple EQUAL type            { $$ = mknbind($2,$4,startlineno,mkno_pramga()); }
605         ;
606
607         
608 datad   :  datakey context DARROW simple EQUAL constrs
609                 { $$ = mktbind($2,$4,$6,all,startlineno,mkno_pragma()); }
610         |  datakey simple EQUAL constrs
611                 { $$ = mktbind(Lnil,$2,$4,all,startlineno,mkno_pragma()); }
612         |  datakey context DARROW simple EQUAL constrs DERIVING tyclses
613                 { $$ = mktbind($2,$4,$6,$8,startlineno,mkno_pragma()); }
614         |  datakey simple EQUAL constrs DERIVING tyclses
615                 { $$ = mktbind(Lnil,$2,$4,$6,startlineno,mkno_pragma()); }
616         ;
617
618 classd  :  classkey context DARROW class cbody  { $$ = mkcbind($2,$4,$5,startlineno,Lnil); }
619         |  classkey class cbody                 { $$ = mkcbind(Lnil,$2,$3,startlineno,Lnil); }
620         ;
621
622 cbody   :  /* empty */                          { $$ = mknullbind(); }
623         |  WHERE ocurly decls ccurly            { checkorder($3); $$ = $3; }
624         |  WHERE vocurly decls vccurly          { checkorder($3); $$ =$3; }
625         ;
626
627
628 instd   :  instkey context DARROW tycls inst rinst      { $$ = mkibind($2,$4,$5,$6,startlineno,Lnil); }
629         |  instkey tycls inst rinst                     { $$ = mkibind(Lnil,$2,$3,$4,startlineno,Lnil); }
630         ;
631
632 rinst   :  /* empty */                          { $$ = mknullbind(); }
633         |  WHERE ocurly valdefs ccurly          { $$ = $3; }
634         |  WHERE vocurly valdefs vccurly        { $$ = $3; }
635         ;
636
637 inst    :  tycon                                { $$ = mktname($1,Lnil); }
638         |  OPAREN simple_long CPAREN            { $$ = $2; }
639     /* partain?: "simple" requires k >= 0, not k > 0 (hence "simple_long" hack) */
640         |  OPAREN tyvar_list CPAREN             { $$ = mkttuple($2); }
641         |  OPAREN CPAREN                        { $$ = mkttuple(Lnil); }
642         |  OBRACK tyvar CBRACK                  { $$ = mktllist($2); }
643         |  OPAREN tyvar RARROW tyvar CPAREN     { $$ = mktfun($2,$4); }
644         |  OPOD tyvar CPOD                      { $$ = mktpod($2); }
645         |  OPROC tyvar_pids SEMI tyvar CPROC    { $$ = mktproc($2,$4); }
646         |  OPOD tyvar_pids SEMI tyvar CPOD      { $$ = mktpod(mktproc($2,$4));}
647         |  OPOD OPROC tyvar_pids SEMI tyvar CPROC CPOD  
648                         { $$ = mktpod(mktproc($3,$5)); }
649         ;
650
651 /* Note (hilly) : Similar to tyvar_list except k>=1 not k>=2 */
652
653 tyvar_pids      : tyvar COMMA tyvar_pids        { $$ = mklcons($1,$3); }
654                 |  tyvar                        { $$ = lsing($1); }
655                 ;
656
657 defaultd:  defaultkey dtypes
658                 { 
659                   $$ = mkdbind($2,startlineno); 
660                 }
661         ;
662
663 dtypes  :  OPAREN type COMMA types CPAREN       { $$ = mklcons($2,$4); }
664         |  ttype                                { $$ = lsing($1); }
665 /*      Omitting this forces () to be the *type* (), which never defaults.  This is a KLUDGE */
666 /*      |  OPAREN CPAREN                        { $$ = Lnil; }*/
667         ;
668
669 decls   :  decls SEMI decl
670                 {
671                   if(SAMEFN)
672                     {
673                       extendfn($1,$3);
674                       $$ = $1;
675                     }
676                   else
677                     $$ = mkabind($1,$3);
678                 }
679         |  decl
680         ;
681
682 /* partain: this "DCOLON context" vs "DCOLON type" is a problem,
683     because you can't distinguish between
684
685         foo :: (Baz a, Baz a)
686         bar :: (Baz a, Baz a) => [a] -> [a] -> [a]
687
688     with one token of lookahead.  The HACK is to have "DCOLON ttype"
689     [tuple type] in the first case, then check that it has the right
690     form C a, or (C1 a, C2 b, ... Cn z) and convert it into a
691     context.  Blaach!
692     (FIXED 90/06/06)
693 */
694
695 decl    :  vars DCOLON type DARROW type iclasop_pragma
696                 { /* type2context.c for code */
697                   $$ = mksbind($1,mkcontext(type2context($3),$5),startlineno,$6);
698                   PREVPATT = NULL;
699                   FN = NULL;
700                   SAMEFN = 0;
701                 }
702         |  sign
703         |  valdef
704         |  /* empty */                  { $$ = mknullbind(); PREVPATT = NULL; FN = NULL; SAMEFN = 0; }
705         ;
706
707 sign    :  vars DCOLON type iclasop_pragma
708                 {
709                   $$ = mksbind($1,$3,startlineno,$4);
710                   PREVPATT = NULL;
711                   FN = NULL;
712                   SAMEFN = 0;
713                 }
714         ;
715
716
717
718 itopdecls : itopdecls SEMI itopdecl             { $$ = mkabind($1,$3); }
719         | itopdecl                              { $$ = $1; }
720         ;
721
722 itopdecl:  ityped                               { $$ = $1; }
723         |  idatad                               { $$ = $1; }
724         |  iclassd                              { $$ = $1; }
725         |  iinstd                               { $$ = $1; }
726         |  ivarsd                               { $$ = $1; }
727         |  /* empty */                          { $$ = mknullbind(); }
728         ;
729
730         /* partain: see comment elsewhere about why "type", not "context" */
731 ivarsd  :  vars DCOLON type DARROW type ival_pragma
732                 { $$ = mksbind($1,mkcontext(type2context($3),$5),startlineno,$6); }
733         |  vars DCOLON type ival_pragma
734                 { $$ = mksbind($1,$3,startlineno,$4); }
735         ;
736
737 ityped  :  typekey simple EQUAL type itype_pragma { $$ = mknbind($2,$4,startlineno,$5); }
738         ;
739         
740 idatad  :  datakey context DARROW simple idata_pragma                   { $$ = mktbind($2,$4,Lnil,Lnil,startlineno,$5); }
741         |  datakey simple idata_pragma                                  { $$ = mktbind(Lnil,$2,Lnil,Lnil,startlineno,$3); }
742         |  datakey context DARROW simple EQUAL constrs                  { $$ = mktbind($2,$4,$6,Lnil,startlineno,mk_nopragma()); }
743         |  datakey simple EQUAL constrs                                 { $$ = mktbind(Lnil,$2,$4,Lnil,startlineno,mk_nopragma()); }
744         |  datakey context DARROW simple EQUAL constrs DERIVING tyclses { $$ = mktbind($2,$4,$6,$8,startlineno,mk_nopragma()); }
745         |  datakey simple EQUAL constrs DERIVING tyclses                { $$ = mktbind(Lnil,$2,$4,$6,startlineno,mk_nopragma()); }
746         ;
747
748
749 iclassd :  classkey context DARROW class cbody pragmas
750                 { $$ = mkcbind($2,$4,$5,startlineno,$6); }
751         |  classkey class cbody pragmas
752                 { $$ = mkcbind(Lnil,$2,$3,startlineno,$4); }
753         ;
754
755 iinstd  :  instkey context DARROW tycls inst pragmas
756                 { $$ = mkibind($2,$4,$5,mknullbind(),startlineno,$6); }
757         |  instkey tycls inst pragmas
758                 { $$ = mkibind(Lnil,$2,$3,mknullbind(),startlineno,$4); }
759         ;
760
761
762 /* obsolete: "(C a, ...)" cause r/r conflict, resolved in favour of context rather than type */
763
764 class   :  tycon tyvar                          { $$ = mktname($1,lsing($2)); }
765             /* partain: changed "tycls" to "tycon" */
766         ;
767
768 types   :  types COMMA type                     { $$ = lapp($1,$3); }
769         |  type                                 { $$ = lsing($1); }
770         ;
771
772 type    :  btype                                { $$ = $1; }
773         |  btype RARROW type                    { $$ = mktfun($1,$3); }
774
775 btype   :  atype                                { $$ = $1; }
776         |  tycon atypes                         { $$ = mktname($1,$2); }
777         ;
778
779 atypes  :  atypes atype                         { $$ = lapp($1,$2); }
780         |  atype                                { $$ = lsing($1); }
781         ;
782
783 /* The split with ntatype allows us to use the same syntax for defaults as for types */
784 ttype   :  ntatype                              { $$ = $1; }
785         |  btype RARROW type                    { $$ = mktfun($1,$3); }
786         |  tycon atypes                         { $$ = mktname($1,$2); }
787         ;
788
789 atype   :  ntatype
790         |  OPAREN type COMMA types CPAREN       { $$ = mkttuple(mklcons($2,$4)); }
791         ;
792
793 ntatype :  tyvar                                { $$ = $1; }
794         |  tycon                                { $$ = mktname($1,Lnil); }
795         |  OPAREN CPAREN                        { $$ = mkttuple(Lnil); }
796         |  OPAREN type CPAREN                   { $$ = $2; }
797         |  OBRACK type CBRACK                   { $$ = mktllist($2); }
798         |  OPOD type CPOD                       { $$ = mktpod($2); }
799         |  OPROC types SEMI type CPROC          { $$ = mktproc($2,$4); }
800         |  OPOD types SEMI type CPOD            { $$ = mktpod(mktproc($2,$4));}
801         ;
802         
803
804 simple  :  tycon                                { $$ = mktname($1,Lnil); }
805         |  tycon tyvars                         { $$ = mktname($1,$2); }
806         ;
807
808
809 simple_long : tycon tyvars                      { $$ = mktname($1,$2); }
810         ; /* partain: see comment in "inst" */
811
812
813 constrs :  constrs VBAR constr                  { $$ = lapp($1,$3); }
814         |  constr                               { $$ = lsing($1); }
815         ;
816
817 /* Using tycon rather than con avoids 5 S/R errors */
818 constr  :  tycon atypes                         { $$ = mkatc($1,$2,hsplineno); }
819         |  OPAREN consym CPAREN atypes          { $$ = mkatc($2,$4,hsplineno); }
820         |  tycon                                { $$ = mkatc($1,Lnil,hsplineno); }
821         |  OPAREN consym CPAREN                 { $$ = mkatc($2,Lnil,hsplineno); }
822         |  btype conop btype                    { $$ = mkatc($2, ldub($1,$3), hsplineno); }
823         ;
824
825 tyclses :  OPAREN tycls_list CPAREN             { $$ = $2; }
826         |  OPAREN CPAREN                        { $$ = Lnil; }
827         |  tycls                                { $$ = lsing($1); }
828         ;
829
830 tycls_list:  tycls COMMA tycls_list             { $$ = mklcons($1,$3); }
831         |  tycls                                { $$ = lsing($1); }
832         ;
833
834 context :  OPAREN context_list CPAREN           { $$ = $2; }
835         |  class                                { $$ = lsing($1); }
836         ;
837
838 context_list:  class COMMA context_list         { $$ = mklcons($1,$3); }
839         |  class                                { $$ = lsing($1); }
840         ;
841
842 valdefs :  valdefs SEMI valdef
843                 {
844                   if(SAMEFN)
845                     {
846                       extendfn($1,$3);
847                       $$ = $1;
848                     }
849                   else
850                     $$ = mkabind($1,$3);
851                 }
852         |  valdef                               { $$ = $1; }
853         |  /* empty */                          { $$ = mknullbind(); }
854         ;
855
856
857 vars    :  vark COMMA varsrest                  { $$ = mklcons($1,$3); }
858         |  vark                                 { $$ = lsing($1); }
859         ;
860
861 varsrest:  varsrest COMMA var                   { $$ = lapp($1,$3); }
862         |  var                                  { $$ = lsing($1); }
863         ;
864
865 cons    :  cons COMMA con                       { $$ = lapp($1,$3); }
866         |  con                                  { $$ = lsing($1); }
867         ;
868
869
870 valdef  :  opatk
871                 {
872                   tree fn = function($1);
873
874                   PREVPATT = $1;
875
876                   if(ttree(fn) == ident)
877                     {
878                       checksamefn(gident(fn));
879                       FN = fn;
880                     }
881
882                   else if (ttree(fn) == tinfixop && ttree(ginfun((struct Sap *) fn)) == ident)
883                     {
884                       checksamefn(gident(ginfun((struct Sap *) fn)));
885                       FN = ginfun((struct Sap *) fn);
886                     }
887
888                   else if(etags)
889                     printf("%u\n",startlineno);
890                 }
891            valrhs
892                 {
893                   if(ispatt($1,TRUE))
894                     {
895                       $$ = mkpbind($3, startlineno);
896                       FN = NULL;
897                       SAMEFN = 0;
898                     }
899                   else
900                     $$ = mkfbind($3,startlineno);
901
902                   PREVPATT = NULL;
903                 }
904         ;
905
906 valrhs  :  valrhs1 maybe_where                  { $$ = lsing(createpat($1, $2)); }
907         ;
908
909 valrhs1 :  gdrhs
910         |  EQUAL exp                            { $$ = lsing(mktruecase($2)); }
911         ;
912
913 gdrhs   :  gd EQUAL exp                         { $$ = lsing(ldub($1,$3)); }
914         |  gd EQUAL exp gdrhs                   { $$ = mklcons(ldub($1,$3),$4); }
915         ;
916
917 maybe_where:
918            WHERE ocurly decls ccurly            { $$ = $3; }
919         |  WHERE vocurly decls vccurly          { $$ = $3; }
920         |  /* empty */                          { $$ = mknullbind(); }
921         ;
922
923 gd      :  VBAR oexp                            { $$ = $2; }
924         ;
925
926
927 lampats :  apat lampats                         { $$ = mklcons($1,$2); }
928         |  apat                                 { $$ = lsing($1); }
929         ;
930
931
932 /*
933         Changed as above to allow for contexts!
934         KH@21/12/92
935 */
936
937
938 exp     :  oexp DCOLON type DARROW type         { $$ = mkrestr($1,mkcontext(type2context($3),$5)); }
939         |  oexp DCOLON type                     { $$ = mkrestr($1,$3); }
940         |  oexp
941         ;
942
943 /*
944   Operators must be left-associative  at the same precedence 
945   for prec. parsing to work.
946 */
947
948         /* Infix operator application */
949 oexp    :  dexp
950         |  oexp op oexp %prec PLUS
951                 { $$ = mkinfixop($2,$1,$3); precparse($$); }
952         ;
953
954 /*
955   This comes here because of the funny precedence rules concerning
956   prefix minus.
957 */
958
959
960 dexp    :  MINUS kexp                           { $$ = mknegate($2); }
961         |  kexp
962         ;  
963
964 /*
965   let/if/lambda/case have higher precedence than infix operators.
966 */
967
968 kexp    :  LAMBDA
969                 { /* enteriscope(); /? I don't understand this -- KH */
970                   hsincindent();    /* added by partain; push new context for */
971                                     /* FN = NULL; not actually concerned about */
972                   FN = NULL;        /* indenting */
973                   $<uint>$ = hsplineno; /* remember current line number */
974                 }
975            lampats
976                 { hsendindent();    /* added by partain */
977                   /* exitiscope();          /? Also not understood */
978                 }
979            RARROW exp   /* lambda abstraction */
980                 {
981                   $$ = mklambda($3, $6, $<uint>2);
982                 }
983
984         /* Let Expression */
985         |  LET ocurly decls ccurly IN exp       { $$ = mklet($3,$6); }    
986         |  LET vocurly decls vccurly IN exp     { $$ = mklet($3,$6); }    
987
988         /* If Expression */
989         |  IF exp THEN exp ELSE exp             { $$ = mkife($2,$4,$6); }
990
991         /* Case Expression */
992         |  CASE exp OF ocurly alts ccurly       { $$ = mkcasee($2,$5); }
993         |  CASE exp OF vocurly alts vccurly     { $$ = mkcasee($2,$5); }
994
995         /* CCALL/CASM Expression */
996         |  CCALL ccallid cexp                   { $$ = mkccall($2,installid("n"),$3); }
997         |  CCALL ccallid                        { $$ = mkccall($2,installid("n"),Lnil); }
998         |  CCALL_DANGEROUS ccallid cexp         { $$ = mkccall($2,installid("p"),$3); }
999         |  CCALL_DANGEROUS ccallid              { $$ = mkccall($2,installid("p"),Lnil); }
1000         |  CASM CLITLIT cexp                    { $$ = mkccall($2,installid("N"),$3); }
1001         |  CASM CLITLIT                         { $$ = mkccall($2,installid("N"),Lnil); }
1002         |  CASM_DANGEROUS CLITLIT cexp          { $$ = mkccall($2,installid("P"),$3); }
1003         |  CASM_DANGEROUS CLITLIT               { $$ = mkccall($2,installid("P"),Lnil); }
1004
1005         /* SCC Expression */
1006         |  SCC STRING exp
1007                 { extern BOOLEAN ignoreSCC;
1008                   extern BOOLEAN warnSCC;
1009                   extern char * input_filename;
1010
1011                   if (ignoreSCC) {
1012                     if (warnSCC) 
1013                         fprintf(stderr,
1014                                 "\"%s\", line %d: scc (`set [profiling] cost centre') ignored\n",
1015                                 input_filename, hsplineno);
1016                     $$ = $3;
1017                   } else {
1018                     $$ = mkscc($2, $3);
1019                   }
1020                 }
1021         |  fexp
1022         ;
1023
1024
1025
1026         /* Function application */
1027 fexp    :  fexp aexp                            { $$ = mkap($1,$2); }
1028         |  aexp
1029         ;
1030
1031 cexp    :  cexp aexp                            { $$ = lapp($1,$2); }
1032         |  aexp                                 { $$ = lsing($1); }
1033         ;
1034
1035
1036 /*
1037    The mkpars are so that infix parsing doesn't get confused.
1038
1039    KH.
1040 */
1041
1042         /* Simple Expressions */
1043 aexp    :  var                                  { $$ = mkident($1); }
1044         |  con                                  { $$ = mkident($1); }
1045         |  literal      
1046         |  OPAREN exp CPAREN                    { $$ = mkpar($2); }
1047         |  OPAREN oexp op CPAREN                { checkprec($2,$3,FALSE); $$ = mklsection($2,$3); }
1048         |  OPAREN op1 oexp CPAREN               { checkprec($3,$2,TRUE);  $$ = mkrsection($2,$3); }
1049
1050         /* structures */
1051         |  tuple
1052         |  list                                 { $$ = mkpar($1); }
1053         |  sequence                             { $$ = mkpar($1); }
1054         |  comprehension                        { $$ = mkpar($1); }
1055         |  OPOD exp VBAR parquals CPOD          { $$ = mkparzf($2,$4); }
1056         |  OPOD exps CPOD                       { $$ = mkpod($2); }
1057         |  processor                            { $$ = mkpar($1); }
1058
1059         /* These only occur in patterns */
1060         |  var AT aexp                          { checkinpat();  $$ = mkas($1,$3); } 
1061         |  WILDCARD                             { checkinpat();  $$ = mkwildp();   }
1062         |  LAZY aexp                            { checkinpat();  $$ = mklazyp($2); }
1063         ;
1064
1065
1066 processor :  OPROC exps SEMI exp CPROC          { $$ = mkproc($2,$4); }
1067           ;
1068
1069 parquals  :  parquals COMMA parqual             { $$ = lapp($1,$3); }
1070           |  parqual                            { $$ = lsing($1); }
1071           ;
1072
1073 parqual  : exp                                  { $$ = mkparfilt($1); }
1074           | processor  DRAWNFROM exp    
1075                 {       $$ = mkpardgen($1,$3); 
1076                         checkpatt($1);
1077                 }
1078           | processor  INDEXFROM exp    
1079                 {       $$ = mkparigen($1,$3); 
1080                         checkpatt(gprocdata($1));
1081                 }
1082           ;
1083
1084
1085 /*
1086         LHS patterns are parsed in a similar way to
1087         expressions.  This avoids the horrible non-LRness
1088         which occurs with the 1.1 syntax.
1089
1090         The xpatk business is to do with accurately recording
1091         the starting line for definitions.
1092 */
1093
1094 /*TESTTEST
1095 bind    :  opatk
1096         |  vark lampats
1097                 { $$ = mkap($1,$2); }
1098         |  opatk varop opat %prec PLUS
1099                 {
1100                   $$ = mkinfixop($2,$1,$3);
1101                 }
1102         ;
1103
1104 opatk   :  dpatk
1105         |  opatk conop opat %prec PLUS
1106                 {
1107                   $$ = mkinfixop($2,$1,$3);
1108                   precparse($$);
1109                 }
1110         ;
1111
1112 */
1113
1114 opatk   :  dpatk
1115         |  opatk op opat %prec PLUS
1116                 {
1117                   $$ = mkinfixop($2,$1,$3);
1118
1119                   if(isconstr(id_to_string($2)))
1120                     precparse($$);
1121                   else 
1122                     {
1123                       checkprec($1,$2,FALSE);   /* Check the precedence of the left pattern */
1124                       checkprec($3,$2,TRUE);    /* then check the right pattern */
1125                     }
1126                 }
1127         ;
1128
1129 opat    :  dpat
1130         |  opat op opat %prec PLUS
1131                 {
1132                   $$ = mkinfixop($2,$1,$3);
1133
1134                   if(isconstr(id_to_string($2)))
1135                     precparse($$);
1136                   else 
1137                     {
1138                       checkprec($1,$2,FALSE);   /* Check the precedence of the left pattern */
1139                       checkprec($3,$2,TRUE);    /* then check the right pattern */
1140                     }
1141                 }
1142         ;
1143
1144 /*
1145   This comes here because of the funny precedence rules concerning
1146   prefix minus.
1147 */
1148
1149
1150 dpat    :  MINUS fpat                                   { $$ = mknegate($2); }
1151         |  fpat
1152         ;  
1153
1154         /* Function application */
1155 fpat    :  fpat aapat                                   { $$ = mkap($1,$2); }
1156         |  aapat
1157         ;
1158
1159 dpatk   :  minuskey fpat                                { $$ = mknegate($2); }
1160         |  fpatk
1161         ;  
1162
1163         /* Function application */
1164 fpatk   :  fpatk aapat                                  { $$ = mkap($1,$2); }
1165         |  aapatk
1166         ;
1167
1168 aapat   :  con                                          { $$ = mkident($1); }
1169         |  var                                          { $$ = mkident($1); }
1170         |  var AT apat                                  { $$ = mkas($1,$3); }
1171         |  literal                                      { $$ = $1; }
1172         |  WILDCARD                                     { $$ = mkwildp(); }
1173         |  OPAREN CPAREN                                { $$ = mktuple(Lnil); }
1174         |  OPAREN var PLUS INTEGER CPAREN               { $$ = mkplusp(mkident($2),mkinteger($4)); }
1175         |  OPAREN WILDCARD PLUS INTEGER CPAREN          { $$ = mkplusp(mkwildp(),mkinteger($4)); }
1176         |  OPAREN opat CPAREN                           { $$ = mkpar($2); }
1177         |  OPAREN opat COMMA pats CPAREN                { $$ = mktuple(mklcons($2,$4)); }
1178         |  OBRACK pats CBRACK                           { $$ = mkllist($2); }
1179         |  OBRACK CBRACK                                { $$ = mkllist(Lnil); }
1180         |  LAZY apat                                    { $$ = mklazyp($2); }
1181         |  OPROC pats SEMI apat CPROC                   { $$ = mkproc($2,$4); }
1182         ;
1183
1184 aapatk  :  conk                                         { $$ = mkident($1); }
1185         |  vark                                         { $$ = mkident($1); }
1186         |  vark AT apat                                 { $$ = mkas($1,$3); }
1187         |  literal                                      { $$ = $1; setstartlineno(); }
1188         |  WILDCARD                                     { $$ = mkwildp(); setstartlineno(); }
1189         |  oparenkey CPAREN                             { $$ = mktuple(Lnil); }
1190         |  oparenkey var PLUS INTEGER CPAREN            { $$ = mkplusp(mkident($2),mkinteger($4)); }
1191         |  oparenkey WILDCARD PLUS INTEGER CPAREN       { $$ = mkplusp(mkwildp(),mkinteger($4)); }
1192         |  oparenkey opat CPAREN                        { $$ = mkpar($2); }
1193         |  oparenkey opat COMMA pats CPAREN             { $$ = mktuple(mklcons($2,$4)); }
1194         |  obrackkey pats CBRACK                        { $$ = mkllist($2); }
1195         |  obrackkey CBRACK                             { $$ = mkllist(Lnil); }
1196         |  lazykey apat                                 { $$ = mklazyp($2); }
1197         |  oprockey pats SEMI opat CPROC                { $$ = mkproc($2,$4); }
1198         ;
1199
1200
1201 /*
1202    The mkpars are so that infix parsing doesn't get confused.
1203
1204    KH.
1205 */
1206
1207 tuple   :  OPAREN exp COMMA texps CPAREN
1208                 { if (ttree($4) == tuple)
1209                     $$ = mktuple(mklcons($2, gtuplelist($4)));
1210                 else
1211                   $$ = mktuple(ldub($2, $4));
1212                 }
1213         |  OPAREN CPAREN
1214                 { $$ = mktuple(Lnil); }
1215         ;
1216
1217 texps   :  exp COMMA texps
1218                 { if (ttree($3) == tuple)
1219                     $$ = mktuple(mklcons($1, gtuplelist($3)));
1220                 else
1221                   $$ = mktuple(ldub($1, $3));
1222                 }
1223         |  exp                                  { $$ = mkpar($1); }
1224         ;
1225
1226
1227 list    :  OBRACK CBRACK                        { $$ = mkllist(Lnil); }
1228         |  OBRACK exps CBRACK                   { $$ = mkllist($2); }
1229         ;
1230
1231 exps    :  exp COMMA exps                       { $$ = mklcons($1,$3); }
1232         |  exp                                  { $$ = lsing($1); }
1233         ;
1234
1235
1236 sequence:  OBRACK exp COMMA exp DOTDOT upto CBRACK      {$$ = mkeenum($2,lsing($4),$6);}
1237         |  OBRACK exp DOTDOT upto CBRACK        { $$ = mkeenum($2,Lnil,$4); }
1238         ;
1239
1240 comprehension:  OBRACK exp VBAR quals CBRACK    { $$ = mkcomprh($2,$4); }
1241         ;
1242
1243 quals   :  quals COMMA qual                     { $$ = lapp($1,$3); }
1244         |  qual                                 { $$ = lsing($1); }
1245         ;
1246
1247 qual    :       { inpat = TRUE; } exp { inpat = FALSE; } qualrest
1248                 { if ($4 == NULL)
1249                     $$ = mkguard($2);
1250                   else 
1251                     {
1252                       checkpatt($2);
1253                       if(ttree($4)==def)
1254                         {
1255                           tree prevpatt_save = PREVPATT;
1256                           PREVPATT = $2;
1257                           $$ = mkdef(mkpbind(lsing(createpat(lsing(mktruecase((tree)(ggdef($4)))),mknullbind())),hsplineno));
1258                           PREVPATT = prevpatt_save;
1259                         }
1260                       else
1261                         $$ = mkqual($2,$4);
1262                     }
1263                 }
1264         ;
1265
1266 qualrest:  LARROW exp                           { $$ = $2; }
1267 /* OLD:
1268         |  EQUAL exp
1269                 { if(nonstandardFlag)
1270                     $$ = mkdef($2); 
1271                   else
1272                     hsperror("Definitions in comprehensions are not standard Haskell");
1273                 }
1274 */
1275         |  /* empty */                          { $$ = NULL; }
1276         ;
1277
1278
1279 alts    :  alts SEMI alt                        { $$ = lconc($1,$3); }
1280         |  alt                                  { $$ = $1; }
1281         ;
1282
1283 alt     :  pat
1284                 { PREVPATT = $1; }
1285            altrest
1286                 { $$ = $3;
1287                   PREVPATT = NULL;
1288                 }
1289         |  /* empty */                          { $$ = Lnil; }
1290         ;
1291
1292 altrest :  gdpat maybe_where                    { $$ = lsing(createpat($1,$2)); }
1293         |  RARROW exp maybe_where               { $$ = lsing(createpat(lsing(mktruecase($2)),$3)); }
1294         ;
1295
1296 gdpat   :  gd RARROW exp gdpat                  { $$ = mklcons(ldub($1,$3),$4);  }
1297         |  gd RARROW exp                        { $$ = lsing(ldub($1,$3)); }
1298         ;
1299
1300 upto    :  /* empty */                          { $$ = Lnil; }
1301         |  exp                                  { $$ = lsing($1); }
1302         ;
1303
1304 pats    :  pat COMMA pats                       { $$ = mklcons($1,$3); }
1305         |  pat                                  { $$ = lsing($1); }
1306         ;
1307
1308 pat     :  bpat
1309         |  pat conop bpat                       { $$ = mkinfixop($2,$1,$3); precparse($$); }
1310         ;
1311
1312 bpat    :  apatc
1313         |  conpat
1314         |  MINUS INTEGER                        { $$ = mkinteger(ineg($2)); }
1315         |  MINUS FLOAT                          { $$ = mkfloatr(ineg($2)); }
1316         ;
1317
1318 conpat  :  con                                  { $$ = mkident($1); }
1319         |  conpat apat                          { $$ = mkap($1,$2); }
1320         ;
1321
1322 apat    :  con                                  { $$ = mkident($1); }
1323         |  apatc
1324         ;
1325
1326 apatc   :  var                                  { $$ = mkident($1); }
1327         |  var AT apat                          { $$ = mkas($1,$3); }
1328         |  literal                              { $$ = $1; }
1329         |  WILDCARD                             { $$ = mkwildp(); }
1330         |  OPAREN CPAREN                        { $$ = mktuple(Lnil); }
1331         |  OPAREN var PLUS INTEGER CPAREN       { $$ = mkplusp(mkident($2),mkinteger($4)); }
1332         |  OPAREN WILDCARD PLUS INTEGER CPAREN  { $$ = mkplusp(mkwildp(),mkinteger($4)); }
1333         |  OPAREN pat CPAREN                    { $$ = mkpar($2); }
1334         |  OPAREN pat COMMA pats CPAREN         { $$ = mktuple(mklcons($2,$4)); }
1335         |  OBRACK pats CBRACK                   { $$ = mkllist($2); }
1336         |  OBRACK CBRACK                        { $$ = mkllist(Lnil); }
1337         |  LAZY apat                            { $$ = mklazyp($2); }
1338         |  OPROC pats SEMI apat CPROC           { $$ = mkproc($2,$4); }
1339         ;
1340
1341 /*
1342 patk    :  bpatk
1343         |  patk conop bpat                      { $$ = mkinfixop($2,$1,$3); precparse($$); }
1344         ;
1345
1346 bpatk   :  apatck
1347         |  conpatk
1348         |  minuskey INTEGER                     { $$ = mkinteger(ineg($2)); }
1349         |  minuskey FLOAT                       { $$ = mkfloatr(ineg($2)); }
1350         ;
1351
1352 conpatk :  conk                                 { $$ = mkident($1); }
1353         |  conpatk apat                         { $$ = mkap($1,$2); }
1354         ;
1355
1356 apatck  :  vark                                 { $$ = mkident($1); }
1357         |  vark AT apat                         { $$ = mkas($1,$3); }
1358         |  literal                              { $$ = $1; setstartlineno(); }
1359         |  WILDCARD                             { $$ = mkwildp(); setstartlineno(); }
1360         |  oparenkey CPAREN                     { $$ = mktuple(Lnil); }
1361         |  oparenkey var PLUS INTEGER CPAREN    { $$ = mkplusp(mkident($2),mkinteger($4)); }
1362         |  oparenkey WILDCARD PLUS INTEGER CPAREN       { $$ = mkplusp(mkwildp(),mkinteger($4)); }
1363         |  oparenkey pat CPAREN                 { $$ = mkpar($2); }
1364         |  oparenkey pat COMMA pats CPAREN      { $$ = mktuple(mklcons($2,$4)); }
1365         |  obrackkey pats CBRACK                        { $$ = mkllist($2); }
1366         |  obrackkey CBRACK                     { $$ = mkllist(Lnil); }
1367         |  lazykey apat                         { $$ = mklazyp($2); }
1368         |  oprockey pats SEMI opat CPROC                { $$ = mkproc($2,$4); }
1369         ;
1370 */
1371
1372 literal :  INTEGER                              { $$ = mkinteger($1); }
1373         |  FLOAT                                { $$ = mkfloatr($1); }
1374         |  CHAR                                 { $$ = mkcharr($1); }
1375         |  STRING                               { $$ = mkstring($1); }
1376         |  CHARPRIM                             { $$ = mkcharprim($1); }
1377         |  INTPRIM                              { $$ = mkintprim($1); }
1378         |  FLOATPRIM                            { $$ = mkfloatprim($1); }
1379         |  DOUBLEPRIM                           { $$ = mkdoubleprim($1); }
1380         |  CLITLIT                              { $$ = mkclitlit($1); }
1381         |  VOIDPRIM                             { $$ = mkvoidprim(); }
1382         ;
1383
1384
1385 /* Keywords which record the line start */
1386
1387 importkey:  IMPORT      { setstartlineno(); }
1388         ;
1389
1390 datakey :   DATA        { setstartlineno();
1391                           if(etags)
1392                             printf("%u\n",startlineno);
1393                         } 
1394         ;
1395
1396 typekey :   TYPE        { setstartlineno();
1397                           if(etags)
1398                             printf("%u\n",startlineno);
1399                         } 
1400         ;
1401
1402 instkey :   INSTANCE    { setstartlineno();
1403                           if(etags)
1404                             printf("%u\n",startlineno);
1405                         } 
1406         ;
1407
1408 defaultkey: DEFAULT     { setstartlineno(); }
1409         ;
1410
1411 classkey:   CLASS       { setstartlineno();
1412                           if(etags)
1413                             printf("%u\n",startlineno);
1414                         }
1415         ;
1416
1417 minuskey:   MINUS       { setstartlineno(); }
1418         ;
1419
1420 oparenkey:  OPAREN      { setstartlineno(); }
1421         ;
1422
1423 obrackkey:  OBRACK      { setstartlineno(); }
1424         ;
1425
1426 lazykey :   LAZY        { setstartlineno(); }
1427         ;
1428
1429 oprockey:   OPROC       { setstartlineno(); }
1430         ;
1431
1432
1433 /* Non "-" op, used in right sections -- KH */
1434 op1     :  conop
1435         |  varop1
1436         ;
1437
1438 op      :  conop
1439         |  varop
1440         ;
1441
1442 varop   :  varsym
1443         |  BQUOTE varid BQUOTE          { $$ = $2; }
1444         ;
1445
1446 /*      Non-minus varop, used in right sections */
1447 varop1  :  VARSYM
1448         |  plus
1449         |  BQUOTE varid BQUOTE          { $$ = $2; }
1450         ;
1451
1452 conop   :  consym
1453         |  BQUOTE conid BQUOTE          { $$ = $2; }
1454         ;
1455
1456 consym  :  CONSYM
1457         ;
1458
1459 varsym  :  VARSYM
1460         |  plus
1461         |  minus
1462         ;
1463
1464 minus   :  MINUS                        { $$ = install_literal("-"); }
1465         ;
1466
1467 plus    :  PLUS                         { $$ = install_literal("+"); }
1468         ;
1469
1470 var     :  VARID
1471         |  OPAREN varsym CPAREN         { $$ = $2; }
1472         ;
1473
1474 vark    :  VARID                        { setstartlineno(); $$ = $1; }
1475         |  oparenkey varsym CPAREN      { $$ = $2; }
1476         ;
1477
1478 /* tycon used here to eliminate 11 spurious R/R errors -- KH */
1479 con     :  tycon
1480         |  OPAREN consym CPAREN         { $$ = $2; }
1481         ;
1482
1483 conk    :  tycon                        { setstartlineno(); $$ = $1; }
1484         |  oparenkey consym CPAREN      { $$ = $2; }
1485         ;
1486
1487 varid   :  VARID
1488         ;
1489
1490 conid   :  CONID
1491         ;
1492
1493 ccallid :  varid
1494         |  conid
1495         ;
1496
1497 /* partain: "tyvar_list" must be at least 2 elements long (defn of "inst") */
1498 tyvar_list: tyvar COMMA tyvar_list              { $$ = mklcons($1,$3); }
1499         |  tyvar COMMA tyvar                    { $$ = mklcons($1,lsing($3)); }
1500         ;
1501
1502 tyvars  :  tyvar tyvars                         { $$ = mklcons($1,$2); }
1503         |  tyvar                                { $$ = lsing($1); }
1504         ;
1505
1506 tyvar   :  VARID                                { $$ = mknamedtvar($1); }
1507         ;
1508
1509 tycls   :  tycon
1510                 /* partain: "aconid"->"tycon" got rid of a r/r conflict
1511                     (and introduced >= 2 s/r's ...)
1512                  */
1513         ;
1514
1515 tycon   :  conid
1516         ;
1517
1518 modid   :  CONID
1519         ;
1520
1521
1522 ocurly  : layout OCURLY                         { hsincindent(); }
1523
1524 vocurly : layout                                { hssetindent(); }
1525         ;
1526
1527 layout  :                                       { hsindentoff(); }
1528         ;
1529
1530 ccurly  :
1531          CCURLY
1532                 {
1533                   FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1534                   hsendindent();
1535                 }
1536         ;
1537
1538 vccurly :  { expect_ccurly = 1; }  vccurly1  { expect_ccurly = 0; }
1539         ;
1540
1541 vccurly1:
1542          VCCURLY
1543                 {
1544                   FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1545                   hsendindent();
1546                 }
1547         | error
1548                 {
1549                   yyerrok;
1550                   FN = NULL; SAMEFN = 0; PREVPATT = NULL;
1551                   hsendindent();
1552                 }
1553         ;
1554         
1555 %%