-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
191 lines (172 loc) · 3.19 KB
/
parser.y
File metadata and controls
191 lines (172 loc) · 3.19 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
%{
void yyerror (char *s);
int yylex();
#include <stdio.h>
#include "ast.h"
#include <string.h>
#include <stdlib.h>
%}
%union {char* str; int num;}
%start program
%type<num> expr
%type<num> exprs
%type<num> type
%type<num> main
%type<num> name
%type<num> VARTYPES
%token<str> CONSTINT
%token<str> COMPOP
%token<str> ENTRY
%token<str> SIMPARITHOP
%token<str> COMPARITHOP
%token FUNDEFKEY
%token PREDEFBOOL
%token PREDEFINT
%token<str> CONDOP
%token<str> CONSTBOOL
%token<str> UNBOOLOP
%token<str> BIBOOLOP
%token<str> VARDEC
%token<str> VARTYPE
%token PREDEF
%token LPAREN
%token RPAREN
%token<str> VARNAME
%%
program: LPAREN main expr RPAREN
{
insert_child($2);
insert_child($3);
insert_node("ENTRY",2,'u');
}|
LPAREN FUNDEFKEY name VARTYPES type expr RPAREN program
{
insert_child($3);
//printf("HERE: %d\n",$3);
//if ($4) insert_child($4);
int start = $3;
int end = $5 - 1;
while(start<end)
{
start++;
insert_child(start);
}
insert_child($5);
//printf("HERE: %d\n",$5);
insert_child($6);
//printf("HERE: %d\n",$6);
insert_node("DEFINE-FUN",0,'u');
}
main: ENTRY
{
$$=insert_node("main",0,'u');
}
VARTYPES: |
LPAREN VARNAME VARTYPE RPAREN VARTYPES
{
int temp=strlen($2)+strlen($3);
char * temp2 = (char*)malloc((temp+2)*sizeof(char));
strncpy(temp2, $2,temp);
strcat(temp2, " ");
strcat(temp2, $3);
$$=insert_node(temp2,0,'u');
//insert_child(1);
//insert_child(3);
//printf(temp2);
//I do not care about a memory leak.
// This is the price I pay for the program to work
//free(temp2);
}
type: VARTYPE
{
char * temp = (char*)malloc((strlen($1)+5)*sizeof(char));
strcpy(temp,"ret ");
strcat(temp,$1);
$$=insert_node(temp,0,$1[0]);
}
name: VARNAME
{
$$=insert_node($1,0,'v');
}
expr: CONSTINT
{
//printf("LOOKING AT ");
//printf($1);
//printf("\n");
$$ = insert_node($1,0,'i');
}|
LPAREN PREDEFINT RPAREN
{
$$ = insert_node("get-int",0,'i');
}|
LPAREN SIMPARITHOP expr expr exprs RPAREN
{
insert_child($3);
insert_child($4);
//insert_child($5);
$$ = insert_node($2,0,'i');
}|
LPAREN COMPARITHOP expr expr RPAREN
{
insert_child($3);
insert_child($4);
$$ = insert_node($2,0,'b');
}|
CONSTBOOL
{
$$ = insert_node($1,0,'b');
}|
VARNAME
{
$$ =insert_node($1,0,'v');
}|
LPAREN PREDEFBOOL RPAREN
{
$$ = insert_node("get-bool",0,'b');
}|
LPAREN COMPOP expr expr RPAREN
{
insert_child($3);
insert_child($4);
$$=insert_node($2,0,'b');
}|
LPAREN UNBOOLOP expr RPAREN
{
insert_child($3);
$$=insert_node("NOT",0,'b');
}|
LPAREN BIBOOLOP expr expr exprs RPAREN
{
insert_child($3);
insert_child($4);
//insert_child($5);
$$=insert_node($2,0,'b');
}|
LPAREN CONDOP expr expr expr RPAREN
{
insert_child($3);
insert_child($4);
insert_child($5);
$$=insert_node($2,0,'c');
}|
LPAREN VARNAME exprs RPAREN
{
//insert_child($3);
$$=insert_node($2,0,'f');
}|
LPAREN VARDEC LPAREN name expr RPAREN expr RPAREN
{
//$$=insert_node($2,0);
insert_child($4);
insert_child($5);
insert_child($7);
$$=insert_node($2,0,'d');
}
exprs: |
expr exprs
{
insert_child($1);
}
%%
void yyerror(char *s) {printf("Error: %s\n",s);}
int yywrap(void){return 1;}