summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/bc/bc.y
blob: f5ba1617bf0577765dd56f3458fe77f9a007fd67 (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
%{
/*	$NetBSD: bc.y,v 1.2 1994/12/02 00:43:23 phil Exp $ */

/* bc.y: The grammar for a POSIX compatable bc processor with some
         extensions to the language. */

/*  This file is part of bc written for MINIX.
    Copyright (C) 1991, 1992, 1993, 1994 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 of the License , 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; see the file COPYING.  If not, write to
    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

    You may contact the author by:
       e-mail:  phil@cs.wwu.edu
      us-mail:  Philip A. Nelson
                Computer Science Department, 9062
                Western Washington University
                Bellingham, WA 98226-9062
       
*************************************************************************/

#include "bcdefs.h"
#include "global.h"
#include "proto.h"
%}

%start program

%union {
	char	 *s_value;
	char	  c_value;
	int	  i_value;
	arg_list *a_value;
       }

/* Extensions over POSIX bc.
   a) NAME was LETTER.  This grammer allows longer names.
      Single letter names will still work.
   b) Relational_expression allowed only one comparison.
      This grammar has added boolean expressions with
      && (and) || (or) and ! (not) and allowed all of them in
      full expressions.
   c) Added an else to the if.
   d) Call by variable array parameters
   e) read() procedure that reads a number under program control from stdin.
   f) halt statement that halts the the program under program control.  It
      is an executed statement.
   g) continue statement for for loops.
   h) optional expressions in the for loop.
   i) print statement to print multiple numbers per line.
   j) warranty statement to print an extended warranty notice.
   j) limits statement to print the processor's limits.
*/

%token <i_value> NEWLINE AND OR NOT
%token <s_value> STRING NAME NUMBER
/*     '-', '+' are tokens themselves		*/
%token <c_value> MUL_OP
/*     '*', '/', '%' 				*/
%token <c_value> ASSIGN_OP
/*     '=', '+=',  '-=', '*=', '/=', '%=', '^=' */
%token <s_value> REL_OP
/*     '==', '<=', '>=', '!=', '<', '>' 	*/
%token <c_value> INCR_DECR
/*     '++', '--' 				*/
%token <i_value> Define    Break    Quit    Length
/*     'define', 'break', 'quit', 'length' 	*/
%token <i_value> Return    For    If    While    Sqrt   Else
/*     'return', 'for', 'if', 'while', 'sqrt', 'else' 	*/
%token <i_value> Scale    Ibase    Obase    Auto  Read
/*     'scale', 'ibase', 'obase', 'auto', 'read' 	*/
%token <i_value> Warranty, Halt, Last, Continue, Print, Limits
/*     'warranty', 'halt', 'last', 'continue', 'print', 'limits'   */

/* Types of all other things. */
%type <i_value> expression return_expression named_expression opt_expression
%type <c_value> '+' '-' 
%type <a_value> opt_parameter_list opt_auto_define_list define_list
%type <a_value> opt_argument_list argument_list
%type <i_value> program input_item semicolon_list statement_list
%type <i_value> statement function   statement_or_error

/* precedence */
%left OR
%left AND
%nonassoc NOT
%left REL_OP
%right ASSIGN_OP
%left '+' '-'
%left MUL_OP
%right '^'
%nonassoc UNARY_MINUS
%nonassoc INCR_DECR

%%
program			: /* empty */
			    {
			      $$ = 0;
			      if (interactive)
				{
				  printf ("%s\n", BC_VERSION);
				  welcome ();
				}
			    }
			| program input_item
			;
input_item		: semicolon_list NEWLINE
			    { run_code (); }
			| function
			    { run_code (); }
			| error NEWLINE
			    {
			      yyerrok;
			      init_gen ();
			    }
			;
semicolon_list		: /* empty */
			    { $$ = 0; }
			| statement_or_error
			| semicolon_list ';' statement_or_error
			| semicolon_list ';'
			;
statement_list		: /* empty */
			    { $$ = 0; }
			| statement_or_error
			| statement_list NEWLINE
			| statement_list NEWLINE statement_or_error
			| statement_list ';'
			| statement_list ';' statement
			;
statement_or_error	: statement
  			| error statement
			    { $$ = $2; }
			;
statement 		: Warranty
			    { warranty (""); }
			| Limits
			    { limits (); }
			| expression
			    {
			      if ($1 & 2)
				warn ("comparison in expression");
			      if ($1 & 1)
				generate ("W");
			      else 
				generate ("p");
			    }
			| STRING
			    {
			      $$ = 0;
			      generate ("w");
			      generate ($1);
			      free ($1);
			    }
			| Break
			    {
			      if (break_label == 0)
				yyerror ("Break outside a for/while");
			      else
				{
				  sprintf (genstr, "J%1d:", break_label);
				  generate (genstr);
				}
			    }
			| Continue
			    {
			      warn ("Continue statement");
			      if (continue_label == 0)
				yyerror ("Continue outside a for");
			      else
				{
				  sprintf (genstr, "J%1d:", continue_label);
				  generate (genstr);
				}
			    }
			| Quit
			    { exit (0); }
			| Halt
			    { generate ("h"); }
			| Return
			    { generate ("0R"); }
			| Return '(' return_expression ')'
			    { generate ("R"); }
			| For 
			    {
			      $1 = break_label; 
			      break_label = next_label++;
			    }
			  '(' opt_expression ';'
			    {
			      if ($4 > 1)
				warn ("Comparison in first for expression");
			      $4 = next_label++;
			      if ($4 < 0)
				sprintf (genstr, "N%1d:", $4);
			      else
				sprintf (genstr, "pN%1d:", $4);
			      generate (genstr);
			    }
			  opt_expression ';'
			    {
			      if ($7 < 0) generate ("1");
			      $7 = next_label++;
			      sprintf (genstr, "B%1d:J%1d:", $7, break_label);
			      generate (genstr);
			      $<i_value>$ = continue_label;
			      continue_label = next_label++;
			      sprintf (genstr, "N%1d:", continue_label);
			      generate (genstr);
			    }
			  opt_expression ')'
			    {
			      if ($10 > 1)
				warn ("Comparison in third for expression");
			      if ($10 < 0)
				sprintf (genstr, "J%1d:N%1d:", $4, $7);
			      else
				sprintf (genstr, "pJ%1d:N%1d:", $4, $7);
			      generate (genstr);
			    }
			  statement
			    {
			      sprintf (genstr, "J%1d:N%1d:",
				       continue_label, break_label);
			      generate (genstr);
			      break_label = $1;
			      continue_label = $<i_value>9;
			    }
			| If '(' expression ')' 
			    {
			      $3 = if_label;
			      if_label = next_label++;
			      sprintf (genstr, "Z%1d:", if_label);
			      generate (genstr);
			    }
			  statement  opt_else
			    {
			      sprintf (genstr, "N%1d:", if_label); 
			      generate (genstr);
			      if_label = $3;
			    }
			| While 
			    {
			      $1 = next_label++;
			      sprintf (genstr, "N%1d:", $1);
			      generate (genstr);
			    }
			'(' expression 
			    {
			      $4 = break_label; 
			      break_label = next_label++;
			      sprintf (genstr, "Z%1d:", break_label);
			      generate (genstr);
			    }
			')' statement
			    {
			      sprintf (genstr, "J%1d:N%1d:", $1, break_label);
			      generate (genstr);
			      break_label = $4;
			    }
			| '{' statement_list '}'
			    { $$ = 0; }
			| Print
			    {  warn ("print statement"); }
			  print_list
			;
print_list		: print_element
 			| print_element ',' print_list
			;
print_element		: STRING
			    {
			      generate ("O");
			      generate ($1);
			      free ($1);
			    }
			| expression
			    { generate ("P"); }
 			;
opt_else		: /* nothing */
			| Else 
			    {
			      warn ("else clause in if statement");
			      $1 = next_label++;
			      sprintf (genstr, "J%d:N%1d:", $1, if_label); 
			      generate (genstr);
			      if_label = $1;
			    }
			  statement
function 		: Define NAME '(' opt_parameter_list ')' '{'
			  NEWLINE opt_auto_define_list 
			    {
			      /* Check auto list against parameter list? */
			      check_params ($4,$8);
			      sprintf (genstr, "F%d,%s.%s[",
				       lookup($2,FUNCTDEF), 
				       arg_str ($4), arg_str ($8));
			      generate (genstr);
			      free_args ($4);
			      free_args ($8);
			      $1 = next_label;
			      next_label = 1;
			    }
			  statement_list /* NEWLINE */ '}'
			    {
			      generate ("0R]");
			      next_label = $1;
			    }
			;
opt_parameter_list	: /* empty */ 
			    { $$ = NULL; }
			| define_list
			;
opt_auto_define_list 	: /* empty */ 
			    { $$ = NULL; }
			| Auto define_list NEWLINE
			    { $$ = $2; } 
			| Auto define_list ';'
			    { $$ = $2; } 
			;
define_list 		: NAME
			    { $$ = nextarg (NULL, lookup ($1,SIMPLE)); }
			| NAME '[' ']'
			    { $$ = nextarg (NULL, lookup ($1,ARRAY)); }
			| define_list ',' NAME
			    { $$ = nextarg ($1, lookup ($3,SIMPLE)); }
			| define_list ',' NAME '[' ']'
			    { $$ = nextarg ($1, lookup ($3,ARRAY)); }
			;
opt_argument_list	: /* empty */
			    { $$ = NULL; }
			| argument_list
			;
argument_list 		: expression
			    {
			      if ($1 > 1) warn ("comparison in argument");
			      $$ = nextarg (NULL,0);
			    }
			| NAME '[' ']'
			    {
			      sprintf (genstr, "K%d:", -lookup ($1,ARRAY));
			      generate (genstr);
			      $$ = nextarg (NULL,1);
			    }
			| argument_list ',' expression
			    {
			      if ($3 > 1) warn ("comparison in argument");
			      $$ = nextarg ($1,0);
			    }
			| argument_list ',' NAME '[' ']'
			    {
			      sprintf (genstr, "K%d:", -lookup ($3,ARRAY));
			      generate (genstr);
			      $$ = nextarg ($1,1);
			    }
			;
opt_expression 		: /* empty */
			    {
			      $$ = -1;
			      warn ("Missing expression in for statement");
			    }
			| expression
			;
return_expression	: /* empty */
			    {
			      $$ = 0;
			      generate ("0");
			    }
			| expression
			    {
			      if ($1 > 1)
				warn ("comparison in return expresion");
			    }
			;
expression		:  named_expression ASSIGN_OP 
			    {
			      if ($2 != '=')
				{
				  if ($1 < 0)
				    sprintf (genstr, "DL%d:", -$1);
				  else
				    sprintf (genstr, "l%d:", $1);
				  generate (genstr);
				}
			    }
			  expression
			    {
			      if ($4 > 1) warn("comparison in assignment");
			      if ($2 != '=')
				{
				  sprintf (genstr, "%c", $2);
				  generate (genstr);
				}
			      if ($1 < 0)
				sprintf (genstr, "S%d:", -$1);
			      else
				sprintf (genstr, "s%d:", $1);
			      generate (genstr);
			      $$ = 0;
			    }
			;
			| expression AND 
			    {
			      warn("&& operator");
			      $2 = next_label++;
			      sprintf (genstr, "DZ%d:p", $2);
			      generate (genstr);
			    }
			  expression
			    {
			      sprintf (genstr, "DZ%d:p1N%d:", $2, $2);
			      generate (genstr);
			      $$ = $1 | $4;
			    }
			| expression OR
			    {
			      warn("|| operator");
			      $2 = next_label++;
			      sprintf (genstr, "B%d:", $2);
			      generate (genstr);
			    }
			  expression
 			    {
			      int tmplab;
			      tmplab = next_label++;
			      sprintf (genstr, "B%d:0J%d:N%d:1N%d:",
				       $2, tmplab, $2, tmplab);
			      generate (genstr);
			      $$ = $1 | $4;
			    }
			| NOT expression
			    {
			      $$ = $2;
			      warn("! operator");
			      generate ("!");
			    }
			| expression REL_OP expression
			    {
			      $$ = 3;
			      switch (*($2))
				{
				case '=':
				  generate ("=");
				  break;

				case '!':
				  generate ("#");
				  break;

				case '<':
				  if ($2[1] == '=')
				    generate ("{");
				  else
				    generate ("<");
				  break;

				case '>':
				  if ($2[1] == '=')
				    generate ("}");
				  else
				    generate (">");
				  break;
				}
			    }
			| expression '+' expression
			    {
			      generate ("+");
			      $$ = $1 | $3;
			    }
			| expression '-' expression
			    {
			      generate ("-");
			      $$ = $1 | $3;
			    }
			| expression MUL_OP expression
			    {
			      genstr[0] = $2;
			      genstr[1] = 0;
			      generate (genstr);
			      $$ = $1 | $3;
			    }
			| expression '^' expression
			    {
			      generate ("^");
			      $$ = $1 | $3;
			    }
			| '-' expression  %prec UNARY_MINUS
			    {
			      generate ("n");
			      $$ = $2;
			    }
			| named_expression
			    {
			      $$ = 1;
			      if ($1 < 0)
				sprintf (genstr, "L%d:", -$1);
			      else
				sprintf (genstr, "l%d:", $1);
			      generate (genstr);
			    }
			| NUMBER
			    {
			      int len = strlen($1);
			      $$ = 1;
			      if (len == 1 && *$1 == '0')
				generate ("0");
			      else if (len == 1 && *$1 == '1')
				generate ("1");
			      else
				{
				  generate ("K");
				  generate ($1);
				  generate (":");
				}
			      free ($1);
			    }
			| '(' expression ')'
			    { $$ = $2 | 1; }
			| NAME '(' opt_argument_list ')'
			    {
			      $$ = 1;
			      if ($3 != NULL)
				{ 
				  sprintf (genstr, "C%d,%s:",
					   lookup ($1,FUNCT),
					   call_str ($3));
				  free_args ($3);
				}
			      else
				{
				  sprintf (genstr, "C%d:", lookup ($1,FUNCT));
				}
			      generate (genstr);
			    }
			| INCR_DECR named_expression
			    {
			      $$ = 1;
			      if ($2 < 0)
				{
				  if ($1 == '+')
				    sprintf (genstr, "DA%d:L%d:", -$2, -$2);
				  else
				    sprintf (genstr, "DM%d:L%d:", -$2, -$2);
				}
			      else
				{
				  if ($1 == '+')
				    sprintf (genstr, "i%d:l%d:", $2, $2);
				  else
				    sprintf (genstr, "d%d:l%d:", $2, $2);
				}
			      generate (genstr);
			    }
			| named_expression INCR_DECR
			    {
			      $$ = 1;
			      if ($1 < 0)
				{
				  sprintf (genstr, "DL%d:x", -$1);
				  generate (genstr); 
				  if ($2 == '+')
				    sprintf (genstr, "A%d:", -$1);
				  else
				      sprintf (genstr, "M%d:", -$1);
				}
			      else
				{
				  sprintf (genstr, "l%d:", $1);
				  generate (genstr);
				  if ($2 == '+')
				    sprintf (genstr, "i%d:", $1);
				  else
				    sprintf (genstr, "d%d:", $1);
				}
			      generate (genstr);
			    }
			| Length '(' expression ')'
			    { generate ("cL"); $$ = 1;}
			| Sqrt '(' expression ')'
			    { generate ("cR"); $$ = 1;}
			| Scale '(' expression ')'
			    { generate ("cS"); $$ = 1;}
			| Read '(' ')'
			    {
			      warn ("read function");
			      generate ("cI"); $$ = 1;
			    }
			;
named_expression	: NAME
			    { $$ = lookup($1,SIMPLE); }
			| NAME '[' expression ']'
			    {
			      if ($3 > 1) warn("comparison in subscript");
			      $$ = lookup($1,ARRAY);
			    }
			| Ibase
			    { $$ = 0; }
			| Obase
			    { $$ = 1; }
			| Scale
			    { $$ = 2; }
			| Last
			    { $$ = 3;
			      warn ("Last variable");
			    }
			;
%%