#-*-gb2312-*-
#filename:Lexer.py
import sys
flag=0
keyword=[]
keyword.extend(['program','var','begin','end','integer','if','then',
'else','while','do','read','write','procedure','function'])
simpleword='+-*/,;()[]'
doubleword='><=!:'
filename=raw_input('Please input the source filename(and path):\n');
try:
fin=open(filename,'r')
except:
print 'source file open error!\n'
sys.exit()
filename=raw_input('Please input the destination filename(and path):\n')
try:
fout=open(filename,'w')
except:
print 'destination file open error!\n'
source=fin.read()
print source
i=0
while i while source[i]==' ' or source[i]=='\t' or source[i]=='\n':
i=i+1
if source[i].isalpha():
temp=source[i]
i=i+1
while source[i].isalpha() or source[i].isdigit():
temp=temp+source[i]
i=i+1
if keyword.count(temp)==0:
if len(temp)>8:
temp=temp[0,7]
print 'warning: sysmol name length>8'
fout.write('ID\t'+temp)
fout.write('\n')
else:
fout.write(temp+'\t'+temp)
fout.write('\n')
elif source[i].isdigit():
temp=source[i]
i=i+1
while source[i].isdigit():
temp=temp+source[i]
i=i+1
fout.write('int\t'+temp)
fout.write('\n')
elif simpleword.count(source[i])==1:
fout.write(source[i]+'\t'+source[i])
fout.write('\n')
i=i+1
elif doubleword.count(source[i])==1:
temp=source[i]
i=i+1
if source[i]=='=':
temp=temp+'='
i=i+1
fout.write(temp+'\t'+temp)
fout.write('\n')
elif source[i]=='{':
i=1+1
while source[i]!='}':
i=i+1
i=i+1
else:
flag=flag+1
print 'Error,Program exit!\n'
break
print('%d,%d',i,len(source))
if flag==0:
print 'Success!\n'
阅读(2446) | 评论(0) | 转发(0) |