-
Notifications
You must be signed in to change notification settings - Fork 7
/
grammar.ebnf
31 lines (31 loc) · 1.43 KB
/
grammar.ebnf
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
program = function;
function = "int", identifier, "(", ")", "{", { block_item }, "}";
block_item = statement | declaration ;
declaration = "int", identifier, [ "=", expr ], ";" ;
statement = "return", expr, ";"
| expr_option (* null statement *)
| "if", "(", expr, ")", statement, [ "else", statement ]
| "{", { block_item }, "}"
| "for", "(", expr_option, ";", expr_option, ";", expr_option, ")", statement
| "for", "(", declaration, expr_option, ";", expr_option, ")", statement
| "while", "(", expr, ")", statement
| "do", statement, "while", "(", expr, ")", ";"
| "break", ";"
| "continue", ";" ;
expr_option = expr | ";" ;
expr = identifier, assignment_operator, expr
| conditional_expr ;
conditional_expr = logical_or_expr, [ "?", expr, ":", conditional_expr ] ;
logical_or_expr = logical_and_expr, { "||", logical_and_expr };
logical_and_expr = equality_expr, { "&&", equality_expr };
equality_expr = relational_expr, { ("!=" | "=="), relational_expr };
relational_expr = bitwise_expr, { ("<" | ">" | "<=" | ">="), bitwise_expr };
bitwise_expr = additive_expr, { ("&" | "|" | "^" | "<<" | ">>"), additive_expr };
additive_expr = term, { ("+" | "-"), term };
term = factor, { ("*" | "/" | "%"), factor };
factor = "(", expr, ")"
| unary_op, factor
| int
| identifier ;
unary_op = "!" | "~" | "-";
assignment_operator = "=" | "+=" | "-=";