aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--compilador/main.py14
-rw-r--r--compilador/parser.py8
3 files changed, 18 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index c2d12e7..4b94b69 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@ __pycache__/
*.tab
*.gv
*.gv.pdf
+*.ast
diff --git a/compilador/main.py b/compilador/main.py
index d3f7444..d419286 100644
--- a/compilador/main.py
+++ b/compilador/main.py
@@ -27,17 +27,18 @@ class Main:
input_file = None
output_file = None
output_table = False
+ output_ast = False
step = None
def print_help (self, arg0):
- print("Uso: % s -i entrada.es -o salida.es [-t]" % arg0)
+ print("Uso: % s -i entrada.es -o salida.es [-t] [-a]" % arg0)
print(" % s -i entrada.es -o salida.es [-l|-p|-s]" % arg0)
print(" % s -h" % arg0)
def main(self, argv):
try:
- opts, args = getopt.getopt(argv[1:], "hi:o:lpst", [
- "input=", "output=", "lex", "parse", "semantic", "table"
+ opts, args = getopt.getopt(argv[1:], "hi:o:lpsta", [
+ "input=", "output=", "lex", "parse", "semantic", "table", "ast"
])
except getopt.GetoptError as err:
print(err)
@@ -53,6 +54,8 @@ class Main:
self.output_file = a
elif o in ("-t", "--table"):
self.output_table = True
+ elif o in ("-a", "--ast"):
+ self.output_ast = True
elif o in ("-l", "--lex"):
self.step = Step.LEXICO
elif o in ("-p", "--parse"):
@@ -64,7 +67,9 @@ class Main:
if self.input_file and self.output_file:
table_file = self.input_file + '.tab'
+ ast_file = self.input_file + '.ast'
delete_tab = not self.step and not self.output_table and not os.path.exists(table_file)
+ delete_ast = not self.step and not self.output_ast and not os.path.exists(ast_file)
try:
if self.step == Step.LEXICO:
sys.exit(Lexer(self.input_file).inicio())
@@ -81,9 +86,10 @@ class Main:
except Exception as e:
traceback.print_exception(type(e), e, e.__traceback__)
sys.exit(1)
- # Borrar tabla de símbolos
if delete_tab:
os.remove(table_file)
+ if delete_ast:
+ os.remove(ast_file)
else:
self.print_help(argv[0])
sys.exit(2)
diff --git a/compilador/parser.py b/compilador/parser.py
index a8f86b9..5330b55 100644
--- a/compilador/parser.py
+++ b/compilador/parser.py
@@ -14,7 +14,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
-import sys
+import sys, pickle
import graphviz as gv
from pprint import pprint
@@ -37,8 +37,14 @@ class Parser:
print (unit.message, file=sys.stderr)
return 1
+ # Renderizar AST
dot = gv.Digraph()
dot.attr('node', fontname='monospace')
unit.graph(dot)
dot.render(self.input_file + '.gv')
+
+ # Serializar AST
+ with open(self.input_file + '.ast', 'wb') as f:
+ pickle.dump(unit, f)
+
return 0