-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaGrammar
263 lines (236 loc) · 4.84 KB
/
JavaGrammar
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
grammar JavaGrammar $
prog
# classDeclaration[0] EOF
{System.out.println(_0.val);}
$
classDeclaration[int i] returns [String val]
# modifier CLASS NAME LEFT bodyDeclaration[i+1] RIGHT
{
String tab = "";
for (int j = 0; j < i; j++) tab += "\t";
val = tab + _0.val + "class " + _2.text + " {\n" + _4.val + tab + "}\n";
}
$
bodyDeclaration[int i] returns [String val]
# declaration[i] classElement[i] bodyDeclaration[i]
{val = _0.val + _1.val + _2.val;}
# EPS
{val = "";}
$
declaration[int i] returns [String val]
# modifier type NAME
{
String tab = "";
for (int j = 0; j < i; j++) tab += "\t";
val = tab + _0.val + _1.val + " " + _2.text;
}
# type NAME
{
String tab = "";
for (int j = 0; j < i; j++) tab += "\t";
val = tab + _0.val + " " + _1.text;
}
$
classElement[int i] returns [String val]
# EQUAL expression SEMICOLON
{val = " = " + _1.val + ";\n";}
# LEFTROUND arguments RIGHTROUND LEFT functionBody[i+1] RIGHT
{
String tab = "";
for (int j = 0; j < i; j++) tab += "\t";
val = "(" + _1.val + ") {\n" + _4.val + tab + "}\n";
}
# SEMICOLON
{val = ";\n";}
$
functionBody[int i] returns [String val]
# functionElement[i] functionBody[i]
{val = _0.val + _1.val;}
# EPS
{val = "";}
$
functionElement[int i] returns [String val]
# NAME EQUAL expression SEMICOLON
{
String tab = "";
for (int j = 0; j < i; j++) tab += "\t";
val = tab + _0.text + " = " + _2.val + ";\n";
}
# ifBlock[i] elseBlock[i]
{val = _0.val + _1.val;}
# whileBlock[i]
{val = _0.val;}
# RETURN expression SEMICOLON
{
String tab = "";
for (int j = 0; j < i; j++) tab += "\t";
val = tab + _0.text + " " + _1.val + ";\n";
}
$
ifBlock[int i] returns [String val]
# IF LEFTROUND expression RIGHTROUND LEFT functionBody[i+1] RIGHT
{
String tab = "";
for (int j = 0; j < i; j++) tab += "\t";
val = tab + "if (" + _2.val + ") {\n" + _5.val + tab + "}";
}
$
elseBlock[int i] returns [String val]
# ELSE LEFT functionBody[i+1] RIGHT
{
String tab = "";
for (int j = 0; j < i; j++) tab += "\t";
val = " else {\n" + _2.val + tab + "}\n";
}
# EPS
{val = "\n";}
$
whileBlock[int i] returns [String val]
# WHILE LEFTROUND expression RIGHTROUND LEFT functionBody[i+1] RIGHT
{
String tab = "";
for (int j = 0; j < i; j++) tab += "\t";
val = tab + "while (" + _2.val + ") {\n" + _5.val + tab + "}\n";
}
$
arguments returns [String val]
# argumentWithoutComma argumentss
{val = _0.val + _1.val;}
# EPS
{val = "";}
$
argumentss returns [String val]
# argumentWithComma argumentss
{val = _0.val + _1.val;}
# EPS
{val = "";}
$
argumentWithoutComma returns [String val]
# type NAME
{val = _0.val + " " + _1.text;}
$
argumentWithComma returns [String val]
# COMMA type NAME
{val = ", " + _1.val + " " + _2.text;}
$
modifier returns [String val]
# PUBLIC
{val = _0.text + " ";}
# PRIVATE
{val = _0.text + " ";}
$
type returns [String val]
# INT
{val = _0.text;}
# LONG
{val = _0.text;}
# BOOLEAN
{val = _0.text;}
# NAME
{val = _0.text;}
$
value returns [String val]
# NUMBER
{val = _0.text;}
# TRUE
{val = _0.text;}
# FALSE
{val = _0.text;}
# NAME
{val = _0.text;}
$
expression returns [String val]
# value rightPartOfExpr
{val = _0.val + _1.val;}
# NEW NAME LEFTROUND newOper
{val = "new " + _1.text + "(" + _3.val;}
# LEFTROUND expression RIGHTROUND rightPartOfExpr
{val = "(" + _1.val + ")" + _3.val;}
$
rightPartOfExpr returns [String val]
# oper expression
{val = " " + _0.val + " " + _1.val;}
# EPS
{val = "";}
$
oper returns [String val]
# PLUS
{val = "+";}
# MUL
{val = "*";}
# DIV
{val = "/";}
# AND
{val = "&&";}
# MOD
{val = "%";}
# OR
{val = "||";}
# EQUAL EQUAL
{val = "==";}
# LESS
{val = "<";}
# GREAT
{val = ">";}
# NOT EQUAL
{val = "!=";}
$
newOper returns [String val]
# argumentsExpr RIGHTROUND
{val = _0.val + ")";}
# RIGHTROUND
{val = ")";}
$
argumentsExpr returns [String val]
# argumentWithoutCommaExpr argumentssExpr
{val = _0.val + _1.val;}
$
argumentssExpr returns [String val]
# argumentWithCommaExpr argumentssExpr
{val = _0.val + _1.val;}
# EPS
{val = "";}
$
argumentWithoutCommaExpr returns [String val]
# expression
{val = _0.val;}
$
argumentWithCommaExpr returns [String val]
# COMMA expression
{val = ", " + _1.val;}
$
PUBLIC : (public) $
PRIVATE : (private) $
CLASS : (class) $
NEW : (new) $
IF : (if) $
ELSE : (else) $
WHILE : (while) $
PLUS : (+) $
MUL : (*) $
DIV : (/) $
AND : (&&) $
OR : (||) $
MOD : (%) $
LEFT : ({) $
RIGHT : (}) $
LEFTROUND : (() $
RIGHTROUND : ()) $
SEMICOLON : (;) $
COMMA : (,) $
POINT : (.) $
EQUAL : (=) $
LESS : (<) $
GREAT : (>) $
NOT : (!) $
INT : (int) $
LONG : (long) $
FLOAT : (float) $
DOUBLE : (double) $
BOOLEAN : (boolean) $
TRUE : (true) $
FALSE : (false) $
NAME : ('[a-zA-Z]+') $
NUMBER : ('\-?\d+') $
RETURN : (return) $
SKIP : ( \t\r\n) -> skip $