-
Notifications
You must be signed in to change notification settings - Fork 1
/
Micro.g4
executable file
·250 lines (202 loc) · 3.62 KB
/
Micro.g4
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
grammar Micro;
@members {
public SymbolTableTree symbolTree = new SymbolTableTree();
}
// Program
program
: 'PROGRAM'
id
'BEGIN'
{
// GLOBAL scope table is created implicitly
// when first SymbolTableTree is initialized
}
pgm_body
'END';
id returns [String identifier]
: IDENTIFIER {
$identifier = (String)$IDENTIFIER.text;
};
pgm_body
: decl func_declarations;
decl
: string_decl decl
| var_decl decl
| //empty
;
// Global String Declaration
string_decl
: 'STRING' id ':=' str ';'
{
symbolTree.addString($id.text, $str.text);
}
;
str: STRINGLITERAL;
// Variable Declaration
var_decl
: var_type id_list ';'
{
// add to symbol table in current scope
symbolTree.addLocals($id_list.ids, $var_type.text);
}
;
var_type
: 'FLOAT'
| 'INT'
;
any_type
: var_type
| 'VOID'
;
id_list returns [ArrayList<String> ids]
: id
id_tail {
$ids = $id_tail.ids_list;
// add the first variable
$ids.add(0, $id.text);
}
;
id_tail returns [ArrayList<String> ids_list]
: ','
id
recurse_tail = id_tail {
$ids_list = $recurse_tail.ids_list;
$ids_list.add(0, $id.text);
}
| // empty (base case)
{
$ids_list = new ArrayList<String>();
}
;
// Function Parameter list
param_decl_list
: param_decl param_decl_tail
| // empty
;
param_decl
: var_type id
{
symbolTree.addParameter($id.text, $var_type.text);
}
;
param_decl_tail
: ',' param_decl param_decl_tail
| // empty
;
// Function Declarations
func_declarations
: func_decl func_declarations
| // empty
;
func_decl
: 'FUNCTION'
any_type id { symbolTree.createScope($id.text); }
'('
param_decl_list
')'
'BEGIN'
func_body
'END' { symbolTree.exitScope(); }
;
func_body: decl stmt_list;
// Statement List
stmt_list
: stmt stmt_list
| // empy;
;
stmt
: base_stmt
| if_stmt
| while_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' { symbolTree.createScope(); }
'(' cond ')'
decl
stmt_list { symbolTree.exitScope(); }
else_part
'ENDIF'
;
else_part
: 'ELSE' { symbolTree.createScope(); }
decl
stmt_list { symbolTree.exitScope(); }
| // empty
;
cond: expr compop expr;
compop
: '<'
| '>'
| '='
| '!='
| '<='
| '>='
;
// While
while_stmt
: 'WHILE' { symbolTree.createScope(); }
'(' cond ')'
decl
stmt_list
'ENDWHILE' { symbolTree.exitScope(); }
;
WHITESPACE: (' ' | '\t' | '\n' | '\r' | '\f')+ -> skip;
IDENTIFIER: [A-z_][A-z0-9_]*;
INTLITERAL: [0-9]+;
FLOATLITERAL: [0-9]*?['.'][0-9]*;
STRINGLITERAL: '"'(~('\n'|'\r'))*?'"';
COMMENT: '--'(~('\r'|'\n'))* -> skip;
OPERATOR: (':=' | '+' | '-' | '/' | '=' | '*'
| '!=' | '<' | '>' | '(' | ')'
| ';' | ',' | '<=' | '>=');