0% found this document useful (0 votes)
33 views6 pages

Python

The document describes a program for analyzing source code. It defines functions for lexical analysis like identifying types of characters and keywords. It also details an automaton for syntax analysis and outputs results to a text box. The program has menus to start new files, open files and save output of the analysis.

Uploaded by

alimo ben
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

Python

The document describes a program for analyzing source code. It defines functions for lexical analysis like identifying types of characters and keywords. It also details an automaton for syntax analysis and outputs results to a text box. The program has menus to start new files, open files and save output of the analysis.

Uploaded by

alimo ben
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

from ast import Import

from cProfile import label


from cgitb import text
import re
from tkinter import *
from importlib.resources import open_text
import tkinter as tk
from tkinter.messagebox import *
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter import filedialog
from tkinter .filedialog import asksaveasfile
from tkinter import END
from urllib import response
import math

pro =Tk()
pro.geometry('800x500')
pro.title('tp compilation')

def nomb(n):
if n >= '0' and n <= '9':
return True
return False

def car(c):
if c >= 'a' and c <= 'z' or c >= 'A' and c <= 'Z':
return True
return False

def nc(nc):
if nomb(nc) or car(nc) :
return True
return False
def sig(s):
if s=='+' or s=='-':
return True
return False
def opr(o):
if sig(o) and o=='=' or o == '*' or o == '/':
return True
return False

def pun(p):
if p==',' or p==';' :
return True
return False
def eg(e):
if e=='!' or e=='<' or e=='>' or e=='=' or e==':':
return True
return False

def newfile():
response= askokcancel("save","voulez-vous enregistrer le fichier texte")
print(response)
if response:
savefile()
else:
fr.delete('1.0',END)
pro.title('NEW FILE -tp compilation')

def openfile():
file=fd.askopenfilename()
if file:
fr.delete('1.0',END)
fh=open(file,'r')
read=fh.read()
fr.insert(END,read)
fh.close()

def savefile():
my_str1=fr.get("1.0",END) # read from one text box t1
fob=filedialog.asksaveasfile(filetypes=[('text file','*.txt')],
defaultextension='.txt',initialdir='D:\\my_data\\my_html',
mode='w')
try:
fob.write(my_str1)
fob.close()
fr.update()
fob.config(text="Saved")
fob.after(3000, lambda: b1.config(text='Save'))
except :
print (" There is an error...")

def com():
v1=fr.get("1.0","end-1c")
v2=v1
v1=''
sp=" "
cp=1
for char in v2:
if cp==1:
if char!='$':
v1+=char
if char=='$':
v1+=sp
cp=cp*(-1)
else:
if char!='$':
v1+=''
if char=='$':
v1 += sp
cp=cp*(-1)
v1=re.sub("\$$|\$", " ",v1)
v1 =" ".join(v1.split())
return v1

def sep(txt) :
v=txt
txt=" "
if len(v)>= 1:
v = v + "#"
for i in range(len(v)) :
if i == len(v) - 1:
break
else:
if nc(v[i]) and nc(v[i+1])or v[i+1]=='_' \
or v[i]=='_'and nc(v[i+1])\
or i==0 and sig(v[i]) and nomb(v[i+1]) or sig(v[i]) and v[i+1]=='.'
and nomb(v[i+2]) and i==0\
or eg(v[i]) and v[i+1]=='='\
or nomb(v[i]) and v[i+1]=='.'\
or v[i]=='.' and nomb(v[i+1]) \
or opr(v[i-1]) and sig(v[i]) and nomb(v[i+1]) \
or nc(v[i - 1]) and v[i]=='+'and v[i+1]=='+' and not nomb(v[i + 2])
and not v[i + 2] == '.' \
or nc(v[i - 1]) and v[i]=='-'and v[i+1]=='-' and not nomb(v[i + 2])
and not v[i + 2] == '.' \
or pun(v[i-1]) and sig(v[i]) and nomb(v[i+1])\
or opr(v[i-1]) and sig(v[i]) and v[i+1]=='.' \
or sig(v[i-1]) and sig(v[i]) and v[i+1]=='.' \
or v[i-1]=='=' and sig(v[i]) and v[i+1]=='.' :

txt= txt + v[i]


elif i==i\
and sig(v[i-1]) and sig(v[i])and nomb(v[i+1]) :

txt +="#" + v[i]

else :
txt +=v[i] +"#"

fr1.delete("1.0", "end")
txt = re.sub("\# #|\##","#",txt)
txt = re.sub("\# #|\##","#",txt)
return txt
def fn2():
fr1.config(state='normal')
fr1.delete('1.0',"end")
txt=com()
txt=sep(txt)
return txt
def fn5():
fr1.config(state='normal')
txt=fn2()
fr1.insert(END, txt)
fr1.config(state='disabled')

def type(ch):
if nomb(ch) :return 0
elif nomb(ch) or car(ch) :return 1
elif ch=='+':return 2
elif ch=='-' :return 3
elif ch=='=' :return 4
elif ch=='.' :return 5
elif ch=='_' :return 6
elif ch=='>' or ch=='<' :return 7
elif ch=='('or ch==')' or ch=='{' or ch=='}' or ch=='['or ch==']' or ch==';'or
ch=='.' :return 8
elif ch=='*' or ch=='/':return 9
elif ch=='!' or ch==':' :return 10
else : return -1

