-
Notifications
You must be signed in to change notification settings - Fork 1
/
cGrammer
243 lines (226 loc) · 4.47 KB
/
cGrammer
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
var
: IDENTIFIER
| IDENTIFIER '[' CONSTANT ']'
| IDENTIFIER '[' CONSTANT ']' '[' CONSTANT ']'
;
arg_var
: IDENTIFIER
| '*' IDENTIFIER
| IDENTIFIER '[' ']'
| '*' IDENTIFIER '[' CONSTANT ']'
| IDENTIFIER '[' ']' '[' CONSTANT ']'
;
variables
: var
| '*' var
| variables ',' var
| variables ',' '*' var
;
struct_var
: IDENTIFIER
| IDENTIFIER '[' CONSTANT ']'
| struct_var ',' IDENTIFIER
| struct_var ',' IDENTIFIER '[' CONSTANT ']'
;
type
: INT
| FLOAT
| CHAR
| VOID
;
inside_dec
: IDENTIFIER
| inside_dec ',' IDENTIFIER
;
struct_declaration
: type inside_dec ';'
;
struct_list
: struct_declaration
| struct_list struct_declaration
;
struct
: STRUCT IDENTIFIER '{' struct_list '}'
;
declaration
: type variables ';'
| struct ';'
| STRUCT IDENTIFIER struct_var ';'
| struct struct_var ';'
;
argument_list
: argument_list ',' type arg_var
| type arg_var
| argument_list ',' STRUCT IDENTIFIER IDENTIFIER
| STRUCT IDENTIFIER arg_var
;
read
: READ '(' type ',' IDENTIFIER ')'
| READ '(' type ',' IDENTIFIER '[' expression ']' ')'
| READ '(' type ',' IDENTIFIER '[' expression ']' '[' expression ']' ')'
| READ '(' type ',' IDENTIFIER '[' expression ']' '.' IDENTIFIER ')'
| READ '(' type ',' IDENTIFIER '.' IDENTIFIER ')'
;
print
: PRINT '(' type ',' IDENTIFIER ')'
| PRINT '(' type ',' IDENTIFIER '[' expression ']' ')'
| PRINT '(' type ',' IDENTIFIER '[' expression ']' '[' expression ']' ')'
| PRINT '(' type ',' IDENTIFIER '[' expression ']' '.' IDENTIFIER ')'
| PRINT '(' type ',' IDENTIFIER '.' IDENTIFIER ')'
;
myfunction
: read ';'
| print ';'
;
statement
: body
| jump_statement
| iteration_statement
| selection_statement
| expression_statement
| declaration
| myfunction
;
iteration_statement
: WHILE '(' expression ')' statement
| FOR '(' expression_statement expression_statement ')' statement
| FOR '(' expression_statement expression_statement expression ')' statement
;
selection_statement
: IF '(' expression ')' body
| IF '(' expression ')' body ELSE statement
;
jump_statement
: CONTINUE ';'
| BREAK ';'
| RETURN ';'
| RETURN expression ';'
;
ass_operator
: '='
| MUL_ASSIGN
| DIV_ASSIGN
| MOD_ASSIGN
| ADD_ASSIGN
| SUB_ASSIGN
| LEFT_ASSIGN
| RIGHT_ASSIGN
| AND_ASSIGN
| XOR_ASSIGN
| OR_ASSIGN
;
expression_statement
: ';'
| expression ';'
;
arg_list
: expression
| arg_list ',' expression
;
starting
: IDENTIFIER
| CONSTANT
| STRING_LITERAL
| '(' expression ')'
;
main_exp
: starting
| IDENTIFIER '['expression']'
| IDENTIFIER '['expression']' '['expression']'
| IDENTIFIER '(' ')'
| IDENTIFIER '(' arg_list ')'
| IDENTIFIER '.' IDENTIFIER
| IDENTIFIER '[' expression ']' '.' IDENTIFIER
;
unary_op
: '+'
| '-'
| '!'
| '~'
| '*'
| '&'
;
unary_exp
: main_exp
| unary_op unary_exp
| SIZEOF '(' type ')'
;
mul_div_mod
: unary_exp
| mul_div_mod '*' unary_exp
| mul_div_mod '/' unary_exp
| mul_div_mod '%' unary_exp
;
plus_minus
: mul_div_mod
| plus_minus '+' mul_div_mod
| plus_minus '-' mul_div_mod
;
shift
: plus_minus
| shift LEFT_OP plus_minus
| shift RIGHT_OP plus_minus
;
lt_gt_lte_gte
: shift
| lt_gt_lte_gte '<' shift
| lt_gt_lte_gte '>' shift
| lt_gt_lte_gte LE_OP shift
| lt_gt_lte_gte GE_OP shift
;
eq_neq
: lt_gt_lte_gte
| eq_neq EQ_OP lt_gt_lte_gte
| eq_neq NE_OP lt_gt_lte_gte
;
and
: eq_neq
| and '&' eq_neq
;
xor
: and
| xor '^' and
;
or
: xor
| or '|' xor
;
andand
: or
| andand AND_OP or
;
oror
: andand
| oror OR_OP andand
;
conditional_exp
: oror
;
expression
: conditional_exp
| unary_exp ass_operator expression
;
statement_list
: statement
| statement_list statement
;
body
: '{' '}'
| '{' statement_list '}'
;
function
: type IDENTIFIER '(' argument_list ')' body
| type IDENTIFIER '(' ')' body
| STRUCT IDENTIFIER IDENTIFIER '(' ')' body
| STRUCT IDENTIFIER IDENTIFIER '(' argument_list ')' body
| type '*' IDENTIFIER '(' argument_list ')' body
| type '*' IDENTIFIER '(' ')' body
| STRUCT IDENTIFIER '*' IDENTIFIER '(' ')' body
| STRUCT IDENTIFIER '*' IDENTIFIER '(' argument_list ')' body
;
program
: declaration
| function
| program declaration
| program function
;