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判断字符串是否为合法标示符操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(Python判断字符串是否为合法标示符操作)