-
Notifications
You must be signed in to change notification settings - Fork 5
/
uc.bnf
327 lines (266 loc) · 7.63 KB
/
uc.bnf
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// A grammar for the µC programming language [1].
//
// The content and structure of this document is heavily influenced by the Go
// Programming Language Specification [2] and some parts are therefore governed
// by a BSD-style license [3]. Any original content of this document is hereby
// released into the public domain [4].
//
// References:
// [1]: https://www.it.uu.se/katalog/aleji304/CompilersProject/uc.html
// [2]: https://golang.org/ref/spec
// [3]: https://golang.org/LICENSE
// [4]: https://creativecommons.org/publicdomain/zero/1.0/
// # Source code representation
//
// ## Characters
//
// An arbitrary ASCII character except null (0x00), new lines (0x0A), carriage
// return (0x0D), apostrophe (0x27), double quote (0x22) and backslash (0x5C).
_ascii_char
: '\x01' - '\x09'
| '\x0B' - '\x0C'
| '\x0E' - '\x21'
| '\x23' - '\x26'
| '\x28' - '\x5B'
| '\x5D' - '\x7F'
;
_ascii_letter : 'a' - 'z' | 'A' - 'Z' ;
_ascii_digit : '0' - '9' ;
// ## Letters and digits
//
_letter : _ascii_letter | '_' ;
_decimal_digit : _ascii_digit ;
_decimals : _decimal_digit { _decimal_digit } ;
// # Lexical elements
//
// ## Comments
//
_line_comment
: '/' '/' { . } '\n'
// TODO: Implement proper support for preprocess directive.
| '#' { . } '\n'
;
_block_comment : '/' '*' { . | '*' } '*' '/' ;
!comment : _line_comment | _block_comment ;
// ## Tokens
//
// White space, formed from spaces (0x20), horizontal tabs (0x09), new line
// (line-feed (0x0A) or carriage-return (0x0D)), vertical tabs (0x0B), and form-
// feeds (0x0C) (§6.4), is ignored except as it separates tokens that would
// otherwise combine into a single token.
!whitespace : ' ' | '\t' | '\v' | '\f' | '\r' | '\n' ;
// ## Identifiers
//
ident : _letter { _letter | _decimal_digit } ;
// ## Integer literals
//
int_lit : _decimals ;
// ## Character literals
//
_escaped_char : '\\' 'n' ;
char_lit : '\'' ( _ascii_char | '"' | _escaped_char ) '\'' ;
// # Syntaxic production rules
//
<< import (
"github.com/mewmew/uc/ast/astx"
"github.com/mewmew/uc/token"
) >>
File
: Decls << astx.NewFile($0) >>
;
Decls
: empty
| DeclList
;
DeclList
: Decl << astx.NewDeclList($0) >>
| DeclList Decl << astx.AppendDecl($0, $1) >>
;
Decl
: VarDecl ";" << $0, nil >>
| FuncDecl ";" << $0, nil >>
| FuncDef
| TypeDef ";" << $0, nil >>
;
FuncDecl
: FuncHeader
;
FuncHeader
// BasicType : "char" | "int" | "void" ;
: BasicType ident "(" Params ")" << astx.NewFuncDecl($0, $1, $2, $3, $4) >>
;
FuncDef
: FuncHeader BlockStmt << astx.SetFuncBody($0, $1) >>
;
VarDecl
: ScalarDecl
| ArrayDecl
;
ScalarDecl
// BasicType : "char" | "int" ;
: BasicType ident << astx.NewScalarDecl($0, $1) >>
;
ArrayDecl
// BasicType : "char" | "int" ;
: BasicType ident "[" IntLit "]" << astx.NewArrayDecl($0, $1, $2, $3, $4) >>
| BasicType ident "[" "]" << astx.NewArrayDecl($0, $1, $2, 0, $3) >>
;
IntLit
: int_lit << astx.NewIntLit($0, token.IntLit) >>
| char_lit << astx.NewIntLit($0, token.CharLit) >>
;
TypeDef
: "typedef" Type ident << astx.NewTypeDef($0, $1, $2) >>
;
BasicType
// BasicType : "char" | "int" | "void" ;
: ident << astx.NewIdent($0) >>
;
Params
: empty
| ParamList
;
ParamList
: Param << astx.NewParamList($0) >>
| ParamList "," Param << astx.AppendParam($0, $2) >>
;
Param
// BasicType : "void" ;
: Type << astx.NewAnonParam($0) >>
| VarDecl
;
// TODO: Add support for array types.
Type
: BasicType
;
Stmt
: MatchedStmt
| OpenStmt
;
// Thanks to https://www.parsifalsoft.com/ifelse.html for loop statement
// resolvning (while, do, for).
OtherStmt
: Expr ";" << astx.NewExprStmt($0) >>
| "return" Expr ";" << astx.NewReturnStmt($0, $1) >>
| "return" ";" << astx.NewReturnStmt($0, nil) >>
| BlockStmt
| ";" << astx.NewEmptyStmt($0) >>
;
BlockStmt
: "{" BlockItems "}" << astx.NewBlockStmt($0, $1, $2) >>
;
MatchedStmt
: "if" Condition MatchedStmt
"else" MatchedStmt << astx.NewIfStmt($0, $1, $2, $4) >>
| "while" Condition MatchedStmt << astx.NewWhileStmt($0, $1, $2) >>
| OtherStmt
;
OpenStmt
: "if" Condition Stmt << astx.NewIfStmt($0, $1, $2, nil) >>
| "if" Condition MatchedStmt
"else" OpenStmt << astx.NewIfStmt($0, $1, $2, $4) >>
| "while" Condition OpenStmt << astx.NewWhileStmt($0, $1, $2) >>
;
Condition
: "(" Expr ")" << $1, nil >>
;
BlockItems
: empty
| BlockItemList
;
BlockItemList
: BlockItem << astx.NewBlockItemList($0) >>
| BlockItemList BlockItem << astx.AppendBlockItem($0, $1) >>
;
BlockItem
: Decl
| Stmt
;
Expr
: Expr2R
;
// Right-associative binary expressions with precedence 2.
//
// 2R: =
Expr2R
: Expr5L
// Right-associative.
| Expr5L "=" Expr2R << astx.NewBinaryExpr($0, $1, $2) >>
;
// Left-associative binary expressions with precedence 5.
//
// 5L: &&
Expr5L
: Expr9L
| Expr5L "&&" Expr9L << astx.NewBinaryExpr($0, $1, $2) >>
;
// Left-associative binary expressions with precedence 9.
//
// 9L: == !=
Expr9L
: Expr10L
| Expr9L "==" Expr10L << astx.NewBinaryExpr($0, $1, $2) >>
| Expr9L "!=" Expr10L << astx.NewBinaryExpr($0, $1, $2) >>
;
// Left-associative binary expressions with precedence 10.
//
// 10L: < > <= >=
Expr10L
: Expr12L
| Expr10L "<" Expr12L << astx.NewBinaryExpr($0, $1, $2) >>
| Expr10L ">" Expr12L << astx.NewBinaryExpr($0, $1, $2) >>
| Expr10L "<=" Expr12L << astx.NewBinaryExpr($0, $1, $2) >>
| Expr10L ">=" Expr12L << astx.NewBinaryExpr($0, $1, $2) >>
;
// Left-associative binary expressions with precedence 12.
//
// 12L: + -
Expr12L
: Expr13L
| Expr12L "+" Expr13L << astx.NewBinaryExpr($0, $1, $2) >>
| Expr12L "-" Expr13L << astx.NewBinaryExpr($0, $1, $2) >>
;
// Left-associative binary expressions with precedence 13.
//
// 13L: * /
Expr13L
: Expr14
| Expr13L "*" Expr14 << astx.NewBinaryExpr($0, $1, $2) >>
| Expr13L "/" Expr14 << astx.NewBinaryExpr($0, $1, $2) >>
;
// Unary expressions with precedence 14.
//
// 14: - !
Expr14
: Expr15
| "-" Expr14 << astx.NewUnaryExpr($0, $1) >>
| "!" Expr14 << astx.NewUnaryExpr($0, $1) >>
;
// TODO: Replace function name with expression in call expression. Do the same
// for array names.
// TODO: Replace Expr15 (and similar names) with CastExpr, PostfixExpr, ...
// (from the C11 spec).
// Expressions with precedence 15.
Expr15
: PrimaryExpr
| ident "[" Expr "]" << astx.NewIndexExpr($0, $1, $2, $3) >>
| ident "(" Args ")" << astx.NewCallExpr($0, $1, $2, $3) >>
;
// Primary expressions with the highest precedence (§A.2.1).
PrimaryExpr
: int_lit << astx.NewBasicLit($0, token.IntLit) >>
| char_lit << astx.NewBasicLit($0, token.CharLit) >>
| ident << astx.NewIdent($0) >>
| ParenExpr
;
ParenExpr
: "(" Expr ")" << astx.NewParenExpr($0, $1, $2) >>
;
Args
: empty
| ExprList
;
ExprList
: Expr << astx.NewExprList($0) >>
| ExprList "," Expr << astx.AppendExpr($0, $2) >>
;