-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini-c.y
201 lines (152 loc) · 5.23 KB
/
mini-c.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
%{
//https://golang.org/ref/spec
#include <cstdio>
using namespace std;
int yylex();
extern int yylineno;
void yyerror(const char * s){
fprintf(stderr, "Line: %d, error: %s\n", yylineno, s);
}
#define YYERROR_VERBOSE 1
#define YYDEBUG 1
%}
%union{
const char * string_t;
int int_t;
float float_t;
}
%token <string_t> STRING_LITERAL
%token <int_t> INT_CONSTANT
%token <float_t> FLOAT_CONSTANT
%token INT_TYPE FLOAT32_TYPE BOOL_TYPE STRING_TYPE ID
%token KW_BREAK KW_FUNC KW_PACKAGE KW_CONTINUE KW_FOR KW_IMPORT KW_RETURN KW_VAR
%token KW_TRUE KW_FALSE
%token KW_IF KW_ELSE
%token PLUS_EQUAL MINUS_EQUAL MULT_EQUAL DIV_EQUAL MOD_EQUAL POW_EQUAL AND_EQUAL OR_EQUAL COLON_EQUAL
%token INCREASE DECREASE
%token OR AND NOT
%token GREATER_OR_EQUAL LESS_OR_EQUAL EQUAL NOT_EQUAL
%%
start: input
input: input external_declaration
| external_declaration
;
external_declaration: method_definition
| declaration
;
method_definition: type ID '(' parameters_type_list ')' block_statement
| type ID '(' ')' block_statement
| type ID '(' parameters_type_list ')' ';'
| type ID '(' ')' block_statement ';'
;
declaration_list: declaration_list declaration
| declaration
;
declaration: type init_declarator_list ';'
;
init_declarator_list: init_declarator_list ',' init_declarator
| init_declarator
;
init_declarator: declarator
| declarator '=' initializer
;
declarator: ID
| ID '[' assignment_expression ']'
| ID '[' ']'
;
parameters_type_list: parameters_type_list ',' parameter_declaration
| parameter_declaration
;
parameter_declaration: type declarator
| type
| type '[' ']'
;
initializer: assignment_expression
| '{' initializer_list '}'
;
initializer_list: initializer_list ',' logical_or_expression
| logical_or_expression
;
block_statement: '{' statement_list '}'
| '{' declaration_list statement_list'}'
| '{' '}'
;
statement_list: statement_list statement
| statement
;
statement: for_statement
| expression_statement
| if_statement
| jump_statement
;
if_statement: KW_IF '(' expression ')' statement
| KW_IF '(' expression ')' statement KW_ELSE statement
;
expression_statement: expression ';'
;
for_statement: KW_FOR '(' expression ')' statement
;
jump_statement: KW_RETURN expression ';'
;
type: INT_TYPE
| FLOAT32_TYPE
| BOOL_TYPE
| STRING_TYPE
;
argument_expression_list: argument_expression_list ',' assignment_expression
| assignment_expression
;
expression: assignment_expression
;
assignment_expression: unary_expression assignment_operator assignment_expression
| logical_or_expression
;
assignment_operator: '='
| PLUS_EQUAL
| MINUS_EQUAL
;
logical_or_expression: logical_or_expression OR logical_and_expression
| logical_and_expression
;
logical_and_expression: logical_and_expression AND equality_expression
| equality_expression
;
equality_expression: equality_expression EQUAL relational_expression
| equality_expression NOT_EQUAL relational_expression
| relational_expression
;
relational_expression: relational_expression '>' additive_expression
| relational_expression '<' additive_expression
| relational_expression GREATER_OR_EQUAL additive_expression
| relational_expression LESS_OR_EQUAL additive_expression
| additive_expression
;
additive_expression: additive_expression '+' multiplicative_expression
| additive_expression '-' multiplicative_expression
| multiplicative_expression
;
multiplicative_expression: multiplicative_expression '*' unary_expression
| multiplicative_expression '/' unary_expression
| unary_expression
;
unary_expression: INCREASE unary_expression
| DECREASE unary_expression
| NOT unary_expression
| postfix_expression
;
postfix_expression: primary_expression
| postfix_expression '[' expression ']'
| postfix_expression '(' ')'
| postfix_expression '(' argument_expression_list ')'
| postfix_expression INCREASE
| postfix_expression DECREASE
;
primary_expression: '(' expression ')'
| ID
| constant
| STRING_LITERAL
;
constant: INT_CONSTANT
| FLOAT_CONSTANT
;
%%