-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpp.y
610 lines (564 loc) · 16.9 KB
/
vpp.y
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
%{
/* Copyright (C) 1996 Himanshu M. Thaker
This file is part of vpp.
Vpp is distributed in the hope that it will be useful,
but without any warranty. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.
Everyone is granted permission to copy, modify and redistribute
vpp, but only under the conditions described in the
document "vpp copying permission notice". An exact copy
of the document is supposed to have been given to you along with
vpp so that you can know how you may redistribute it all.
It should be in a file named COPYING. Among other things, the
copyright notice and this notice must be preserved on all copies. */
/*
* $Header: /home/cvspsrv/cvsroot/eda/vbpp/vpp.y,v 1.4 2002/01/28 10:04:43 jching Exp $
* Program :
* Author : Himanshu M. Thaker
* Date : Apr. 18, 1995
* Description :
* $Log: vpp.y,v $
* Revision 1.4 2002/01/28 10:04:43 jching
* Stop parsing after 100 parse errors.
*
* Revision 1.3 2002/01/27 09:51:25 jching
* Do not output comment starters. That is done in lexor.
*
* Revision 1.2 2001/01/25 07:38:03 jching
* Return non-zero when parsing error detected.
*
* Revision 1.1.1.1 2000/02/21 04:05:37 ahvezda
* Import of vbpp
*
* Revision 1.8 1996/07/18 02:53:03 hmthaker
* Initial revision
*
* Revision 1.7 1995/04/23 17:18:12 hemi
* All features except string handling.
*/
#include <stdio.h>
#include "common.h"
#include "yacc_stuff.h"
#include "proto.h"
char ttid[MAXNAME];
var_list *head_table = NULL;
var_values *head_variable_list = NULL;
gen_ll *cstack = NULL;
extern gen_ll_mgr *code_pointer;
int loop_struct_count = 0;
int yyerror_count;
varp *vp1;
%}
%union {
int ival; /* integer value */
char *cp; /* character pointer */
expr *ep; /* expression pointer */
varp *vp; /* variable identifier pointer */
}
%token TTIF
%token TTIFDEF
%token TTIFNDEF
%token TTELSE
%token TTENDIF
%token TTFOR
%token TTENDFOR
%token TTLET
%token TTWHILE
%token TTENDWHILE
%token TTSWITCH
%token TTCASE
%token TTDEFAULT
%token TTBREAKSW
%token TTENDSWITCH
%token TTID
%token TTNUM
%token TTREALNUM
%token TTQS
%token TTNAME
%token TTWHITE
%token TTL_EQ
%token TTL_NEQ
%token TTL_AND
%token TTL_OR
%token TTL_LEQ
%token TTL_SHIFTR
%token TTL_SHIFTL
%token TT_POWER
%token TT_UNKNOWN
%token TTLOG2
%token TTROUND
%token TTCEIL
%token TTFLOOR
%token TTEVEN
%token TTODD
%token TTMAX
%token TTMIN
%token TTABS
%token TTSTOI
%token TTITOS
%token TTSUBSTR
%token TTSYSTEM
%token TTCOMMENT
%token TTCOMMENT2
%nonassoc '<' '>'
%left TTL_EQ TTL_GEQ TTL_LEQ TTL_NEQ
%left TTL_OR
%left TTL_AND
%left TTL_XNOR
%left TTL_NAND
%left TTL_NOR
%left TTL_XOR
%left '+' '-'
%left '*' '/' '%'
%left TT_POWER
%left TTL_SHIFTL TTL_SHIFTR
%left '|' '&' '^'
%left '(' ')'
%right '~' '!'
%right UNARY
%type <ep> expression primary
%type <cp> identifier
%%
source_text : /* empty */
| source_text if_declaration
| source_text ifdef_declaration
| source_text ifndef_declaration
| source_text else_declaration
| source_text endif_declaration
| source_text for_declaration
| source_text endfor_declaration
| source_text while_declaration
| source_text endwhile_declaration
| source_text switch_declaration
| source_text case_declaration
| source_text breaksw_declaration
| source_text default_declaration
| source_text endswitch_declaration
| source_text let_declaration
| source_text other_stuff
| source_text error {
set_to_last_mode();
yyclearin;
yyerrok;
}
;
other_stuff : TTNAME {printf("%s", ttid);}
| TTNUM {printf("%s", ttid);}
| TTREALNUM {printf("%s", ttid);}
| TTWHITE {printf("%s", ttid);}
;
switch_declaration : TTSWITCH {set_to_vpp_mode();} expression '\n'
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
cstack = push(cstack,
create_generic_ll(SWITCH_STRUCTURE, NULL,
create_switch_structure($3)
)
);
loop_struct_count++;
set_to_copy();
}
;
case_declaration : TTCASE {set_to_vpp_mode();} expression optional_colon '\n'
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
if (cstack != NULL) {
cstack = push(cstack, create_generic_ll(CASE_STRUCTURE,NULL,
create_case_structure($3))
);
set_to_last_mode();
}
else {
fprintf(stderr,"ERROR : got default without matching switch!!!\n");
set_to_initial_mode();
}
}
;
optional_colon : /* empty */
| ':'
;
default_declaration : TTDEFAULT {set_to_vpp_mode();} optional_colon vpp_line_termination
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
if (cstack != NULL) {
cstack = push(cstack, create_generic_ll(DEFAULT_STRUCTURE,NULL,
NULL)
);
set_to_last_mode();
}
else {
fprintf(stderr,"ERROR : got default without matching switch!!!\n");
set_to_initial_mode();
}
}
;
breaksw_declaration : TTBREAKSW {set_to_vpp_mode();} vpp_line_termination
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
if (cstack != NULL) {
cstack = push(cstack, create_generic_ll(BREAKSW_STRUCTURE,NULL,
NULL)
);
set_to_last_mode();
}
else {
fprintf(stderr,"ERROR : got breaksw without matching switch!!!\n");
set_to_initial_mode();
}
}
;
endswitch_declaration : TTENDSWITCH {set_to_vpp_mode();} vpp_line_termination_opt
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
if (cstack != NULL)
cstack = push(cstack, create_generic_ll(ENDSWITCH_STRUCTURE,NULL,
NULL)
);
else
fprintf(stderr,"ERROR : got an endswitch without matching switch!!!\n");
if (loop_struct_count != 0) {
loop_struct_count--;
if (loop_struct_count == 0) {
eval_stack(cstack);
cstack = NULL;
set_to_initial_mode();
}
else set_to_last_mode();
}
if (cstack == NULL) set_to_initial_mode();
}
;
while_declaration : TTWHILE {set_to_vpp_mode();} expression vpp_line_termination
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
cstack = push(cstack,
create_generic_ll(WHILE_STRUCTURE, NULL,
create_while_structure($3)
)
);
loop_struct_count++;
set_to_copy();
}
;
for_declaration : TTFOR {set_to_vpp_mode();}
'(' identifier '=' expression
{
vp1 = create_variable_ref($4,$6,TRUE);
free($4); /* create_variable_ref duplicates the name */
}
';' expression ';' identifier '=' expression ')' vpp_line_termination
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
cstack = push(cstack,
create_generic_ll(FOR_STRUCTURE, NULL,
create_for_structure(vp1,$9,$11,$13)
)
);
loop_struct_count++;
set_to_copy();
}
;
endfor_declaration : TTENDFOR {set_to_vpp_mode();} vpp_line_termination_opt
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
if (cstack != NULL)
cstack = push(cstack, create_generic_ll(ENDFOR_STRUCTURE,NULL,
NULL)
);
else
fprintf(stderr,"ERROR : got an endfor without matching for!!!\n");
if (loop_struct_count != 0) {
loop_struct_count--;
if (loop_struct_count == 0) {
eval_stack(cstack);
cstack = NULL;
set_to_initial_mode();
}
else set_to_last_mode();
}
if (cstack == NULL) set_to_initial_mode();
}
;
endwhile_declaration : TTENDWHILE {set_to_vpp_mode();} vpp_line_termination_opt
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
if (cstack != NULL)
cstack = push(cstack, create_generic_ll(ENDWHILE_STRUCTURE,NULL,
NULL)
);
else
fprintf(stderr,"ERROR : got an endwhile without matching while!!!\n");
if (loop_struct_count != 0) {
loop_struct_count--;
if (loop_struct_count == 0) {
eval_stack(cstack);
cstack = NULL;
set_to_initial_mode();
}
else set_to_last_mode();
}
if (cstack == NULL) set_to_initial_mode();
}
;
if_declaration : TTIF {set_to_vpp_mode();} expression vpp_line_termination
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
cstack = push(cstack,
create_generic_ll(IF_STRUCTURE, NULL,
create_if_structure($3)
)
);
loop_struct_count++;
set_to_copy();
}
;
ifdef_declaration : TTIFDEF {set_to_vpp_mode();} expression vpp_line_termination
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
cstack = push(cstack,
create_generic_ll(IFDEF_STRUCTURE, NULL,
create_if_structure($3)
)
);
loop_struct_count++;
set_to_copy();
}
;
ifndef_declaration : TTIFNDEF {set_to_vpp_mode();} expression vpp_line_termination
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
cstack = push(cstack,
create_generic_ll(IFNDEF_STRUCTURE, NULL,
create_if_structure($3)
)
);
loop_struct_count++;
set_to_copy();
}
;
else_declaration : TTELSE {set_to_vpp_mode();} vpp_line_termination
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
if (cstack != NULL)
cstack = push(cstack, create_generic_ll(ELSE_STRUCTURE,NULL,
NULL)
);
else
fprintf(stderr,"ERROR : got an else without matching if!!!\n");
set_to_last_mode();
}
;
endif_declaration : TTENDIF {set_to_vpp_mode();} vpp_line_termination_opt
{
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
if (cstack != NULL)
cstack = push(cstack, create_generic_ll(ENDIF_STRUCTURE,NULL,
NULL)
);
else
fprintf(stderr,"ERROR : got an endif without matching if!!!\n");
if (loop_struct_count != 0) {
loop_struct_count--;
if (loop_struct_count == 0) {
eval_stack(cstack);
cstack = NULL;
set_to_initial_mode();
}
else set_to_last_mode();
}
if (cstack == NULL) set_to_initial_mode();
}
;
vpp_line_termination_opt : /* empty */
| vpp_line_termination
;
vpp_line_termination : '\n'
| TTCOMMENT
| TTCOMMENT2
;
let_declaration : TTLET {set_to_vpp_mode();}
identifier '=' expression optional_semi vpp_line_termination_opt
{
if (loop_struct_count > 0) {
if (code_pointer != NULL)
cstack = push(cstack, create_generic_ll(CODE_POINTER,NULL,
code_pointer)
);
code_pointer = NULL;
cstack = push(cstack, create_generic_ll(LET_STRUCTURE,NULL,
create_variable_ref($3,$5,FALSE))
);
free($3); /* create_variable_ref duplicates the name */
}
else {
create_variable_ref($3,$5, TRUE);
}
set_to_last_mode();
}
;
optional_semi : /* empty */
| ';'
;
expression : primary {$$ = $1;}
/*
* unary operators ...
* + - ! ~ & ~& | ^| ^ ~^
*/
| '+' primary %prec UNARY {$$ = create_expression(UPLUS,$2,NULL,NULL);}
| '-' primary %prec UNARY {$$ = create_expression(UMINUS,$2,NULL,NULL);}
| '!' primary %prec UNARY {$$ = create_expression(UNOT,$2,NULL,NULL);}
| '~' primary %prec UNARY {$$ = create_expression(UTILDA,$2,NULL,NULL);}
| '&' primary %prec UNARY {$$ = create_expression(UAND,$2,NULL,NULL);}
| TTL_NAND primary %prec UNARY {$$ = create_expression(UNAND,$2,NULL,NULL);}
| TTL_NOR primary %prec UNARY /* +++ lrm : NOTE not in lrm */ {$$ = create_expression(UNOR,$2,NULL,NULL);}
| '|' primary %prec UNARY {$$ = create_expression(UPIPE,$2,NULL,NULL);}
| '^' primary %prec UNARY {$$ = create_expression(UCARAT,$2,NULL,NULL);}
| TTL_XNOR primary %prec UNARY {$$ = create_expression(UXNOR,$2,NULL,NULL);}
/*
* binary operators ...
* + - * / % == != && || < <= > >= & | ^ >> <<
*/
| expression '+' expression {$$ = create_expression(ADD,$1,$3,NULL);}
| expression '-' expression {$$ = create_expression(SUB,$1,$3,NULL);}
| expression '*' expression {$$ = create_expression(MULT,$1,$3,NULL);}
| expression '/' expression {$$ = create_expression(DIV,$1,$3,NULL);}
| expression '%' expression {$$ = create_expression(MOD,$1,$3,NULL);}
| expression TTL_EQ expression {$$ = create_expression(EQ,$1,$3,NULL);}
| expression TTL_NEQ expression {$$ = create_expression(NEQ,$1,$3,NULL);}
| expression TTL_AND expression {$$ = create_expression(AND,$1,$3,NULL);}
| expression TTL_OR expression {$$ = create_expression(OR,$1,$3,NULL);}
| expression '<' expression {$$ = create_expression(LT,$1,$3,NULL);}
| expression TTL_LEQ expression {$$ = create_expression(LEQ,$1,$3,NULL);}
| expression '>' expression {$$ = create_expression(GT,$1,$3,NULL);}
| expression TTL_GEQ expression {$$ = create_expression(GEQ,$1,$3,NULL);}
| expression '&' expression {$$ = create_expression(BITAND,$1,$3,NULL);}
| expression '|' expression {$$ = create_expression(BITOR,$1,$3,NULL);}
| expression '^' expression {$$ = create_expression(BITXOR,$1,$3,NULL);}
| expression TTL_SHIFTR expression {$$ = create_expression(SHIFTR,$1,$3,NULL);}
| expression TTL_SHIFTL expression {$$ = create_expression(SHIFTL,$1,$3,NULL);}
| expression TT_POWER expression {$$ = create_expression(POWER,$1,$3,NULL);}
| TTLOG2 '(' expression ')' {$$ = create_expression(LOG2,$3,NULL,NULL);}
| TTROUND '(' expression ')' {$$ = create_expression(ROUND,$3,NULL,NULL);}
| TTCEIL '(' expression ')' {$$ = create_expression(CEIL,$3,NULL,NULL);}
| TTFLOOR '(' expression ')' {$$ = create_expression(FLOOR,$3,NULL,NULL);}
| TTEVEN '(' expression ')' {$$ = create_expression(EVEN,$3,NULL,NULL);}
| TTODD '(' expression ')' {$$ = create_expression(ODD,$3,NULL,NULL);}
| TTMAX '(' expression ',' expression ')' {$$ = create_expression(MAX,$3,$5,NULL);}
| TTMIN '(' expression ',' expression ')' {$$ = create_expression(MIN,$3,$5,NULL);}
| TTABS '(' expression ')' {$$ = create_expression(ABS,$3,NULL,NULL);}
| TTSTOI '(' expression ')' {$$ = create_expression(STOI,$3,NULL,NULL);}
| TTITOS '(' expression ',' expression ')' {$$ = create_expression(ITOS,$3,$5,NULL);}
| TTSUBSTR '(' expression ',' expression ',' expression ')' {$$ = create_expression(SUBSTR,$3,$5,$7);}
| TTSYSTEM '(' expression ')' {$$ = create_expression(SYSTEM,$3,NULL,NULL);}
;
primary : TTNUM
{$$ = create_primary(NMBR,ttid,NULL,NULL,NULL);}
| TTREALNUM
{$$ = create_primary(REAL,ttid,NULL,NULL,NULL);}
| TTNAME
{$$ = create_primary(STRING,ttid,NULL,NULL,NULL);}
| TTQS {
$$ = create_primary(QUOTED_STRING,ttid,NULL,NULL,NULL);
}
| '(' expression ')' {$$ = $2;}
;
identifier : TTNAME
{
$$ = (char *) malloc(strlen(ttid) + 1);
strcpy($$, ttid);
}
;
/**********************************************************************
*
* end of included from previous yacc..
*
**********************************************************************/
%%
void
yyerror(char *s)
{
int len_tok; /* length of yytext */
int len_line; /* length of current line */
int i;
fprintf(stderr,"%5d:%s\n",nl_count-1,old_line);
fprintf(stderr,"%5d:%s\n",nl_count,current_line);
len_tok = strlen(yytext);
len_line = strlen(current_line);
if ((len_line-len_tok) > 0) {
for (i=0; i<(len_line-len_tok-1+7); i++) fprintf(stderr," ");
for (i=len_line-len_tok; i<len_line; i++) fprintf(stderr,"^");
fprintf(stderr,"\n");
}
fprintf(stderr,"%s\n\tfile '%s'",s, current_file);
fprintf(stderr," near line %d\n",nl_count);
/* Keep track of number of errors for program exit. */
++yyerror_count;
if (yyerror_count > 100) {
fflush(stdout);
fprintf(stderr, "\nToo many errors, giving up.\n");
exit(yyerror_count);
}
}
int
yywrap ()
{
return (1);
}