-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBNF
95 lines (73 loc) · 2.48 KB
/
BNF
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
program: function_declaration
| var_declaration
function_declaration: datatype identifier '(' ')' statement_block
;
var_declaration: datatype identifier [',' identifier]* ';'
| datatype identifier '=' expr [',' identifier '=' expr] ';'
| ;
datatype: 'int'
| 'char'
| 'void'
| 'long'
;
statement_block: statement
| '{' statements '}'
;
statements: statement
| statement statements
;
statement: ';'
| var_declaration
| expression ';'
| if_statement
| while_statement
| do_while_statement
| for_statement
| 'break' ';'
| 'return' expression ';'
;
if_statement: 'if' '(' expression ')' statement_block
| 'if' '(' expression ')' statement_block 'else' statement_block
| 'if' '(' expression ')' statement_block 'else' if_statement
while_statement: 'while' '(' expression ')' statement_block
do_while_statement: 'do' statement_block 'while' '(' expression ')' ';'
# TODO: revesit the syntax for for loops
for_statement: 'for' '(' statement expression ';' assignment_expression ')' statement_block
expression: comparison_expression
| assignment_expression
;
assignment_expression: lvalue '=' expression
func_call_expression: identifier '(' args? ')'
args: expression [',' expression]*
comparison_expression: additive_expression
| additive_expression '<' additive_expression
| additive_expression '>' additive_expression
| additive_expression '<=' additive_expression
| additive_expression '>=' additive_expression
| additive_expression '==' additive_expression
| additive_expression '!=' additive_expression
;
additive_expression:
multiplicative_expression
| additive_expression '+' multiplicative_expression
| additive_expression '-' multiplicative_expression
;
multiplicative_expression: val
| val '*' multiplicative_expression
| val '/' multiplicative_expression
;
lvalue: dref_expression
| identifier
;
dref_expression: '*'+ val
val: number
| identifier
| '(' expression ')'
| func_call_expression
| lvalue
| '&' identifier
;
number: INTLIT
;
identifier: IDENTIFIER
;