-
Notifications
You must be signed in to change notification settings - Fork 0
/
Micro.g4
189 lines (94 loc) · 3.29 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
grammar Micro;
KEYWORD: 'PROGRAM' | 'BEGIN' | 'END' | 'FUNCTION'|
'READ' | 'WRITE' | 'IF' | 'ELSE' |
'ENDIF' | 'FOR' | 'ENDFOR' | 'RETURN' |
'INT' | 'VOID' | 'STRING' | 'FLOAT' ;
OPERATOR: ':=' | '+' | '-' | '*' | '/' | '=' | '!=' |
'<' | '>' | '(' | ')' | ';' | ',' | '<=' | '>=';
IDENTIFIER: ([a-z] | [A-Z]) ([a-z] | [A-Z] | [0-9])*;
INTLITERAL: [0-9]+;
FLOATLITERAL: [0-9]*'.'[0-9]+;
STRINGLITERAL: '"' ~'"'* '"';
COMMENT: '--' ~'\n'* '\n' -> skip;
WS : (' '|'\t'|'\r'|'\n')+ -> skip ; // skip spaces, tabs, newlines
// pruduction rules
// program
program: 'PROGRAM' id 'BEGIN' pgm_body 'END'; // global scope
id: IDENTIFIER;
pgm_body: data_declarations func_declarations;
// data declarations
data_declarations: string_decl data_declarations
| var_decl data_declarations
|
;
string_decl: 'STRING' variable_name=id ':=' string_value=STRINGLITERAL ';'; // declaration
var_decl: var_type id_list ';'; // declaration, get type from here
var_type: 'INT' #declareInt
| 'FLOAT' #declareFloat
;
id_list: variable_name=id id_tail; // declaration
id_tail: ',' variable_name=id id_tail // declaration
|
;
// function declarations
func_declarations: func_decl func_declarations #nextFunction
| #noFunction
;
func_decl: 'FUNCTION' func_type function_name=id '(' parameter_list ')' 'BEGIN' func_body 'END'; // function scope
func_type: 'VOID' | var_type;
parameter_list: parameter_decl parameter_tail | ;
parameter_decl: var_type variable_name=id; //declaration
parameter_tail: ',' parameter_decl parameter_tail
|
;
// function body
func_body: data_declarations stmt_list;
stmt_list: stmt stmt_list | ;
stmt: basic_stmt | if_stmt | for_stmt;
// basic statment
basic_stmt: (assign_stmt | read_stmt | write_stmt | return_stmt) ';';
assign_stmt: id ':=' expr; // to be defined later
read_stmt: 'READ' '(' var_list ')';
write_stmt: 'WRITE' '(' var_list ')';
return_stmt: 'RETURN' expr;
var_list: variable_name=id var_list_tail;
var_list_tail: ',' variable_name=id var_list_tail
|
;
// if statment
if_stmt: 'IF' '(' condition ')' data_declarations stmt_list else_part 'ENDIF'; // if scope
else_part: 'ELSE' data_declarations myVar=stmt_list #elseRule // else scope
| #noElse
;
condition: expr1 compare expr2;
expr1: expr;
expr2: expr;
compare: '=' | '!=' | '<' | '>' | '<=' | '>=';
// for statment
for_stmt: 'FOR' '(' init_stmt ';' condition ';' incr_stmt ')' data_declarations stmt_list 'ENDFOR'; // for scope
init_stmt: assign_stmt
|
;
incr_stmt: assign_stmt
|
;
// expressions
expr: expr_prefix term;
expr_prefix: expr_prefix term op=addop
|
;
term: factor_prefix factor;
factor_prefix: factor_prefix factor op=mulop
|
;
factor: primary | call_expr;
primary: '(' expr ')' #exprFactor
| INTLITERAL #intImmediateFactor
| FLOATLITERAL #floatImmediateFactor
| id #symbolFactor
;
call_expr: id '(' expr_list ')'; // function call
expr_list: expr expr_list_tail | ;
expr_list_tail: ',' expr expr_list_tail | ;
addop: '+' | '-';
mulop: '*' | '/';