-
Notifications
You must be signed in to change notification settings - Fork 0
/
myLang.y
187 lines (151 loc) · 6.85 KB
/
myLang.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
%{
#include <stdio.h>
#include <string.h>
#include "ast.h"
int yylex(void);
void yyerror(char *);
extern char* yytext;
%}
%union{
int iValue;
char* icontent;
past pAst1, pAst2, pAst3, pAst4;
};
%token <iValue> NUMBER INT STR VOID IF ELSE WHILE RETURN CMP
%token <icontent> STRING ID
%type <pAst1> program external_declaration function_definition declaration init_declarator_list init_declarator intstr_list
%type <pAst2> initializer declarator direct_declarator parameter_list parameter type statement compound_statement begin_scope
%type <pAst3> end_scope statement_list expression_statement selection_statement iteration_statement jump_statement
%type <pAst4> expr assign_expr cmp_expr add_expr mul_expr primary_expr expr_list
%%
program
: external_declaration { $$ = newList(NULL,$1); strcpy($$->nodeName,"program"); astRoot=$$; }
| program external_declaration { $$ = newList($1,$2);}
;
external_declaration
: function_definition { $$ = $1; }
| declaration { $$ = $1; }
;
function_definition
: type declarator compound_statement { $$ = newDecl("funcDef", $1->ivalue, ".", $2, $3); }
;
declaration
: type init_declarator_list ';' { $$ = newDecl("decl", $1->ivalue, ".", $1, $2); }
;
init_declarator_list
: init_declarator { $$ = newList(NULL,$1); }
| init_declarator_list ',' init_declarator { $$ = newList($1, $3); }
;
init_declarator
: declarator { $$ = $1; }
| declarator '=' add_expr { $$ = newDecl("varInitDecl", 0, "=", $1, $3); }
| declarator '=' '{' intstr_list '}' { $$ = newDecl("arrayInitDecl", 1, "=", $1, $4); }
;
intstr_list
: initializer { $$ = newList(NULL,$1); }
| intstr_list ',' initializer { $$ = newList($1, $3); }
;
initializer
: NUMBER { $$ = newNum($1); }
| STRING { $$ = newStr(STRING, $1); }
;
declarator
: direct_declarator { $$ = $1; }
;
direct_declarator
: ID { $$ = newDecl("varDecl", ID, $1, NULL, NULL); }
| direct_declarator '(' parameter_list ')' { $$ = newDecl("funcDecl", $1->ivalue, $1->nodeName, $1, $3); }
| direct_declarator '(' ')' { $$ = newDecl("funcDecl", $1->ivalue, $1->nodeName, $1, NULL); }
| ID '[' expr ']' { past temp= newId(ID, $1); $$ = newDecl("arrayDecl", ID, temp->nodeName, $3, NULL); }
| ID '[' ']' { past temp= newId(ID, $1); $$ = newDecl("arrayDecl", ID, temp->nodeName, NULL, NULL); }
;
parameter_list
: parameter { $$ = newList(NULL,$1); }
| parameter_list ',' parameter { $$ = newList($1, $3); }
;
parameter
: type ID { past temp= newId(ID, $2); $$ = newPara(temp->nodeName, $1); }
;
type
: INT { $$ = newType($1); }
| STR { $$ = newType($1); }
| VOID { $$ = newType($1); }
;
statement
: compound_statement { $$ = $1; }
| expression_statement { $$ = $1; }
| selection_statement { $$ = $1; }
| iteration_statement { $$ = $1; }
| jump_statement { $$ = $1; }
| declaration { $$ = $1; }
;
compound_statement
: begin_scope end_scope { $$ = NULL; }
| begin_scope statement_list end_scope { $$ = newStmt("compStmt", 0, $2, NULL); }
;
begin_scope
: '{' {}
;
end_scope
: '}' {}
;
statement_list
: statement { $$ = newList(NULL,$1); }
| statement_list statement { $$ = newList($1, $2); }
;
expression_statement
: ';' {}
| expr ';' { $$ = $1; }
;
selection_statement
: IF '(' expr ')' statement { $$ = newStmt("ifStmt", $1, $3, $5); }
| IF '(' expr ')' statement ELSE statement { past temp1 = newStmt("elseStmt",$6,NULL,$7); past temp2 = newStmt("ifStmt",$1,$3,$5); $$ = newStmt("if_elseStmt", $1, temp2, temp1); }
;
iteration_statement
: WHILE '(' expr ')' statement { $$ = newStmt("whileStmt", $1, $3, $5); }
;
jump_statement
: RETURN ';' { $$ = newStmt("returnStmt", $1, NULL, NULL); }
| RETURN expr ';' { $$ = newStmt("returnStmt", $1, NULL, $2); }
;
expr
: assign_expr { $$ = $1; }
;
assign_expr
: cmp_expr { $$ = $1; }
| ID '=' assign_expr { past temp = newId(ID, $1); strcpy(temp->nodeType,"idRef");$$ = newExpr('=', temp, $3); }
| ID '[' expr ']' '=' assign_expr { past temp = newId(ID, $1); strcpy(temp->nodeType,"idRef");$$ = newExtend("expr", '=', temp, $3, $6); }
;
cmp_expr
: add_expr { $$ = $1; }
| cmp_expr CMP add_expr { $$ = newExpr($2, $1, $3); }
;
add_expr
: mul_expr { $$ = $1; }
| add_expr '+' mul_expr { $$ = newExpr('+', $1, $3); }
| add_expr '-' mul_expr { $$ = newExpr('-', $1, $3); }
;
mul_expr
: primary_expr { $$ = $1; }
| mul_expr '*' primary_expr { $$ = newExpr('*', $1, $3); }
| mul_expr '/' primary_expr { $$ = newExpr('/', $1, $3); }
| mul_expr '%' primary_expr { $$ = newExpr('%', $1, $3); }
| '-' primary_expr { $$ = newExpr('-', NULL, $2); }
;
primary_expr
: ID '(' expr_list ')' { past temp = newId(ID, $1); strcpy(temp->nodeType,"idRef");$$ = newRef("funcRef", temp, $3); }
| ID '(' ')' { past temp = newId(ID, $1); strcpy(temp->nodeType,"idRef");$$ = newRef("funcRef", temp, NULL); }
| '(' expr ')' { $$ = $2; }
| ID { $$ = newId(ID, $1); strcpy($$->nodeType,"idRef");}
| initializer { $$ = $1; }
| ID '[' expr ']' { past temp = newId(ID, $1); strcpy(temp->nodeType,"idRef");$$ = newRef("arrayRef", temp, $3); }
;
expr_list
: expr { $$ = newList(NULL,$1); }
| expr_list ',' expr { $$ = newList($1, $3); }
;
%%
void yyerror(char *s)
{
fprintf(stderr, "%s\n", s);
}