-
Notifications
You must be signed in to change notification settings - Fork 0
/
uccompiler.y
executable file
·184 lines (152 loc) · 10.7 KB
/
uccompiler.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
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ast.h"
int yylex(void);
void yyerror (const char *s);
extern int line, col;
%}
%union{
char* value;
struct node *node;
}
%token CHAR ELSE IF INT SHORT DOUBLE RETURN VOID BITWISEAND BITWISEOR BITWISEXOR
%token AND ASSIGN MUL COMMA DIV EQ GE GT LBRACE LE LPAR LT MINUS MOD NE NOT OR PLUS
%token RESERVED RBRACE RPAR SEMI CHRLIT_INV CHRLIT_UNT
%token <value> ID INTLIT CHRLIT REALLIT WHILE
%left COMMA
%right ASSIGN
%left OR
%left AND
%left BITWISEOR
%left BITWISEXOR
%left BITWISEAND
%left EQ NE
%left LT GT LE GE
%left PLUS MINUS
%left MUL DIV MOD
%right NOT
%nonassoc THEN
%nonassoc ELSE
%type <node> Program FunctionsAndDeclarations FunctionDefinition
FunctionDeclaration Declaration FunctionDeclarator TypeSpec FunctionBody Declarator
Expr ParameterList ParameterDeclaration CommaExpr CommaExprTwo DeclarationsAndStatements
Statement CommaParamDeclaration CommaDeclarator StatementWithError StatementList ID_terminal
%%
Program: FunctionsAndDeclarations {ast = insert_node(line, col, "Program", NULL, 1, $1);}
;
FunctionsAndDeclarations: FunctionsAndDeclarations FunctionDefinition {$$ = add_sibling($1, $2);}
| FunctionsAndDeclarations FunctionDeclaration {$$ = add_sibling($1, $2);}
| FunctionsAndDeclarations Declaration {$$ = add_sibling($1, $2);}
| FunctionDefinition {$$ = $1;}
| FunctionDeclaration {$$ = $1;}
| Declaration {$$ = $1;}
;
FunctionDefinition: TypeSpec FunctionDeclarator FunctionBody {$$ = insert_node(line, col, "FuncDefinition", NULL, 3, $1, $2, $3);}
;
FunctionBody: LBRACE RBRACE {$$ = insert_node(line, col, "FuncBody", NULL, 0);}
| LBRACE DeclarationsAndStatements RBRACE {$$ = insert_node(line, col, "FuncBody", NULL, 1, $2);}
;
DeclarationsAndStatements: Statement DeclarationsAndStatements {$$ = add_sibling($1, $2);}
| Declaration DeclarationsAndStatements {$$ = add_sibling($1, $2);}
| Statement {$$ = $1;}
| Declaration {$$ = $1;}
;
StatementWithError: Statement {$$=$1;}
| error SEMI {$$ = NULL;}
;
Statement: CommaExpr SEMI {$$=$1;}
| SEMI {$$ = NULL;}
| LBRACE StatementList RBRACE { if($2 != NULL && $2->sibling != NULL) {
$$ = insert_node(line, col, "StatList", NULL, 1, $2);
}
else {
$$ = $2;
}
}
| LBRACE RBRACE {$$ = NULL;}
| LBRACE error RBRACE {$$ = NULL;}
| IF LPAR CommaExpr RPAR StatementWithError %prec THEN { $3 = make_node_correct(line, col, $3); $5 = make_node_correct(line, col, $5);
$$ = insert_node(line, col, "If", NULL, 3, $3, $5, insert_node(line, col, "Null", NULL, 0));
}
| IF LPAR CommaExpr RPAR StatementWithError ELSE StatementWithError { $3 = make_node_correct(line, col, $3); $5 = make_node_correct(line, col, $5); $7 = make_node_correct(line, col, $7);
$$ = insert_node(line, col, "If", NULL, 3, $3, $5, $7);
}
| WHILE LPAR CommaExpr RPAR StatementWithError { $3 = make_node_correct(line, col, $3); $5 = make_node_correct(line, col, $5);
$$ = insert_node(line, col, "While", NULL, 2, $3, $5);}
| RETURN CommaExpr SEMI { $2 = make_node_correct(line, col, $2);
$$ = insert_node(line, col, "Return", NULL, 1, $2);}
| RETURN SEMI {$$ = insert_node(line, col, "Return", NULL, 1, insert_node(line, col, "Null", NULL, 0));}
;
StatementList: StatementList StatementWithError {$$ = add_sibling($1, $2);}
| StatementWithError {$$=$1;}
;
FunctionDeclaration: TypeSpec FunctionDeclarator SEMI {$$ = insert_node(line, col, "FuncDeclaration", NULL, 2, $1, $2);}
;
FunctionDeclarator: ID_terminal LPAR ParameterList RPAR {$$ = add_sibling($1, $3);}
;
ParameterList: ParameterDeclaration CommaParamDeclaration {$$ = insert_node(line, col, "ParamList", NULL, 2, $1, $2);}
;
ParameterDeclaration: TypeSpec ID_terminal {$$ = insert_node(line, col, "ParamDeclaration", NULL, 2, $1, $2);}
| TypeSpec {$$ = insert_node(line, col, "ParamDeclaration", NULL, 1, $1);}
;
CommaParamDeclaration: COMMA ParameterDeclaration CommaParamDeclaration {$$ = add_sibling($2, $3);}
| /*empty*/ {$$ = NULL;}
;
Declaration: TypeSpec Declarator CommaDeclarator SEMI {
$2 = add_sibling($2, $3);
insert_node_special($1, $2);
$$ = $2;
}
| error SEMI {$$ = NULL;}
;
CommaDeclarator: COMMA Declarator CommaDeclarator {$$ = add_sibling($2, $3);}
| /*empty*/ {$$ = NULL;}
;
TypeSpec: CHAR {$$ = insert_node(line, col - 4, "Char", NULL, 0);}
| INT {$$ = insert_node(line, col - 3, "Int", NULL, 0);}
| VOID {$$ = insert_node(line, col - 4, "Void", NULL, 0);}
| SHORT {$$ = insert_node(line, col - 5, "Short", NULL, 0);}
| DOUBLE {$$ = insert_node(line, col - 6, "Double", NULL, 0);}
;
Declarator: ID_terminal ASSIGN Expr {$$ = insert_node(line, col, "Declaration", NULL, 2, $1, $3);}
| ID_terminal {$$ = insert_node(line, col, "Declaration", NULL, 1, $1);}
;
ID_terminal: ID {$$ = insert_node(line, col-strlen($1), "Id", $1, 0);}
Expr: Expr ASSIGN Expr {$$ = insert_node(line, col, "Store", NULL, 2, $1, $3);}
| Expr PLUS Expr {$$ = insert_node(line, col, "Add", NULL, 2, $1, $3);}
| Expr MINUS Expr {$$ = insert_node(line, col, "Sub", NULL, 2, $1, $3);}
| Expr MUL Expr {$$ = insert_node(line, col, "Mul", NULL, 2, $1, $3);}
| Expr DIV Expr {$$ = insert_node(line, col, "Div", NULL, 2, $1, $3);}
| Expr MOD Expr {$$ = insert_node(line, col, "Mod", NULL, 2, $1, $3);}
| Expr OR Expr {$$ = insert_node(line, col, "Or", NULL, 2, $1, $3);}
| Expr AND Expr {$$ = insert_node(line, col, "And", NULL, 2, $1, $3);}
| Expr BITWISEAND Expr {$$ = insert_node(line, col, "BitWiseAnd", NULL, 2, $1, $3);}
| Expr BITWISEOR Expr {$$ = insert_node(line, col, "BitWiseOr", NULL, 2, $1, $3);}
| Expr BITWISEXOR Expr {$$ = insert_node(line, col, "BitWiseXor", NULL, 2, $1, $3);}
| Expr EQ Expr {$$ = insert_node(line, col, "Eq", NULL, 2, $1, $3);}
| Expr NE Expr {$$ = insert_node(line, col, "Ne", NULL, 2, $1, $3);}
| Expr LE Expr {$$ = insert_node(line, col, "Le", NULL, 2, $1, $3);}
| Expr GE Expr {$$ = insert_node(line, col, "Ge", NULL, 2, $1, $3);}
| Expr LT Expr {$$ = insert_node(line, col, "Lt", NULL, 2, $1, $3);}
| Expr GT Expr {$$ = insert_node(line, col, "Gt", NULL, 2, $1, $3);}
| PLUS Expr %prec NOT {$$ = insert_node(line, col, "Plus", NULL, 1, $2);}
| MINUS Expr %prec NOT {$$ = insert_node(line, col, "Minus", NULL, 1, $2);}
| NOT Expr {$$ = insert_node(line, col, "Not", NULL, 1, $2);}
| ID_terminal LPAR CommaExprTwo RPAR {$$ = insert_node(line, col, "Call", NULL, 2, $1, $3);}
| ID_terminal LPAR RPAR {$$ = insert_node(line, col, "Call", NULL, 1, $1);}
| ID_terminal {$$ = $1;}
| INTLIT {$$ = insert_node(line, col, "IntLit", $1, 0);}
| CHRLIT {$$ = insert_node(line, col, "ChrLit", $1, 0);}
| REALLIT {$$ = insert_node(line, col, "RealLit", $1, 0);}
| LPAR CommaExpr RPAR {$$ = $2;}
| ID_terminal LPAR error RPAR {$$ = NULL;}
| LPAR error RPAR {$$ = NULL;}
;
CommaExpr: CommaExpr COMMA CommaExpr {$$ = insert_node(line, col, "Comma", NULL, 2, $1, $3);}
| Expr {$$ = $1;}
;
CommaExprTwo: CommaExprTwo COMMA CommaExprTwo {$$ = add_sibling($1, $3);}
| Expr {$$ = $1;}
;