-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.txt
63 lines (58 loc) · 2.02 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
Grammar
enum_decl = NAME '{' (NAME ('=' expr)? ','?)* '}'
var_decl = NAME '=' expr
| NAME ':' type ('=' expr)?
const_decl = NAME '=' expr
typedef_decl = NAME '=' type
directive_decl = NAME '(' (NAME '=' expr ','?)+ ')'
import_decl = NAME ('{' '...' | (NAME ',')+ '}')?
decl = 'enum' enum_decl
| 'struct' aggregate_decl
| 'union' aggregate_decl
| 'var' var_decl
| 'const' const_decl
| 'typedef' typedef_decl
| 'func' func_decl
| '#' directive_decl
| 'import' import_decl
assign_op = EQ | AUTO_EQ | ADD_EQ | SUB_EQ | MUL_EQ | DIV_EQ | MOD_EQ | LSHIFT_EQ | RSHIFT_EQ | XOR_EQ | AND_EQ | OR_EQ
stmt_block = '{' stmt* '}'
stmt = 'return' expr? ';'
| 'continue' ';'
| 'break' ';'
| stmt_block
| 'if' '(' expr ')' stmt_block elseif* ('else' stmt_block)?
| 'for' '(' for_init? ';' expr? ';' for_next? ')' stmt_block
| 'do' stmt_block 'while' '(' expr ')' ';'
| 'while' '(' expr ')' stmt_block
| 'switch' '(' expr ')' case* ('default' ':' stmt*)?
| expr (INC | DEC | assign_op expr)?
| 'defer' stmt ';'
base_type = NAME
| 'func' '(' type_list? ')' (':' type)?
| '(' type ')'
type = base_type ('[' expr? ']' | '*')*
typespec = NAME | '(' ':' type ')'
base_expr = INT
| FLOAT
| INCREMENT | DECREMENT
| STR
| TRUE | FALSE
| NAME
| CAST '(' type ',' expr ')'
| '(' expr ')'
| typespec? '{' expr_list '}'
| SIZEOF '(' ( ':' typespec | expr ) ')'
call_expr = base_expr ('(' param* ')' | '[' expr ']' | '.' NAME)*
unary_expr = [+-&*~!] unary_expr
| call_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_EQ | NOTEQ | LT | GT | 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