-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminipython.grammar
148 lines (118 loc) · 3.35 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
/*
* Test Comments
*/
Package minipython;
Helpers
digit = ['0' .. '9'];
letter = ['a' .. 'z']|['A' .. 'Z'];
cr = 13;
lf = 10;
space = 32;
all = [0..127];
eol = lf | cr | cr lf ;
not_eol = [all - [cr + lf]];
Tokens
tab = 9;
plus = '+';
minus = '-';
mult = '*';
pow = '**';
mineq = '-=';
diveq = '/=';
div = '/';
dot = '.';
eq = '=';
deq = '==';
exclam = '!';
dictt = 'dict';
def = 'def';
logic_plus = '&&';
l_par = '(';
r_par = ')';
l_br = '[';
r_br = ']';
comma=',';
qmark = '?';
gqmark = ';';
if = 'if';
elif = 'elif';
else = 'else';
while = 'while';
for = 'for';
from = 'from';
imp = 'import';
assert = 'assert';
as = 'as';
print = 'print';
return = 'return';
in = 'in';
less = '<';
great = '>';
dif = '!=';
true = 'true';
semi = ':';
false = 'false';
quote = '"';
blank = (' ' | lf | cr);
line_comment = '#' not_eol* eol;
id = letter (letter | digit)*;
float = digit* '.' digit+;
integer = ([digit-'0'] digit* | '0');
string = '"'(letter|space)*'"'|'''(letter|space)*''';
Ignored Tokens
blank, line_comment;
Productions
goal = commands*;
commands = {func}function | {state}statement;
iddot = id dot;
eqvalue = eq value;
commaid = comma id eqvalue?;
commaexp = comma expression;
commavalue = comma value;
asid = as id;
commamoduleasid = comma module asid?;
commaidasid = comma id asid?;
sign = {negative} minus | {possitive} plus;
hp_expression = {power} power |
{multiplication} hp_expression mult power|
{division} hp_expression div power;
power = {something} something |
{power} power pow something;
morevalue = {morevalue} value commavalue* ;
function = def id l_par argument? r_par semi statement;
argument = id eqvalue? commaid*;
statement = {if} tab* if comparison semi statement|
{while} tab* while comparison semi statement|
{for} tab* for [left_id]:id in [right_id]:id semi statement|
{return} tab* return expression|
{print_simple} tab* print expression commaexp* |
{equals} tab* id eq expression|
{subeq} tab* id mineq expression|
{diveq} tab* id diveq expression|
{list} tab* id l_br [left_id]:expression r_br eq [right_id]:expression|
{assert_exp} assert expression commaexp?|
{assert_comp} assert comparison commaexp?|
{funccall} tab* functioncall|
{import} import;
expression = {multiplication} hp_expression |
{addition} expression plus hp_expression|
{subtraction} expression minus hp_expression;
something = {id} id |
{val} value |
{func_call} functioncall |
{par_exp} l_par expression r_par |
{br_exp} l_br morevalue* r_br |
{id2} id l_br expression r_br;
import = {importas} [left_imp]:imp [module1]:module [as_id1]:asid? commamoduleasid* |
{fromimport} from [module2]:module [imp2]:imp id [as_id2]:asid? commaidasid*;
module = iddot* id;
comparison = {great} [left_exp]:expression great [right_exp]:expression |
{less} [left_exp]:expression less [right_exp]:expression |
{dif} [left_exp]:expression dif [right_exp]:expression |
{eq} [left_exp]:expression deq [right_exp]:expression |
{true} true |
{false} false;
functioncall = id l_par arglist? r_par;
arglist = expression commaexp*;
value = {funccall} id dot functioncall | {number} number | {string} string;
number = {integer} sign? integer|{float} sign? float;