python中的C词法分析器
作为开发解析器的一部分,我正在使用python创建一个C词法分析器。在我的代码中,我编写了一些识别关键字、数字、运算符等的方法。编译后不会显示任何错误。在执行时,我可以输入一个.c文件。我的输出应该列出输入文件中的所有关键字、标识符等。但它没有显示任何东西。有人能帮我吗。代码见附件python中的C词法分析器,python,parsing,lexical-analysis,Python,Parsing,Lexical Analysis,作为开发解析器的一部分,我正在使用python创建一个C词法分析器。在我的代码中,我编写了一些识别关键字、数字、运算符等的方法。编译后不会显示任何错误。在执行时,我可以输入一个.c文件。我的输出应该列出输入文件中的所有关键字、标识符等。但它没有显示任何东西。有人能帮我吗。代码见附件 import sys import string delim=['\t','\n',',',';','(',')','{','}','[',']','#','<','>'] oper=['+','-','
import sys
import string
delim=['\t','\n',',',';','(',')','{','}','[',']','#','<','>']
oper=['+','-','*','/','%','=','!']
key=["int","float","char","double","bool","void","extern","unsigned","goto","static","class","struct","for","if","else","return","register","long","while","do"]
predirect=["include","define"]
header=["stdio.h","conio.h","malloc.h","process.h","string.h","ctype.h"]
word_list1=""
i=0
j=0
f=0
numflag=0
token=[0]*50
def isdelim(c):
for k in range(0,14):
if c==delim[k]:
return 1
return 0
def isop(c):
for k in range(0,7):
if c==oper[k]:
ch=word_list1[i+1]
i+=1
for j in range(0,6):
if ch==oper[j]:
fop=1
sop=ch
return 1
#ungetc(ch,fp);
return 1
j+=1
return 0;
k+=1
def check(t):
print t
if numflag==1:
print "\n number "+str(t)
return
for k in range(0,2):#(i=0;i<2;i++)
if strcmp(t,predirect[k])==0:
print "\n preprocessor directive "+str(t)
return
for k in range(0,6): #=0;i<6;i++)
if strcmp(t,header[k])==0:
print "\n header file "+str(t)
return
for k in range(0,21): #=0;i<21;i++)
if strcmp(key[k],t)==0:
print "\n keyword "+str(key[k])
return
print "\n identifier \t%s"+str(t)
def skipcomment():
ch=word_list[i+1]
i+=1
if ch=='/':
while word_list1[i]!='\0':
i+=1#ch=getc(fp))!='\0':
elif ch=='*':
while f==0:
ch=word_list1[i]
i+=1
if c=='/':
f=1
f=0
a=raw_input("Enter the file name:")
s=open(a,"r")
str1=s.read()
word_list1=str1.split()
i=0
#print word_list1[i]
for word in word_list1 :
print word_list1[i]
if word_list1[i]=="/":
print word_list1[i]
elif word_list1[i]==" ":
print word_list1[i]
elif word_list1[i].isalpha():
if numflag!=1:
token[j]=word_list1[i]
j+=1
if numflag==1:
token[j]='\0'
check(token)
numflag=0
j=0
f=0
if f==0:
f=1
elif word_list1[i].isalnum():
if numflag==0:
numflag=1
token[j]=word_list1[i]
j+=1
else:
if isdelim(word_list1[i]):
if numflag==1:
token[j]='\0'
check(token)
numflag=0
if f==1:
token[j]='\0'
numflag=0
check(token)
j=0
f=0
print "\n delimiters : "+word_list1[i]
elif isop(word_list1[i]):
if numflag==1:
token[j]='\0'
check(token)
numflag=0
j=0
f=0
if f==1:
token[j]='\0'
j=0
f=0
numflag=0
check(token)
if fop==1:
fop=0
print "\n operator \t"+str(word_list1[i])+str(sop)
else:
print "\n operator \t"+str(c)
elif word_list1[i]=='.':
token[j]=word_list1[i]
j+=1
i+=1
导入系统
导入字符串
delim=['\t'、'\n'、'、';'、'('、')'、'{'、'}'、'['、']'、'#'、']
运算符=['+'、'-'、'*'、'/'、'%'、'='、'!']
key=[“int”,“float”,“char”,“double”,“bool”,“void”,“extern”,“unsigned”,“goto”,“static”,“class”,“struct”,“for”,“if”,“else”,“return”,“register”,“long”,“while”,“do”]
predirect=[“包括”、“定义”]
header=[“stdio.h”、“conio.h”、“malloc.h”、“process.h”、“string.h”、“ctype.h”]
word_list1=“”
i=0
j=0
f=0
numflag=0
令牌=[0]*50
def isdelim(c):
对于范围(0,14)内的k:
如果c==delim[k]:
返回1
返回0
def isop(c):
对于范围(0,7)内的k:
如果c==oper[k]:
ch=单词列表1[i+1]
i+=1
对于范围(0,6)内的j:
如果ch==oper[j]:
fop=1
sop=ch
返回1
#ungetc(ch,fp);
返回1
j+=1
返回0;
k+=1
def检查(t):
打印t
如果numflag==1:
打印“\n编号”+str(t)
返回
对于范围(0,2)中的k:#(i=0;i您的代码不好。请尝试将其拆分为较小的函数,以便单独测试。您是否尝试过调试该程序?一旦找到导致问题的地方,您可以回到这里,问一个更具体的问题 更多提示。您可以实现
isdelim
更简单,如下所示:
def isdelim(c):
return c in delim
for item in collection:
# do stuff with item
for k in range(0,14)
token[j]=word_list1[i]
token.append (word_list[i])
要比较字符串是否相等,请使用Python中不存在的string1==string2
strcmp
。我不知道您是否知道Python通常是解释的,而不是编译的。这意味着,如果调用不存在的函数,您将不会得到编译器错误。程序只会在运行时到达这是我的电话
在函数isop
中有无法访问的代码。无法访问j+=1
和k+=1
行,因为它们位于return
语句之后
在Python中,对集合的迭代如下所示:
def isdelim(c):
return c in delim
for item in collection:
# do stuff with item
for k in range(0,14)
token[j]=word_list1[i]
token.append (word_list[i])
这些只是一些提示。你真的应该读一下
您应该了解更多关于Python basics.ATM的知识,因为您的代码包含太多的if
s和for
s
试着学习它。它似乎为我输出了很多输出,但代码很难理解。我自己运行了它,它出错了,如下所示:
Traceback (most recent call last):
File "C:\dev\snippets\lexical.py", line 92, in <module>
token[j]=word_list1[i]
IndexError: list assignment index out of range
我的意思是,你已经列了一个清单,你可以用这个范围
for k in range(delim)
有点道理
但你只是想确定c是否在delim列表中,所以说:
if c in delim
为什么返回1和0,它们是什么意思?为什么不使用True和False
可能还有其他几个显而易见的问题,比如代码的整个“主要”部分
这不是很像蟒蛇:
token=[0]*50
你真的只是想说
token = []
现在它只是一个空列表
不要尝试使用这样的计数器:
def isdelim(c):
return c in delim
for item in collection:
# do stuff with item
for k in range(0,14)
token[j]=word_list1[i]
token.append (word_list[i])
您要附加,如下所示:
def isdelim(c):
return c in delim
for item in collection:
# do stuff with item
for k in range(0,14)
token[j]=word_list1[i]
token.append (word_list[i])
老实说,我觉得你一开始的问题太难了。我是Python新手。无论如何,thanx。@Aneeshia:“我是Python新手”。这意味着你必须先阅读Python教程。然后,在阅读教程后,你应该在谷歌上搜索“Python词汇扫描”阅读你在那里找到的代码。从像这样一大块糟糕的代码开始是个坏主意。本教程是个好主意。哇。重新发明轮子需要做很多工作。为什么不下载
ply
并从现有的C语言解析器开始?为什么要这么做?我不明白你为什么要这么做。你有很多好的adv请继续回答前面的问题(我认为这是您的动机),包括对python中完整C解析器的引用。