-
Notifications
You must be signed in to change notification settings - Fork 0
/
c.y
187 lines (149 loc) · 3.17 KB
/
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
%{
#include "util.hpp"
%}
%union{
char cval;
int ival;
float fval;
char* sval;
}
%token <ival> IVAL
%token <cval> CVAL
%token <fval> FVAL
%token <sval> SVAL
%token INT FLOAT CHAR ID
%token LP RP LCB RCB LSB RSB SEMI COMMA COLON PRINT
%token IF ELSE WHILE FOR BREAK RETURN ASSIGN SWITCH CONTINUE CASE DEFAULT SIZEOF
%token PLUS MINUS MUL DIV MOD AND OR NOT
%left OR
%left AND
%left EQ NE
%left LT GT LE GE
%left PLUS MINUS
%left MUL DIV MOD
%start program
%%
expression_list
: expression
| expression_list COMMA expression
;
expression
: assignment_expression
| compound_expression
;
assignment_expression
: ID ASSIGN compound_expression
| ID LSB compound_expression RSB ASSIGN compound_expression
;
primary_expression
: ID
| constant
| LP compound_expression RP
| function_invoking
;
constant
: IVAL
| FVAL
| CVAL
;
function_invoking
: ID argue_list RP SEMI
;
argue_list
: LP ID
| argue_list COMMA ID
;
compound_expression
: primary_expression
| compound_expression MUL primary_expression
| compound_expression DIV primary_expression
| compound_expression MOD primary_expression
| compound_expression PLUS primary_expression
| compound_expression MINUS primary_expression
| compound_expression LT primary_expression
| compound_expression GT primary_expression
| compound_expression LE primary_expression
| compound_expression GE primary_expression
| compound_expression EQ primary_expression
| compound_expression NE primary_expression
| compound_expression AND primary_expression
| compound_expression OR primary_expression
;
declaration_list
: declaration
| declaration_list declaration
;
declaration
: type_specifier init_declarator_list
;
type_specifier
: INT
| FLOAT
| CHAR
;
init_declarator_list
: init_declarator SEMI
| init_declarator COMMA init_declarator_list
;
init_declarator
: ID
| ID ASSIGN constant
;
statement_list
: statement
| statement_list statement
;
statement
: block_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
| PRINT LP compound_expression RP
;
block_statement
: LCB RCB
| LCB declaration_list statement_list RCB
| LCB declaration_list RCB
| LCB statement_list RCB
;
expression_statement
: SEMI
| expression_list SEMI
;
selection_statement
: IF LP expression_list RP statement ELSE statement
;
iteration_statement
: WHILE LP expression_list RP statement
| FOR LP expression_statement expression_statement expression_list RP statement
;
jump_statement
: CONTINUE SEMI
| BREAK SEMI
| RETURN expression_statement
;
program
: function_definition_list
| declaration_list function_definition_list
;
function_definition_list
: function_definition
| function_definition_list function_definition
;
function_definition
: type_specifier ID LP RP block_statement
| type_specifier ID parameter_list RP block_statement
;
parameter_list
: parameter
| parameter_list parameter
;
parameter
: LP type_specifier ID
;
%%
int main(){
yyparse();
return 0;
}