SoFunction
Updated on 2024-11-10

Python Determining if a String is a Legitimate Marker Operation

This semester in the study of the principles of compilation, the recent on-line homework is to do a simple lexical analyzer, in the process of doing, suddenly there is a need to determine whether a string is a legal marker, because I am using python language to do the Python lexical analyzer, and so the following share the following how to determine a string is a legal marker.

First, let's familiarize ourselves with what the following python markers are defined as.

Definition: starts with a letter or underscore, consists of letters, numbers, or underscores, but cannot be a python reserved word.

Another question, what are the reserved words in python and what are they?

# 
import keyword
print 
# 
import keyword
print()


# Output:
 ['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 in total
# Output:
['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 in total

Okay, here's the judgment call.

# python2.7

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import keyword
import string
def is_signal(s):
 kw = 
 if s in kw:
  return 0
 elif s[0] == '_' or s[0] in : # Determine if it starts with a letter or an underscore
  for i in s:
   if i == '_' or i in  or i in : # Determine if it consists of alphanumerics or underscores
    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 = 
 if s in kw:
  return 0
 elif s[0] == '_' or s[0] in string.ascii_letters: # Determine if it starts with a letter or an underscore
  for i in s:
   if i == '_' or i in string.ascii_letters or i in : # Determine if it consists of alphanumerics or underscores
    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()

Judge by the keyboard input, is a marker, then return True, otherwise return False

Additional knowledge:python:Identifiers must start with a letter or an underscore, followed by a letter, underscore, or number.

Identifiers are checked for legality by starting with a letter or underscore followed by a letter, underscore or number. This small example only checks identifiers of length greater than or equal to 2.

#!/usr/bin/env python
'''
 -- checks identifiers for validity
'''
 
import string    # string utility module
 
# create alphabet and number sets
alphas = string.ascii_letters + '_'
nums = 
 
# 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')

Run Results 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

Run Results 2:

Welcome to the Identifier Checker v1.0
Testees must be at least 2 chars long.
Identifier to test -> _123sdad
okay as an identifier

Above this Python to determine whether the string is a legal marker operation is all that I have shared with you, I hope to be able to give you a reference, and I hope you will support me more.