forked from MarkusEble/Compilerbau25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStmtParser.java
More file actions
368 lines (292 loc) · 12.7 KB
/
StmtParser.java
File metadata and controls
368 lines (292 loc) · 12.7 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package com.compiler;
import java.util.ArrayList;
import java.util.List;
import com.compiler.TokenIntf.Type;
import com.compiler.ast.*;
public class StmtParser {
private Lexer m_lexer;
private ExpressionParser m_exprParser;
private SymbolTable m_symbolTable;
private FunctionTable m_functionTable;
public StmtParser(Lexer lexer) {
m_lexer = lexer;
m_symbolTable = new SymbolTable();
m_functionTable = new FunctionTable();
m_exprParser = new ExpressionParser(lexer, m_symbolTable, m_functionTable);
}
public StmtParser(Lexer lexer, SymbolTable symbolTable, FunctionTable functionTable) {
m_lexer = lexer;
m_symbolTable = symbolTable;
m_functionTable = functionTable;
m_exprParser = new ExpressionParser(lexer, m_symbolTable, m_functionTable);
}
public ASTStmtNode parseProgram(String program) throws Exception {
m_lexer.init(program);
return parseBlockStmt();
}
public ASTStmtNode parseBlockStmt() throws Exception {
m_lexer.expect(TokenIntf.Type.LBRACE);
ASTStmtNode result = parseStmtlist();
m_lexer.expect(TokenIntf.Type.RBRACE);
return result;
}
public ASTStmtListNode parseStmtlist() throws Exception {
Token curToken = m_lexer.lookAhead();
final List<ASTStmtNode> stmtList = new ArrayList<>();
while (curToken.m_type != com.compiler.TokenIntf.Type.RBRACE && curToken.m_type != Type.CASE) { // RBrace and Case in Follow set of Statementlist
stmtList.add(parseStmt());
curToken = m_lexer.lookAhead();
}
return new ASTStmtListNode(stmtList);
}
public ASTStmtNode parseStmt() throws Exception {
Token token = m_lexer.lookAhead();
TokenIntf.Type type = token.m_type;
if (type == TokenIntf.Type.DECLARE) {
return parseDeclareStmt();
}
if (type == TokenIntf.Type.IDENT) {
return parseAssignStmt();
}
if (type == TokenIntf.Type.PRINT) {
return parsePrintStmt();
}
if (type == TokenIntf.Type.BLOCK) {
return parseJumpBlockStmt();
}
if (type == Type.FUNCTION) {
return parseFunctionStmt();
}
if (type == TokenIntf.Type.WHILE) {
return parseWhileLoopStmt();
}
if (type == TokenIntf.Type.DO) {
return parseDoWhileLoopStmt();
}
if (type == TokenIntf.Type.EXECUTE) {
return parseExecuteNTimesStmt();
}
if (type == TokenIntf.Type.NUMERIC_IF) {
return parseNumericIfStmt();
}
if (type == Type.SWITCH) {
return parseSwitchStmt();
}
if (type == Type.IF) {
return parseIfElseStmt();
}
m_lexer.throwCompilerException("Invalid begin of statement", "DECLARE or IDENTIFIER or PRINT or NUMERIC_IF");
return null; // unreachable
}
public ASTStmtNode parseFunctionStmt() throws Exception {
// functionDecl: FUNCTION IDENTIFIER RPAREN paramList RPAREN LBRACE functionBody RBRACE SEMICOLON
m_lexer.expect(Type.FUNCTION);
String functionName = m_lexer.m_currentToken.m_value;
m_lexer.expect(TokenIntf.Type.IDENT);
m_lexer.expect(Type.LPAREN);
List<String> parameterList = parseParameterList();
parameterList.forEach(m_symbolTable::createSymbol);
m_functionTable.createFunction(functionName, parameterList);
m_lexer.expect(TokenIntf.Type.RPAREN);
m_lexer.expect(TokenIntf.Type.LBRACE);
List<ASTStmtNode> functionBody = parseFunctionBody();
m_lexer.expect(TokenIntf.Type.RBRACE);
m_lexer.expect(Type.SEMICOLON);
return new ASTFunctionStmtNode(functionName, parameterList, functionBody);
}
private List<ASTStmtNode> parseFunctionBody() throws Exception {
// functionBody: returnStmt | stmt functionBody
List<ASTStmtNode> stmtList = new ArrayList<>();
while(m_lexer.m_currentToken.m_type != Type.RETURN){
if(m_lexer.m_currentToken.m_type == Type.RBRACE){
m_lexer.throwCompilerException("Invalid end of function body", "RETURN");
}
stmtList.add(parseStmt());
}
stmtList.add(parseReturnStmt());
return stmtList;
}
private ASTStmtNode parseReturnStmt() throws Exception {
// returnStmt: RETURN expr
m_lexer.expect(Type.RETURN);
ASTStmtNode returnStmtNode = new ASTReturnStmtNode(m_exprParser.getQuestionMarkExpr());
m_lexer.expect(Type.SEMICOLON);
return returnStmtNode;
}
private List<String> parseParameterList() throws Exception {
// paramList: IDENTIFIER paramListPos | eps
// paramListPost: eps | COMMA IDENFIER paramListPost
List<String> parameterList = new ArrayList<>();
if(m_lexer.m_currentToken.m_type != Type.IDENT){
return parameterList;
} else {
parameterList.add(m_lexer.m_currentToken.m_value);
m_lexer.advance();
while (m_lexer.m_currentToken.m_type == Type.COMMA) {
m_lexer.advance();
parameterList.add(m_lexer.m_currentToken.m_value);
m_lexer.expect(Type.IDENT);
}
return parameterList;
}
}
public ASTStmtNode parsePrintStmt() throws Exception {
m_lexer.expect(Type.PRINT);
ASTPrintStmtNode astPrintStmtNode = new ASTPrintStmtNode(m_exprParser.getQuestionMarkExpr());
m_lexer.expect(Type.SEMICOLON);
return astPrintStmtNode;
}
public ASTStmtNode parseAssignStmt() throws Exception {
String identifier = m_lexer.m_currentToken.m_value;
m_lexer.expect(TokenIntf.Type.IDENT);
Symbol ident = m_symbolTable.getSymbol(identifier);
if (ident != null) {
m_lexer.advance(); // ASSIGN
ASTExprNode expr = m_exprParser.getQuestionMarkExpr();
m_lexer.expect(TokenIntf.Type.SEMICOLON);
return new ASTAssignStmtNode(ident, expr);
} else {
m_lexer.throwCompilerException(String.format("%s not declared", identifier), "");
}
return null;
}
public ASTStmtNode parseDeclareStmt() throws Exception {
// Consume declare
m_lexer.expect(TokenIntf.Type.DECLARE);
if (m_lexer.m_currentToken.m_type != TokenIntf.Type.IDENT) {
throw new Exception("Expected token of type Identifier");
}
String identifier = m_lexer.m_currentToken.m_value;
if (m_symbolTable.getSymbol(identifier) != null) {
throw new Exception("Variable was already declared: " + identifier);
}
m_symbolTable.createSymbol(identifier);
m_lexer.advance(); // IDENT
m_lexer.expect(TokenIntf.Type.SEMICOLON);
return new ASTDeclareStmtNode(identifier);
}
ASTStmtNode parseJumpBlockStmt() throws Exception {
// BLOCK LBRACE stmtList RBRACE
m_lexer.expect(Type.BLOCK);
m_lexer.expect(Type.LBRACE);
ASTStmtListNode stmtlistNode = parseStmtlist();
m_lexer.expect(Type.RBRACE);
return new ASTJumpBlockNode(stmtlistNode);
}
ASTStmtNode parseIfElseStmt() throws Exception {
// ifStmt -> IF LPAREN condition RPAREN LBRACE stmtList RBRACE elseStmt
// elseStmt -> eps
// elseStmt -> ELSE continueStmt
// continueStmt -> ifStmt
// continueStmt -> LBRACE stmtList RBRACE
m_lexer.expect(Type.IF);
m_lexer.expect(Type.LPAREN);
ASTExprNode condition = this.m_exprParser.getQuestionMarkExpr();
m_lexer.expect(Type.RPAREN);
m_lexer.expect(Type.LBRACE);
ASTStmtListNode ifStmtList = this.parseStmtlist();
m_lexer.expect(Type.RBRACE);
if(m_lexer.lookAhead().m_type == Type.ELSE) {
m_lexer.expect(Type.ELSE);
if(m_lexer.lookAhead().m_type == Type.LBRACE) {
m_lexer.expect(Type.LBRACE);
ASTStmtListNode elseStmtList = this.parseStmtlist();
m_lexer.expect(Type.RBRACE);
return new ASTIfElseNode(condition, ifStmtList, elseStmtList);
}
if(m_lexer.lookAhead().m_type == Type.IF) {
return new ASTIfElseNode(condition, ifStmtList, parseStmtlist());
}
}
return new ASTIfElseNode(condition, ifStmtList, null);
}
ASTStmtNode parseNumericIfStmt() throws Exception {
// NUMERIC_IF LPAR expr RPAR numericIfBlock
m_lexer.expect(Type.NUMERIC_IF);
m_lexer.expect(Type.LPAREN);
final ASTExprNode predicate = this.m_exprParser.getQuestionMarkExpr();
m_lexer.expect(Type.RPAREN);
// numericIfBlock positiveBlock negativeBlock zeroBlock
// positiveBlock: POSITIVE LBRACE stmtlist RBRACE
final ASTStmtListNode positiveStmtList = parseNumericIfBlock(Type.POSITIVE);
// negativeBlock: NEGATIVE LBRACE stmtlist RBRACE
final ASTStmtListNode negativeStmtList = parseNumericIfBlock(Type.NEGATIVE);
// zeroBlock: ZERO LBRACE stmtlist RBRACE
final ASTStmtListNode zeroStmtList = parseNumericIfBlock(Type.ZERO);
return new ASTNumericIfNode(predicate, positiveStmtList, negativeStmtList, zeroStmtList);
}
private ASTStmtListNode parseNumericIfBlock(final Type type) throws Exception {
m_lexer.expect(type);
m_lexer.expect(Type.LBRACE);
final ASTStmtListNode stmtList = parseStmtlist();
m_lexer.expect(Type.RBRACE);
return stmtList;
}
ASTWhileLoopStmtNode parseWhileLoopStmt() throws Exception {
// whileStmt: WHILE LPAREN questionMarkExpr RPAREN blockStmt SEMICOLON
m_lexer.expect(TokenIntf.Type.WHILE);
m_lexer.expect(TokenIntf.Type.LPAREN);
ASTExprNode predicate = this.m_exprParser.getQuestionMarkExpr();
m_lexer.expect(TokenIntf.Type.RPAREN);
ASTStmtNode body = parseBlockStmt();
m_lexer.expect(Type.SEMICOLON);
return new ASTWhileLoopStmtNode(predicate, body);
}
ASTDoWhileLoopStmtNode parseDoWhileLoopStmt() throws Exception {
// doWhileStmt: DO blockStmt WHILE questionMarkExpr SEMICOLON
m_lexer.expect(Type.DO);
ASTStmtNode body = parseBlockStmt();
m_lexer.expect(Type.WHILE);
m_lexer.expect(Type.LPAREN);
ASTExprNode predicate = this.m_exprParser.getQuestionMarkExpr();
m_lexer.expect(Type.RPAREN);
m_lexer.expect(Type.SEMICOLON);
return new ASTDoWhileLoopStmtNode(predicate, body);
}
ASTStmtNode parseExecuteNTimesStmt() throws Exception {
// EXECUTE integer|identifier TIMES LBRACE stmtList RBRACE SEMICOLON
m_lexer.expect(Type.EXECUTE);
if(m_lexer.m_currentToken.m_type != Type.INTEGER && m_lexer.m_currentToken.m_type != Type.IDENT) {
throw new Exception("Expected token of type Integer or Identifier");
}
ASTExprNode count;
count = m_exprParser.getQuestionMarkExpr();
m_lexer.expect(Type.TIMES);
m_lexer.expect(Type.LBRACE);
ASTStmtListNode stmtlistNode = parseStmtlist();
m_lexer.expect(Type.RBRACE);
m_lexer.expect(Type.SEMICOLON);
return new ASTExecuteNTimesNode(count, stmtlistNode);
}
private ASTStmtNode parseSwitchStmt() throws Exception {
// switch_statement -> SWITCH LPAREN expression RPAREN LBRACE case_list RBRACE
m_lexer.expect(Type.SWITCH);
m_lexer.expect(Type.LPAREN);
ASTExprNode expression = m_exprParser.getQuestionMarkExpr();
m_lexer.expect(Type.RPAREN);
m_lexer.expect(Type.LBRACE);
ASTCaseListNode caseList = parseCaseList();
m_lexer.expect(Type.RBRACE);
return new ASTSwitchStmtNode(expression, caseList);
}
private ASTCaseListNode parseCaseList() throws Exception {
// case_list -> case_item case_list | epsilon
Token curToken = m_lexer.lookAhead();
final List<ASTCaseNode> caseList = new ArrayList<>();
while (curToken.m_type == Type.CASE) {
caseList.add(parseCaseStmt());
curToken = m_lexer.lookAhead();
}
return new ASTCaseListNode(caseList);
}
private ASTCaseNode parseCaseStmt() throws Exception {
// case_item -> CASE LITERAL COLON statement_list
m_lexer.expect(Type.CASE);
Token curToken = m_lexer.lookAhead();
m_lexer.expect(Type.INTEGER);
ASTIntegerLiteralNode value = new ASTIntegerLiteralNode(curToken.m_value); //Integer
m_lexer.expect(Type.DOUBLECOLON); // should be renamed to COLON as double colon is "::"
ASTStmtListNode stmtList = parseStmtlist();
return new ASTCaseNode(value, stmtList);
}
}