-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.txt
142 lines (107 loc) · 2.96 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
Functionarray存的是所有的函数
startcode 存的是起始函数和全局变量
// # 单词
// ## 关键字
FN_KW -> 'fn'
LET_KW -> 'let'
CONST_KW -> 'const'
AS_KW -> 'as'
WHILE_KW -> 'while'
IF_KW -> 'if'
ELSE_KW -> 'else'
RETURN_KW -> 'return'
BREAK_KW -> 'break'
CONTINUE_KW -> 'continue'
// ## 字面量
digit -> [0-9]
UINT_LITERAL -> digit+
DOUBLE_LITERAL -> digit+ '.' digit+ ([eE] digit+)?
escape_sequence -> '\' [\\"'nrt]
string_regular_char -> [^"\\]
STRING_LITERAL -> '"' (string_regular_char | escape_sequence)* '"'
char_regular_char -> [^'\\]
CHAR_LITERAL -> '\'' (char_regular_char | escape_sequence) '\''
// ## 标识符
IDENT -> [_a-zA-Z] [_a-zA-Z0-9]*
// ## 符号
PLUS -> '+'
MINUS -> '-'
MUL -> '*'
DIV -> '/'
ASSIGN -> '='
EQ -> '=='
NEQ -> '!='
LT -> '<'
GT -> '>'
LE -> '<='
GE -> '>='
L_PAREN -> '('
R_PAREN -> ')'
L_BRACE -> '{'
R_BRACE -> '}'
ARROW -> '->'
COMMA -> ','
COLON -> ':'
SEMICOLON -> ';'
// ## 注释
COMMENT -> '//' regex(.*) '\n'
///////////////////////////////////////////////////////////////////////
// # 表达式
expr ->
assign_expr
assign_expr -> l_expr '=' operator_expr
binary_operator -> '+' | '-'
operator_expr -> multiplicative_expr (binary_operator multiplicative_expr)*
relational-operator -> '==' | '!=' | '<' | '>' | '<=' | '>='
condition ->
operator_expr[relational-operator operator_expr]
multiplicative_operator -> '*' | '/'
multiplicative_expr ->
as_expr (multiplicative_operator as_expr)*
as_expr -> primary_expr {'as' ty}
primary_expr ->
negate_expr
| call_expr
| group_expr
| ident_expr
| literal_expr
negate_expr -> '-' primary_expr
call_param_list -> operator_expr (',' operator_expr)*
call_expr -> IDENT '(' call_param_list? ')'
literal_expr -> UINT_LITERAL | DOUBLE_LITERAL | STRING_LITERAL | CHAR_LITERAL
ident_expr -> IDENT
group_expr -> '(' operator_expr ')'
// ## 左值表达式
l_expr -> IDENT
// ## 类型
ty -> IDENT
///////////////////////////////////////////////////////////////////////
// # 语句
stmt ->
expr_stmt
| decl_stmt
| if_stmt
| while_stmt
| break_stmt
| continue_stmt
| return_stmt
| block_stmt
| empty_stmt
expr_stmt -> expr|operator_expr ';'
let_decl_stmt -> 'let' IDENT ':' ty ('=' operator_expr)? ';'
const_decl_stmt -> 'const' IDENT ':' ty '=' operator_expr ';'
decl_stmt -> let_decl_stmt | const_decl_stmt
if_stmt -> 'if' condition block_stmt ('else' 'if' condition block_stmt)* ('else' block_stmt)?
while_stmt -> 'while' condition block_stmt
break_stmt -> 'break' ';'
continue_stmt -> 'continue' ';'
return_stmt -> 'return' expr? ';'
block_stmt -> '{' stmt* '}'
empty_stmt -> ';'
// # 函数
function_param -> 'const'? IDENT ':' ty
function_param_list -> function_param (',' function_param)*
function -> 'fn' IDENT '(' function_param_list? ')' '->' ty block_stmt
// # 程序
item -> function | decl_stmt
program -> item*