-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar.txt
More file actions
62 lines (52 loc) · 2.22 KB
/
grammar.txt
File metadata and controls
62 lines (52 loc) · 2.22 KB
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
##### Program #####
program -> PROGRAM id BEGIN pgm_body END
id -> IDENTIFIER
pgm_body -> decl func_declarations
decl -> string_decl decl | var_decl decl | empty
##### Global String Declaration #####
string_decl -> STRING id := str ;
str -> STRINGLITERAL
##### Variable Declaration #####
var_decl -> var_type id_list ;
var_type -> FLOAT | INT
any_type -> var_type | VOID
id_list -> id id_tail
id_tail -> , id id_tail | empty
##### Function Paramater List #####
param_decl_list -> param_decl param_decl_tail | empty
param_decl -> var_type id
param_decl_tail -> , param_decl param_decl_tail | empty
##### Function Declarations #####
func_declarations -> func_decl func_declarations | empty
func_decl -> FUNCTION any_type id (param_decl_list) BEGIN func_body END
func_body -> decl stmt_list
##### Statement List #####
stmt_list -> stmt stmt_list | empty
stmt -> base_stmt | if_stmt | for_stmt
base_stmt -> assign_stmt | read_stmt | write_stmt | return_stmt
##### Basic Statements #####
assign_stmt -> assign_expr ;
assign_expr -> id := expr
read_stmt -> READ ( id_list );
write_stmt -> WRITE ( id_list );
return_stmt -> RETURN expr ;
##### Expressions #####
expr -> expr_prefix factor
expr_prefix -> expr_prefix factor addop | empty
factor -> factor_prefix postfix_expr
factor_prefix -> factor_prefix postfix_expr mulop | empty
postfix_expr -> primary | call_expr
call_expr -> id ( expr_list )
expr_list -> expr expr_list_tail | empty
expr_list_tail -> , expr expr_list_tail | empty
primary -> ( expr ) | id | INTLITERAL | FLOATLITERAL
addop -> + | -
mulop -> * | /
##### Complex Statements and Condition #####
if_stmt -> IF ( cond ) decl stmt_list else_part FI
else_part -> ELSE decl stmt_list | empty
cond -> expr compop expr
compop -> < | > | = | != | <= | >=
init_stmt -> assign_expr | empty
incr_stmt -> assign_expr | empty
for_stmt -> FOR ( init_stmt ; cond ; incr_stmt ) decl stmt_list ROF