summaryrefslogtreecommitdiff
path: root/web/web2w/pascal.y
blob: 4d6f3671407b8d68dda0c688e7693ed44c067413 (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
%{
/*
This file is part of web2w.
Copyright 2017 Martin Ruckert

web2w is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web2w is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with web2w.  If not, see <http://www.gnu.org/licenses/>.
 
Martin Ruckert, Hochschule Muenchen, Lothstrasse 64, 80336 Muenchen
*/

#include <stdio.h>
#include "web2w.h"

/* the tag=token number of the left hand side symbol of a rule */
#define LHSS (yyr1[yyn]+FIRST_PASCAL_TOKEN-3)

  static int function=0;

%}

%code requires { 
#define PPSTYPE token *
#define YYSTYPE PPSTYPE

  extern int ppparse(void);
  extern int ppdebug;
 }

%token-table
%defines
%error_verbose
%debug
%define api.prefix "pp"
%expect 1

%token PEOF 0    "end of file"
%token WEBEOF      "end of web"
%token HEAD
%token BAR
%token PLEFT 
%token MLEFT 
%token RIGHT
%token OPEN 
%token CLOSE
%token TEXT
%token NL 
%token HASH
%token NMACRO
%token OMACRO
%token PMACRO
%token PARAM
%token EQ 
%token EQEQ
%token ATSTAR
%token ATSPACE
%token ATD
%token ATF
%token ATLESS 
%token ATGREATER
%token ELIPSIS
%token ATP
%token OCTAL
%token HEX
%token ATAT
%token ATDOLLAR 
%token ATLEFT
%token ATRIGHT
%token ATINDEX
%token ATINDEXTT
%token ATINDEX9
%token ATT 
%token ATEQ
%token ATAND
%token ATBACKSLASH
%token ATEX
%token ATQM
%token ATCOMMA
%token ATSLASH
%token ATBAR
%token ATHASH
%token ATPLUS
%token ATSEMICOLON
%token STRING
%token CHAR
%token INDENT
%token METACOMMENT
%token CSEMICOLON
%token ID 

%token WDEBUG
%token WSTAT
%token WINIT
%token WTINI
%token WTATS
%token WGUBED

%token PRETURN  "return"

%token FIRST_PASCAL_TOKEN 

%token PPLUS	"+"
%token PMINUS	"-"
%token PSTAR	"*"
%token PSLASH	"/"
%token PEQ	"="
%token PNOTEQ	"<>"
%token PLESS	"<"
%token PGREATER	">"
%token PLESSEQ	"<="
%token PGREATEREQ	">="
%token POPEN	"("
%token PCLOSE	")"
%token PSQOPEN	"["
%token PSQCLOSE	"]"
%token PASSIGN	":="
%token PDOT	"."
%token PCOMMA	","
%token PSEMICOLON	";" 
%token PMOD	"mod"
%token PDIV	"div"
%token PNIL	"nil"
%token POR	"or"
%token PAND	"and"
%token PNOT	"not"
%token PIF	"if"
%token PTHEN	"then"
%token PELSE	"else"
%token PREPEAT	"repeat"
%token PUNTIL	"until"
%token PWHILE	"while"
%token PDO	"do"
%token PFOR	"for"
%token PTO	"to"
%token PDOWNTO	"downto" 
%token PBEGIN	"begin"
%token PEND	"end"
%token PGOTO	"goto"
%token PINTEGER	"0-9"
%token PREAL	"real"
%token POTHERS	"others"
%token PSTRING	"'...'"
%token PCHAR	"'.'"
%token PTYPECHAR "char type"
%token PTYPEBOOL "bool type"
%token PTYPEINT "integer type"
%token PTYPEREAL "real type"
%token PTYPEINDEX "index type"

%token PID	"identifier"
%token PDEFVARID "variable definition"
%token PDEFPARAMID "parameter definition"
%token PDEFREFID "reference parameter definition"
%token PCONSTID	"constant"
%token PDEFCONSTID	"constant definition"
%token PDEFTYPEID	"typename definition"
%token PDEFTYPESUBID	"subrange typename definition"
%token PARRAYFILETYPEID "array of file type"
%token PARRAYFILEID "array of file name"
%token PFUNCID	"functionname"
%token PDEFFUNCID	"functionname definition"
%token PPROCID	"procedurename"
%token PCALLID  "call"
%token PRETURNID "return value"


%token PEXIT    "final_end"
%token PFBEGIN  "function begin"
%token PFEND    "function end"
%token PDOTDOT	".."
%token PCOLON	":"
%token PUP	"^"
%token PIN	"in"
%token PCASE	"case"
%token POF	"of"
%token PWITH	"with"
%token PCONST	"const"
%token PVAR	"var"
%token PTYPE	"type"
%token PARRAY	"array"
%token PRECORD	"record"
%token PSET	"set"
%token PFILE	"file"
%token PFUNCTION	"function" 
%token PPROCEDURE	"procedure"
%token PLABEL	"label"
%token PPACKED	"packed"
%token PPROGRAM	"program"
%token PFORWARD	"forward"

%token CIGNORE
%token CLABEL
%token CLABELN
%token CINTDEF
%token CSTRDEF
%token CMAIN
%token CMAINEND
%token CUNION
%token CTSUBRANGE
%token CINT
%token CREFID "reference variable"
%token CRETURN  "C function return"
%token CPROCRETURN  "C procedure return"
%token CCASE    "C case"
%token CCOLON   "C :"
%token CBREAK   "break"
%token CEMPTY   "empty statement"
%%

program: programheading  globals  
         PBEGIN statements PEND PDOT 
         { CHGTAG($3,CMAIN); CHGTAG($5,CMAINEND); IGN($6); 
           wsemicolon($4,$5);
         }
       ;

programheading: PPROGRAM PID PSEMICOLON  {IGN($2); IGN($3); }
              ;

globals: labels constants types variables procedures ;


labels:
      | PLABEL labellist PSEMICOLON  { IGN($3); }
      ;

labellist: labeldecl
         | labellist PCOMMA labeldecl  { IGN($2); }  
         ;

labeldecl: NMACRO  {IGN($1); SYM($1)->obsolete=1; } 
         | PINTEGER  {IGN($1); }
         | PEXIT  {IGN($1); } 
         | labeldecl PPLUS PINTEGER  {IGN($2); IGN($3); } 
         ; 

constants: 
         | PCONST constdefinitions
         | PCONST constdefinitions conststringdefinition
         ; 

constdefinitions:  constdefinition
                |  constdefinitions  constdefinition 
                ;

constdefinition: PID PEQ PINTEGER PSEMICOLON { LNK($1,$2); LNK($2,$4);  
                   SETVAL($1,getval($3)); CHGID($1,PCONSTID);
                   CHGTAG($1,CINTDEF); }
               ;

conststringdefinition: PID PEQ PSTRING PSEMICOLON 
               { seq($1,$4); CHGID($1,PCONSTID);
                 CHGTAG($1,CSTRDEF);CHGTAG($2,PASSIGN); } 
                     ;


types:
     | PTYPE  typedefinitions { IGN($1); }
     ;

typedefinitions: typedefinition
               | typedefinitions typedefinition
               ;

typedefinition: PID PEQ subrange PSEMICOLON 
                                           { DBG(dbgparse,"New Subrange Type: %s\n",
						  SYM($1)->name);
                                              LNK($1,$2); IGN($2);LNK($2,$4);
                                              CHGTYPE($1,$3); 
                                              CHGTAG($1,PDEFTYPEID);
                                              CHGTAG($2,CTSUBRANGE); UP($2,$3);
                                             }
              | PID PEQ type PSEMICOLON 
                                           { DBG(dbgparse,"New Type: %s\n",
					      SYM($1)->name);
                                              LNK($1,$2); IGN($2); LNK($2,$4); 
                                              CHGTYPE($1,$3); LNK($3,$4);
                                              CHGTAG($1,PDEFTYPEID);
                                            }
              ;


subrange: iconst PDOTDOT iconst  
          {$$=join(PDOTDOT,$1,$3,$3->value-$1->value+1); }
        | PTYPECHAR  
          {$$=join(PDOTDOT,join(PTYPECHAR,$1,$1,0),
                           join(PTYPECHAR,$1,$1,255),256); }
        ;

iconst: signed_iconst   {$$=$1; }
      | iconst PPLUS simple_iconst  
          {$$=join(PPLUS,$1,$3,$1->value+$3->value); }
      | iconst PMINUS simple_iconst  
          {$$=join(PMINUS,$1,$3,$1->value-$3->value); }
      ;

signed_iconst: simple_iconst   {$$=$1; }
             | PPLUS simple_iconst   {$$=join(PPLUS,NULL,$2,$2->value); }
             | PMINUS simple_iconst 
               {$$=join(PMINUS,NULL,$2,-($2->value)); }
             ;


simple_iconst: PINTEGER   {$$=join(PINTEGER,$1,NULL,getval($1)); }
             | NMACRO    {$$=join(NMACRO,$1,NULL,getval($1)); }
             | PCONSTID  {$$=join(PCONSTID,$1,NULL,getval($1)); }
             ;

file_type: packed PFILE POF typename  {$$=$2; }
         |packed PFILE POF subrange  {$$=$2; }
         ;

packed: PPACKED | ;

typename:  PTYPEINT {$$=NULL; }
        | PTYPEREAL  {$$=NULL; }
        | PTYPEBOOL  {$$=NULL; }
        | PID  {$$=NULL; }
        ;


record_type: packed PRECORD fields PEND  { LNK($2,$4); LNK($3,$4);  
                                        if ($3) CHGTAG($4,PSEMICOLON); else IGN($4); $$=NULL; }
           | packed PRECORD variant_part PEND  
             { LNK($2,$4); LNK($3,$4);  IGN($4); $$=NULL; }
           | packed PRECORD fields PSEMICOLON variant_part PEND 
             { LNK($2,$6); LNK($3,$4); LNK($5,$6); IGN($6); $$=NULL; }
           ;


fields: recordsection {$$=$1; }
      | fields PSEMICOLON recordsection  { LNK($1,$2); $$=$3; }
      ;

/* in a recordsection the first PID links to the PCOLON, 
   the recordsection points to the PCOLON 
*/
recordsection: {$$=NULL; }
             | recids PCOLON type {LNK($1,$2); IGN($2); $$=$2; }
             | recids PCOLON subrange 
               {LNK($1,$2); CHGTAG($2,CTSUBRANGE); UP($2,$3); $$=$2; }
             ;
      
/* recids point to the first PID which is changed to PDEFVARID */
recids: PID  {$$=$1;  CHGTAG($1,PDEFVARID); }
      | recids PCOMMA PID  {$$=$1; }
      ;

variant_part: PCASE PID POF variants {IGN($1);IGN($2); 
                                       CHGTAG($3,CUNION); $$=$3; }
            ;

variants: variant
        | variants variant 
        ;


variant: PINTEGER PCOLON POPEN recordsection PCLOSE  PSEMICOLON  
                 { IGN($1); IGN($2); IGN($3); 
                   LNK($4,$5); 
                   IGN($5); }
       | PINTEGER PCOLON POPEN recordsection  PSEMICOLON 
                               recordsection PCLOSE  PSEMICOLON 
                 { IGN($1); IGN($2); CHGTAG($3,PRECORD);  
                   LNK($3,$8); LNK($4,$5); LNK($6,$7); CHGTAG($7,PSEMICOLON); } 
       ;


type: typename
    | file_type 
    | record_type 
    ;




array_type: packed PARRAY PSQOPEN iconst PDOTDOT iconst PSQCLOSE 
            POF type {LNK($2,$3); 
                 UP($2,join(PDOTDOT,$4,$6,$6->value-$4->value+1)); 
                 LNK($3,$5); LNK($5,$7); LNK($7,$8);$$=$8; }
          | packed PARRAY PSQOPEN iconst PDOTDOT iconst PSQCLOSE 
            POF subrange {LNK($2,$3); 
                 UP($2,join(PDOTDOT,$4,$6,$6->value-$4->value+1)); 
                 LNK($3,$5); LNK($5,$7); LNK($7,$8);
                 CHGTAG($8,CTSUBRANGE); UP($8,$9);$$=$8; }
          | packed PARRAY PSQOPEN PID      PSQCLOSE POF type {LNK($2,$3); 
                 UP($2,$4);LNK($3,$4); LNK($4,$5); LNK($5,$6);$$=$6; }
          | packed PARRAY PSQOPEN PID      PSQCLOSE POF subrange 
             {LNK($2,$3); UP($2,$4); LNK($3,$4); LNK($4,$5); 
              LNK($5,$6); CHGTAG($6,CTSUBRANGE); UP($6,$7);$$=$6; }
          | packed PARRAY PSQOPEN PTYPECHAR  PSQCLOSE 
            POF type {LNK($2,$3); UP($2,join(PDOTDOT,
            join(PTYPECHAR,$1,$1,0),join(PTYPECHAR,$1,$1,255),256)); 
            $3->link=join(PTYPECHAR,$3,$5,256); $3->link->link=$5; 
             /* the PTYPECHAR comes from a macroexpansion, so we can not 
             link it directly */ LNK($5,$6); $$=$6; }
          ;

variables:
         | PVAR vardeclarations { IGN($1); }
         ;

vardeclarations: vardeclaration
               | vardeclarations vardeclaration  
               ;

vardeclaration: varids PCOLON type PSEMICOLON { LNK($1,$2);
                                                IGN($2); LNK($2,$4); }
              | varids PCOLON array_type PSEMICOLON { LNK($1,$2);
                                                      IGN($2); LNK($3,$4); LNK($2,$4); }
              | varids PCOLON subrange PSEMICOLON { LNK($1,$2); 
                                                    CHGTAG($2,CTSUBRANGE); UP($2,$3); LNK($2,$4); }
             ;
  

varids: entire_var  {CHGTAG($1,PDEFVARID); $$=$1; }
      | varids PCOMMA entire_var  {LNK($1,$3);$$=$3; }
      ;


procedures:
          | procedures procedure 
          | procedures function 
          ;

locals: PVAR localvardeclarations { CHGTAG($1,PBEGIN); } 
      | PLABEL locallabellist PSEMICOLON localvariables 
        { CHGTAG($1,PBEGIN); IGN($3); }
      ;

locallabellist: locallabeldecl
         | locallabellist PCOMMA locallabeldecl  {IGN($2); }  
         ;

locallabeldecl: NMACRO  {IGN($1);  SYM($1)->obsolete=1; localize($1); } 
         | PINTEGER  {IGN($1); }  
         | labeldecl PPLUS PINTEGER  {IGN($2); IGN($3); } 
         ; 


localvariables:
         | PVAR localvardeclarations { IGN($1); }
         ;

localvardeclarations: localvardeclaration
               | localvardeclarations localvardeclaration  
               ;

localvardeclaration: localvarids PCOLON type PSEMICOLON 
                 { LNK($1,$2); IGN($2); LNK($2,$4); }
              | localvarids PCOLON array_type PSEMICOLON 
                 { LNK($1,$2); IGN($2); LNK($3,$4); LNK($2,$4); }
              | localvarids PCOLON subrange PSEMICOLON 
                 { LNK($1,$2); CHGTAG($2,CTSUBRANGE); 
                   UP($2,$3); LNK($2,$4); }
             ;
localvarids: localentire_var  {CHGTAG($1,PDEFVARID); $$=$1; }
      | localvarids PCOMMA localentire_var  {LNK($1,$3);$$=$3; }
      ;

localentire_var: PID {$$=$1; localize($1); }
         | CREFID {$$=$1; CHGTAG($1,PID); 
            CHGID($1,PID); localize($1); } 
          ;




procedure: pheading locals  PBEGIN statements PEND  PSEMICOLON 
          {IGN($3); IGN($6);  wend($4,$5); wsemicolon($4,$5); 
           scope_close(); }
         | pheading PBEGIN statements PEND  PSEMICOLON 
	 { IGN($5); wend($3,$4); wsemicolon($3,$4);  scope_close(); }
         | pheading PFORWARD  PSEMICOLON { scope_close(); }
         ;
 
function: fheading  PBEGIN {function=1; } statements PEND  PSEMICOLON 
              {function=0; wreturn($4, 1,NULL); IGN($6);
               wsemicolon($4,$5); scope_close(); }
        | fheading locals   PBEGIN {function=1; } 
          statements PEND  PSEMICOLON 
	{ int f_no=$1->sym_no;
          function=0; 
          if (f_no==x_over_n || f_no==xn_over_d)
          { DBG(dbgcweb,"Discovered function %s; in line %d\n",
               SYM($1)->name,$1->lineno);
            CHGTAG($3,PFBEGIN); $3->sym_no=f_no;
            CHGTAG($6,PFEND); $6->sym_no=f_no; 
	  }
	  else
	    { IGN($3);
	      wreturn($5,1,NULL);
	    } 
	  wsemicolon($5,$6);
	  IGN($7);
	  scope_close();
	}
        ;

pid: PID {scope_open(); $$=$1; START_PARAM; }
   |PPROCID  {scope_open(); $$=$1; START_PARAM; } 
   |PFUNCID  {scope_open(); $$=$1; START_PARAM; } 
   ;

pheading: PPROCEDURE pid PSEMICOLON  
           { LNK($1,$3); CHGID($2,PPROCID); CHGVALUE($2,1); IGN($3); }
        | PPROCEDURE pid POPEN formals PCLOSE PSEMICOLON  
          { LNK($1,$3); CHGID($2,PPROCID); CHGVALUE($2,param_mask);
	    LNK($4,$5); IGN($6); }
        ;

fheading: PFUNCTION pid PCOLON typename PSEMICOLON  
          {$$=$2; LNK($1,$3); CHGID($2,PFUNCID); 
           CHGVALUE($2,1);IGN($3); LNK($3,$5); IGN($5); }
        | PFUNCTION pid POPEN formals PCLOSE 
          PCOLON typename PSEMICOLON {$$=$2; LNK($1,$3); 
             CHGID($2,PFUNCID); CHGVALUE($2,param_mask); 
              LNK($4,$5); LNK($5,$6); IGN($6); LNK($6,$8); IGN($8); }
        ;

formals: formalparameters {$$=$1; }
       | formals PSEMICOLON  formalparameters 
         { LNK($1,$2); CHGTAG($2,PCOMMA); $$=$3; }
       ;  

formalparameters: params PCOLON typename 
         { LNK($1,$2); IGN($2); $$=$2; } 
                ;

params: param {$$=$1; }
        | params PCOMMA param  {LNK($1,$3);$$=$3; }
        ;

param: entire_var  {NEXT_PARAM; CHGTAG($1,PDEFPARAMID); $$=$1; }
     | PVAR entire_var  {REF_PARAM; NEXT_PARAM; IGN($1);
        CHGTAG($2,PDEFREFID);CHGID($2,CREFID); $$=$2; }
     ;


proc_stmt: PPROCID POPEN args PCLOSE {CHGTAG($1,PCALLID); $$=$1; 
           UP($2,$1); pstring_args($1,$3); }
         | PCALLID POPEN args PCLOSE  
           {$$=$1; UP($2,$1); pstring_args($1,$3); }
         | PPROCID  {CHGTAG($1,PCALLID); $$=$1; }
         | PCALLID  { $$=$1; }
         ;

function_call: PFUNCID POPEN args PCLOSE  
               {CHGTAG($1,PCALLID); $$=$4; UP($2,$1); }
             | PCALLID POPEN args PCLOSE  { $$=$4; UP($2,$1); }
             | PFUNCID  {CHGTAG($1,PCALLID);$$=$1; }
             | PCALLID  {$$=$1; }
             ;


args: arg  {$$=$1; }
    | args PCOMMA arg  
      {if ($3==NULL) $$=$1; else if ($1==NULL) $$=$3; 
       else $$=join(PCOMMA,$1,$3,0); }
      ;

arg: expression {$$=$1; }
     | write_arg  {$$=$1; }
     | STRING  {$$=$1; }
     | CHAR  {$$=$1; }
     ;


write_arg: expression PCOLON expression {$$=$2; }
            ;

statements: statement {$$=$1; }
          | statements PSEMICOLON statement 
           {$$=join(PSEMICOLON,$1,$3,0); }

          ;

statement: stmt {$$=$1; }
         | label PCOLON stmt  { clabel($1,0);$$=join(PCOLON,$1,$3,0); } 
         | PEXIT PCOLON stmt  
           { IGN($1); IGN($2); $$=join(PCOLON,$1,$3,0); }
         ;

goto_stmt: PGOTO label  { clabel($2,1); $$=join(PGOTO,$2,NULL,0); }
         | PGOTO PEXIT  { IGN($1); $$=$2; }
         | CIGNORE PEXIT  { $$=$2; }
         | PRETURN  { if (function) clabel($1,1); 
           else { CHGTAG($1,CPROCRETURN);$1->sym_ptr->value++; }
                  $$=$1; }
         ;


label: PINTEGER 
     | NMACRO 
     | CLABEL 
     | NMACRO PPLUS PINTEGER  {seq($1,$3); $$=$1; }
     ; 



stmt: simple_stmt | structured_stmt ;

simple_stmt:  empty_stmt | assign_stmt | return_stmt | goto_stmt | proc_stmt  ;

empty_stmt: {$$=join(CEMPTY,NULL,NULL,0); } ;

assign_stmt: variable PASSIGN expression {$$=$2; }
           | variable PASSIGN STRING {$$=$2; pstring_assign($1,$3); }
           | variable PASSIGN POPEN STRING PCLOSE
             {$$=$2;  pstring_assign($1,$4); }
           ;

return_stmt: PFUNCID PASSIGN expression  { $$=$1; }
           | CRETURN CIGNORE expression {$$=$1; }
           | CRETURN CIGNORE expression CIGNORE CIGNORE 
             {$$=join(CRETURN,NULL,NULL,0);  }
           | CRETURN {$$=$1; }
           | CPROCRETURN {$$=$1; }
           ;
          

structured_stmt: compound_stmt | conditional_stmt | repetitive_stmt  ;

compound_stmt: PBEGIN statements PEND 
               {$$=join(PBEGIN,$2,NULL,0); wsemicolon($2,$3); } ;

conditional_stmt: if_stmt | case_stmt;

if_stmt: PIF expression PTHEN statement {$$=join(PIF,$4,NULL,0); }
       | PIF expression PTHEN statement PELSE statement 
       {wsemicolon($4,$5);  $$=join(PELSE,$4,$6,0); } 
       ;

case_stmt: PCASE expression POF case_list PEND {LNK($1,$3);
                wsemicolon($4,$5);$$=join(PCASE,$4,NULL,0); }
         | PCASE expression POF case_list PSEMICOLON PEND 
           {LNK($1,$3);$$=join(PCASE,$4,NULL,0); }
         ;

case_list: case_element 
         | case_list PSEMICOLON case_element {$$=join(CCASE,$1,$3,0); 
                                              wsemicolon($1,$2); CHGTAG($2,CBREAK); UP($2,$1); }
          | case_list CBREAK case_element
            {$$=join(CCASE,$1,$3,0); /* etex parses same module twice */ }
         ;

case_element: case_labels PCOLON statement { $$=$3; }
            | POTHERS statement {$$=$2; }
            ;

case_labels: case_label
           | case_labels PCOMMA  case_label 
             {CHGTAG($2,CCOLON); CHGTEXT($2,": "); }
           | case_labels CCOLON  case_label
           ;

case_label: PINTEGER { winsert_after($1->previous,CCASE,"case "); }
          | NMACRO { winsert_after($1->previous,CCASE,"case "); }
          | PINTEGER PPLUS NMACRO  
            {winsert_after($1->previous,CCASE,"case "); }
          | NMACRO PPLUS NMACRO  
            {winsert_after($1->previous,CCASE,"case "); }
          | NMACRO PPLUS PINTEGER  
            {winsert_after($1->previous,CCASE,"case "); }
          | CCASE NMACRO
          | CCASE PINTEGER
          | CCASE NMACRO PPLUS NMACRO 
          | NMACRO PMINUS NMACRO PPLUS NMACRO  
	  {winsert_after($1->previous,CCASE,"case ");  /* etex */ }
          ;

repetitive_stmt: while_stmt | repeat_stmt | for_stmt ;

while_stmt: PWHILE expression PDO statement 
            {LNK($1,$3); $$=join(PWHILE,$4,NULL,0); } ;

repeat_stmt: PREPEAT statements PUNTIL expression  
            { wsemicolon($2,$3); $$=join(PREPEAT,$2,NULL,0); } ;

for_stmt: PFOR PID PASSIGN expression PTO varlimit PDO statement
              { mark_for_variable($2,$1->lineno,0,VAR_LOOP); 
                DBG(dbgfor,"for variable %s, limit variable in line %d\n",
                SYM($2)->name,$2->lineno);
                $$=join(PFOR,$8,NULL,0);LNK($1,$5);LNK($5,$7); } 
        | PFOR PID PASSIGN expression PTO iconst PDO statement 
	      { mark_for_variable($2,$1->lineno,$6->value,TO_LOOP); 
                DBG(dbgfor,"for variable %s, limit up in line %d\n",
                SYM($2)->name,$2->lineno);
                $$=join(PFOR,$8,NULL,0);LNK($1,$5);LNK($5,$7); } 
        | PFOR PID PASSIGN expression PDOWNTO iconst PDO statement
	      { mark_for_variable($2,$1->lineno,$6->value,DOWNTO_LOOP); 
                DBG(dbgfor,"for variable %s, limit down in line %d\n",
                SYM($2)->name,$2->lineno);
                $$=join(PFOR,$8,NULL,0);LNK($1,$5);LNK($5,$7); } 
        ;

varlimit: variable | variable PMINUS expression | variable PPLUS expression| iconst PSTAR expression;

variable: PID
        | CREFID
        | indexed_var 
        | field_var 
        | file_var;


entire_var: PID {$$=$1; }
         | CREFID {$$=$1; CHGTAG($1,PID); CHGID($1,PID); } 
          ;

indexed_var: variable PSQOPEN expression PSQCLOSE 
           | variable PSQOPEN STRING PSQCLOSE 
           | PARRAYFILEID PSQOPEN expression PSQCLOSE 
           ;


field_var: variable PDOT PID 
         ;

file_var: variable PUP 
        ;

expression: simple_expr {$$=$1; }
          | simple_expr relop simple_expr {$$=$3; }
          | simple_expr PEQ STRING {$$=$3; }
          ;

relop: PEQ | PNOTEQ | PLESS | PLESSEQ | PGREATER | PGREATEREQ;

simple_expr: term  {$$=$1; }
           | sign term  {$$=$2; }
           | simple_expr addop term {$$=$3; }
           | simple_expr addop sign term  {$$=$4; }
           ;

sign: PPLUS | PMINUS ;

addop: PPLUS | PMINUS | POR ;

term: factor {$$=$1; }
    | term mulop factor  {$$=$3; }
    ;

mulop: PSTAR 
     | PSLASH { DBG(dbgslash,"Pascal / in line %d\n",$1->lineno); }
     | PDIV 
     | PMOD 
     | PAND ;

factor: variable 
      | unsigned_const
      | POPEN expression PCLOSE {$$=$3; }
      | function_call
      | PNOT factor  {$$=$2; }
      ;


unsigned_const: real
              | PINTEGER
              | NMACRO
              | PSTRING
              | PCHAR
              | PCONSTID
              ;

real: PREAL
    | PINTEGER PDOT PINTEGER  {$$=$3; /* used in line 2361 */};
 

%%

const char *tagname(int tag)
{ return yytname[YYTRANSLATE(tag)];
}