aboutsummaryrefslogtreecommitdiff
path: root/gcc/treelang/parse.y
blob: e7c98386aaba4d3f0533921f816cb2f40de0b6f2 (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
%{ /* -*- c -*- emacs mode c */
  /* 

     TREELANG Compiler parser.  

     ---------------------------------------------------------------------

     Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.

     This program 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 2, or (at your option) any
     later version.

     This program 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 this program; if not, write to the Free Software
     Foundation, 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.

     In other words, you are welcome to use, share and improve this program.
     You are forbidden to forbid anyone else to use, share and improve
     what you give them.   Help stamp out software-hoarding!  

     ---------------------------------------------------------------------

     Written by Tim Josling 1999-2001, based in part on other parts of
     the GCC compiler.
 
   */

  /* 

     Grammar Conflicts
     *****************

     There are no conflicts in this grammar.  Please keep it that way.

   */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "diagnostic.h"
#include "timevar.h"

#include "treelang.h"
#include "treetree.h"

#define YYDEBUG 1
#define YYPRINT(file, type, value) print_token (file, type, value) 
#define YYERROR_VERBOSE YES

/* My yylex routine used to intercept calls to flex generated code, to
     record lex time.  */
int yylex (void);
static inline int my_yylex (void);
/* Call lex, but ensure time is charged to TV_LEX.  */ 
static inline int
my_yylex (void)
{
  int res;
  timevar_push (TV_LEX);
  res = yylex ();
  timevar_pop (TV_LEX);
  return res;
}
#define yylex my_yylex

extern int option_parser_trace;

/* Local prototypes.  */

static void yyerror (const char *error_message);
int yyparse (void);
void print_token (FILE * file, unsigned int type ATTRIBUTE_UNUSED, YYSTYPE value);
static struct prod_token_parm_item *reverse_prod_list (struct prod_token_parm_item *old_first);
static void ensure_not_void (unsigned int type, struct prod_token_parm_item* name);
static int check_type_match (int type_num, struct prod_token_parm_item *exp);
static int get_common_type (struct prod_token_parm_item *type1,
                            struct prod_token_parm_item *type2);
static struct prod_token_parm_item *make_integer_constant (struct prod_token_parm_item* value);
static struct prod_token_parm_item *make_plus_expression
  (struct prod_token_parm_item* tok, struct prod_token_parm_item* op1,
   struct prod_token_parm_item* op2, int type_code, int prod_code);
static void set_storage (struct prod_token_parm_item *prod);

/* File global variables.  */

static struct prod_token_parm_item *current_function=NULL;

%}

/* Not %raw - seems to have bugs.  */
%token_table

/* Punctuation.  */
%token RIGHT_BRACE
%token LEFT_BRACE
%token RIGHT_SQUARE_BRACKET
%token LEFT_SQUARE_BRACKET
%token RIGHT_PARENTHESIS
%token LEFT_PARENTHESIS
%token SEMICOLON
%token ASTERISK
%token COMMA
%right EQUALS
%right ASSIGN
%left  tl_PLUS
%left  tl_MINUS

/* Literals.  */
%token INTEGER

/* Keywords.  */
%token IF
%token ELSE
%token tl_RETURN
%token CHAR
%token INT
%token UNSIGNED
%token VOID
%token TYPEDEF
%token NAME
%token STATIC
%token AUTOMATIC
%token EXTERNAL_DEFINITION
%token EXTERNAL_REFERENCE

/* Tokens not passed to parser.  */
%token WHITESPACE
%token COMMENT

/* Pseudo tokens - productions.  */
%token PROD_VARIABLE_NAME
%token PROD_TYPE_NAME
%token PROD_FUNCTION_NAME
%token PROD_INTEGER_CONSTANT
%token PROD_PLUS_EXPRESSION
%token PROD_MINUS_EXPRESSION
%token PROD_ASSIGN_EXPRESSION
%token PROD_VARIABLE_REFERENCE_EXPRESSION
%token PROD_PARAMETER
%token PROD_FUNCTION_INVOCATION
%expect 0 
%% 

file:
/* Nil.   */ {
  /* Nothing to do.  */
}
|declarations {
  /* Nothing to do.  */
}
;


declarations:
declaration {
  /* Nothing to do.  */
}
| declarations declaration {
  /* Nothing to do.  */
}
;

declaration:
variable_def {
  /* Nothing to do.  */
}
|function_prototype {
  /* Nothing to do.  */
}
|function {
  /* Nothing to do.  */
}
;

variable_def:
storage typename NAME init_opt SEMICOLON {
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *prod;
  tok = $3;
  prod = make_production (PROD_VARIABLE_NAME, tok);
  SYMBOL_TABLE_NAME (prod) = tok;
  EXPRESSION_TYPE (prod) = $2;
  VAR_INIT (prod) = $4;
  NUMERIC_TYPE (prod) = NUMERIC_TYPE (( (struct prod_token_parm_item*)EXPRESSION_TYPE (prod)));
  ensure_not_void (NUMERIC_TYPE (prod), tok);
  if (insert_tree_name (prod))
    {
      YYERROR;
    }
  STORAGE_CLASS_TOKEN (prod) = $1;
  set_storage (prod);

  if (VAR_INIT (prod))
    {
      if (! ((struct prod_token_parm_item*)VAR_INIT (prod))->tp.pro.code)
        abort ();
    if (STORAGE_CLASS (prod) == EXTERNAL_REFERENCE_STORAGE)
      {
        fprintf (stderr, "%s:%i:%i: External reference variables may not have initial value\n",
		 tok->tp.tok.location.file,
		 tok->tp.tok.location.line, tok->tp.tok.charno);
        print_token (stderr, 0, tok);
        errorcount++;
        YYERROR;
      }
    }
  prod->tp.pro.code = tree_code_create_variable
    (STORAGE_CLASS (prod), 
     ((struct prod_token_parm_item*)SYMBOL_TABLE_NAME (prod))->tp.tok.chars,
     ((struct prod_token_parm_item*)SYMBOL_TABLE_NAME (prod))->tp.tok.length,
     NUMERIC_TYPE (prod),
     VAR_INIT (prod)? ((struct prod_token_parm_item*)VAR_INIT (prod))->tp.pro.code:NULL,
     tok->tp.tok.location);
  if (!prod->tp.pro.code) 
    abort ();
}
;

storage:
STATIC
|AUTOMATIC
|EXTERNAL_DEFINITION
|EXTERNAL_REFERENCE
;

parameter:
typename NAME {
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *prod;
  struct prod_token_parm_item *prod2;
  tok = $2;
  prod = make_production (PROD_VARIABLE_NAME, tok);
  SYMBOL_TABLE_NAME (prod) = $2;
  EXPRESSION_TYPE (prod) = $1;
  NUMERIC_TYPE (prod) = NUMERIC_TYPE (( (struct prod_token_parm_item*)EXPRESSION_TYPE (prod)));
  ensure_not_void (NUMERIC_TYPE (prod), tok);
  if (insert_tree_name (prod))
    {
      YYERROR;
    }
  prod2 = make_production (PROD_PARAMETER, tok);
  VARIABLE (prod2) = prod;
  $$ = prod2;
}
;

function_prototype:
storage typename NAME LEFT_PARENTHESIS parameters RIGHT_PARENTHESIS SEMICOLON {
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *prod;
  struct prod_token_parm_item *type;
  struct prod_token_parm_item* first_parms;
  struct prod_token_parm_item* last_parms;
  struct prod_token_parm_item* this_parms;
  struct prod_token_parm_item *this_parm;
  struct prod_token_parm_item *this_parm_var;
  tok = $3;
  prod = make_production (PROD_FUNCTION_NAME, $3);
  SYMBOL_TABLE_NAME (prod) = $3;
  EXPRESSION_TYPE (prod) = $2;
  NUMERIC_TYPE (prod) = NUMERIC_TYPE (( (struct prod_token_parm_item*)EXPRESSION_TYPE (prod)));
  PARAMETERS (prod) = reverse_prod_list ($5); 
  insert_tree_name (prod);
  STORAGE_CLASS_TOKEN (prod) = $1;
  set_storage (prod);
  switch (STORAGE_CLASS (prod))
    { 
    case STATIC_STORAGE:
    case EXTERNAL_DEFINITION_STORAGE:
      break;
      
    case AUTOMATIC_STORAGE:
      fprintf (stderr, "%s:%i:%i: A function cannot be automatic\n",
	       tok->tp.tok.location.file,
	       tok->tp.tok.location.line, tok->tp.tok.charno);
      print_token (stderr, 0, tok);
      errorcount++;
      YYERROR;
      break;

    default:
      abort ();
    }
  type = EXPRESSION_TYPE (prod);
  /* Create a parameter list in a non-front end specific format.  */
  for (first_parms = NULL, last_parms = NULL, this_parm = PARAMETERS (prod);
       this_parm;
       this_parm = this_parm->tp.pro.next)
    {
      if (this_parm->category != production_category)
        abort ();
      this_parm_var = VARIABLE (this_parm);
      if (!this_parm_var)
        abort ();
      if (this_parm_var->category != production_category)
        abort ();
      this_parms = my_malloc (sizeof (struct prod_token_parm_item));
      if (!this_parm_var->tp.pro.main_token)
        abort ();
      this_parms->tp.par.variable_name = this_parm_var->tp.pro.main_token->tp.tok.chars;
      this_parms->category = parameter_category;
      this_parms->type = NUMERIC_TYPE 
        (( (struct prod_token_parm_item*)EXPRESSION_TYPE (this_parm_var)));
      if (last_parms)
        {
          last_parms->tp.par.next = this_parms;
          last_parms = this_parms;
        }
      else
        {
          first_parms = this_parms;
          last_parms = this_parms;
        }
      this_parms->tp.par.where_to_put_var_tree = 
        & (( (struct prod_token_parm_item*)VARIABLE (this_parm))->tp.pro.code);
    }
  FIRST_PARMS (prod) = first_parms;

  prod->tp.pro.code = tree_code_create_function_prototype
    (tok->tp.tok.chars, STORAGE_CLASS (prod), NUMERIC_TYPE (type),
     first_parms, tok->tp.tok.location);
}
;

function:
NAME LEFT_BRACE {
  struct prod_token_parm_item *proto;
  struct prod_token_parm_item search_prod;
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *this_parm;
  tok = $1;
  SYMBOL_TABLE_NAME ((&search_prod)) = tok;
  search_prod.category = token_category;
  current_function = proto = lookup_tree_name (&search_prod);
  if (!proto)
    {
      fprintf (stderr, "%s:%i:%i: Function prototype not found\n",
	       tok->tp.tok.location.file,
	       tok->tp.tok.location.line, tok->tp.tok.charno);
      print_token (stderr, 0, tok);
      errorcount++;
      YYERROR;
    }
  if (!proto->tp.pro.code)
    abort ();
  tree_code_create_function_initial
    (proto->tp.pro.code, tok->tp.tok.location,
     FIRST_PARMS (current_function));

  /* Check all the parameters have code.  */
  for (this_parm = PARAMETERS (proto);
       this_parm;
       this_parm = this_parm->tp.pro.next)
    {
      if (! (struct prod_token_parm_item*)VARIABLE (this_parm))
        abort ();
      if (! (( (struct prod_token_parm_item*)VARIABLE (this_parm))->tp.pro.code))
        abort ();
    }
}
variable_defs_opt statements_opt RIGHT_BRACE {
  struct prod_token_parm_item* tok;
  tok = $1;
  tree_code_create_function_wrapup (tok->tp.tok.location);
  current_function = NULL;
}
;

variable_defs_opt:
/* Nil.   */ {
  $$ = 0;
}
|variable_defs {
  $$ = $1;
}
;

statements_opt:
/* Nil.   */ {
  $$ = 0;
}
|statements {
  $$ = $1;
}
;

variable_defs:
variable_def {
  /* Nothing to do.  */
}
|variable_defs variable_def {
  /* Nothing to do.  */
}
;

typename:
INT {
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *prod;
  tok = $1;
  prod = make_production (PROD_TYPE_NAME, tok);
  NUMERIC_TYPE (prod) = SIGNED_INT;
  prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
  $$ = prod;
}
|UNSIGNED INT {
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *prod;
  tok = $1;
  prod = make_production (PROD_TYPE_NAME, tok);
  NUMERIC_TYPE (prod) = UNSIGNED_INT;
  prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
  $$ = prod;
}
|CHAR {
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *prod;
  tok = $1;
  prod = make_production (PROD_TYPE_NAME, tok);
  NUMERIC_TYPE (prod) = SIGNED_CHAR;
  prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
  $$ = prod;
}
|UNSIGNED CHAR {
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *prod;
  tok = $1;
  prod = make_production (PROD_TYPE_NAME, tok);
  NUMERIC_TYPE (prod) = UNSIGNED_CHAR;
  prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
  $$ = prod;
}
|VOID {
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *prod;
  tok = $1;
  prod = make_production (PROD_TYPE_NAME, tok);
  NUMERIC_TYPE (prod) = VOID_TYPE;
  prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
  $$ = prod;
}
;

parameters:
parameter {
  /* Nothing to do.  */
  $$ = $1;
}
|parameters COMMA parameter {
  struct prod_token_parm_item *prod1;
  prod1 = $3;
  prod1->tp.pro.next = $1; /* Insert in reverse order.  */
  $$ = prod1;
}
;

statements:
statement {
  /* Nothing to do.  */
}
|statements statement {
  /* Nothing to do.  */
}
;

statement:
expression SEMICOLON {
  struct prod_token_parm_item *exp;
  exp = $1;
  tree_code_output_expression_statement (exp->tp.pro.code,
					 exp->tp.pro.main_token->tp.tok.location);
}
|return SEMICOLON {
  /* Nothing to do.  */
}
|if_statement {
  /* Nothing to do.  */
}
;

if_statement:
IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS {
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *exp;
  tok = $1;
  exp = $3;
  ensure_not_void (NUMERIC_TYPE (exp), exp->tp.pro.main_token);
  tree_code_if_start (exp->tp.pro.code, tok->tp.tok.location);
}
LEFT_BRACE statements_opt RIGHT_BRACE {
  /* Just let the statements flow.  */
}
ELSE {
  struct prod_token_parm_item* tok;
  tok = $1;
  tree_code_if_else (tok->tp.tok.location);
}
LEFT_BRACE statements_opt RIGHT_BRACE {
  struct prod_token_parm_item* tok;
  tok = $12;
  tree_code_if_end (tok->tp.tok.location);
}
;


return:
tl_RETURN expression_opt {
  struct prod_token_parm_item *type_prod;
  struct prod_token_parm_item* ret_tok;
  ret_tok = $1;
  type_prod = EXPRESSION_TYPE (current_function);
  if (NUMERIC_TYPE (type_prod) == VOID)
    if ($2 == NULL)
      tree_code_generate_return (type_prod->tp.pro.code, NULL);
    else
      {
        fprintf (stderr, "%s:%i:%i: Redundant expression in return\n",
		 ret_tok->tp.tok.location.file,
		 ret_tok->tp.tok.location.line, ret_tok->tp.tok.charno);
        print_token (stderr, 0, ret_tok);
        errorcount++;
        tree_code_generate_return (type_prod->tp.pro.code, NULL);
      }
  else
    if ($2 == NULL)
      {
        fprintf (stderr, "%s:%i:%i: Expression missing in return\n",
		 ret_tok->tp.tok.location.file,
		 ret_tok->tp.tok.location.line, ret_tok->tp.tok.charno); 
        print_token (stderr, 0, ret_tok);
        errorcount++;
      }
    else
      {
        struct prod_token_parm_item *exp;
        exp = $2;
        /* Check same type.  */
        if (check_type_match (NUMERIC_TYPE (type_prod), $2))
          {
            if (!type_prod->tp.pro.code)
              abort ();
            if (!exp->tp.pro.code)
              abort ();
            /* Generate the code. */
            tree_code_generate_return (type_prod->tp.pro.code, exp->tp.pro.code);
          }
      }
}
;

expression_opt:
/* Nil.   */ {
  $$ = 0;
}
|expression {
  struct prod_token_parm_item *exp;
  exp = $1;
  if (!exp->tp.pro.code)
    abort ();
  
  $$ = $1;
}
;

expression:
INTEGER {
  $$ = make_integer_constant ($1);
}
|variable_ref {
  $$ = $1;
}
|expression tl_PLUS expression {
  struct prod_token_parm_item *tok = $2;
  struct prod_token_parm_item *op1 = $1;
  struct prod_token_parm_item *op2 = $3;
  int type_code = get_common_type (op1, op2);
  if (!type_code)
    YYERROR;
  $$ = make_plus_expression
     (tok, op1, op2, type_code, EXP_PLUS);
}
|expression tl_MINUS expression %prec tl_PLUS {
  struct prod_token_parm_item *tok = $2;
  struct prod_token_parm_item *op1 = $1;
  struct prod_token_parm_item *op2 = $3;
  int type_code = get_common_type (op1, op2);
  if (!type_code)
    YYERROR;
  $$ = make_plus_expression
     (tok, op1, op2, type_code, EXP_MINUS);
}
|expression EQUALS expression {
  struct prod_token_parm_item *tok = $2;
  struct prod_token_parm_item *op1 = $1;
  struct prod_token_parm_item *op2 = $3;
  $$ = make_plus_expression
     (tok, op1, op2, SIGNED_INT, EXP_EQUALS);
}
|variable_ref ASSIGN expression {
  struct prod_token_parm_item *tok = $2;
  struct prod_token_parm_item *op1 = $1;
  struct prod_token_parm_item *op2 = $3;
  int type_code = NUMERIC_TYPE (op1);
  if (!type_code)
    YYERROR;
  $$ = make_plus_expression
     (tok, op1, op2, type_code, EXP_ASSIGN);
}
|function_invocation {
  $$ = $1;
}
;

function_invocation:
NAME LEFT_PARENTHESIS expressions_with_commas RIGHT_PARENTHESIS {
  struct prod_token_parm_item *prod;
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item search_prod;
  struct prod_token_parm_item *proto;
  struct prod_token_parm_item *exp;
  struct prod_token_parm_item *exp_proto;
  struct prod_token_parm_item *var;
  int exp_proto_count;
  int exp_count;
  tree parms;
  tree type;
  
  tok = $1;
  prod = make_production (PROD_FUNCTION_INVOCATION, tok);
  SYMBOL_TABLE_NAME (prod) = tok;
  PARAMETERS (prod) = reverse_prod_list ($3);
  SYMBOL_TABLE_NAME ((&search_prod)) = tok;
  search_prod.category = token_category;
  proto = lookup_tree_name (&search_prod);
  if (!proto)
    {
      fprintf (stderr, "%s:%i:%i: Function prototype not found\n",
	       tok->tp.tok.location.file,
	       tok->tp.tok.location.line, tok->tp.tok.charno);
      print_token (stderr, 0, tok);
      errorcount++;
      YYERROR;
    }
  EXPRESSION_TYPE (prod) = EXPRESSION_TYPE (proto);
  NUMERIC_TYPE (prod) = NUMERIC_TYPE (proto);
  /* Count the expressions and ensure they match the prototype.  */
  for (exp_proto_count = 0, exp_proto = PARAMETERS (proto); 
       exp_proto; exp_proto = exp_proto->tp.pro.next)
    exp_proto_count++;

  for (exp_count = 0, exp = PARAMETERS (prod); exp; exp = exp->tp.pro.next)
    exp_count++;

  if (exp_count !=  exp_proto_count)
    {
      fprintf (stderr, "%s:%i:%i: expression count mismatch with prototype\n",
	       tok->tp.tok.location.file,
	       tok->tp.tok.location.line, tok->tp.tok.charno);
      print_token (stderr, 0, tok);
      errorcount++;
      YYERROR;
    }
  parms = tree_code_init_parameters ();
  for (exp_proto = PARAMETERS (proto), exp = PARAMETERS (prod);
       exp_proto;
       exp = exp->tp.pro.next, exp_proto = exp_proto->tp.pro.next)
  {
    if (!exp)
      abort ();
    if (!exp_proto)
      abort ();
    if (!exp->tp.pro.code)
      abort ();
    var = VARIABLE (exp_proto);
    if (!var)
      abort ();
    if (!var->tp.pro.code)
      abort ();
    parms = tree_code_add_parameter (parms, var->tp.pro.code, exp->tp.pro.code);
  }
  type = get_type_for_numeric_type (NUMERIC_TYPE (prod));
  prod->tp.pro.code = tree_code_get_expression
    (EXP_FUNCTION_INVOCATION, type, proto->tp.pro.code, parms, NULL);
  $$ = prod;
}
;

expressions_with_commas:
expression {
  struct prod_token_parm_item *exp;
  exp = $1;
  ensure_not_void (NUMERIC_TYPE (exp), exp->tp.pro.main_token);
  $$ = $1;
}
|expressions_with_commas COMMA expression {
  struct prod_token_parm_item *exp;
  exp = $3;
  ensure_not_void (NUMERIC_TYPE (exp), exp->tp.pro.main_token);
  exp->tp.pro.next = $1; /* Reverse order.  */
  $$ = exp;
}
;

variable_ref:
NAME {
  struct prod_token_parm_item search_prod;
  struct prod_token_parm_item *prod;
  struct prod_token_parm_item *symbol_table_entry;
  struct prod_token_parm_item* tok;
  tree type;

  tok = $1;
  SYMBOL_TABLE_NAME ((&search_prod)) = tok;
  search_prod.category = token_category;
  symbol_table_entry = lookup_tree_name (&search_prod);
  if (!symbol_table_entry)
    {
      fprintf (stderr, "%s:%i:%i: Variable referred to but not defined\n",
              tok->tp.tok.location.file,
              tok->tp.tok.location.line, tok->tp.tok.charno);
      print_token (stderr, 0, tok);
      errorcount++;
      YYERROR;
    }

  prod = make_production (PROD_VARIABLE_REFERENCE_EXPRESSION, tok);
  NUMERIC_TYPE (prod) = NUMERIC_TYPE (symbol_table_entry);
  type = get_type_for_numeric_type (NUMERIC_TYPE (prod));
  if (!NUMERIC_TYPE (prod))
    YYERROR;
  OP1 (prod) = $1;
  
  prod->tp.pro.code = tree_code_get_expression (EXP_REFERENCE, type, 
                                      symbol_table_entry->tp.pro.code, NULL, NULL);
  $$ = prod;
}
;

init_opt:
/* Nil.   */ {
  $$ = 0;
}
|init {
  /* Nothing to do.  */
};

init:
ASSIGN init_element {
}
;

init_element:
INTEGER {
  $$ = make_integer_constant ($1);
}
;

%%

/* Print a token VALUE to file FILE.  Ignore TYPE which is the token
   type. */

void
print_token (FILE * file, unsigned int type ATTRIBUTE_UNUSED, YYSTYPE value) 
{
  struct prod_token_parm_item *tok;
  unsigned int  ix;

  tok  =  value;
  fprintf (file, "%d \"", tok->tp.tok.location.line);
  for (ix  =  0; ix < tok->tp.tok.length; ix++)
    fprintf (file, "%c", tok->tp.tok.chars[ix]);
  fprintf (file, "\"");
}

/* Output a message ERROR_MESSAGE from the parser.  */
static void
yyerror (const char *error_message)
{
  struct prod_token_parm_item *tok;
  
  tok = yylval;
  if (tok)
    {
      fprintf (stderr, "%s:%i:%i: %s\n", tok->tp.tok.location.file,
	       tok->tp.tok.location.line, tok->tp.tok.charno, error_message);
      print_token (stderr, 0, tok);
    }
  else
    fprintf (stderr, "%s\n", error_message);
  
  errorcount++;

}

/* Reverse the order of a token list, linked by parse_next, old first
   token is OLD_FIRST.  */

static struct prod_token_parm_item*
reverse_prod_list (struct prod_token_parm_item *old_first)
{
  struct prod_token_parm_item *current;
  struct prod_token_parm_item *next;
  struct prod_token_parm_item *prev = NULL;
  
  current = old_first;
  prev = NULL;

  while (current) 
    {
      if (current->category != production_category)
        abort ();
      next = current->tp.pro.next;
      current->tp.pro.next = prev;
      prev = current;
      current = next; 
    }
  return prev;
}

/* Ensure TYPE is not VOID. Use NAME as the token for the error location.  */

static void
ensure_not_void (unsigned int type, struct prod_token_parm_item* name)
{
  if (type == VOID)
    {
      fprintf (stderr, "%s:%i:%i: Type must not be void in this context\n",
	       name->tp.tok.location.file,
	       name->tp.tok.location.line, name->tp.tok.charno);
      print_token (stderr, 0, name);
      errorcount++;
    }
}

/* Check TYPE1 and TYPE2 which are integral types.  Return the lowest
   common type (min is signed int).  */

static int 
get_common_type (struct prod_token_parm_item *type1, struct prod_token_parm_item *type2)
{
  if (NUMERIC_TYPE (type1) == UNSIGNED_INT)
    return UNSIGNED_INT;
  if (NUMERIC_TYPE (type2) == UNSIGNED_INT)
    return UNSIGNED_INT;

  return SIGNED_INT;
}

/* Check type (TYPE_NUM) and expression (EXP) match.  Return the 1 if
   OK else 0.  Must be exact match - same name unless it is an
   integral type.  */

static int 
check_type_match (int type_num, struct prod_token_parm_item *exp)
{
  switch (type_num)
    {
    case SIGNED_INT:
    case UNSIGNED_INT:
    case SIGNED_CHAR:
    case UNSIGNED_CHAR:
      switch (NUMERIC_TYPE (exp))
        {
        case SIGNED_INT:
        case UNSIGNED_INT:
        case SIGNED_CHAR:
        case UNSIGNED_CHAR:
          return 1;
          
        case VOID:
          abort ();
      
        default: 
          abort ();
        }
      break;
      
    case VOID:
      abort ();
      
    default:
      abort ();
      
    }
}

/* Make a production for an integer constant VALUE.  */

static struct prod_token_parm_item *
make_integer_constant (struct prod_token_parm_item* value)
{
  struct prod_token_parm_item* tok;
  struct prod_token_parm_item *prod;
  tok = value;
  prod = make_production (PROD_INTEGER_CONSTANT, tok);
  if ((tok->tp.tok.chars[0] == (unsigned char)'-')|| (tok->tp.tok.chars[0] == (unsigned char)'+'))
    NUMERIC_TYPE (prod) = SIGNED_INT;
  else
    NUMERIC_TYPE (prod) = UNSIGNED_INT;
  prod->tp.pro.code = tree_code_get_integer_value (tok->tp.tok.chars, tok->tp.tok.length);
  return prod;
}


/* Build a PROD_PLUS_EXPRESSION.  This is uses for PLUS, MINUS, ASSIGN
   and EQUALS expressions.  */

static struct prod_token_parm_item *
make_plus_expression (struct prod_token_parm_item* tok,
		      struct prod_token_parm_item* op1,
		      struct prod_token_parm_item* op2,
		      int type_code, int prod_code)
{
  struct prod_token_parm_item *prod;
  tree type;

  ensure_not_void (NUMERIC_TYPE (op1), op1->tp.pro.main_token);
  ensure_not_void (NUMERIC_TYPE (op2), op2->tp.pro.main_token);

  prod = make_production (PROD_PLUS_EXPRESSION, tok);

  NUMERIC_TYPE (prod) = type_code;
  type = get_type_for_numeric_type (NUMERIC_TYPE (prod));
  if (!type)
    abort ();
  OP1 (prod) = op1;
  OP2 (prod) = op2;
      
  prod->tp.pro.code = tree_code_get_expression
     (prod_code, type, op1->tp.pro.code,
      op2->tp.pro.code, NULL);

  return prod;
}


/* Set STORAGE_CLASS in PROD according to CLASS_TOKEN.  */

static void
set_storage (struct prod_token_parm_item *prod)
{
  struct prod_token_parm_item* stg_class;
  stg_class = STORAGE_CLASS_TOKEN (prod);
  switch (stg_class->type)
    {
    case STATIC:
      STORAGE_CLASS (prod) = STATIC_STORAGE;
      break;
      
    case AUTOMATIC:
      STORAGE_CLASS (prod) = AUTOMATIC_STORAGE;
      break;
      
    case EXTERNAL_DEFINITION:
      STORAGE_CLASS (prod) = EXTERNAL_DEFINITION_STORAGE;
      break;

    case EXTERNAL_REFERENCE:
      STORAGE_CLASS (prod) = EXTERNAL_REFERENCE_STORAGE;
      break;

    default:
      abort ();
    }
}

/* Set parse trace.  */

void
treelang_debug (void)
{
  if (option_parser_trace)
    yydebug = 1;
}