-
Notifications
You must be signed in to change notification settings - Fork 1
/
syntax.txt
139 lines (101 loc) · 2.7 KB
/
syntax.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
Tokens:
Grouping tokens:
( ) [ ] { }
Unary/binary operators:
+ - ! ~ & *
LSHIFT = '<<'
RSHIFT = '>>'
EQ = '=='
NOTEQ = '!='
LTEQ = '<='
RTEQ = '>='
AND = '&&'
OR = '||'
+ - | ^ LSHIFT RSHIFT
* / % &
EQ NOTEQ < LTEQ > RTEQ
AND
OR
? :
Assignment operators:
COLON_ASSIGN = ':='
ADD_ASSIGN = '+='
SUB_ASSIGN = '-='
AND_ASSIGN = '&='
OR_ASSIGN = '|='
XOR_ASSIGN = '^='
LSHIFT_ASSIGN = '<<='
RSHIFT_ASSIGN = '>>='
MUL_ASSIGN = '*='
DIV_ASSIGN = '/='
MOD_ASSIGN = '%='
=
INC = '++'
DEC = '--'
Names/literals:
NAME = [a-zA-Z_][a-zA-Z0-9_]*
INT = [1-9][0-9]* | 0[xX][0-9a-fA-F]+ | 0[0-7]+ | 0[0bB][0-1]+
FLOAT = [0-9]*[.][0-9]*([eE][+-]?[0-9]+)?
CHAR = '\'' . '\''
STR = '"' [^"]* '"'
EBNF grammer:
type_list = type (',' type)*
name_list = NAME (',' NAME)*
base_type = NAME
| 'func' '(' type_list? ')' (':' type)?
| '(' base_type ')'
type = base_type ('[' expr ']' | '*')*
enum_item = NAME ('=' expr)?
enum_items = enum_item (',' enum_item)* ','?
enum_decl = NAME '{' enum_items? '}'
aggregate_field = name_list ':' type
aggregate_decl = NAME '{' (aggregate_field ';')* '}'
var_decl = NAME '=' expr
| NAME ':' type ('=' expr)?
const_decl = NAME '=' expr
typedef_decl = NAME '=' type
func_param = NAME ':' type
func_param_list = func_param (',' func_param)*
func_decl = NAME '(' func_param_list? ')' (':' type)? '{' stmt_block '}'
decl ='enum' enum_decl
|'struct' aggregate_decl
|'union' aggregate_decl
|'var' var_decl
|'const' const_decl
|'typedef' typedef_decl
|'func' func_decl
Statements:
assign_op = '=' | COLON_ASSIGN | ADD_ASSIGN |....
switch_case = (CASE expr | DEFAULT) ':' stmt*
switch_block = '{' switch_case* '}'
stmt = 'return' expr
| 'if' '(' expr ')' stmt_block ('else' 'if' '(' expr ')' stmt_block)* ('else' stmt_block)?
| 'while' '(' expr ')' stmt_block
| 'for' '(' stmt_list ';' expr ';' stmt_list ')' stmt_block
| 'do' stmt_block 'while' '(' expr ')' ';'
| 'break' ';'
| 'continue' ';'
| '{' stmt* '}'
| 'switch' '(' expr ')' switch_block
| expr (INC | DEC | assign_op expr)?
type_spec = NAME | '(' ':' type ')'
operand_expr = NAME
| INT
| FLOAT
| STR
| CAST '(' type ')' expr
| '(' expr ')'
| type_spec? '{' expr_list '}'
base_expr = operand_expr ('(' expr_list ')' | '[' expr ']' | '.' NAME)*
unary_expr = [+-&*] unary_expr
| base_expr
mul_op = '*' | '/' | '%' | '&' | LSHIFT | RSHIFT
mul_expr = unary_expr(mul_op unary_expr)*
add_op = '+' | '-' | '|' | '^'
add_expr = mul_expr (add_op mul_expr)*
cmp_op = EQ | NOTEQ | LTEQ | GTEQ | '<' | '>'
cmp_expr = add_expr (cmp_op add_expr)*
and_expr = cmp_expr (AND cmp_expr)*
or_expr = and_expr (OR and_expr)*
ternary_expr = or_expr ('?' ternary_expr ':' ternary_expr)?
expr = ternary_expr