Python判断字符串是否为合法标⽰符操作
这学期在学习编译原理,最近的上机作业就是做⼀个简单的词法分析器,在做的过程中,突然有个需求就是判断⼀个字符串是否为合法的标⽰符,因为我是⽤python语⾔做的,做的是Python的词法分析器,于是下⾯分享以下怎样判断⼀个字符串是合法的标⽰符。
⾸先,我们来熟悉以下python标⽰符的定义是什么?
定义:以字母或下划线开始的,由字母,数字或下划线组成,但是不能是python的保留字。
⼜有疑问了,python有哪些保留字,分别是什么?
# python2.x
import keyword
print keyword.kwlis
# python3.x
import keyword
print(keyword.kwlist)
# python2.x输出:
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
# 共31个
# python3.x输出:
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] # 共33个
好了,下⾯开始判断
# python2.7
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import keyword
import string
def is_signal(s):
kw = keyword.kwlist
if s in kw:
return 0
elif s[0] == '_' or s[0] in string.letters: # 判断是否为字母或下划线开头
for i in s:
if i == '_' or i in string.letters or i in string.digits: # 判断是否由字母数字或下划线组成
pass
else:
return 0
return 1
else:
return 0
def main():
s = raw_input()
if is_signal(s) == 1:
print "True"
else:
print "False"
if __name__ == '__main__':
main()
# python3.4
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import keyword
import string
def is_signal(s):
kw = keyword.kwlist
if s in kw:
return 0
字符串长度判断
elif s[0] == '_' or s[0] in string.ascii_letters: # 判断是否为字母或下划线开头
for i in s:
if i == '_' or i in string.ascii_letters or i in string.digits: # 判断是否由字母数字或下划线组成
pass
else:
return 0
return 1
else:
return 0
def main():
s = input()
if is_signal(s) == 1:
print("True")
else:
print("False")
if __name__ == '__main__':
main()
通过键盘输⼊判断,是标⽰符,则返回True,否则返回False
补充知识:python:标识符必须以字母或下划线开头,后⾯跟字母,下划线或者数字
标识符合法性检查,⾸先要以字母或者下划线开始,后⾯要跟字母,下划线或者或数字.这个⼩例⼦只检查长度⼤于等于 2 的标识符
idcheck.py
#!/usr/bin/env python
'''
idcheck.py -- checks identifiers for validity
'''
import string    # string utility module
# create alphabet and number sets
alphas = string.ascii_letters + '_'
nums = string.digits
# salutation message and input prompt
print ('Welcome to the Identifier Checker v1.0')
print ('Testees must be at least 2 chars long.')
inp = input('Identifier to test ?')
if len(inp) >= 1:
if inp[0] not in alphas:
print ('invalid: first symbol must be alphabetic')
else:
for otherChar in inp[1:]:
if otherChar not in alphas + nums:
print ('invalid: remaining symbols must be alphanumeric')
break
else:
print ("okay as an identifier")
else:
print ('invalid: length must be >= 1')
运⾏结果 1:
Welcome to the Identifier Checker v1.0
Testees must be at least 2 chars long.
Identifier to test -> 123_das
invalid: first symbol must be alphabetic
运⾏结果 2:
Welcome to the Identifier Checker v1.0
Testees must be at least 2 chars long.
Identifier to test -> _123sdad
okay as an identifier
以上这篇Python判断字符串是否为合法标⽰符操作就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。