aboutsummaryrefslogtreecommitdiff
path: root/compilador
diff options
context:
space:
mode:
Diffstat (limited to 'compilador')
-rw-r--r--compilador/lexer.py11
-rw-r--r--compilador/main.py13
-rw-r--r--compilador/tabla.py17
3 files changed, 36 insertions, 5 deletions
diff --git a/compilador/lexer.py b/compilador/lexer.py
index 1952a16..ec7d0a4 100644
--- a/compilador/lexer.py
+++ b/compilador/lexer.py
@@ -33,7 +33,7 @@ class Selector(Enum):
ENTERO = 5
class Lexer:
- def __init__(self, data):
+ def __init__(self, data, input_file):
self.tabla = TablaLex()
self.numlinea = 1
self.selector = Selector.NINGUNO
@@ -44,6 +44,9 @@ class Lexer:
self.recol_ident = ''
self.recol_entero = ''
self.data = data
+ self.tabla_file = None
+ if input_file:
+ self.tabla_file = input_file + '.tab'
def inicio(self):
for l in self.data.splitlines():
@@ -56,7 +59,8 @@ class Lexer:
self.numlinea += 1
# Imprimir tabla de símbolos
- print (str(self.tabla))
+ if self.tabla_file:
+ self.tabla.exportar(self.tabla_file)
Parser(self.tabla).inicio()
@@ -203,3 +207,6 @@ class Lexer:
def insertar_tabla(self, token, nombre, valor):
self.tabla.insertar(LexToken(token, nombre, valor, self.numlinea))
+
+
+
diff --git a/compilador/main.py b/compilador/main.py
index c27c107..186ca83 100644
--- a/compilador/main.py
+++ b/compilador/main.py
@@ -6,14 +6,16 @@ from lexer import *
class Main:
input_file = None
output_file = None
+ output_table = False
def print_help (self, arg0):
- print("Uso: % s -i entrada.ñ -o salida.ñ" % arg0)
+ print("Uso: % s -i entrada.es -o salida.es" % arg0)
+ print(" % s -i entrada.es -o salida.es -t")
print(" % s -h" % arg0)
def main(self, argv):
try:
- opts, args = getopt.getopt(argv[1:], "hi:o:", ["input=", "output="])
+ opts, args = getopt.getopt(argv[1:], "hi:o:t", ["input=", "output=", "table"])
except getopt.GetoptError as err:
print(err)
print_help(argv[0]);
@@ -26,13 +28,18 @@ class Main:
self.input_file = a
elif o in ("-o", "--output"):
self.output_file = a
+ elif o in ("-t", "--table"):
+ self.output_table = True
else:
assert False, "opción desconocida"
if self.input_file and self.output_file:
with open(self.input_file) as f:
data = f.read()
- Lexer(data).inicio()
+ if self.output_table:
+ Lexer(data, self.input_file).inicio()
+ else:
+ Lexer(data, None).inicio()
if __name__ == "__main__":
Main().main(sys.argv)
diff --git a/compilador/tabla.py b/compilador/tabla.py
index c8ca424..637b9a6 100644
--- a/compilador/tabla.py
+++ b/compilador/tabla.py
@@ -1,3 +1,4 @@
+import json, os
from enum import Enum
from dataclasses import dataclass
from typing import Any
@@ -63,6 +64,22 @@ class TablaLex:
self.tabla[i] = tok
return
+ def exportar(self, output_file):
+ data = []
+ for t in self.tabla:
+ data.append({
+ 'tipo': t.tipo,
+ 'nombre': t.nombre,
+ 'valor': str(t.valor),
+ 'numlinea': t.numlinea
+ })
+ output = json.dumps(data)
+ if os.path.exists(output_file):
+ os.remove(output_file)
+ with open(output_file, 'w+') as f:
+ f.truncate(0)
+ f.write(output)
+
def __str__(self):
output = ""
for t in self.tabla: