Analizador Sintactico
Analizador Sintactico
UNIVERSIDAD CENTRAL
FACULTAD DE INGENIERIA Y CIENCIAS BASICAS
DEPARTAMENTO DE INGENIERIA DE SISTEMAS
JULIO CESAR SIERRA GONZALES
BOGOTA
2021
Se desarrolló un programa en Python que implementa un analizador sintáctico según
descripción en clase de arquitectura y funcionalidad.
data={'id':['E→TE\'','','T→FT\'','','F→id'],
'+':['','E\'→+TE\'','t','T\'→ε','t'],
'*':['','','','T\'→*FT\'','t'],
'(':['E→TE\'','','T→FT\'','','F→(E)'],
')':['t','E\'→ε','t','T\'→ε','t'],
'#':['t','E\'→ε','t','T\'→ε','t']}
frame=pd.DataFrame(data,index=['E','E\'','T','T\'','F'])
stk="#E"
ahead=""
sub=""
def nextToken():
global sub
if(sub[0:2]=="id"):
sub=sub[2:]
return "id"
else:
s=sub[0:1]
sub=sub[1:]
return s
def empty():
if(len(stk)==0):
return True
return False
def top():
global stk
if(stk[-1:]=='\''or stk[-1:]=='d'):
return stk[-2:]
else:
return stk[-1:]
def pop():
global stk
if(stk[-1:]=='\''or stk[-1:]=='d'):
stk=stk[:-2];
else:
stk=stk[:-1]
def push(s):
global stk
while s[-1:]!='→':
if(s[-1:]=='\'' or s[-1:]=='d'):
stk+=s[-2:]
s=s[0:-2]
else:
stk+=s[-1:]
s=s[0:-1]
def handle(top,head):
global ahead
if top==head :
if head!='#':
output.write('Finaliza '+head+'\n')
ahead=nextToken()
pop()
else:
s=frame[head][top]
if s=='':
ahead = nextToken()
elif s=='t':
else:
output.write(s+'\n')
pop()
if(s[-1:]!='ε'):
def createDir():
root="."
dir="resources"
path=f'{root}/{dir}'
try:
os.makedirs(path)
except FileExistsError:
pass
def readGrammar():
with open('resources\grammar.txt','r',encoding="UTF-8") as gram:
gram_content=gram.read()
return gram_content
def readTS():
with open('resources\sintacticTable.txt','r',encoding="UTF-8") as table:
table_content=table.read()
return table_content
def readRes():
with open('resources\prodRes.txt','r',encoding='UTF-8') as res:
res_content=res.read()
return res_content
def createFileG():
gt=open('resources\grammar.txt','w+',encoding='UTF-8')
tst=open('resources\sintactictable.txt','w+',encoding='UTF-8')
gt.write('id+id*id')
tst.write("""+------------------------+-------+---------+---------+-------+------+------+
| Simbolos No Terminales | id | + | * | ( | ) | $ |
+------------------------+-------+---------+---------+-------+------+------+
|E | E→TE' | | | E→TE' | | |
| E' | | E'→+TE' | | | E'→ε | E'→ε |
|T | T→FT' | | | T→FT' | | |
| T' | | T'→ε | T'→*FT' | | T'→ε | T'→ε |
|F | F→id | | | F→(E) | | |
+------------------------+-------+---------+---------+-------+------+------+""")
tst.close()
gt.close()
if __name__ == '__main__':
createDir()
createFileG()
output=open('resources\prodRes.txt','w',encoding='UTF-8')
sub=readGrammar()
sub+='#'
ahead=nextToken()
while(empty()==False):
t=top()
handle(t,ahead)
output.close()
import tkinter as tk
from tkinter import font
import biz
import os
import shutil
import subprocess as sb
cmd='python biz.py'
p=sb.call(cmd,shell=True)
from tkinter.constants import *
mainWindow=tk.Tk()
mainFrame=tk.Frame(mainWindow,bg="#BFD9D7")
frameTitle=tk.Frame(mainWindow, bg="#BFD9D7")
def mainWindowConfig():
mainWindow.title("Analizador Sintactico Introduccion a la teoria de la computacion")
mainWindow.geometry("425x600")
mainWindow.resizable(False,False)
def loadFrames():
mainFrame.place(relheight=1,
relwidth=1)
frameTitle.place(relheight=0.1,
relwidth=1)
def loadLbl():
title=tk.Label(frameTitle,
text="ANALIZADOR SINTACTICO",
bg="#BFD9D7",
fg="black",
font="MANIFESTO")
title.pack(side=LEFT)
grammarLbl=tk.Label(mainWindow,
text="GRAMATICA:",
bg="#BFD9D7",
fg="black",
font=('Louis George Café', 14)
)
grammarLbl.place(x=1,y=50)
global grammarLblC
grammarLblC=tk.Label(mainWindow,
padx=10,
pady=5,
width=10,
bg="white",
fg="red",
bd=1,
relief="sunken",
font=("Consolas",12)
)
grammarLblC.place(x=1,y=80)
grammarLbl=tk.Label(mainWindow,
text="TABLA DE ANALISIS SINTACTICO:",
bg="#BFD9D7",
fg="black",
font=('Louis George Café', 14)
)
grammarLbl.place(x=1,y=120)
global sTableLblC
sTableLblC=tk.Label(mainWindow,
padx=1,
pady=5,
bg="white",
fg="black",
bd=1,
relief="sunken",
font=("Consolas",10)
)
sTableLblC.place(x=1,y=145)
resultLbl=tk.Label(mainWindow,
text="PRUEBA DE ESCRITORIO:",
bg="#BFD9D7",
fg="black",
font=('Louis George Café', 14)
)
resultLbl.place(x=1,y=290)
global result
result=tk.Label(
mainWindow,
padx=1,
pady=5,
bg="white",
fg="black",
bd=1,
relief="sunken",
font=("Consolas",10)
)
result.place(x=1,y=320)
def setCont():
grammarLblC.configure(text=biz.readGrammar())
sTableLblC.configure(text=biz.readTS())
def setRes():
result.configure(text=biz.readRes())
def loadBtn():
btn1=tk.Button(mainWindow,
text="Cargar Gramatica",
padx=1,
pady=5,
fg="black",
bg="#FFFFFF",
font=('Louis George Café', 10),
command= setCont
)
btn1.place(x=1,y=570)
btn2=tk.Button(mainWindow,
text="Evaluar Segun Tabla",
padx=10,
pady=5,
fg="black",
bg="#FFFFFF",
font=('Louis George Café', 10),
command= setRes
)
btn2.place(x=300,y=570)
def deleteResult():
path='resources'
try:
shutil.rmtree(path)
except FileNotFoundError:
pass
mainWindow.destroy()
mainWindowConfig()
loadFrames()
loadLbl()
loadBtn()
mainWindow.protocol('WM_DELETE_WINDOW',deleteResult)
mainWindow.mainloop()