-
Notifications
You must be signed in to change notification settings - Fork 1
/
extra.y
223 lines (176 loc) · 5.24 KB
/
extra.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
221
222
223
ROOT:
ExtDef { ; }
;
// EXTERNAL DEFINITION
ExtDef:
ExtDeclaration // { printf("$0 : %d", $0);} // g_root->push($1);
|ExtDef ExtDeclaration // {g_root->push($2); }
;
ExtDeclaration:
Declaration // { $$ = $1; }
|FuncDef {printf("FUNCTION DEF");} // { $$ = $1; }
;
// FUNCTION DEFINITION
FuncDef:
KW_SUB IDENTIFIER CompoundStatement
;
// DECLARATION
Declaration:
IDENTIFIER OP_LEFT_PARENTHESIS ParameterList OP_RIGHT_PARENTHESIS
;
// in sahu_conversion_parser.y
ParameterList:
Parameter
| ParameterList OP_COMMA Parameter ParameterList_1
;
ParameterList_1:
%empty
| OP_COMMA Parameter ParameterList_1
;
Parameter:
VARIABLE
;
// ParameterList: "a,b,c"
// CALLING
Calling:
Declaration
|VARIABLE OP_EQUAL Declaration
;
StatementList:
Statement // { $$ = new StatementList($1); }
|StatementList Statement // { $$->push($2); }
;
Statement:
CompoundStatement // { $$ = $1; }
|SelectionStatement // { $$ = $1; }
|ExpressionStatement // {$$=$1;}
|JumpStatement // {$$=$1;}
|IterationStatement // {$$=$1;}
|PrintStatement // {$$=$1;}
;
CompoundStatement:
LEFT_CURLY_BRACKET CompoundStatement_2 // { $$ = $2; }
;
CompoundStatement_2:
RIGHT_CURLY_BRACKET // { $$ = new CompoundStatement; }
|DeclarationList RIGHT_CURLY_BRACKET // { $$ = new CompoundStatement($1); }
|DeclarationList StatementList RIGHT_CURLY_BRACKET // { $$ = new CompoundStatement($1, $2); }
|StatementList RIGHT_CURLY_BRACKET // { $$ = new CompoundStatement($1); }
;
SelectionStatement:
KW_IF OP_LEFT_PARENTHESIS Expression OP_RIGHT_PARENTHESIS Statement // { $$ = new SelectionStatement($5); }
|KW_IF OP_LEFT_PARENTHESIS Expression OP_RIGHT_PARENTHESIS Statement KW_ELSE Statement // { $$ = new SelectionStatement($5, $7); }
;
ExpressionStatement:
OP_SEMICOLON // { $$ = new ExpressionStatement(); }
|Expression OP_SEMICOLON // { $$ = $1; }
;
JumpStatement:
KW_RETURN ExpressionStatement // { $$ = $2; }
;
IterationStatement:
KW_WHILE OP_LEFT_PARENTHESIS Expression OP_RIGHT_PARENTHESIS Statement // { $$ = $5; }
|KW_DO Statement KW_WHILE OP_LEFT_PARENTHESIS Expression OP_RIGHT_PARENTHESIS OP_SEMICOLON // { $$ = $2; }
|KW_FOR OP_LEFT_PARENTHESIS Expression OP_SEMICOLON Expression OP_SEMICOLON Expression OP_RIGHT_PARENTHESIS Statement // { $$ = $9; }
;
PrintStatement: KW_PRINT ExpressionStatement
|KW_PRINT CONSTANT_STRING
// Expressions
Expression:
AssignmentExpression // { $$ = $1; }
;
AssignmentExpression:
ConditionalExpression // { $$ = $1; }
|UnaryExpression ASSIGN_OPER AssignmentExpression // { $$ = $1; }
;
ASSIGN_OPER:
T_ASSIGN_OPER { ; }
|OP_EQUAL { ; }
;
ConditionalExpression:
LogicalOrExpression // { $$ = $1; }
|LogicalOrExpression OP_QUESTION Expression OP_COLON ConditionalExpression // { $$ = $1; }
;
LogicalOrExpression:
LogicalAndExpression // { $$ = $1; }
|LogicalOrExpression KW_OR LogicalAndExpression // { $$ = $3; }
;
LogicalAndExpression:
InclusiveOrExpression // { $$ = $1; }
|LogicalAndExpression KW_AND InclusiveOrExpression // { $$ = $3; }
;
InclusiveOrExpression:
ExclusiveOrExpression // { $$ = $1; }
|InclusiveOrExpression OP_BITWISE_OR ExclusiveOrExpression // { $$ = $3; }
;
ExclusiveOrExpression:
AndExpression // { $$ = $1; }
|ExclusiveOrExpression OP_XOR AndExpression // { $$ = $3; }
;
AndExpression:
EqualityExpression // { $$ = $1; }
|AndExpression OP_BITWISE_AND EqualityExpression // { $$ = $3; }
;
EqualityExpression:
RelationalExpression // { $$ = $1; }
|EqualityExpression OP_EQUALITY RelationalExpression // { $$ = $3; }
;
RelationalExpression:
ShiftExpression // { $$ = $1; }
|RelationalExpression OP_RELATIONAL ShiftExpression // { $$ = $3; }
;
ShiftExpression:
AdditiveExpression // { $$ = $1; }
|ShiftExpression OP_SHIFT AdditiveExpression // { $$ = $3; }
;
AdditiveExpression:
MultiplicativeExpression // { $$ = $1; }
|AdditiveExpression OP_ADDSUB MultiplicativeExpression // { $$ = $3; }
;
MultiplicativeExpression:
CastExpression // { $$ = $1; }
|MultiplicativeExpression MultDivRemOP CastExpression // { $$ = $3; }
;
MultDivRemOP:
OP_MULT // { $$ = $1; }
|OP_DIVIS // { $$ = $1; }
|OP_REM // { $$ = $1; }
;
CastExpression:
UnaryExpression // { $$ = $1; }
;
UnaryExpression:
PostfixExpression // { $$ = $1; }
|OP_INCDEC UnaryExpression // { $$ = $2; }
|UnaryOperator CastExpression // { $$ = $2; }
;
UnaryOperator:
OP_BITWISE_AND // { $$ = $1; }
|OP_ADDSUB // { $$ = $1; }
|OP_MULT // { $$ = $1; }
|OP_TILDE // { $$ = $1; }
|OP_NOT // { $$ = $1; }
;
PostfixExpression:
PrimaryExpression // { $$ = $1; }
|PostfixExpression OP_LEFT_BRACKET Expression OP_RIGHT_BRACKET // { $$ = $3; }
|PostfixExpression OP_LEFT_PARENTHESIS PostfixExpression2 // { $$ = $3; }
|PostfixExpression DOT_OPERATOR IDENTIFIER // { $$ = new Expression(); }
|PostfixExpression OP_INCDEC // { $$ = new Expression(); }
;
PostfixExpression2:
OP_RIGHT_PARENTHESIS // { $$ = new Expression(); }
|ArgumentExpressionList OP_RIGHT_PARENTHESIS // { $$ = $1; }
;
ArgumentExpressionList:
AssignmentExpression // { $$ = $1; }
|ArgumentExpressionList OP_COMMA AssignmentExpression // { $$ = $3; }
;
PrimaryExpression:
VARIABLE // { $$ = new Expression(); }
|Constant // { $$ = new Expression(); }
|OP_LEFT_PARENTHESIS Expression OP_RIGHT_PARENTHESIS // { $$ = $2; }
;
Constant:
POSITIVE_INT // { $$ = $1; }
;