-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathminipython.grammar
More file actions
272 lines (231 loc) · 10.1 KB
/
minipython.grammar
File metadata and controls
272 lines (231 loc) · 10.1 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
/*
* Γεωργιάδη Δέσποινα P3180026
* Κωνσταντίνος Βασιλόπουλος P3180018
* Χρήστος Παυλίδης P3190167
* Αναστασία Πετρουλάκη P3190171
*/
Package minipython;
Helpers
digit = ['0' .. '9'];
letter = ['a' .. 'z']|['A' .. 'Z'];
cr = 13;
lf = 10;
all = [0..127];
space = 32;
eol = lf | cr | cr lf ;
not_eol = [all - [cr + lf]];
pound = '#';
underscore = '_';
dquote = 34;
squote = 39;
dot = '.';
Tokens
dott = dot;
tab = 9;
plus = '+';
minus = '-';
mult = '*';
exp = '**';
div = '/';
mod = '%';
assign = '=';
minusassign = '-=';
divassign = '/=';
dict = 'dict';
def = 'def';
l_par = '(';
r_par = ')';
l_br = '[';
r_br = ']';
comma = ',';
if = 'if';
else = 'else';
while = 'while';
for = 'for';
in = 'in';
print = 'print';
return = 'return';
eq = '==';
noteq = '!=';
less = '<';
great = '>';
lesseq = '<=';
greateq = '>=';
semi = ':';
true = 'true';
false = 'false';
blank = space | eol;
line_comment = pound not_eol* eol;
assert = 'assert';
len = 'len';
max = 'max';
min = 'min';
import = 'import';
as = 'as';
from = 'from';
not = 'not';
and = 'and' | '&&';
or = 'or';
none = 'None';
id = (letter | underscore)(letter | digit | underscore)*;
int = digit+;
double = digit+ dot digit+;
string = (dquote (space | letter)* dquote) | (squote (space | letter)* squote);
Ignored Tokens
blank, line_comment;
Productions
goal = commands* {-> New goal( [commands] ) };
commands = {function} function {-> New commands.func( function ) }
| {statement} statement {-> New commands.stat( statement ) };
function = def identifier l_par argument? r_par semi statement {-> New function(identifier,[argument],statement)};
/* Argument */
argument = identifier assign_value? more_assignments* {-> New argument(identifier,[assign_value],[more_assignments])};
more_assignments = comma identifier assign_value? {-> New more_assignments(identifier,[assign_value])};
assign_value = assign value {-> New assign_value(value.arithmetics)};
/* Statement */
/*
NOTE: "arithmetics" is used
as BNF's expression and contains
the "expression" defined later
*/
statement = {import} import_statement {-> import_statement.statement}
| {if} tab* if comparison_lvl_four semi statement {-> New statement.if(comparison_lvl_four,statement)}
| {while} tab* while comparison_lvl_four semi {-> New statement.while(comparison_lvl_four)}
| {for} tab* for [id1]:identifier in [id2]:identifier semi statement {-> New statement.for(id1,id2,statement)} /********** */
| {return} tab* return arithmetics {-> New statement.return(arithmetics)}
| {print} tab* print arithmetics comma_expr* {-> New statement.print(arithmetics,[comma_expr])}
| {assignment} tab* identifier assign arithmetics {-> New statement.assignment(identifier,arithmetics)}
| {minusassignment} tab* identifier minusassign arithmetics {-> New statement.minusassignment(identifier,arithmetics)}
| {divassignment} tab* identifier divassign arithmetics {-> New statement.divassignment(identifier,arithmetics)}
| {listassignment} tab* identifier l_br [index]:arithmetics r_br assign [value]:arithmetics {-> New statement.listassignment(identifier,index,value)} /********** */
| {assert} tab* assert arithmetics comma_expr? statement {-> New statement.assert(arithmetics,[comma_expr],statement)}
| {call} function_call {-> New statement.call(function_call)};
/* Comparison | Level 1 */
comparison {-> comparison_lvl_four} = {rule1} [exp1]:arithmetics comparison_operators [exp2]:arithmetics {-> New comparison_lvl_four.rule1(exp1, exp2)}
| {rule2} true {-> New comparison_lvl_four.rule2(true)}
| {rule3} false {-> New comparison_lvl_four.rule3(false)};
comparison_operators{->} = {eq} eq {->}
| {noteq} noteq {->}
| {less} less {->}
| {great} great {->}
| {lesseq} lesseq {->}
| {greateq} greateq {->};
/* Level 2 */
comparison_lvl_two {-> comparison_lvl_four} = {lvl1} comparison {-> comparison.comparison_lvl_four}
| {not} not comparison_lvl_two {-> New comparison_lvl_four.not(comparison_lvl_two.comparison_lvl_four)};
/* Level 3 */
comparison_lvl_three {->comparison_lvl_four} = {lvl2} comparison_lvl_two {->comparison_lvl_two.comparison_lvl_four}
| {and} comparison_lvl_three and comparison_lvl_two {-> New comparison_lvl_four.and(comparison_lvl_three.comparison_lvl_four,comparison_lvl_two.comparison_lvl_four)};
/* Level 4 */
comparison_lvl_four = {lvl3} comparison_lvl_three {->comparison_lvl_three.comparison_lvl_four}
| {or} comparison_lvl_four or comparison_lvl_three {-> New comparison_lvl_four.or(Null,comparison_lvl_three.comparison_lvl_four)};
/* Arithmetics | Level 3 */
arithmetics = {rule1} multdivexpr{-> multdivexpr.arithmetics}
| {rule2} arithmetics plus multdivexpr {-> New arithmetics.plus(arithmetics, multdivexpr.arithmetics)}
| {rule3} arithmetics minus multdivexpr {-> New arithmetics.minus(arithmetics, multdivexpr.arithmetics)};
/* Level 2 */
multdivexpr {-> arithmetics} = {expr} expexpr {-> expexpr.arithmetics}
| {mult} multdivexpr mult expexpr {-> New arithmetics.mult(multdivexpr.arithmetics, expexpr.arithmetics)}
| {div} multdivexpr div expexpr {-> New arithmetics.div(multdivexpr.arithmetics, expexpr.arithmetics)};
/* Level 1 */
expexpr {-> arithmetics} = {expr} expression {-> expression.arithmetics}
| {multdiv} expexpr exp expression {-> New arithmetics.exp(expexpr.arithmetics, expression.arithmetics)};
/* Expression */
expression {-> arithmetics} = {element} element{-> element.arithmetics}
| {listcall} identifier l_br arithmetics r_br {-> New arithmetics.listcall(identifier, arithmetics)}
| {len} len l_par arithmetics r_par {-> New arithmetics.len(arithmetics)}
| {maxmin} maxmin l_par arithmetics comma_expr* r_par {-> New arithmetics.maxmin(arithmetics, [comma_expr])}
| {list} l_br arg_list r_br {-> New arithmetics.list(arg_list)};
/* Element */
element{-> arithmetics} = {value} value {-> value.arithmetics}
| {identifier} identifier {-> New arithmetics.identifier(identifier)}
| {functioncall} function_call {-> New arithmetics.function(function_call)}
| {pars} l_par arithmetics r_par {-> New arithmetics.arithmetics(arithmetics)};
/* Function Call */
function_call = identifier l_par arg_list? r_par {-> New function_call(identifier,[arg_list])};
arg_list = arithmetics comma_expr* {-> New arg_list(arithmetics, [comma_expr])};
comma_expr = comma arithmetics {-> New comma_expr(arithmetics)};
/* Max min */
maxmin{->} = {max} max{->}
| {min} min{->};
/* Import & module */
import_statement{-> statement} = {import} import module as_id? more_modules* {-> New statement.import(module.statement,[as_id],[more_modules.statement])}
| {from} from module import identifier as_id? more_ids* {-> New statement.from(module.statement,identifier,[as_id],[more_ids])};
module{-> statement} = id_dot* identifier {-> New statement.module([id_dot],identifier)};
as_id = as identifier {-> New as_id(identifier)};
more_modules{-> statement} = comma module as_id? {-> New statement.more_modules(module.statement, [as_id])};
more_ids = comma identifier as_id? {-> New more_ids(identifier, [as_id])};
id_dot = identifier dot {-> New id_dot(identifier)};
/* Value */
value{-> arithmetics} = {methodcall} identifier dot function_call {-> New arithmetics.methodcall(identifier,function_call)}
| {number} number {-> New arithmetics.number(number)}
| {strlit} string {-> New arithmetics.strlit(string)}
| {none} none {-> New arithmetics.none(none)};
/* Identifier */
identifier = id {-> New identifier(id)};
/* Dot */
dot = dott {-> New dot(dott)};
/* Number */
number = {int} int{-> New number.int(int)} | {double} double{-> New number.double(double)};
Abstract Syntax Tree
goal = commands*;
commands = {stat} statement
| {func} function;
function = identifier argument* statement;
/* Argument */
argument = identifier assign_value* more_assignments*;
more_assignments = identifier assign_value*;
assign_value = arithmetics;
/* Statement */
statement = {if} comparison_lvl_four statement
| {while} comparison_lvl_four
| {for} [id1]:identifier [id2]:identifier statement
| {return} arithmetics
| {print} arithmetics comma_expr*
| {assignment} identifier arithmetics
| {minusassignment} identifier arithmetics
| {divassignment} identifier arithmetics
| {listassignment} identifier [index]:arithmetics [value]:arithmetics
| {assert} arithmetics comma_expr* statement
| {call} function_call
| {import} [l]:statement as_id* [r]:statement*
| {from} statement identifier as_id* more_ids*
| {module} id_dot* identifier
| {more_modules} statement as_id*;
/* Comparison */
comparison_lvl_four = {not} comparison_lvl_four
| {and} [l]:comparison_lvl_four [r]:comparison_lvl_four
| {or} [l]:comparison_lvl_four [r]:comparison_lvl_four
| {rule1} [expr1]:arithmetics [expr2]:arithmetics
| {rule2} true
| {rule3} false;
/* Arithmetics */
arithmetics = {plus} [l]:arithmetics [r]:arithmetics
| {minus} [l]:arithmetics [r]:arithmetics
| {mult} [l]:arithmetics [r]:arithmetics
| {div} [l]:arithmetics [r]:arithmetics
| {exp} [l]:arithmetics [r]:arithmetics
| {listcall} identifier arithmetics
| {len} arithmetics
| {maxmin} arithmetics comma_expr*
| {list} arg_list
| {identifier} identifier
| {function} function_call
| {arithmetics} arithmetics
| {methodcall} identifier function_call /* value */
| {number} number
| {strlit} string
| {none} none;
/* Function Call */
function_call = identifier arg_list*;
arg_list = arithmetics comma_expr*;
comma_expr = arithmetics;
as_id = identifier;
more_ids = identifier as_id*;
id_dot = identifier;
/* Identifier */
identifier = id;
/* Dot */
dot = dott;
/* Number */
number = {int} int | {double} double;