Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Computation and Compiler

-Assignment 2-

By
Salar Aziz Ahmad

Computer Science Department-3rd stage


Faculty of Science
University of Zakho

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

if self.current_char=='\n': #when pointer is in the end of line

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

self.movePointer() #go to next character

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:

Full code of program


Following is the full code of the program:
#Type of tokens, keywords and symbols

INTEGER, PLUS, SUB, MUL, DIV, ID,KEYWORD,SYMBOL,OPERATORS, EOF= 'INTEGER', 'PLUS', 'SUB',
'MUL', 'DIV', 'ID','KEYWORD','SYMBOL','OPERATORS', 'EOF'

# Keywords

Keywords =["if", "int", "float", "double", "while", "for", "const", "goto"]

# Symbols

Symbols= [",",";","(",")","{","}",".","?"]

#Operators

Operators = ["!","=",">","<",">","<"]

############################### TOKEN CLASS ###################

#Do not change this class

class Token(object):

4
Salar Aziz Ahmad
def __init__(self, type, value,r =0, c =0):

self.type = type

self.value = value

# position of tokens (assignment 2)

self.row = r

self.column = c

def __str__(self):

return 'Token({type},{value},Position {row}:{column})'.format(

type=self.type,

value=repr(self.value),

row = self.row,

column = self.column

######################################################################################

# 2- Scanner

class Scanner(object):

def __init__(self, text):

self.text = text

self.pos = 0

self.current_char = self.text[self.pos]

# Positions (row,column)

# position = position(1,1), before changing.

self.row = 1

self.column = 0

def error(self,token):

raise Exception('Invalid Token: '+str(token))

def movePointer(self):

# change this function to find correct row and colum counters

# 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

if self.current_char=='\n': #used to find end of current row

self.row+=1 #add 1 to row pointer (means next row)

self.column=0 #reset column pointer

if self.pos > len(self.text) - 1:

self.current_char = None # Indicates end of input

else:

self.current_char = self.text[self.pos]

def skip_whitespace(self):

while self.current_char is not None and self.current_char.isspace():

self.movePointer()

def integer(self):

result = ''

while self.current_char is not None and self.current_char.isdigit():

result += self.current_char

self.movePointer()

return int(result)

# this function find a charactor after [pos]

def next(self):

peek_pos = self.pos + 1

if peek_pos > len(self.text) - 1:

return None

else:

return self.text[peek_pos]

#=================================================================================

def StartComment(self):

startcomment=0

i=0

while self.text[i] is not None and i < len(self.text) - 1:

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

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')

#=================================================================================

def get_next_token(self):

while self.current_char is not None:

if self.current_char.isspace():

self.skip_whitespace()

continue

# add if statments to deal with comments validation

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()=='/':

while self.current_char!='\n' and self.pos<len(self.text)-1:

self.movePointer()

continue

elif self.next()=='*':

self.ignoreComments()

#==================================================================================

if self.current_char.isdigit():

return Token(INTEGER, self.integer(),self.row,self.column)

if self.current_char == '+':

self.movePointer()

return Token(PLUS, '+',self.row,self.column)

#3-SUB (Lab 1)

if self.current_char == '-':

self.movePointer()

return Token(SUB, '-',self.row,self.column)

#4-MUL (Lab 1)

if self.current_char == '*':

self.movePointer()

return Token(MUL, '*',self.row,self.column)

if self.current_char == '/' and self.current_char is not None:

self.movePointer()

return Token(DIV, '/',self.row,self.column)

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= ''

while text[self.pos] in Operators:

op += text[self.pos]

self.movePointer()

return Token(OPERATORS,op,self.row,self.column)

self.error(self.current_char)

return Token(EOF, None,self.row,self.column)

#Default --> undefined Token

self.error("Undefined Token")

# Do not change anything after this.

def printTokens(self):

self.current_token = self.get_next_token()

while self.current_token.value is not None:

print(self.current_token)

self.current_token = self.get_next_token()

# Do not change this code in this lab

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

You might also like