Uploaded by NI Shakil

420

advertisement
import re
readFile = open('/content/cse/CSE420_Lab01_20101345/input.txt', 'r')
readFile = readFile.read()
writeFile = open('/content/cse/CSE420_Lab01_20101345/output.txt', 'w')
tokens = readFile.split()
keywordsList = ['abstract','continue', 'for','new','switch',
'assert','default','goto','package','synchronized',
'boolean','do','if','private' ,'this',
'break','double','implements','protected','throw', 'byte', 'else',
'import', 'public', 'throws', 'case', 'enum', 'instanceof', 'return',
'transient', 'catch','extends', 'int', 'short','try', 'char', 'final',
'interface', 'static', 'void', 'class', 'finally', 'long', 'strictfp',
'volatile', 'const', 'float', 'native', 'super', 'while']
#https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.h
tml
arithmeticOperatorList = ['=', '+', '-', '*', '/', '+=', '-=', '*=',
'/=', '%=', '&=', '|=', '^=', '>>=', '<<=']
#https://www.w3schools.com/java/java_operators.asp
logicalOperatorList = ['==', '!=', '!', '&&', '||', '>', '<', '>=',
'<='] #https://www.w3schools.com/java/java_operators.asp
alphabetsRegex = re.compile("[a-zA-Z]+")
numRegex = re.compile("([+-]?[0-9]+)+[.]?[0-9]*")
currKeywords = []
currIdentifiers = []
currNumbers = []
currArithmeticOperator = []
currLogicalOperator = []
currOthers = []
for token in tokens:
if token in keywordsList and token not in currKeywords:
currKeywords.append(token)
elif token in arithmeticOperatorList and token not in
currArithmeticOperator:
currArithmeticOperator.append(token)
elif token in logicalOperatorList and token not in
currLogicalOperator:
currLogicalOperator.append(token)
elif alphabetsRegex.fullmatch(token) is not None and token not in
keywordsList and token not in currIdentifiers:
currIdentifiers.append(token)
elif numRegex.fullmatch(token) is not None and token not in
currNumbers:
currNumbers.append(token)
elif token not in keywordsList and token not in currIdentifiers
and token not in currNumbers and token not in currOthers and token not
in currLogicalOperator and token not in currArithmeticOperator:
currOthers.append(token)
writeFile.write("currKeywords: ")
writeFile.write(', '.join(currKeywords))
writeFile.write("\ncurrIdentifiers: ")
writeFile.write(', '.join(currIdentifiers))
writeFile.write("\nMath Operators: ")
writeFile.write(', '.join(currArithmeticOperator))
writeFile.write("\nLogical Operators: ")
writeFile.write(', '.join(currLogicalOperator))
writeFile.write("\nNumerical Values: ")
writeFile.write(', '.join(currNumbers))
writeFile.write("\nOthers: ")
writeFile.write(' '.join(currOthers))
writeFile.close()
lab2
def mail_checker(test_string, state):
if test_string == '' and state == 'q10':
return 1
elif test_string == '':
return 0
if state == 'q0':
if test_string[0].isalpha():
return mail_checker(test_string[1:], 'q6')
else:
return 0
if state == 'q6':
if test_string[0].isalpha() or test_string[0].isdigit():
return mail_checker(test_string[1:], 'q6')
elif test_string[0] == "@":
return mail_checker(test_string[1:], 'q7')
else:
return 0
if state == 'q7':
if test_string[0].isalpha() or test_string[0].isdigit():
return mail_checker(test_string[1:], 'q8')
else:
return 0
if state == 'q8':
if test_string[0].isalpha() or test_string[0].isdigit():
return mail_checker(test_string[1:], 'q8')
elif test_string[0] == ".":
return mail_checker(test_string[1:], 'q9')
else:
return 0
if state == 'q9':
if test_string[0].isalpha():
return mail_checker(test_string[1:], 'q10')
elif test_string[0].isdigit():
return mail_checker(test_string[1:], 'q8')
else:
return 0
if state == 'q10':
if test_string[0].isalpha():
return mail_checker(test_string[1:], 'q10')
elif test_string[0].isdigit():
return mail_checker(test_string[1:], 'q8')
elif test_string[0] == ".":
return mail_checker(test_string[1:], 'q9')
else:
return 0
#print (mail_checker('asfety@g.bracu.ac.bd', 'q0'))
def web_checker(test_string, state):
if test_string == '' and state == 'q5':
return 1
elif test_string == '':
return 0
if state == 'q0':
if test_string[0:3] == "www":
return web_checker(test_string[3:], 'q1')
else:
return 0
if state == 'q1':
if test_string[0] == ".":
return web_checker(test_string[1:], 'q2')
else:
return 0
if state == 'q2':
if test_string[0].isalpha() or test_string[0].isdigit():
return web_checker(test_string[1:], 'q3')
else:
return 0
if state == 'q3':
if test_string[0].isalpha() or test_string[0].isdigit():
return web_checker(test_string[1:], 'q3')
elif test_string[0] == ".":
return web_checker(test_string[1:], 'q4')
else:
return 0
if state == 'q4':
if test_string[0].isalpha():
return web_checker(test_string[1:], 'q5')
elif test_string[0].isdigit():
return web_checker(test_string[1:], 'q3')
else:
return 0
if state == 'q5':
if test_string[0].isalpha():
return web_checker(test_string[1:], 'q5')
elif test_string[0].isdigit():
return web_checker(test_string[1:], 'q3')
elif test_string[0] == ".":
return web_checker(test_string[1:], 'q4')
else:
return 0
#print (web_checker('www.sdsadasfw.com', 'q0'))
input = open("/content/cse/CSE420_Lab02_20101345/input.txt", 'r')
input = input.read()
input = input.split('\n')
print(input)
noOfChecks = int(input[0].rstrip())
for i in range(noOfChecks):
test_string = input[1+i].rstrip()
if mail_checker(test_string, 'q0'):
print('Email, ' + str(i + 1))
elif web_checker(test_string, 'q0'):
print('Web, ' + str(i + 1))
['2', 'dilrubashowkat@gmail.1com', 'www.dilrubashowkat.com']
Web, 2
lab3
import re
input = open("/content/cse/CSE420_Lab03_20101345/input.txt", 'r')
input = input.read()
input = input.split('\n')
noReg = int(input[0])
noStr = int(input[noReg+1])
regex = input[1:noReg+1]
strings = input[noReg+2:noReg+noStr+2]
for val in strings:
result = ["No",0]
for idx2, val2 in enumerate(regex):
reg=re.compile(val2)
if re.match(reg,val):
result = ["yes",idx2+1]
print("{},{}".format(result[0], result[1]))
Download