-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.ebnf
More file actions
333 lines (234 loc) · 8.24 KB
/
Copy pathgrammar.ebnf
File metadata and controls
333 lines (234 loc) · 8.24 KB
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
328
329
330
331
332
333
(* @taxonomy: spec/grammar *)
(* SPDX-License-Identifier: MPL-2.0 *)
(* Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> *)
(* Error-Lang Grammar - EBNF Specification *)
(* A Turing-complete teaching language with intentional error injection *)
(* ============================================ *)
(* PROGRAM STRUCTURE *)
(* ============================================ *)
program = { declaration } ;
declaration = function_decl
| struct_decl
| main_block
| statement ;
main_block = "main" , block ;
block = { statement } , "end" ;
(* ============================================ *)
(* DECLARATIONS *)
(* ============================================ *)
function_decl = "function" , IDENTIFIER , "(" , [ param_list ] , ")" ,
[ ":" , type_expr ] , block ;
param_list = param , { "," , param } ;
param = IDENTIFIER , [ ":" , type_expr ] ;
struct_decl = "struct" , IDENTIFIER ,
{ IDENTIFIER , ":" , type_expr } , "end" ;
type_expr = "Int" | "Float" | "String" | "Bool" | "Array" , "<" , type_expr , ">"
| IDENTIFIER ;
(* ============================================ *)
(* STATEMENTS *)
(* ============================================ *)
statement = let_stmt
| assign_stmt
| if_stmt
| while_stmt
| for_stmt
| return_stmt
| break_stmt
| continue_stmt
| print_stmt
| gutter_block
| expression_stmt ;
let_stmt = "let" , [ "mutable" ] , IDENTIFIER , [ ":" , type_expr ] , "=" , expression ;
assign_stmt = IDENTIFIER , [ "[" , expression , "]" ] , "=" , expression ;
if_stmt = "if" , expression , block ,
{ "elseif" , expression , block } ,
[ "else" , block ] ;
while_stmt = "while" , expression , block ;
for_stmt = "for" , IDENTIFIER , "in" , expression , block ;
return_stmt = "return" , [ expression ] ;
break_stmt = "break" ;
continue_stmt = "continue" ;
print_stmt = ( "print" | "println" ) , "(" , [ expression_list ] , ")" ;
expression_stmt = expression ;
(* ============================================ *)
(* GUTTER - ERROR INJECTION ZONE *)
(* ============================================ *)
(* The gutter block is intentionally loosely defined.
During parsing, errors WILL be injected here.
Parser must recover and continue after 'end'. *)
gutter_block = "gutter" , { any_token } , "end" ;
any_token = (* any valid or invalid token - parser recovers *) ;
(* ============================================ *)
(* EXPRESSIONS *)
(* ============================================ *)
expression_list = expression , { "," , expression } ;
expression = ternary ;
ternary = logical_or , [ "?" , expression , ":" , expression ] ;
logical_or = logical_and , { "or" , logical_and } ;
logical_and = equality , { "and" , equality } ;
equality = comparison , { ( "==" | "!=" ) , comparison } ;
comparison = bitwise_or , { ( "<" | ">" | "<=" | ">=" ) , bitwise_or } ;
bitwise_or = bitwise_xor , { "|" , bitwise_xor } ;
bitwise_xor = bitwise_and , { "^" , bitwise_and } ;
bitwise_and = shift , { "&" , shift } ;
shift = term , { ( "<<" | ">>" ) , term } ;
term = factor , { ( "+" | "-" ) , factor } ;
factor = unary , { ( "*" | "/" | "%" ) , unary } ;
unary = ( "-" | "not" | "~" ) , unary
| postfix ;
postfix = primary , { call | index | member } ;
call = "(" , [ expression_list ] , ")" ;
index = "[" , expression , "]" ;
member = "." , IDENTIFIER ;
primary = INTEGER
| FLOAT
| STRING
| BOOL
| "nil"
| IDENTIFIER
| array_literal
| lambda_expr
| "(" , expression , ")" ;
array_literal = "[" , [ expression_list ] , "]" ;
lambda_expr = "fn" , "(" , [ param_list ] , ")" , "->" , expression
| "fn" , "(" , [ param_list ] , ")" , block ;
(* ============================================ *)
(* LEXICAL TOKENS *)
(* ============================================ *)
IDENTIFIER = LETTER , { LETTER | DIGIT | "_" } ;
INTEGER = [ "-" ] , DIGIT , { DIGIT }
| "0x" , HEX_DIGIT , { HEX_DIGIT }
| "0b" , BIN_DIGIT , { BIN_DIGIT } ;
FLOAT = [ "-" ] , DIGIT , { DIGIT } , "." , DIGIT , { DIGIT } , [ exponent ] ;
exponent = ( "e" | "E" ) , [ "+" | "-" ] , DIGIT , { DIGIT } ;
STRING = '"' , { string_char } , '"'
| '"""' , { multiline_char } , '"""' ;
string_char = PRINTABLE - '"' - "\\"
| escape_seq ;
multiline_char = PRINTABLE | NEWLINE ;
escape_seq = "\\" , ( "n" | "r" | "t" | "\\" | '"' | "0" | "x" , HEX_DIGIT , HEX_DIGIT ) ;
BOOL = "true" | "false" ;
COMMENT = "#" , { PRINTABLE } , NEWLINE ;
(* ============================================ *)
(* CHARACTER CLASSES *)
(* ============================================ *)
LETTER = "A".."Z" | "a".."z" | "_" ;
DIGIT = "0".."9" ;
HEX_DIGIT = DIGIT | "A".."F" | "a".."f" ;
BIN_DIGIT = "0" | "1" ;
PRINTABLE = (* any Unicode printable character *) ;
NEWLINE = "\n" | "\r\n" ;
WHITESPACE = " " | "\t" | NEWLINE ;
(* ============================================ *)
(* KEYWORDS (Reserved) *)
(* ============================================ *)
(*
The following identifiers are reserved keywords:
Control Flow:
if, elseif, else, while, for, in, break, continue, return
Declarations:
let, mutable, function, struct, main, end
Operators:
and, or, not
Literals:
true, false, nil
Types:
Int, Float, String, Bool, Array
Special:
gutter, fn
Built-in Functions:
print, println, len, push, pop, type, str, int, float
*)
(* ============================================ *)
(* OPERATOR PRECEDENCE (lowest to highest) *)
(* ============================================ *)
(*
1. ? : (ternary, right-associative)
2. or (logical or)
3. and (logical and)
4. == != (equality)
5. < > <= >= (comparison)
6. | (bitwise or)
7. ^ (bitwise xor)
8. & (bitwise and)
9. << >> (shift)
10. + - (additive)
11. * / % (multiplicative)
12. - not ~ (unary, right-associative)
13. () [] . (postfix: call, index, member)
*)
(* ============================================ *)
(* TURING COMPLETENESS PROOF *)
(* ============================================ *)
(* Error-Lang is Turing complete because it provides:
1. UNBOUNDED STORAGE
- Mutable variables (let mutable)
- Arrays with dynamic length
- Recursive data structures via structs
2. CONDITIONAL BRANCHING
- if/elseif/else statements
- Ternary expressions
- Boolean operators (and, or, not)
3. ITERATION/RECURSION
- while loops
- for loops
- Recursive function calls
4. ARBITRARY COMPUTATION
- Integer and float arithmetic
- Bitwise operations
- First-class functions (lambdas)
Example: Universal Turing Machine simulation is possible
by encoding tape as an array and transition function
as a lookup table or switch/if chain.
Note: Turing completeness is probabilistic due to error
injection. On runs where critical code falls in a gutter
block, the program may not complete correctly. This is
intentional and part of the teaching methodology.
*)
(* ============================================ *)
(* EXAMPLE PROGRAMS *)
(* ============================================ *)
(*
Example 1: Hello World
------------------------
main
println("Hello, Error-Lang!")
end
Example 2: Fibonacci (recursive)
---------------------------------
function fib(n: Int): Int
if n <= 1
return n
end
return fib(n - 1) + fib(n - 2)
end
main
println("Fibonacci of 10: ", fib(10))
end
Example 3: With Gutter (error zone)
------------------------------------
main
let x = 42
gutter
# Something will break here on some runs
let y = "unterminated
let z = @#$%
end
# This still executes!
println("x = ", x)
end
Example 4: Lambda and Higher-Order
-----------------------------------
function map(arr, f)
let mutable result = []
for item in arr
push(result, f(item))
end
return result
end
main
let numbers = [1, 2, 3, 4, 5]
let squared = map(numbers, fn(x) -> x * x)
println(squared) # [1, 4, 9, 16, 25]
end
*)