-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
55 lines (55 loc) · 1.87 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
program ::= extern_list decl_list
| decl_list
extern_list ::= extern_list extern
| extern
extern ::= "extern" type_spec IDENT "(" params ")" ";"
decl_list ::= decl_list decl
| decl
decl ::= var_decl
| fun_decl
var_decl ::= var_type IDENT ";"
type_spec ::= "void"
| var_type
var_type ::= "int" | "float" | "bool"
fun_decl ::= type_spec IDENT "(" params ")" block
params ::= param_list
| "void" | epsilon
param_list ::= param_list "," param
| param
param ::= var_type IDENT
block ::= "{" local_decls stmt_list "}"
local_decls ::= local_decls local_decl
| epsilon
local_decl ::= var_type IDENT ";"
stmt_list ::= stmt_list stmt
| epsilon
stmt ::= expr_stmt
| block
| if_stmt
| while_stmt
| return_stmt
expr_stmt ::= expr ";"
| ";"
while_stmt ::= "while" "(" expr ")" stmt
if_stmt ::= "if" "(" expr ")" block else_stmt
else_stmt ::= "else" block
| epsilon
return_stmt ::= "return" ";"
| "return" expr ";"
# operators in order of increasing precedence
expr ::= IDENT "=" expr
| rval
rval ::= rval "||" rval
| rval "&&" rval
| rval "==" rval | rval "!=" rval
| rval "<=" rval | rval "<" rval | rval ">=" rval | rval ">" rval
| rval "+" rval | rval "-" rval
| rval "*" rval | rval "/" rval | rval "%" rval
| "-" rval | "!" rval
| "(" expr ")"
| IDENT | IDENT "(" args ")"
| INT_LIT | FLOAT_LIT | BOOL_LIT
args ::= arg_list
| epsilon
arg_list ::= arg_list "," expr
| expr