Chinaunix首页 | 论坛 | 博客
  • 博客访问: 259500
  • 博文数量: 33
  • 博客积分: 861
  • 博客等级: 军士长
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-26 09:35
文章存档

2013年(1)

2012年(8)

2011年(25)

分类: Python/Ruby

2011-05-24 22:36:42

#-*-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'
 
阅读(2044) | 评论(0) | 转发(0) |
0

上一篇:Networking

下一篇:wireshark的高级过滤设置

给主人留下些什么吧!~~