-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtema1.y
More file actions
194 lines (168 loc) · 6.69 KB
/
tema1.y
File metadata and controls
194 lines (168 loc) · 6.69 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
%{
#include <stdio.h>
extern FILE* yyin;
extern char* yytext;
extern int yylineno;
char varDeclarateGl[250][250];
int k;
int i;
int ok = 1;
int verifica(char *var,char* tip){
for(i=1;i<=k;++i) if(strcmp(varDeclarateGl[i],var) == 0) {printf("%s %s de la linia %d a mai fost declarata\n",tip,var,yylineno);ok = 0;return 0;}
strcpy(varDeclarateGl[++k],var);
return 1;
}
%}
%union
{
int number;
char *string;
}
%type <string> ID
%token ID TIP BGIN END ASSIGN NR DEF MCRO LIB IFF ELSEE FOR WHILE CMP_OP_ING INC_DEC CMP_OP_EG STRUCT RETURN COMM
%start program
%%
program : bloc_de_start structuri declaratii_globale functii {if(ok)printf("program corect sintactic\n"); else printf("programul nu este corect\n");}
| bloc_de_start declaratii_globale functii {if(ok)printf("program corect sintactic\n"); else printf("programul nu este corect\n");}
;
/* DECLARATII GLOBALE */
bloc_de_start : start_statement
| bloc_de_start start_statement
;
start_statement : DEF MCRO NR
| LIB
;
declaratii_globale : declaratie_globala ';'
| declaratii_globale declaratie_globala ';'
| COMM
| declaratii_globale COMM
;
declaratie_globala : TIP ID {verifica($2,"variabila");}
| TIP ID '[' NR ']' {verifica($2,"variabila");
}
| TIP ID '[' NR ']' '[' NR ']' {verifica($2,"variabila");}
| tip_functie
;
tip_functie : TIP ID '(' ')' {verifica($2,"functia");}
| TIP ID '(' lista_param_decl ')' {verifica($2,"functia");}
| TIP ID ':' ID '(' ')'
| TIP ID ':' ID '(' lista_param_decl ')'
;
param_decl : TIP ID
| TIP ID '[' NR ']'
| TIP ID '[' NR ']' '[' NR ']'
| tip_functie
;
lista_param_decl : param_decl
| lista_param_decl ',' param_decl
expresie : e '+' e
| e '*' e
| e '/' e
| e '-' e
;
e : NR
| ID
;
/* FUNCTII */
functii : tip_functie functie
| functii tip_functie functie
;
functie : BGIN declaratii_locale lista_instructiuni lista_instr_ctrl return_type END
| BGIN declaratii_locale lista_instructiuni return_type END
| BGIN return_type END
| BGIN declaratii_locale return_type END
| BGIN lista_instructiuni return_type END
;
return_type : RETURN ID ';'
| RETURN NR ';'
|
;
declaratii_locale : declaratie_locala ';'
| declaratii_locale declaratie_locala ';'
;
declaratie_locala : TIP ID
| TIP ID '[' NR ']'
| TIP ID '[' NR ']' '[' NR ']'
| /* Epsilon */
;
lista_instructiuni : instructiune ';'
| lista_instructiuni instructiune ';'
| COMM
| lista_instructiuni COMM
;
instructiune : ID ASSIGN ID
| ID ASSIGN NR
| ID ASSIGN expresie
| ID ASSIGN INC_DEC ID
| ID ASSIGN INC_DEC NR
| ID ASSIGN INC_DEC '(' expresie ')'
| ID '(' lista_apel ')'
| '[' ID ID ']'
| '[' ID ID '(' lista_apel ')' ']'
| ID INC_DEC
;
lista_instr_ctrl : instructiune_ctrl
| lista_instr_ctrl instructiune_ctrl
;
instructiune_ctrl : IFF '(' list_exp ')' '{' lista_instructiuni '}' ELSEE '{' '}'
| IFF '(' list_exp ')' '{' '}' ELSEE '{' '}'
| IFF '(' list_exp ')' '{' lista_instructiuni '}'
| IFF '(' list_exp ')' '{' '}'
| IFF '(' list_exp ')' '{' lista_instructiuni '}' ELSEE '{' lista_instructiuni '}'
| IFF '(' list_exp ')' '{' '}' ELSEE '{' lista_instructiuni '}'
| FOR '(' ID ASSIGN NR ';' ID CMP_OP_ING NR ';' ID INC_DEC')' '{' lista_instructiuni '}'
| FOR '(' ID ASSIGN NR ';' ID CMP_OP_ING NR ';' ID INC_DEC')' '{' '}'
| FOR '(' ID ASSIGN NR ';' ID CMP_OP_ING ID ';' ID INC_DEC')' '{' lista_instructiuni '}'
| FOR '(' ID ASSIGN NR ';' ID CMP_OP_ING ID ';' ID INC_DEC')' '{' '}'
| FOR '(' ID ASSIGN NR ';' ID CMP_OP_ING MCRO ';' ID INC_DEC')' '{' lista_instructiuni '}'
| FOR '(' ID ASSIGN NR ';' ID CMP_OP_ING MCRO ';' ID INC_DEC')' '{' '}'
| WHILE '(' ID CMP_OP_ING NR ')' '{' lista_instructiuni '}'
| WHILE '(' ID CMP_OP_ING NR ')' '{' '}'
| WHILE '(' ID CMP_OP_EG NR ')' '{' lista_instructiuni '}'
| WHILE '(' ID CMP_OP_EG NR ')' '{' '}'
;
list_exp : exp
| list_exp '^' exp
| list_exp '|' exp
;
exp : '!' ID
| ID
| ID operator NR
| ID operator ID
| '!' ID operator NR
| '!' ID operator ID
| MCRO
| '!' MCRO
| MCRO operator ID
| ID operator MCRO
| NR operator ID
;
operator : CMP_OP_ING
| CMP_OP_EG
| CMP_OP_ING CMP_OP_EG
;
lista_apel : NR
| ID
| lista_apel ',' NR
| lista_apel ',' ID
| expresie
| lista_apel ',' expresie
| MCRO
| lista_apel ',' MCRO
| /* EPSILON */
;
structuri : structura
| structuri structura
;
structura : STRUCT ID '{' '}' {verifica($2,"structura");}
| STRUCT ID '{' declaratii_globale '}' {verifica($2,"structura");}
| STRUCT ID '{' declaratii_globale functii '}' {verifica($2,"structura");}
;
%%
int yyerror(char * s){
printf("eroare: %s la linia:%d\n",s,yylineno);
}
int main(int argc, char** argv){
yyin=fopen(argv[1],"r");
yyparse();
}