Passing off strings to “alphabet” that contains specific characters for lexical analysis

Passing off strings to “alphabet” that contains specific characters for lexical analysis



In python, I want to know how I can pass off the strings that checks:



I am having a hard time figuring out the syntax and I specifically need strings like these instead of using built-in functions for lexical analysis. The following will clear the idea of what I'am trying to do:


alphanumeric=r'[a-zA-Z]+'
digit=r'[0-9]'
other=r'![a-zA-Z_0-9]'
alphabet = alphanumeric ,digit,other



This alphabet along with other DFA attributes will be fed into the dfa object's function run_with_input_list() with some user defined string. The dfa class structure is below:


class DFA:
current_state = None

def __init__(self, states, alphabet, transition_function, start_state, accept_states): #5-tupple
self.states = states
self.alphabet = alphabet
self.transition_function = transition_function
self.start_state = start_state
self.accept_states = accept_states
self.current_state = start_state
return

def transition_to_state_with_input(self, input_value):
if ((self.current_state, input_value) not in self.transition_function.keys()):
self.current_state = None
return
self.current_state = self.transition_function[(self.current_state, input_value)]
return

def in_accept_state(self):
if self.current_state in self.accept_states:
print("String Accepted")
else:
print("String Rejected")

def go_to_initial_state(self):
self.current_state = self.start_state
return

def run_with_input_list(self, input_list):
self.go_to_initial_state()
for inp in input_list:
self.transition_to_state_with_input(inp)
continue
return self.in_accept_state()

pass

def validity(self,input_list): #checking whether the input string is valid
for a in input_list:
if a in self.alphabet:
continue
else:
print("Invalid String")
return 0
return 1




3 Answers
3



I did not quite understand what "passing off" means, but these are the methods you can use :



1) Check if it contains only alphabets


print("abcd".isalpha()) #True
print("abcd123".isalpha()) #False
print("123".isalpha()) #False



2) Check if it contains only numbers


print("123".isnumeric()) #True
print("abcd".isnumeric()) #False
print("abcd123".isnumeric()) #False



3) Check if it contains only alphabets / numbers


print("abc123".isalnum()) #True
print("abc???".isalnum()) #False






actually, I need to use specifically, the strings I specified like [A-Za-z].

– Mujtaba Faizi
Sep 15 '18 at 13:53






@MujtabaFaizi which is taken care of by isalpha

– Sruthi V
Sep 15 '18 at 14:03



isalpha






I have edited the question for better query

– Mujtaba Faizi
Sep 15 '18 at 15:17



Python regular expressions already have special characters for this - digit has d alphanumeric has w, non-alphanumeric character has W.


d


w


W



References: https://docs.python.org/3/howto/regex.html



Python provides standard helpers for such operation, look at this example:


def what_is(s):
is_num = s.isnumeric()
is_alpha = s.isalpha()
is_other = not is_num and not is_alpha
return is_num, is_alpha, is_other


def main():
alpha = "Ciao"
num = "0102"
other = "£*+]"
print(what_is(alpha))
print(what_is(num))
print(what_is(other))






actually, I need to use specifically, the strings I specified like [A-Za-z].

– Mujtaba Faizi
Sep 15 '18 at 13:53



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế