-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectCompiler.java
399 lines (332 loc) · 11.1 KB
/
ProjectCompiler.java
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import java.util.LinkedList;
public class ProjectCompiler {
/////////// Language Symbol Sets /////////////////////////////////////
//////////////////////////////////////////////////////////////////////
static char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
static char negative[] = { '-' };
static char dot[] = { '.' };
//////////////////////////////////////////////////////////////////////
// Presents the source code
private static void showSourceCode(String sourceCode) {
if (sourceCode != null) {
System.out.println("=====================================================");
System.out.println("==================== SOURCE CODE ====================");
System.out.println(sourceCode);
System.out.println("=====================================================");
} // end if
}// end showSourceCode
// Presents the sequence of tokens analyzed
private static void showSequenceOfTokens(LinkedList<Token> tokens) {
int i;
///////
if (tokens != null) {
System.out.println("");
System.out.println("");
System.out.println("======================================");
System.out.println("=============== TOKENS ===============");
i = 0;
while (i < tokens.size()) {
System.out.println(i + ") " + tokens.get(i));
i = i + 1;
} // end while
System.out.println("======================================");
} // end if
}// end showSequenceOfTokens
// Reads the source code
private static String loadSourceCode(String path, String fileName, String extension) {
UdlapSequentialFile file;
String sourceCode;
String line;
///////////////////
sourceCode = "";
file = new UdlapSequentialFile(path, fileName, extension);
file.open();
while (!file.eof) {
line = file.readString();
if (line != null)
sourceCode = sourceCode + " \n" + line;
// end if
} // end while
// then we remove the first 2 characters ( " \n" )
sourceCode = sourceCode.substring(2, sourceCode.length());
return sourceCode;
}// end loadSourceCode
///////////////////////////////////////////////////////////////////
public static void main(String[] args) {
// Our vocabulary is a set of Lexemas
LinkedList<Lexema> vocabulary;
DFA dfa;
Lexema lexema;
Transition transition;
String sourceCode;
LinkedList<Token> tokens;
Rule rule;
LinkedList<Rule> grammar;
String objectiveCode;
/////////////////////////////////
// We obtain the source code C:\Users\Santiago\Documents
// sourceCode = loadSourceCode("C:/Users/gerardoayala/Desktop/datos",
// "miPrograma", "txt");
sourceCode = loadSourceCode("C:\\Users\\Santiago\\Documents", "miPrograma", "txt");
// We show the source code
showSourceCode(sourceCode);
// We construct the Vector of Lexemas,
// which will be our vocabulary
vocabulary = new LinkedList<Lexema>();
/////////////////////////
// GRAMMAR
/////////////////////////
// 0 F' -> F
// 1 F -> math ( num , num )
// 2 F -> math ( F , num )
// 3 F -> math ( F , LF )
// 4 F -> LF
// 5 LF -> listMath ( L )
// 6 L -> [ E ]
// 7 E -> E , num
// 8 E -> num
//
// math = { add | sub | mult | div }
// listMath = { sum | max | min | avg }
/////////////////////////
// Include in the vocabulary
// the operators and special symbols
lexema = new Lexema("parentesisApertura", "(");
vocabulary.add(lexema);
lexema = new Lexema("parentesisCerradura", ")");
vocabulary.add(lexema);
lexema = new Lexema("corcheteApertura", "[");
vocabulary.add(lexema);
lexema = new Lexema("corcheteCerradura", "]");
vocabulary.add(lexema);
lexema = new Lexema("coma", ",");
vocabulary.add(lexema);
lexema = new Lexema("math", "add");
vocabulary.add(lexema);
lexema = new Lexema("math", "sub");
vocabulary.add(lexema);
lexema = new Lexema("math", "mult");
vocabulary.add(lexema);
lexema = new Lexema("math", "div");
vocabulary.add(lexema);
lexema = new Lexema("listMath", "sum");
vocabulary.add(lexema);
lexema = new Lexema("listMath", "max");
vocabulary.add(lexema);
lexema = new Lexema("listMath", "min");
vocabulary.add(lexema);
lexema = new Lexema("listMath", "avg");
vocabulary.add(lexema);
// Construct the DFA for recognizing a num
dfa = new DFA();
dfa.addFinalState(2);
dfa.addFinalState(4);
transition = new Transition(0, digit, 2);
dfa.addTransition(transition);
transition = new Transition(0, negative, 1);
dfa.addTransition(transition);
transition = new Transition(1, digit, 2);
dfa.addTransition(transition);
transition = new Transition(2, digit, 2);
dfa.addTransition(transition);
transition = new Transition(2, dot, 3);
dfa.addTransition(transition);
transition = new Transition(3, digit, 4);
dfa.addTransition(transition);
transition = new Transition(4, digit, 4);
dfa.addTransition(transition);
lexema = new Lexema("num", dfa);
vocabulary.add(lexema);
/////////////////////////////////////////////////////
// We set the vocabulary
Lexer.setVocabulary(vocabulary);
// We set the separation symbols
Lexer.setSeparationSymbols(" " + "\n");
// We make the lexical analysis
// and obtain the tokens
tokens = Lexer.lexicalAnalysis(sourceCode);
/////////////////////////
// GRAMMAR
/////////////////////////
// 0 F' -> F
// 1 F -> math ( num , num )
// 2 F -> math ( F , num )
// 3 F -> math ( F , LF )
// 4 F -> LF
// 5 LF -> listMath ( [ L ] )
// 6 L -> [ E ]
// 7 E -> E , num
// 8 E -> num
//
// math = { add | sub | mult | div }
// listMath = { sum | max | min | avg }
/////////////////////////
if (tokens != null) {
// We show the tokens
showSequenceOfTokens(tokens);
//////////////////// PARSER /////////////////
// Define the grammar
grammar = new LinkedList<Rule>();
// Rule 0
rule = new Rule(0, "Fprima");
rule.addSymbol("F");
grammar.add(rule);
// Rule 1
rule = new Rule(1, "F");
rule.addSymbol("math");
rule.addSymbol("parentesisApertura");
rule.addSymbol("num");
rule.addSymbol("coma");
rule.addSymbol("num");
rule.addSymbol("parentesisCerradura");
grammar.add(rule);
// Rule 2
rule = new Rule(2, "F");
rule.addSymbol("math");
rule.addSymbol("parentesisApertura");
rule.addSymbol("F");
rule.addSymbol("coma");
rule.addSymbol("num");
rule.addSymbol("parentesisCerradura");
grammar.add(rule);
// Rule 3
rule = new Rule(3, "F");
rule.addSymbol("math");
rule.addSymbol("parentesisApertura");
rule.addSymbol("F");
rule.addSymbol("coma");
rule.addSymbol("LF");
rule.addSymbol("parentesisCerradura");
grammar.add(rule);
// Rule 4
rule = new Rule(4, "F");
rule.addSymbol("LF");
grammar.add(rule);
// Rule 5
rule = new Rule(5, "LF");
rule.addSymbol("listMath");
rule.addSymbol("parentesisApertura");
rule.addSymbol("corcheteApertura");
rule.addSymbol("E");
rule.addSymbol("corcheteCerradura");
rule.addSymbol("parentesisCerradura");
grammar.add(rule);
// Rule 6
rule = new Rule(6, "E");
rule.addSymbol("E");
rule.addSymbol("coma");
rule.addSymbol("num");
grammar.add(rule);
// Rule 7
rule = new Rule(7, "E");
rule.addSymbol("num");
grammar.add(rule);
// Set the grammar
Parser.setGrammar(grammar);
// Define the Parser Table
//////////////////////////
// state 0
ParserTable.addAction(0, "math", "S4");
ParserTable.addAction(0, "listMath", "S5");
ParserTable.addAction(0, "num", "S6");
ParserTable.addAction(0, "F", "1");
ParserTable.addAction(0, "LF", "2");
ParserTable.addAction(0, "E", "3");
// state 1
ParserTable.addAction(1, "$", "OK");
// state 2
ParserTable.addAction(2, "coma", "R4");
ParserTable.addAction(2, "$", "R4");
// state 3
ParserTable.addAction(3, "coma", "S7");
// state 4
ParserTable.addAction(4, "parentesisApertura", "S8");
// state 5
ParserTable.addAction(5, "parentesisApertura", "S9");
// state 6
ParserTable.addAction(6, "corcheteCerradura", "R7");
ParserTable.addAction(6, "coma", "R7");
// state 7
ParserTable.addAction(7, "num", "S10");
// state 8
ParserTable.addAction(8, "math", "S4");
ParserTable.addAction(8, "listMath", "S5");
ParserTable.addAction(8, "num", "S11");
ParserTable.addAction(8, "F", "12");
ParserTable.addAction(8, "LF", "2");
// state 9
ParserTable.addAction(9, "corcheteApertura", "S13");
// state 10
ParserTable.addAction(10, "corcheteCerradura", "R6");
ParserTable.addAction(10, "coma", "R6");
// state 11
ParserTable.addAction(11, "coma", "S14");
// state 12
ParserTable.addAction(12, "coma", "S15");
// state 13
ParserTable.addAction(13, "num", "S6");
ParserTable.addAction(13, "E", "16");
// state 14
ParserTable.addAction(14, "num", "S17");
// state 15
ParserTable.addAction(15, "listMath", "S5");
ParserTable.addAction(15, "num", "S18");
ParserTable.addAction(15, "LF", "19");
// state 16
ParserTable.addAction(16, "corcheteCerradura", "S20");
ParserTable.addAction(16, "coma", "S7");
// state 17
ParserTable.addAction(17, "parentesisCerradura", "S21");
// state 18
ParserTable.addAction(18, "parentesisCerradura", "S22");
// state 19
ParserTable.addAction(19, "parentesisCerradura", "S23");
// state 20
ParserTable.addAction(20, "parentesisCerradura", "S24");
// state 21
ParserTable.addAction(21, "coma", "R1");
ParserTable.addAction(21, "$", "R1");
// state 22
ParserTable.addAction(22, "coma", "R2");
ParserTable.addAction(22, "$", "R2");
// state 23
ParserTable.addAction(23, "coma", "R3");
ParserTable.addAction(23, "$", "R3");
// state 24
ParserTable.addAction(24, "parentesisCerradura", "R5");
ParserTable.addAction(24, "coma", "R5");
ParserTable.addAction(24, "$", "R5");
// Set error messages
Parser.setErrorMessage("E1", "Se espera un operando.");
Parser.setErrorMessage("E2", "No hay código fuente.");
Parser.setErrorMessage("E3", "ERROR");
Parser.setErrorMessage("E4", "ERROR");
Parser.setErrorMessage("E5", "Sobra un operando. Favor de quitarlo.");
Parser.setErrorMessage("E6", "Se espera un paréntesis de cerradura.");
Parser.setErrorMessage("E7", "Se espera un paréntesis de apertura.");
//////////////////////////////////////////////////////////////////////
// PARSING
System.out.println("");
System.out.println("");
System.out.println("//============================================");
System.out.println("//============= COMPILING..... ===============");
Parser.parsing(tokens);
System.out.println("//============================================");
//////////////////////////////////////////////////////////////////////
// CODE GENERATION
if (!Parser.weHaveAnError) {
// Get objective code
objectiveCode = CodeGenerator.getObjectiveCode();
// show objective code
System.out.println("");
System.out.println("");
System.out.println("//============================================");
System.out.println("//============= OBJECTIVE CODE ===============");
System.out.println(objectiveCode);
System.out.println("//============================================");
} // end if
else
System.out.println("WE HAVE SYNTAX ERRORS.");
} // end else
}// end main
}// end class