-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhile.y
More file actions
198 lines (171 loc) · 3.17 KB
/
while.y
File metadata and controls
198 lines (171 loc) · 3.17 KB
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
%{
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "type.h"
extern int nb_lines ;
extern NODE *mk_node(int type, NODE *n1, NODE *n2);
extern NODE *mk_leaf_int ();
extern NODE *mk_leaf_str();
extern NODE *maj_leaf_str();
%}
%token While
%token Skip
%token If
%token Then
%token Else
%token Do
%token Od
%token Assign
%token And
%token Declare
%token Begin
%token End
%token Integer
%token Identifier
%token Call
%token Proc
%token Is
%token Var
%token Leq
%token Geq
%token Lexical_error
%union{
PTR_NODE u_node;
char u_char[120];
int u_int ;
}
%type <u_node> program
%type <u_node> bloc
%type <u_node> declaration_list
%type <u_node> declaration
%type <u_node> instruction_list
%type <u_node> instruction
%type <u_node> formal_param_list
%type <u_node> effective_param_list
%type <u_node> e
%type <u_node> t
%type <u_node> f
%type <u_node> b
%type <u_node> bb
%type <u_int> Integer
%type <u_char> Identifier
%start program
%%
program : bloc
{root=$1;YYACCEPT;}
;
bloc :
Declare declaration_list Begin instruction_list End
{$$= (PTR_NODE) mk_node(BLOC,$2,$4);}
;
declaration_list :
declaration
{$$=$1 ;}
|
declaration_list ';' declaration
{$$= (PTR_NODE) mk_node(SEMI_COLON,$1,$3);}
;
declaration :
Var Identifier
{$$= (PTR_NODE) mk_leaf_str($2);} ;
|
Proc Identifier '(' formal_param_list ')' Is bloc
{$$= (PTR_NODE) mk_node(PROC_DECL, mk_leaf_str($2), (PTR_NODE) mk_node(PROC ,$4, $7));}
;
formal_param_list :
Identifier ',' formal_param_list
{$$= (PTR_NODE) mk_node(COMMA,mk_leaf_str($1),$3);}
|
Identifier
{$$= mk_leaf_str($1);}
|
{$$ = NULL;}
;
instruction_list :
instruction
{$$=$1 ;}
|
instruction_list ';' instruction
{$$= (PTR_NODE) mk_node(SEMI_COLON,$1,$3);}
;
instruction :
Identifier Assign e
{$$= (PTR_NODE) mk_node(ASSIGN,maj_leaf_str($1),$3);}
|
Skip
{$$= (PTR_NODE) mk_node(SKIP, NULL,NULL);}
|
While b Do instruction Od
{$$= (PTR_NODE) mk_node(WHILE,$2,$4);}
|
If b Then instruction Else instruction
{$$= (PTR_NODE) mk_node(IF,$2, (PTR_NODE) mk_node(THENELSE,$4,$6));}
|
Call Identifier '(' effective_param_list ')'
{$$= (PTR_NODE) mk_node(CALL,mk_leaf_str($2), $4);}
;
effective_param_list :
e ',' effective_param_list
{$$= (PTR_NODE) mk_node(COMMA,$1,$3);}
|
e
{$$= $1;}
|
{$$ = NULL;}
;
e :
e '+' t
{$$= (PTR_NODE) mk_node(PLUS,$1,$3);}
|
t
{$$=$1;}
;
t :
t '*' f
{$$= (PTR_NODE) mk_node(TIMES,$1,$3);}
|
f
{$$=$1;}
;
f :
'(' e ')'
{$$=$2;}
|
Identifier
{$$= (PTR_NODE) maj_leaf_str($1);}
|
Integer
{$$= (PTR_NODE) mk_leaf_int($1);}
;
b :
b And bb
{$$= (PTR_NODE) mk_node(AND,$1,$3);}
|
bb
{$$=$1;}
;
bb :
e Geq e
{$$= (PTR_NODE) mk_node(SUPEQ,$1,$3);}
|
e Leq e
{$$= (PTR_NODE) mk_node(INFEQ,$1,$3);}
|
e '<' e
{$$= (PTR_NODE) mk_node(INF,$1,$3);}
|
e '>' e
{$$= (PTR_NODE) mk_node(SUP,$1,$3);}
|
e '=' e
{$$= (PTR_NODE) mk_node(EGAL,$1,$3);}
|
'^' bb
{$$= (PTR_NODE) mk_node(NOT, $2,NULL);}
|
'('b')'
{$$=$2;}
;
%%
#include "lex.yy.c"