def mot_cle(m):
m=re.sub(" ", "",m)
if m=="BEGIN" or m=="END" or m=="PROGRAMME" or m=="INT" or m=="REAL" or
m=="CHAR"or m=="STRING"\
or m=="OR" or m=="AND" or m=="IF" or m=="ELSE" or m=="boolean" or m=="FOR":
return True
return False

def fnsn(ec,t):
k=''
t=re.sub(" ", "",t)
if ec==1 :
if len(t)<8 : k+=" "+t+": entite est un nombre entier \n"
else :k+=" "+t+":entite incorrect\n "
elif ec==3 :
if len(t)<10 :k+=" "+t+":entite est un nombre real \n"
else :k+=" "+t+":entite incorrect\n "
elif ec==4 : k+=" "+t+":entite est un opérateur + \n"
elif ec==5 :k+=" "+t+":entite est un pas positif \n"
elif ec==6 :
if len(t)<8:k+=" "+t+":entite est un nombre entier positif \n"
else :k+=" "+t+":entite incorrect\n "
elif ec==8 :
if len(t)<10 :k+=" "+t+":entite est un nombre real positif \n"
else :k+=" "+t+":entite incorrect\n "
elif ec==9 :k+=" "+t+":entite est operateur - \n "
elif ec==10 :k+=" "+t+":entite est un pas negatif \n"
elif ec==11 :
if len(t)<8:k+=" "+t+":entite est un nombre entier negatif\n"
else :k+=" "+t+":entite incorrect\n "
elif ec==13 :
if len(t)<10 :k+=" "+t+":entite est un nombre real negatif \n"
else :k+=" "+t+":entite incorrect\n "
elif ec==14 :k+=" "+t+":entite est un opérateur \n"
elif ec==15 :k+=" "+t+":entite est un séparateur \n"
elif ec==16 :k+=" "+t+":entite est un séparateur \n"
elif ec==17 :k+=" "+t+":entite est un operateur \n"
elif ec==18 :k+=" "+t+":entite est un operateur \n"
elif ec==19 :k+=" "+t+":entite est un operateur \n"
elif ec==20 :
print(t)
if mot_cle(t) :k+=" "+t+":entite est un mot clé \n"
elif len(t)<11 :k+=" "+t+":entite est un identificteur \n"
else :k+=" "+t+":entite incorrect\n "
else :k+=" "+t+":entite incorrect\n "
fr1.config(state='normal')
fr1.insert(END, k)
fr1.config(state='disabled')

def automate():
#0,#1,#2,#3,#4,#5,#6,#7,#8,#9,#10
mat =[[1,20,4,9,18,15,-1,18,15,14,16],#0
[1,-1,-1,-1,-1,2,-1,-1,-1,-1,-1],#1
[3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#2
[3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#3
[6,-1,5,-1,-1,-1,-1,-1,-1,-1,-1],#4
[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#5
[6,-1,-1,-1,-1,7,-1,-1,-1,-1,-1],#6
[8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#7
[8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#8
[11,-1,-1,10,-1,-1,-1,-1,-1,-1,-1],#9
[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#10
[11,-1,-1,-1,-1,12,-1,-1,-1,-1,-1],#11
[13,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#12
[13,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#13
[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#14
[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#15
[-1,-1,-1,-1,17,-1,-1,-1,-1,-1,-1],#16
[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#17
[-1,-1,-1,-1,19,-1,-1,-1,-1,-1,-1],#18
[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],#19
[20,20,-1,-1,-1,-1,21,-1,-1,-1,-1],#20
[20,20,-1,-1,-1,-1,22,-1,-1,-1,-1],#21
[20,20,-1,-1,-1,-1,23,-1,-1,-1,-1],#22
[-1,-1,-1,-1,-1,-1,22,-1,-1,-1,-1],#23
]

ab=fn2()
fr1.delete("1.0", "end")
i=0
k=''
ec=0
for i in range(len(ab)):
print(ec)
tc=ab[i]
if tc!='#':
k=k+ab[i]
if ec!=-1 and type(tc)!=-1:
ec=mat[ec][type(tc)]
# else :ec=-1
else :
fnsn(ec,k)
k=''
ec=0

def exit():
response= askokcancel("quitter","etes vous sur de voluoir quitter")
print(response)
if response:
pro.destroy()

menubar = Menu(pro)
f = Menu(menubar, tearoff=0)
f.add_command(label='new ', command=newfile )
f.add_command(label='open',command=openfile)
f.add_command(label='save',command=lambda:savefile())
f.add_command(label='exit',command=exit)

lb1 = Label(pro, text='Saiser votre code ici :', fg='black', bg='white',


font='12')
lb1.place(x=30, y=13)

fr = tk.Text(pro,height='25',width='40')
fr.place(x=30,y=40)

lb2 = Label(pro, text='Console :', fg='black', bg='white', font='12')


lb2.place(x=473, y=13)

fr1 = tk.Text (pro,width='40',height='25',bg='black',fg='white')


fr1.place(x=470,y=40)
fr1.config(state='disable')

f.add_separator()
menubar.add_cascade(label='file',menu=f)
pro.config(menu=menubar)

menubar2=Menu(pro)
p = Menu(menubar2, tearoff=0)
p.add_command(label='lexical', command=fn5 )
p.add_command(label='syntaxique', command=automate)
p.add_command(label='symentique')
menubar.add_cascade(label='analyse',menu=p)
pro.config(menu=menubar)
pro.mainloop()

You might also like