Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
76 lines (67 loc) · 3.01 KB
/
Copy pathmain.js
File metadata and controls
76 lines (67 loc) · 3.01 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
import kdgramaticaLexer from "./kdgramaticaLexer.js";
import kdgramaticaParser from "./kdgramaticaParser.js";
import antlr4 from "antlr4";
import fs from "fs";
import MyVisitor from './MyVisitor.js';
// Función que encapsula todo el proceso de análisis para un archivo dado.
function analyzeFile(filePath) {
try {
// 1. Leer el archivo de entrada
const fileContent = fs.readFileSync(filePath, 'utf8');
const chars = new antlr4.InputStream(fileContent);
// 2. Crear el lexer y el stream de tokens
const localLexer = new kdgramaticaLexer(chars);
localLexer.removeErrorListeners();
localLexer.addErrorListener({
syntaxError(recognizer, offendingSymbol, line, column, msg, err) {
console.error(`Error léxico en ${filePath} línea ${line}:${column} - ${msg}`);
}
});
const tokens = new antlr4.CommonTokenStream(localLexer);
tokens.fill();
console.log(`\n=== Analizando archivo: ${filePath} ===\n`);
// 3. Imprimir tabla de lexemas y tokens
console.log("Tabla de Lexemas y Tokens:");
console.log("------------------------------------------------");
console.log("| Lexema | Token |");
console.log("------------------------------------------------");
tokens.tokens.forEach(token => {
if (token.type !== antlr4.Token.EOF) {
let tokenName = kdgramaticaLexer.symbolicNames[token.type] || token.text || "UNKNOWN";
console.log(`| ${token.text.padEnd(20)} | ${tokenName.padEnd(20)} |`);
}
});
console.log("------------------------------------------------");
// 4. Crear el parser y generar el árbol sintáctico
const localParser = new kdgramaticaParser(tokens);
localParser.removeErrorListeners();
localParser.addErrorListener({
syntaxError(recognizer, offendingSymbol, line, column, msg, err) {
console.error(`Error sintáctico en ${filePath} línea ${line}:${column} - ${msg}`);
}
});
localParser.buildParseTrees = true;
const tree = localParser.programa();
console.log("Árbol de Análisis Sintáctico:");
console.log(tree.toStringTree(localParser.ruleNames));
// 5. Interpretación: Visitor para traducir el árbol a código JavaScript
const visitor = new MyVisitor();
const jsCode = visitor.visit(tree);
console.log("Código JavaScript generado:");
console.log(jsCode);
// 6. Ejecutar el código generado (usa eval con precaución)
try {
eval(jsCode);
} catch (e) {
console.error(`Error al ejecutar el código generado en ${filePath}: ${e.message}`);
}
console.log("\n------------------------------------------------\n");
} catch (err) {
console.error(`Error al procesar el archivo ${filePath}: ${err.message}`);
}
}
// Llamadas de ejemplo para analizar distintos archivos de entrada:
analyzeFile("input_correct1.txt");
analyzeFile("input_correct2.txt");
analyzeFile("input_incorrect1.txt");
analyzeFile("input_incorrect2.txt");