-
Notifications
You must be signed in to change notification settings - Fork 5
/
Grammar.txt
147 lines (111 loc) · 3.54 KB
/
Grammar.txt
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
///////////////////////////////////////////////////////////////////////////////
left-associative: PLUS, MINUS
left-associative: MUL, DIV, FLOORDIV, MOD, PARALLEL
///////////////////////////////////////////////////////////////////////////////
Grammar
///////////////////////////////////////////////////////////////////////////////
start -> stmt_list | command
command -> ...
stmt_list -> full_stmt start
| END
full_stmt -> statement PRINT
statement -> func_def
| list_def
| deletion
| expr
func_def -> FUNC ident param_list ASSIGN func_term
param_list -> LPAREN params RPAREN
params -> ident COMMA params
| ident
func_term -> expr
deletion -> DELETE strings
strings -> strings ident
| ident
list_def -> ident ASSIGN list
list -> LBRACKET list_elem RBRACKET
| LBRACKET FOR ident ASSIGN primary COMMA primary func_term RBRACKET
| LBRACKET FOR ident ASSIGN primary COMMA primary COLON primary func_term RBRACKET
var_def -> ident ASSIGN expr
list_elem -> expr COMMA list_elem
| expr
expr -> expr PLUS term
| expr MINUS term
| term
term -> term MUL sign
| term DIV sign
| term FLOORDIV sign
| term MOD sign
| term PARALLEL sign
| sign
sign -> MINUS postfix
| PLUS postfix
| postfix
postfix -> primary POW sign
| primary var
| primary func_call
| primary enc_expr
| primary FACT
| primary
primary -> ureal
| var
| func_call
| enc_expr
var -> ident
func_call -> ident arg_list
arg_list -> LPAREN args RPAREN
args -> arg COMMA args
| arg
arg -> list_call
| primary
enc_expr -> LPAREN expr RPAREN
ureal -> digits DOT digits
| digits DOT
| DOT digits
| ureal EXP digits
| ureal EXP PLUS digits
| ureal EXP MINUS digits
| digits
digits -> digits DIGIT
| DIGIT
ident -> ALPHA chars
| UNDERSCORE chars
chars -> chars char
| char
char -> DIGIT | ALPHA | UNDERSCORE
ALPHA -> 'a' | 'b' | 'c' | ... | 'z' | 'A' | 'B' | ... | 'Z'
DIGIT -> '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
UNDERSCORE -> '_'
EXP -> 'e' | 'E'
DOT -> '.'
PLUS -> '+'
MINUS -> '-'
MUL -> '*'
DIV -> '/'
FLOORDIR -> '//' | 'div'
MOD -> '%' | 'mod'
FACT -> '!'
LPAREN -> '('
RPAREN -> ')'
PARALLEL -> '||'
POW -> '^' | '**'
COMMA -> ','
COLON -> ':'
PRINT -> '\n' | ';'
FUNC -> 'fn'
DELETE -> 'del'
ASSIGN -> '='
END -> <EOF>
///////////////////////////////////////////////////////////////////////////////
Compact grammar (e.g., ANTLR notation?) -- INCOMPLETE
///////////////////////////////////////////////////////////////////////////////
grammar MathParser;
expr: term ((PLUS|MINUS) term)*
term: sign ((MUL|DIV|FLOORDIV|MOD|PARALLEL) sign)*
sign: (MINUS|PLUS)? postfix
postifx: primary (POW sign|identifier|enc_expr|FACT)*
primary: number|enc_expr
enc_expr: LPAREN expr RPAREN
number: ...
ident: ('_'|alpha) ('_'|alpha|digit)*
alpha: ['a'-'z'] | ['A'-'Z']
digit: ['0'-'9']