turbo6412

why is printing everything txt

Sep 22nd, 2021
885
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ```py
  2. import re
  3. from lexer import *
  4.  
  5. def main():
  6.     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'}
  7.     separator = {'(' : 'separator', ')' : 'separator', ';' : 'separator', '%%' : 'separator'}
  8.     operator = { '/' : 'operator', '=' : 'operator', '>' : 'operator', '<' : 'operator', '+' : 'operator', '-' : 'operator', '==' : 'operator', '/=' : 'operator', '*' : 'operator'}
  9.     punctuation_symbol = {':':'colon', ';':'semi-colon', '.':'dot', ',':'comma'}
  10.  
  11.     dataFlag = False
  12.  
  13.     # Class obj
  14.     lexerClass = lexer("test")
  15.  
  16.     val = input("Enter filename: ")
  17.  
  18.     file = open(val)
  19.     #fileName = val
  20.     #sourceCodeFile = open(fileName, encoding = 'utf-8')
  21.  
  22.     a = file.read() #Reads the entire textfile first.
  23.     outputFile = open("output_test1.txt", "w")
  24.  
  25.     sourceCodeFile = a.split(" ")
  26.  
  27.     """for line in sourceCodeFile:
  28.        line_stripped = line.strip()
  29.  
  30.        # if it's keyword token
  31.        if line_stripped in keyWords:
  32.            outputFile.write( f"{ keyWords[line_stripped] }  : {line_stripped} \n")
  33.  
  34.        #if its separator token
  35.        elif line_stripped in separator:
  36.            outputFile.write( f"{ separator[line_stripped] }  : {line_stripped} \n")
  37.  
  38.        # if its operator token
  39.        elif line_stripped in operator:
  40.            outputFile.write( f"{ operator[line_stripped] }  : {line_stripped} \n")
  41.  
  42.        # if its an identifier token (still need a good elif case for this, and for real and int)
  43.        #need to figure out what to do if the line contains multiple tokens like:
  44.        #sum = 0;
  45.        #while (i)
  46.  
  47.        else:
  48.            result = lexerClass.identifierFsm(line_stripped)
  49.            # it is identifier
  50.            if result == 1:
  51.                print(f"is Identifier?:  {result}")
  52.                outputFile.write(f"Identifier: {line_stripped} \n")
  53.  
  54.  
  55.    sourceCodeFile.close()
  56.    outputFile.close()"""
  57.  
  58.     for line in sourceCodeFile:
  59.         line_stripped = line.split(' ') #Splits the whitespace in the one line.
  60.         for token in line_stripped: #Inner for loop where you check char by char.
  61.             if token in operator:
  62.                 print(token, "-------------", operator[token])
  63.             elif token in keyWords:
  64.                 print(token, "------------- keyword")
  65.             elif token in punctuation_symbol: #Ask why we need this?
  66.                 print(token, "-------------", punctuation_symbol[token])
  67.             elif token in separator:
  68.                 print(token, "-------------", separator[token])
  69.             else:
  70.                 try:
  71.                     variable = int(token) #int (What does it do when its an int? )
  72.                     variable = float(token) #real
  73.                 except ValueError:
  74.                     variable = None
  75.                 if variable:
  76.                     print(token, "--------- real")
  77.                 elif token == ' ':
  78.                     pass #null
  79.                 else:
  80.                     print(token, "--------- identifier")
  81.  
  82.         dataFlag = False
  83.  
  84.     file.close()
  85.     outputFile.close()
  86.  
  87.  
  88.  
  89. if __name__ == "__main__":
  90.     main()
  91.  
  92.  
  93. ```
RAW Paste Data