-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHAP7.C
1653 lines (1285 loc) · 29.8 KB
/
CHAP7.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
The programs contained herein are adapted from
C: Power User's Guide
by Herbert Schildt published by Osborne/McGraw-Hil, Copyright
1987, Osborne/McGraw-Hill. Used with the permission of
Osborne/McGraw-Hill. Program adaptations are solely the
work of Herbert Schildt and are not a publication of
Osborne/McGraw-Hill.
*/
listing 7-1
#define DELIMITER 1
#define VARIABLE 2
#define NUMBER 3
#define COMMAND 4
#define STRING 5
#define QUOTE 6
external int tok, token_type;
extern char *prog; /* holds expression to be analyzed */
/* Get a token. */
get_token()
{
register char *temp;
token_type=0; tok=0;
temp=token;
if(*prog=='\0') { /* end of file */
*token=0;
tok = FINISHED;
return(token_type=DELIMITER);
}
while(iswhite(*prog)) ++prog; /* skip over white space */
if(*prog=='\r') { /* crlf */
++prog; ++prog;
tok = EOL; *token='\r';
token[1]='\n'; token[2]=0;
return (token_type = DELIMITER);
}
if(strchr("+-*^/%=;(),><", *prog)){ /* delimiter */
*temp=*prog;
prog++; /* advance to next position */
temp++;
*temp=0;
return (token_type=DELIMITER);
}
if(*prog=='"') { /* quoted string */
prog++;
while(*prog!='"'&& *prog!='\r') *temp++=*prog++;
if(*prog=='\r') serror(1);
prog++;*temp=0;
return(token_type=QUOTE);
}
if(isdigit(*prog)) { /* number */
while(!isdelim(*prog)) *temp++=*prog++;
*temp = '\0';
return(token_type = NUMBER);
}
if(isalpha(*prog)) { /* var or command */
while(!isdelim(*prog)) *temp++=*prog++;
token_type=STRING;
}
*temp = '\0';
/* see if a string is a command or a variable */
if(token_type==STRING) {
tok=look_up(token); /* convert to internal rep */
if(!tok) token_type = VARIABLE;
else token_type = COMMAND; /* is a command */
}
return token_type;
}
listing 7-2
/* Return a token to input stream. */
void putback()
{
char *t;
t = token;
for(; *t; t++) prog--;
}
listing 7-3
/* recursive descent parser for integer expressions
which may include variables */
#include "setjmp.h"
#include "math.h"
#include "ctype.h"
#include "stdlib.h"
#define DELIMITER 1
#define VARIABLE 2
#define NUMBER 3
#define COMMAND 4
#define STRING 5
#define QUOTE 6
#define EOL 9
#define FINISHED 10
extern char *prog; /* holds expression to be analyzed */
extern jmp_buf e_buf; /* hold environment for longjmp() */
extern int variables[26]; /* variables */
extern struct commands {
char command[20];
char tok;
} table[];
extern char token[80]; /* holds string representation of token */
extern char token_type; /* contains type of token */
extern char tok; /* holds the internal representation of token */
void get_exp(),level2(), level3(), level4(), level5();
void level6(), primitive(), arith(), unary();
void serror(), putback();
/* Entry point into parser. */
void get_exp(result)
int *result;
{
get_token();
if(!*token) {
serror(2);
return;
}
level2(result);
putback(); /* return last token read to input stream */
}
/* Add or subtract two terms. */
void level2(result)
int *result;
{
register char op;
int hold;
level3(result);
while((op = *token) == '+' || op == '-') {
get_token();
level3(&hold);
arith(op, result, &hold);
}
}
/* Multiply or divide two factors. */
void level3(result)
int *result;
{
register char op;
int hold;
level4(result);
while((op = *token) == '*' || op == '/' || op == '%') {
get_token();
level4(&hold);
arith(op, result, &hold);
}
}
/* Process integer exponent. */
void level4(result)
int *result;
{
int hold;
level5(result);
if(*token== '^') {
get_token();
level4(&hold);
arith('^', result, &hold);
}
}
/* Is a unary + or -. */
void level5(result)
int *result;
{
register char op;
op = 0;
if((token_type==DELIMITER) && *token=='+' || *token=='-') {
op = *token;
get_token();
}
level6(result);
if(op)
unary(op, result);
}
/* Process parenthesized expression. */
void level6(result)
int *result;
{
if((*token == '(') && (token_type == DELIMITER)) {
get_token();
level2(result);
if(*token != ')')
serror(1);
get_token();
}
else
primitive(result);
}
/* Find value of number or variable. */
void primitive(result)
int *result;
{
switch(token_type) {
case VARIABLE:
*result = find_var(token);
get_token();
return;
case NUMBER:
*result = atoi(token);
get_token();
return;
default:
serror(0);
}
}
/* Perform the specified arithmetic. */
void arith(o, r, h)
char o;
int *r, *h;
{
register int t, ex;
switch(o) {
case '-':
*r = *r-*h;
break;
case '+':
*r = *r+*h;
break;
case '*':
*r = *r * *h;
break;
case '/':
*r = (*r)/(*h);
break;
case '%':
t = (*r)/(*h);
*r = *r-(t*(*h));
break;
case '^':
ex = *r;
if(*h==0) {
*r = 1;
break;
}
for(t=*h-1; t>0; --t) *r = (*r) * ex;
break;
}
}
/* Reverse the sign. */
void unary(o, r)
char o;
int *r;
{
if(o=='-') *r = -(*r);
}
/* Find the value of a variable. */
int find_var(s)
char *s;
{
if(!isalpha(*s)){
serror(4); /* not a variable */
return 0;
}
return variables[toupper(*token)-'A'];
}
/* display an error message */
void serror(error)
int error;
{
static char *e[]= {
"syntax error",
"unbalanced parentheses",
"no expression present",
"equals sign expected",
"not a variable",
"Label table full",
"duplicate label",
"undefined label",
"THEN expected",
"TO expected",
"too many nested FOR loops",
"NEXT without FOR",
"too many nested GOSUBs",
"RETURN without GOSUB"
};
printf("%s\n", e[error]);
longjmp(e_buf, 1); /* return to save point */
}
/* Get a token. */
get_token()
{
register char *temp;
token_type=0; tok=0;
temp=token;
if(*prog=='\0') { /* end of file */
*token=0;
tok = FINISHED;
return(token_type=DELIMITER);
}
while(iswhite(*prog)) ++prog; /* skip over white space */
if(*prog=='\r') { /* crlf */
++prog; ++prog;
tok = EOL; *token='\r';
token[1]='\n'; token[2]=0;
return (token_type = DELIMITER);
}
if(strchr("+-*^/%=;(),><", *prog)){ /* delimiter */
*temp=*prog;
prog++; /* advance to next position */
temp++;
*temp=0;
return (token_type=DELIMITER);
}
if(*prog=='"') { /* quoted string */
prog++;
while(*prog!='"'&& *prog!='\r') *temp++=*prog++;
if(*prog=='\r') serror(1);
prog++;*temp=0;
return(token_type=QUOTE);
}
if(isdigit(*prog)) { /* number */
while(!isdelim(*prog)) *temp++=*prog++;
*temp = '\0';
return(token_type = NUMBER);
}
if(isalpha(*prog)) { /* var or command */
while(!isdelim(*prog)) *temp++=*prog++;
token_type=STRING;
}
*temp = '\0';
/* see if a string is a command or a variable */
if(token_type==STRING) {
tok=look_up(token); /* convert to internal rep */
if(!tok) token_type = VARIABLE;
else token_type = COMMAND; /* is a command */
}
return token_type;
}
/* Return a token to input stream. */
void putback()
{
char *t;
t = token;
for(; *t; t++) prog--;
}
/* Look up a a token's internal representation in the
token table.
*/
look_up(s)
char *s;
{
register int i,j;
char *p;
/* convert to lowercase */
p = s;
while(*p){ *p = tolower(*p); p++; }
/* see if token is in table */
for(i=0; *table[i].command; i++)
if(!strcmp(table[i].command, s)) return table[i].tok;
return 0; /* unknown command */
}
/* Return true if c is a delimiter. */
isdelim(c)
char c;
{
if(strchr(" ;,+-<>/*%^=()", c) || c==9 || c=='\r' || c==0)
return 1;
return 0;
}
/* Return 1 if c is space or tab. */
iswhite(c)
char c;
{
if(c==' ' || c=='\t') return 1;
else return 0;
}
listing 7-4
int variables[26]= { /* 26 user variables, A-Z */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
};
listing 7-5
/* Find the value of a variable. */
int find_var(s)
char *s;
{
if(!isalpha(*s)){
serror(4); /* not a variable */
return 0;
}
return variables[toupper(*token)-'A'];
}
listing 7-6
#define PRINT 1
#define INPUT 2
#define IF 3
#define THEN 4
#define FOR 5
#define NEXT 6
#define TO 7
#define GOTO 8
#define EOL 9
#define FINISHED 10
#define GOSUB 11
#define RETURN 12
#define END 13
listing 7-7
struct commands { /* keyword lookup table */
char command[20];
char tok;
} table[] = { /* Commands must be entered lowercase */
"print", PRINT, /* in this table. */
"input", INPUT,
"if", IF,
"then", THEN,
"goto", GOTO,
"for", FOR,
"next", NEXT,
"to", TO,
"gosub", GOSUB,
"return", RETURN,
"end", END,
"", END /* mark end of table */
};
listing 7-8
/* Look up a a token's internal representation in the
token table.
*/
look_up(s)
char *s;
{
register int i,j;
char *p;
/* convert to lowercase */
p = s;
while(*p){ *p = tolower(*p); p++; }
/* see if token is in table */
for(i=0; *table[i].command; i++)
if(!strcmp(table[i].command, s)) return table[i].tok;
return 0; /* unknown command */
}
listing 7-9
/* Load a program. */
load_program(p, fname)
char *p;
char *fname;
{
FILE *fp;
int i=0;
if(!(fp=fopen(fname, "rb"))) return 0;
i = 0;
do {
*p = getc(fp);
p++; i++;
} while(!feof(fp) && i<PROG_SIZE);
*(p-2) = '\0'; /* null terminate the program */
fclose(fp);
return 1;
}
listing 7-10
do {
token_type = get_token();
/* check for assignment statement */
if(token_type==VARIABLE) {
putback(); /* return the var to the input stream */
assignment(); /* must be assignment statement */
}
else /* is command */
switch(tok) {
case PRINT:
print();
break;
case GOTO:
exec_goto();
break;
case IF:
exec_if();
break;
case FOR:
exec_for();
break;
case NEXT:
next();
break;
case INPUT:
input();
break;
case GOSUB:
gosub();
break;
case RETURN:
greturn();
break;
case END:
exit(0);
}
} while (tok != FINISHED);
listing 7-11
/* Assign a variable a value. */
assignment()
{
int var, value;
/* get the variable name */
get_token();
if(!isalpha(*token)) {
serror(4); /* not a variable */
return;
}
/* find the index of the variable */
var = toupper(*token)-'A';
/* get the equals sign */
get_token();
if(*token!='=') {
serror(3);
return;
}
/* get the value to assign to var */
get_exp(&value);
/* assign the value */
variables[var] = value;
}
listing 7-12
/* Execute a simple version of the BASIC PRINT statement */
void print()
{
int answer;
int len=0, spaces;
char last_delim;
do {
get_token(); /* get next list item */
if(tok==EOL || tok==FINISHED) break;
if(token_type==QUOTE) { /* is string */
printf(token);
len += strlen(token);
get_token();
}
else { /* is expression */
putback();
get_exp(&answer);
get_token();
len += printf("%d", answer);
}
last_delim = *token;
if(*token==';') {
/* compute number of spaces to move to next tab */
spaces = 8 - (len % 8);
len += spaces; /* add in the tabbing position */
while(spaces) {
printf(" ");
spaces--;
}
}
else if(*token==',') /* do nothing */;
else if(tok!=EOL && tok!=FINISHED) serror(0);
} while (*token==';' || *token==',');
if(tok==EOL || tok==FINISHED) {
if(last_delim != ';' && last_delim!=',') printf("\n");
}
else serror(0); /* error is not , or ; */
}
listing 7-13
PRINT X; Y; "THIS IS A STRING"
PRINT 10 / 4
PRINT
listing 7-14
/* Execute a simple form of the BASIC INPUT command */
void input()
{
char str[80], var;
int i;
get_token(); /* see if prompt string is present */
if(token_type==QUOTE) {
printf(token); /* if so, print it and check for comma */
get_token();
if(*token!=',') serror(1);
get_token();
}
else printf("? "); /* otherwise, prompt with ? */
var = toupper(*token)-'A'; /* get the input var */
scanf("%d", &i); /* read input */
variables[var] = i; /* store it */
}
listing 7-15
struct label {
char name[LAB_LEN]; /* name of label */
char *p; /* points to place of label in source file*/
};
struct label label_table[NUM_LAB];
listing 7-16
/* Find all labels. */
void scan_labels()
{
register int loc;
char *temp;
label_init(); /* zero all labels */
temp = prog; /* save pointer to top of program */
/* if the first token in the file is a label */
get_token();
if(token_type==NUMBER) {
strcpy(label_table[0].name,token);
label_table[0].p=prog;
}
find_eol();
do {
get_token();
if(token_type==NUMBER) {
loc=get_next_label(token);
if(loc==-1||loc==-2) {
(loc==-1) ?serror(5):serror(6);
}
strcpy(label_table[loc].name, token);
label_table[loc].p = prog; /* current point in program */
}
/* if not on a blank line, find next line */
if(tok!=EOL) find_eol();
} while(tok!=FINISHED);
prog = temp; /* restore to original */
}
/* Initialize the array that holds the labels.
By convention, a null label name indicates that
array position is unused.
*/
void label_init()
{
register int t;
for(t=0;t<NUM_LAB;++t) label_table[t].name[0]='\0';
}
/* Find the start of the next line. */
void find_eol()
{
while(*prog!='\n' && *prog!='\0') ++prog;
if(*prog) prog++;
}
/* Return index of next free position in label array.
A -1 is returned if the array is full.
A -2 is returned if a duplicate label is found.
*/
get_next_label(s)
char *s;
{
register int t;
for(t=0;t<NUM_LAB;++t) {
if(label_table[t].name[0]==0) return t;
if(!strcmp(label_table[t].name,s)) return -2; /* dup */
}
return -1;
}
listing 7-17
/* Execute a GOTO statement. */
void exec_goto()
{
char *loc;
get_token(); /* get label to go to */
/* find the location of the label */
loc = find_label(token);
if(loc=='\0')
serror(7); /* label not defined */
else prog=loc; /* start program running at that loc */
}
/* Find location of given label. A null is returned if
label is not found; otherwise a pointer to the position
of the label is returned.
*/
char *find_label(s)
char *s;
{
register int t;
for(t=0;t<NUM_LAB;++t)
if(!strcmp(label_table[t].name,s)) return label_table[t].p;
return '\0'; /* error condition */
}
listing 7-18
/* Execute an IF statement. */
void exec_if()
{
int x , y, cond;
char op;
get_exp(&x); /* get left expression */
get_token(); /* get the operator */
if(!strchr("=<>", *token)) {
serror(0); /* not a legal operator */
return;
}
op=*token;
get_exp(&y); /* get right expression */
/* determine the outcome */
cond=0;
switch(op) {
case '=':
if(x==y) cond=1;
break;
case '<':
if(x<y) cond=1;
break;
case '>':
if(x>y) cond=1;
break;
}
if(cond) { /* is true so process target of IF */
get_token();
if(tok!=THEN) {
serror(8);
return;
}/* else prog exec starts on next line */
}
else find_eol(); /* find start of next line */
}
listing 7-19
struct for_stack {
int var; /* counter variable */
int target; /* target value */
char *loc;
} fstack[FOR_NEST]; /* stack for FOR/NEXT loop */
int ftos; /* index to top of FOR stack */
listing 7-20
/* Push function for the FOR stack. */
void fpush(i)
struct for_stack i;
{
if(ftos>FOR_NEST)
serror(10);
fstack[ftos]=i;
ftos++;
}
struct for_stack fpop()
{
ftos--;
if(ftos<0) serror(11);
return(fstack[ftos]);
}
listing 7-21
/* Execute a FOR loop. */
void exec_for()
{
struct for_stack i;
int value;
get_token(); /* read the control variable */
if(!isalpha(*token)) {
serror(4);
return;
}
i.var=toupper(*token)-'A'; /* save its index */
get_token(); /* read the equals sign */
if(*token!='=') {
serror(3);
return;
}
get_exp(&value); /* get initial value */
variables[i.var]=value;
get_token();
if(tok!=TO) serror(9); /* read and discard the TO */
get_exp(&i.target); /* get target value */
/* if loop can execute at least once, push info on stack */
if(value>=variables[i.var]) {
i.loc = prog;
fpush(i);
}
else { /* otherwise, skip loop code altogether */
while(tok!=NEXT) get_token();
}
}
/* Execute a NEXT statement. */
void next()
{
struct for_stack i;
i = fpop(); /* read the loop info */
variables[i.var]++; /* increment control variable */
if(variables[i.var]>i.target) return; /* all done */
fpush(i); /* otherwise, restore the info */
prog = i.loc; /* loop */
}
listing 7-22
char *gstack[SUB_NEST]; /* stack for gosub */
int gtos; /* index to top of GOSUB stack */