forked from k-souvatzidaki/miniPython-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminipython.grammar
203 lines (175 loc) · 7.94 KB
/
minipython.grammar
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
Package minipython;
Helpers
digit = ['0' .. '9'];
letter = ['a' .. 'z']|['A' .. 'Z'];
cr = 13;
lf = 10;
all = [0..127];
eol = lf | cr | cr lf;
not_eol = [all - [cr + lf]] ;
not_single = [not_eol - 39];
not_double = [not_eol - 34];
Tokens
def = 'def';
l_par = '(';
r_par = ')';
semi = ':';
assign = '=';
comma=',';
tab = 9;
if = 'if';
while = 'while';
for = 'for';
in = 'in';
return = 'return';
print = 'print';
assign_minus = '-=';
assign_div = '/=';
l_br = '[';
r_br = ']';
assert = 'assert';
plus = '+';
minus = '-';
mult = '*';
multmult = '**';
div = '/';
mod = '%';
open = 'open';
type = 'type';
max = 'max';
min = 'min';
and = 'and';
or = 'or';
not = 'not';
less = '<';
great = '>';
geq = '>=';
leq = '<=';
eq = '==';
neq = '!=';
true = 'true';
false = 'false';
dot = '.';
none = 'None';
line_comment = '#' not_eol* eol;
number = digit+ | (digit+ '.' digit+);
id = letter (letter | digit)*;
string = '"' not_double* '"' | ''' not_single* ''';
blank = (' ' | lf | cr);
Ignored Tokens
blank, line_comment;
Productions
goal = commands*{-> New goal([commands])};
commands = {fun} function{-> New commands.fun(function)} |
{stat} statement{-> New commands.stat(statement)};
function = def id l_par argument? r_par semi statement{-> New function(id,[argument],statement)};
argument = id assign_v? comma_assign*{-> New argument(id,[assign_v],[comma_assign])};
comma_assign = comma id assign_v?{-> New comma_assign(id,[assign_v])};
assign_v = assign value{-> New assign_v(value)};
statement = {if} tab* if comparison_or semi statement{-> New statement.if(comparison_or,statement)} |
{while} tab* while comparison_or semi statement{-> New statement.while(comparison_or,statement)} |
{for} tab* for [forid]:id in [inid]:id semi statement{-> New statement.for(forid,inid,statement)} |
{return} tab* return expression{-> New statement.return(expression)} |
{print} tab* print expression comma_exp*{-> New statement.print(expression,[comma_exp])} |
{assign} tab* id assign expression{-> New statement.assign(id, expression)} |
{assignmin} tab* id assign_minus expression{-> New statement.assignmin(id, expression)} |
{assigndiv} tab* id assign_div expression{-> New statement.assigndiv(id, expression)} |
{list} tab* id l_br [lbr]:expression r_br assign [rbr]:expression{-> New statement.list(id,lbr,rbr)} |
{assert} tab* assert expression comma_exp?{-> New statement.assert(expression,[comma_exp])} |
{func} tab* function_call{-> New statement.func(function_call)};
expression = {expr_mod} expression_mod{->expression_mod.expression}|
{minus} expression_mod minus expression{->New expression.minus(expression_mod.expression,expression)} |
{add} expression_mod plus expression{->New expression.add(expression_mod.expression,expression)};
expression_mod{->expression} = {expr_multmult} expression_multmult{->expression_multmult.expression}|
{mod} expression_multmult mod expression_mod{->New expression.mod(expression_multmult.expression,expression_mod.expression)} |
{div} expression_multmult div expression_mod{->New expression.div(expression_multmult.expression,expression_mod.expression)} |
{mult} expression_multmult mult expression_mod{->New expression.mult(expression_multmult.expression,expression_mod.expression)};
expression_multmult{->expression} = {expr} expr{->expr.expression}|
{multmult} expr multmult expression_multmult{->New expression.multmult(expr.expression,expression_multmult.expression)};
expr{->expression} = {sth} something{->something.expression} |
{listexp} id l_br expression r_br{->New expression.listexp(id,expression)}|
{open} open l_par [lpar]:expression comma [rpar]:expression r_par{->New expression.open(open,lpar,rpar)}|
{list_con} l_br expression comma_exp* r_br{->New expression.list_con(l_br,expression,[comma_exp])};
something{->expression} = {func_call} function_call{->New expression.func_call(function_call)}|
{value} value{->New expression.value(value)}|
{id} id{->New expression.id(id)}|
{type} type l_par id r_par{->New expression.type(id)}|
{max} max l_par value comma_v+ r_par{->New expression.max(value,[comma_v])}|
{min} min l_par value comma_v+ r_par{->New expression.min(value,[comma_v])}|
{par} l_par expression r_par{->New expression.par(expression)};
comma_v = comma value{->New comma_v(value)};
comparison_or = {comp} comparison_and {-> comparison_and.comparison_or} |
{or} comparison_or or comparison_and {->New comparison_or.or(comparison_or,comparison_and.comparison_or)};
comparison_and{->comparison_or} = {not_comp} not_comp {-> not_comp.comparison_or}|
{and} comparison_and and not_comp{->New comparison_or.and(comparison_and.comparison_or,not_comp.comparison_or)};
not_comp{->comparison_or} = {comp} comp {-> comp.comparison_or}|
{not} not not_comp{->New comparison_or.not(not_comp.comparison_or)};
comp{->comparison_or} = {true} true{->New comparison_or.true()}|
{false} false{->New comparison_or.false()}|
{less} [lpar]:expression less [rpar]:expression {->New comparison_or.less(lpar,rpar)}|
{great} [lpar]:expression great [rpar]:expression {->New comparison_or.great(lpar,rpar)}|
{geq} [lpar]:expression geq [rpar]:expression {->New comparison_or.geq(lpar,rpar)}|
{leq} [lpar]:expression leq [rpar]:expression {->New comparison_or.leq(lpar,rpar)}|
{neq} [lpar]:expression neq [rpar]:expression {->New comparison_or.neq(lpar,rpar)}|
{eq} [lpar]:expression eq [rpar]:expression {->New comparison_or.eq(lpar,rpar)};
function_call = id l_par arglist? r_par {->New function_call(id,[arglist])};
arglist = expression comma_exp* {->New arglist(expression,[comma_exp])};
comma_exp = comma expression {->New comma_exp(expression)};
value = {method} id dot function_call {->New value.method(id,function_call)}|
{num} number {->New value.num(number)}|
{string} string {->New value.string(string)}|
{none} none {->New value.none(none)};
Abstract Syntax Tree
goal = commands*;
commands = {fun} function |
{stat} statement;
function = id argument* statement;
argument = id assign_v* comma_assign*;
comma_assign = id assign_v* ;
assign_v = value;
statement = {if} comparison_or statement |
{while} comparison_or statement |
{for} [forid]:id [inid]:id statement |
{return} expression |
{print} expression comma_exp* |
{assign} id expression |
{assignmin} id expression |
{assigndiv} id expression |
{list} id [lbr]:expression [rbr]:expression |
{assert} expression comma_exp* |
{func} function_call;
expression = {minus} [lpar]:expression [rpar]:expression |
{add} [lpar]:expression [rpar]:expression|
{mod} [lpar]:expression [rpar]:expression|
{div} [lpar]:expression [rpar]:expression|
{mult} [lpar]:expression [rpar]:expression|
{multmult} [lpar]:expression [rpar]:expression|
{func_call} function_call|
{value} value|
{id} id|
{type} id|
{max} value comma_v*|
{min} value comma_v*|
{par} expression |
{listexp} id expression|
{open} open [lpar]:expression [rpar]:expression|
{list_con} l_br expression comma_exp*;
comma_v = value;
comparison_or = {or} [lpar]:comparison_or [rpar]:comparison_or|
{and} [lpar]:comparison_or [rpar]:comparison_or|
{not} comparison_or|
{true} |
{false} |
{less} [lpar]:expression [rpar]:expression|
{great} [lpar]:expression [rpar]:expression|
{geq} [lpar]:expression [rpar]:expression|
{leq} [lpar]:expression [rpar]:expression|
{neq} [lpar]:expression [rpar]:expression|
{eq} [lpar]:expression [rpar]:expression;
function_call = id arglist*;
arglist = expression comma_exp*;
comma_exp = expression;
value = {method} id function_call |
{num} number |
{string} string |
{none} none ;