-
Notifications
You must be signed in to change notification settings - Fork 0
/
toy.y
220 lines (164 loc) · 6.51 KB
/
toy.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
%{
#include <stdio.h>
int yylex();
int yyerror(const char *msg);
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "sym.h"
#include "ast.c"
#include "sym.c"
int printg(){
printf("blah");
return 0;
}
int error = 1;
int valid = 1;
char *fields;
char *dot;
%}
%union{
char* str;
int val;
Expression *expression;
ExpType *expt;
}
%token BOOL TRUE FALSE VOID PRINTF STRUCT IF THEN ELSE FOR RETURN MOD INT;
%token AND OR NOT;
%token <str> ID;
%token <val> NUMBER;
%token STRING COMMENT STRING_LITERAL;
%token EQU LTE GTE NEQ OB CB SEMICOLON NEGATE OP CP;
%token ADD SUB MUL DIV DOT COMMA ASSIGN LT GT;
%type <val> type;
%type <val> declaration;
%type <val> struct_;
%type <val> proc;
%type <val> Name;
%type <val> int_literal;
%type <val> string_literal;
%type <val> bool_literal;
%type <val> stmt;
%type <val> stmt_seq;
%type <val> binary_maths_op;
%type <val> binary_boolean_op_r;
%type <val> binary_boolean_op_nr;
%type <val> unary_maths_op;
%type <val> unary_boolean_op;
%type <val> return_type;
%type <val> pgm;
%type <val> pgm2;
%type <val> line;
%type <val> input;
%type <expression> exp;
%type <val> declaration_check;
%type <expt> l_exp;
%%
input: { $$ = 1; } /* empty*/
|input line {if(($1 == 1) && ($2 == 1)) { valid = 1; $$ = 1; } else{ valid = 0; $$ = 0; }}
;
line:
|pgm { if(check_for_main() == 1 && ($1 == 1)) { $$ = 1; }else { $$ = 0; }}
;
pgm2:
| proc pgm2 { if(($1 == 1) && ($2 == 1)){ $$=1; } else { $$=0; }}
| struct_ pgm2 { if(($1 == 1) && ($2 == 1)){ $$=1; } else { $$=0; }}
;
pgm: proc pgm2 { if(($1 == 1) && ($2 == 1)){ $$=1; } else { $$=0; } }
| struct_ pgm { if(($1 == 1) && ($2 == 1)){ $$=1; } else { $$=0; } }
;
exp: /* nothing */ { $$ = NULL; }
| OP exp CP {$$ = $2; }
| unary_boolean_op exp { $$ = add_expression(5,9,13,NULL,$2,NULL); }
| unary_maths_op exp { $$ = add_expression(4,11,13,NULL,$2,NULL); }
| exp binary_maths_op exp { $$ = add_expression(4,11,12,$1,$3,NULL); }
| exp binary_boolean_op_r exp { $$ = add_expression(5,10,12,$1,$3,NULL); }
| exp binary_boolean_op_nr exp { $$ = add_expression(5,10,12,$1,$3,NULL); }
| int_literal { $$ = add_expression(4,14,14,NULL,NULL,NULL); }
| string_literal { $$ = add_expression(6,14,14,NULL,NULL,NULL); }
| bool_literal { $$ = add_expression(5,14,14,NULL,NULL,NULL); }
| ID OP declaration_check CP { printf("calling expression\n");$$ = add_expression(get_return_type_of_a_proc($1),14,14,NULL,NULL,NULL); }
| l_exp { /*printf("right side exp: %d, %s\n",$1->type,$1->sname);*/ $$ = add_expression($1->type,14,14,NULL,NULL,$1->sname); }
;
int_literal: NUMBER {if($1 >= 32768 || $1 < -32768 ) {printf("too big int");$$ = 0;} else{$$ = 1;}}
;
string_literal: STRING_LITERAL {$$ = 1;}
;
bool_literal: TRUE {$$ = 1;}| FALSE {$$ = 1;}
;
binary_maths_op: ADD {$$ = 1;}
| MUL {$$ = 1;}
| DIV {$$ = 1;}
| SUB {$$ = 1;}
| MOD {$$ = 1;}
;
unary_maths_op: SUB {$$ = 1;}
;
unary_boolean_op: NEGATE {$$ = 1;}
;
binary_boolean_op_r: LTE {$$ = 1;}
| GTE {$$ = 1;}
| LT {$$ = 1;}
| GT {$$ = 1;}
| OR {$$ = 1;}
| AND {$$ = 1;}
;
binary_boolean_op_nr: EQU {$$ = 1;}
| NEQ {$$ = 1;}
;
type : INT { $$ = 4; }
| BOOL { $$ = 5; }
| STRING { $$ = 6; }
| ID {store_struct_name(strtok($1, " ")); $$ = 7; }
;
declaration: type ID {if($1 != 0) { if(add_to_scope($1, $2) == 1){ $$ = 1; if($1 == 7) {add_struct_name(); add_struct_to_scope(strtok($2, ";")); struct_name = NULL;} } else { $$ = 0;} } else {$$ = 0;}}
| declaration COMMA declaration { if($1 == 0 || $3 == 0) { $$ = 0; } else {$$ = 1;} }
;
declaration_check: exp { $$=1;}
| declaration_check COMMA exp { $$=1;}
;
return_type : type {store_return_type($1); $$ = $1; }
| VOID { store_return_type(1); $$ = 1;}
;
struct_ : STRUCT Name OB declaration CB { is_struct(1); new_scope(); if($4 == 1 && $2 == 1){ $$ = 1; } else {$$ = 0;} }
;
l_exp : ID { print_symbol_table(); fields = strdup($1); dot = strdup("."); printf("fields : %s\n", fields); if(check_scope($1) == 1) { $$ = return_type($1); } printf("found that this is of type: %d\n", $$->type); } /* return type, if 0 then invalid */
| ID DOT l_exp { dot = strdup("."); printf("fields : %s\n", fields); strcat(dot, fields); strcat($1,dot); fields = strdup($1); printf("fields : %s\n", fields); if (check_if_field(fields) != 0) { printf("valid\n"); $$=$3; } else { printf("cant get it");$$ = NULL; }}
;
intern_scope_then: THEN {add_internal_scope(); }
;
intern_scope_else: ELSE { delete_scope(); add_internal_scope(); }
;
FOR_LOOP: FOR { add_internal_scope(); }
;
stmt : FOR_LOOP OP ID ASSIGN exp SEMICOLON exp SEMICOLON stmt CP stmt { delete_scope(); if(check_scope($3) == 1 && $9 == 1 && $11 == 1 && check_compatibility( 5, $7 )==1 && check_compatibility(return_type($3)->type, $5)) { $$ = 1; } else { $$ = 0; } }
| FOR_LOOP OP SEMICOLON exp SEMICOLON stmt CP stmt { delete_scope(); if( $6 == 1 && $8 == 1 && check_compatibility( 5, $4 )==1 ) { $$ = 1; } else { $$ = 0; } }
| IF OP exp CP intern_scope_then stmt { delete_scope(); if(check_compatibility( 5, $3 )!=1 || $6 == 0){$$ = 0;}else{$$ = 1;} }
| IF OP exp CP intern_scope_then stmt intern_scope_else stmt { delete_scope(); if($6 == 0 || $8 == 0 || check_compatibility( 5, $3 )==0) { $$ = 0; } else { $$ = 1; } }
| PRINTF OP exp CP SEMICOLON { $$ = check_compatibility(6,$3); }
| RETURN exp SEMICOLON {ExpType* r = get_return_type_current_proc(); $$ = check_compatibility_dyn(r,$2, 1); }
| OB stmt_seq CB { $$ = $2; }
| ID OP declaration_check CP SEMICOLON { if (get_return_type_of_a_proc($1) == 1) {$$ = 1;}else{$$=0;} }
| type ID SEMICOLON { if($1 == 0 || add_to_scope($1, $2) == 0) { $$ = 0; } else { $$ = 1; } if($1 == 7) { add_struct_name(); add_struct_to_scope($2); struct_name = NULL;}}
| l_exp ASSIGN exp SEMICOLON { if( check_compatibility_dyn($1, $3, 0) == 1 && ($1 != 0) ) { $$ = 1; } else { $$ = 0; }}
;
stmt_seq : /* empty */
| stmt stmt_seq { if($1 == 0 || $2 == 0) { $$ = 0; } else { $$ = 1;}}
;
Name : ID { if(add_name($1) == 1){ $$ = 1; } else {$$ = 0;} }
;
proc : return_type Name OP declaration CP OB stmt_seq CB {new_scope(); if($4 == 1 && $2 == 1 && $7 == 1){ $$ = 1; } else {$$ = 0;}}
;
%%
main()
{
extern FILE *yyin, yyout;
yyin = fopen("Test7VALID.txt", "r");
int parse = yyparse();
printf("valid: %d\nerror: %d\n", valid, error);
if(valid == 0 || error == 0) printf ("Error\n");
else printf("\nValid\n\n");
}
int yyerror(const char *msg){
error = 0;
}