Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```py
- import re
- from lexer import *
- def main():
- keyWords = {'while': 'keyword', 'integer': 'keyword', 'if': 'keyword', 'else': 'keyword', 'endif': 'keyword', 'get': 'keyword', 'put': 'keyword', 'boolean': 'keyword', 'begin': 'keyword', 'end': 'keyword', 'true': 'keyword', 'false': 'keyword'}
- separator = {'(' : 'separator', ')' : 'separator', ';' : 'separator', '%%' : 'separator'}
- operator = { '/' : 'operator', '=' : 'operator', '>' : 'operator', '<' : 'operator', '+' : 'operator', '-' : 'operator', '==' : 'operator', '/=' : 'operator', '*' : 'operator'}
- punctuation_symbol = {':':'colon', ';':'semi-colon', '.':'dot', ',':'comma'}
- dataFlag = False
- # Class obj
- lexerClass = lexer("test")
- val = input("Enter filename: ")
- file = open(val)
- #fileName = val
- #sourceCodeFile = open(fileName, encoding = 'utf-8')
- a = file.read() #Reads the entire textfile first.
- outputFile = open("output_test1.txt", "w")
- sourceCodeFile = a.split(" ")
- """for line in sourceCodeFile:
- line_stripped = line.strip()
- # if it's keyword token
- if line_stripped in keyWords:
- outputFile.write( f"{ keyWords[line_stripped] } : {line_stripped} \n")
- #if its separator token
- elif line_stripped in separator:
- outputFile.write( f"{ separator[line_stripped] } : {line_stripped} \n")
- # if its operator token
- elif line_stripped in operator:
- outputFile.write( f"{ operator[line_stripped] } : {line_stripped} \n")
- # if its an identifier token (still need a good elif case for this, and for real and int)
- #need to figure out what to do if the line contains multiple tokens like:
- #sum = 0;
- #while (i)
- else:
- result = lexerClass.identifierFsm(line_stripped)
- # it is identifier
- if result == 1:
- print(f"is Identifier?: {result}")
- outputFile.write(f"Identifier: {line_stripped} \n")
- sourceCodeFile.close()
- outputFile.close()"""
- for line in sourceCodeFile:
- line_stripped = line.split(' ') #Splits the whitespace in the one line.
- for token in line_stripped: #Inner for loop where you check char by char.
- if token in operator:
- print(token, "-------------", operator[token])
- elif token in keyWords:
- print(token, "------------- keyword")
- elif token in punctuation_symbol: #Ask why we need this?
- print(token, "-------------", punctuation_symbol[token])
- elif token in separator:
- print(token, "-------------", separator[token])
- else:
- try:
- variable = int(token) #int (What does it do when its an int? )
- variable = float(token) #real
- except ValueError:
- variable = None
- if variable:
- print(token, "--------- real")
- elif token == ' ':
- pass #null
- else:
- print(token, "--------- identifier")
- dataFlag = False
- file.close()
- outputFile.close()
- if __name__ == "__main__":
- main()
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement