-
Notifications
You must be signed in to change notification settings - Fork 0
/
decl.atg
232 lines (199 loc) · 5.3 KB
/
decl.atg
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
//0426-8026317
//Luiisaweed@hotmail.com
//21170102.jared
//Luisa25161818
//3591413
#include <stdlib.h>
#include <iostream>
using namespace std;
COMPILER Pro
class VarData{
public:
int kind = 0; //var = 0 fun = 1 puntero = 2
int deph = 0;
string * type;
string * name;
VarData * cvd=NULL;
VarData(){
name = new string("");
type = new string("");
};
};
void _array(VarData * vd){
_array1(vd,0);
}
void _array1(VarData * vd,int size){
VarData * cvd = new VarData();
cvd->kind = 0;
cvd->type = new string("array");
cvd->name = vd->name;
cvd->cvd = new VarData();
*cvd->cvd = *vd;
*vd = *cvd;
}
void funcion(VarData * vd){
VarData * cvd = new VarData();
cvd->kind =1;
cvd->type = new string("function");
cvd->name = vd->name;
cvd->cvd = new VarData();
*cvd->cvd = *vd;
*vd = *cvd;
}
void puntero(VarData * vd){//puntero
//vd->kind=2;
VarData * cvd = new VarData();
cvd->kind = 0;
cvd->type = new string("puntero");
cvd->name = vd->name;
cvd->cvd = new VarData();
*cvd->cvd = *vd;
*vd = *cvd;
}
void dump(VarData * vd,int d){
for (int i = 0; i < d;i++)
{
cout<<" ";
}
cout<<"name: "<<*vd->name<<", tipo: "<<*vd->type<<", kind: "<<vd->kind<<endl;
if(vd->cvd!=NULL){
dump(vd->cvd,++d);
}
}
VarData* last(VarData * vd){//importante
if(vd->cvd==NULL){
return vd;
}else{
return last(vd->cvd);
}
}
void LexString(string *lex){
*lex = *new string(string(coco_string_create_char(t->val)));
//cout<<" ft "<<ft->c_str()<<endl;
//lex->append(*ft);
//*lex = *ft;
}
CHARACTERS
whiteSpace = ' ' + '\t' + '\r' +'\n'.
digit = "0123456789" .
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" .
TOKENS
number = digit { digit } .
name = letter { letter } .
IGNORE whiteSpace
PRODUCTIONS
//int ** a
//**a:int
Pro = {VarDecl|FuncDecl}.
VarDecl = (.
int c_declaraciones = 0;
VarData ** declaraciones = (VarData**)malloc(sizeof(VarData*)*100);
string * Tipe = new string(""); .)
//":"
(.VarData * vd = new VarData();.)
VarDcl<vd> (.declaraciones[c_declaraciones++]=vd;.)
{
(. vd = new VarData();.)
","
VarDcl<vd>
(.declaraciones[c_declaraciones++]=vd;.)
}
":"
(
name (. Tipe = new string(""); LexString(Tipe);
cout << " " << *Tipe << endl<<endl;
//declaraciones[c_declaraciones++]=vd;
for (int i = 0; i < c_declaraciones; i++)
{
last(declaraciones[i])->name = declaraciones[i]->name;
last(declaraciones[i])->type = Tipe;
}
//tab->insert(vd->name,vd)
//dump(vd,0);
.))
(.
for (int i = 0; i < c_declaraciones; i++)
{
dump(declaraciones[i],0);
}
.)
/*{","
(.VarData * vd = new VarData();.)
VarDcl<vd> (.
cout << " " << *Tipe << endl<<endl;
last(vd)->name = vd->name;
last(vd)->type = Tipe;
//tab->insert(vd->name,vd)
dump(vd,0);
.)
}*/
.
VarDcl<VarData *vd> =
("*")
VarDcl<vd> (. cout << " puntero a";.)
(.
puntero(last(vd));
.)
| VarDirectDcl<vd> .
VarDirectDcl<VarData *vd> = (. string * Name = new string("");; .)
( name (. LexString(Name);
cout <<endl<< " " << *Name << " es";
vd->name=Name;
.)
| "(" VarDcl<vd> ")"
) { VarSuffix<vd> } .
VarSuffix<VarData *vd> = (.VarData * vdp = new VarData(); string * buff = new string(""); .)
"[" (. cout << " array ["; .)
[ number (. LexString(buff);
cout << atoi(buff->c_str()); .)
]
"]" (. _array(last(vd)); cout << "] de"; .)
| (..)"(" [VarDecl{","VarDecl}] ")" (.funcion(last(vd));;cout << " function q retorna"; .) .
///////////////////
FuncDecl = (. string * Tipe = new string(""); int anon = 1;.)
"func" (. cout << " fun "; .)
//["[" ("="|"=="|"!="|">"|">="|"<"|"<=") "]"] // para metodos
[name
(. anon=0; Tipe = new string(""); LexString(Tipe); .) ]
(. if(anon==1){
cout<<" anonima retorna "<<*Tipe<<" " ;
}else{
cout<<*Tipe<<" retorna ";
} .)
["(" [VarDecl{","VarDecl}] ")" ]
[FuncDcl]
":"
name (. Tipe = new string(""); LexString(Tipe); cout<< *Tipe << endl; .)
"{"
(.string *Name = new string(""); .)Expr<Name>
"}"
.
FuncDcl =
{"*" FuncDcl (. cout << " puntero a"; .) | }
| FuncDirectDcl .
FuncDirectDcl = (. string * Name = new string(""); cout<<""; .)
( "(" FuncDcl ")"
) { FuncSuffix } .
FuncSuffix = (. string * buff = new string(""); .)
"[" (. cout << " array ["; .)
[ number (. LexString(buff);
cout << atoi(buff->c_str()); .)
]
"]" (. cout << "] de"; .)
| "(" [VarDecl{","VarDecl}] ")" (. cout << " function q retorna"; .) .
Expr<string *n> = {
Factor<n>
}.
Factor<string *n> =
(name (. LexString(n); cout<<" load "<<*n<<endl; .)
|"&"Factor<n> (. cout<<" referenciar "<<*n<<endl; .) // al refenciar el embuelve en un puntero
|"*"Factor<n> (. cout<<" desreferenciar "<<*n<<endl; .)
|"["number"]" (. cout<<" desreferenciar "<<*n<<endl; .)
|"."Factor<n> (. cout<<" acceder "<<*n<<endl; .)
| "("Expr<n>")"
|FuncDecl (. *n=*new string(" function declarada "); .))
{ FuncParams (. cout << " call "<<*n<<endl; *n=*new string(" resultado de funcion "); .) }
.
FuncParams = (. string * buff = new string(""); .)
"(" [Expr<buff>{","Expr<buff>}] ")" .
END Pro.