-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grammar Documentation.txt
127 lines (93 loc) · 3.92 KB
/
Grammar Documentation.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Types: integer, real, bool, array-> rectangular, jagged
Variable Name: alphabet, digits, underscore. Not start with digit
Keywords: program: PROGRAM, declare: DECLARE, list: LIST, of: OF , variables: VARIABLES, array: ARRAY, size: SIZE, values: VALUES, jagged array: JAGARR, of: OF, integer: INTEGER, real: REAL, boolean: BOOLEAN
Symbols: ( : RBOP , ) : RBCL , { : CBOP , }: CBCL , : : COLON , ; : SEMICOLON, [ : SBOP , ] : SBCL
Arithmetic operators: +: PLUS, -: MINUS, *: MULT, /: DIV
Boolean operators: &&&: AND, |||: OR
No relational operators
//Start of program
s => PROGRAM RBOP RBCL CBOP list_of_statements CBCL
//Program is a list of declaration and assignment statements
list_of_statements => declaration_statements assignment_statements
DECLARATION STATEMENTS
// Declaration statements is a list of declarations
declaration_statements => declaration declaration_statements
declaration_statements => declaration
// Declarations can be of two type -> declaring single variable or declaring multiple variables
declaration => primitive_decl
declaration => jagged_array_decl
// Declaration statement for primitive variables
primitive_decl => DECLARE declare_vars COLON declaration_type SEMICOLON
// Declaration statement for jagged array variables
jagged_array_decl => DECLARE declare_vars COLON declare_jagged
// Declare single variable or a list of variables
declare_vars => ID
declare_vars => LIST OF VARIABLES var_name_list
// variable name list
var_name_list => ID var_name_list
var_name_list => ID
declaration_type => primitive
declaration_type => rect_array
// Primitive are further categorized into 3 types
primitive => INTEGER
primitive => REAL
primitive => BOOLEAN
// Rectangular array
rect_array => ARRAY array_dim OF INTEGER
// Array dimensions are [num .. num][num .. num]....[num .. num]
array_dim => SBOP idx TWODOT idx SBCL array_dim
array_dim => SBOP idx TWODOT idx SBCL
// Jagged array - 2D or 3D
declare_jagged => declare_twod_jagged
declare_jagged => declare_threed_jagged
// 2D [num .. num][ ]
declare_twod_jagged => JAGGED ARRAY SBOP NUM TWODOT NUM SBCL SBOP SBCL OF INTEGER SEMICOLON twod_jagged_statements
twod_jagged_statements => twod_jagged_statement twod_jagged_statements
twod_jagged_statements => twod_jagged_statement
twod_jagged_statement => R SBOP INTEGER SBCL COLON SIZE INTEGER COLON VALUES CBOP twod_values CBCL
twod_values => NUM SEMICOLON twod_values |
twod_values => NUM
// 3D [num .. num][ ][ ]
declare_threed_jagged => JAGGED ARRAY SBOP NUM TWODOT NUM SBCL SBOP SBCL SBOP SBCL OF INTEGER SEMICOLON threed_jagged_statements
threed_jagged_statements => threed_jagged_statement threed_jagged_statements
threed_jagged_statements => threed_jagged_statement
threed_jagged_statement=>R SBOP INTEGER SBCL COLON SIZE INTEGER COLON VALUES CBOP threed_values CBCL
threed_values => list_num SEMICOLON threed_values
threed_values => list_num
list_num => NUM list_num
list_num => NUM
// Index of array
idx => ID
idx => NUM
ASSIGNMENT STATEMENTS
// Assignment statements are list of assignments
assignment_statements => assignment assignment_statements
assignment_statements => assignment
// An assignment -> lhs = rhs
assignment => lhs EQUALS rhs SEMICOLON
// LHS is basically a variable or an array element
lhs => ID
lhs => array_ele
// Array element - arr[2]
array_ele => ID SBOP list SBCL
list => idx list
list => idx
// Right hand expression can be an arithmetic expression or a boolean expression
rhs => arithmetic_expr
rhs => boolean_expr
// grammar for arithmetic expressions
arithmetic_expr => arithmetic_term op1 arithmetic_expr
arithmetic_expr => arithmetic_term
arithmetic_term => IDX op2 arithmetic_term
arithmetic_term => IDX
op1 => PLUS
op1 => MINUS
op2 => MULT
op2 => DIV
// grammar for boolean expressions
boolean_expr => boolean_term OR boolean_expr
boolean_expr => boolean_term
boolean_term => boolean_factor AND boolean_term
boolean_term => boolean_factor
boolean_factor => BOOLEAN
boolean_factor => ID