Professional Documents
Culture Documents
Assignment-2 - Salar Aziz
Assignment-2 - Salar Aziz
-Assignment 2-
By
Salar Aziz Ahmad
Instructor
Dr. Muhsin Atto
Salar Aziz Ahmad
1. First Task
answer of task one is to do the following processes:
• Increase column pointer by 1 each time pos pointer increases because it means read another
character from file.
• Check if current character is the end of current line.
• If current character is the end of line then:
o Increase row pointer by 1.
o Reset column pointer.
Following is the code for getting correct position of tokens that’s added to movepointer ():
self.pos += 1
self.column+=1
self.row+=1
self.column=0
2. Second Task
For task two that is asked to add if statements to deal with comments validation.
For single line comments idea is to check for current character and it’s next character if both are
‘/’ then everything between this characters and end of line ‘\n’ should be ignored.
Following is the code for single line comment in get_next_token function:
if self.current_char=='/' and self.text[self.pos+1]==’/’:
while self.current_char!='\n':# loop until current_char pointer isn’t in the end of line
continue
and for multi-line it needs to check if current character and character in its next position are start
of a multi-line comment or not that’s done with the following elif statement:
elif self.current_char=='/' and self.text[self.pos+1]==’*’:
if this statement is correct then its needed to check for nested comments and that by checking
number of begin and of comments.
Following is the code for multi-line and nested comments:
Following is full code of above three functions:
2
Salar Aziz Ahmad
def StartComment(self):
startcomment=0
i=0
while self.text[i] is not None and i < len(self.text) - 1:
if self.text[i]=='/' and self.text[i+1]=='*':
startcomment+=1
i+=1
return startcomment
def EndComment(self):
Endcomment=0
i=0
while self.text[i] is not None and i < len(self.text) - 1:
if self.text[i]=='*' and self.text[i+1]=='/':
Endcomment+=1
i+=1
return Endcomment
def ignoreComments(self):
endcomment=0
if self.StartComment==self.EndComment():
if self.current_char=='/' and self.next()=='*':
while self.pos<len(self.text)-1 and endcomment!=self. StartComment():
if self.current_char=='*' and self.next()=='/':
endcomment+=1
self.movePointer()
continue
else:
self.error('Wrong number of Comment blocks')
3
Salar Aziz Ahmad
Program Output
Program output with both examples in Moodle are as following
Example1:
Example2:
INTEGER, PLUS, SUB, MUL, DIV, ID,KEYWORD,SYMBOL,OPERATORS, EOF= 'INTEGER', 'PLUS', 'SUB',
'MUL', 'DIV', 'ID','KEYWORD','SYMBOL','OPERATORS', 'EOF'
# Keywords
# Symbols
Symbols= [",",";","(",")","{","}",".","?"]
#Operators
Operators = ["!","=",">","<",">","<"]
class Token(object):
4
Salar Aziz Ahmad
def __init__(self, type, value,r =0, c =0):
self.type = type
self.value = value
self.row = r
self.column = c
def __str__(self):
type=self.type,
value=repr(self.value),
row = self.row,
column = self.column
######################################################################################
# 2- Scanner
class Scanner(object):
self.text = text
self.pos = 0
self.current_char = self.text[self.pos]
# Positions (row,column)
self.row = 1
self.column = 0
def error(self,token):
def movePointer(self):
# Think how rwo and column counters must be updates to find position of given tokens
self.pos += 1
5
Salar Aziz Ahmad
self.column+=1 #increase column pointer after reading each character
else:
self.current_char = self.text[self.pos]
def skip_whitespace(self):
self.movePointer()
def integer(self):
result = ''
result += self.current_char
self.movePointer()
return int(result)
def next(self):
peek_pos = self.pos + 1
return None
else:
return self.text[peek_pos]
#=================================================================================
def StartComment(self):
startcomment=0
i=0
6
Salar Aziz Ahmad
if self.text[i]=='/' and self.text[i+1]=='*':
startcomment+=1
i+=1
return startcomment
def EndComment(self):
Endcomment=0
i=0
Endcomment+=1
i+=1
return Endcomment
def ignoreComments(self):
endcomment=0
if self.StartComment==self.EndComment():
endcomment+=1
self.movePointer()
continue
else:
#=================================================================================
def get_next_token(self):
if self.current_char.isspace():
self.skip_whitespace()
continue
7
Salar Aziz Ahmad
# you will need to define some functions to deal with comments
# please, make sure you undrestand how comments can be matched and scanned before adding code.
#==================================================================================
if self.current_char=='/':
if self.next()=='/':
self.movePointer()
continue
elif self.next()=='*':
self.ignoreComments()
#==================================================================================
if self.current_char.isdigit():
if self.current_char == '+':
self.movePointer()
#3-SUB (Lab 1)
if self.current_char == '-':
self.movePointer()
#4-MUL (Lab 1)
if self.current_char == '*':
self.movePointer()
self.movePointer()
if self.current_char.isalpha():
8
Salar Aziz Ahmad
current_id=self.current_char
self.movePointer()
return Token(ID,current_id,self.row,self.column)
#Symbols (Lab 2)
if self.current_char in Symbols:
s = self.current_char
self.movePointer()
return Token(SYMBOL,s,self.row,self.column)
# Operators(Lab2)
if self.current_char in Operators:
op= ''
op += text[self.pos]
self.movePointer()
return Token(OPERATORS,op,self.row,self.column)
self.error(self.current_char)
self.error("Undefined Token")
def printTokens(self):
self.current_token = self.get_next_token()
print(self.current_token)
self.current_token = self.get_next_token()
def main():
f = open("SourceCode.txt", "r")
text = f.read()
scanner = Scanner(text)
9
Salar Aziz Ahmad
scanner.printTokens()
f.close()
if __name__ == '__main__':
main()
10