Facebook
From Reliable Treeshrew, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 109
  1. -> no of charecters words lines
  2. FILE_PATH = "./text.txt"
  3. defmain() ->None:
  4.     c, l, w = 0, 0, 0
  5. withopen(FILE_PATH, "r") as f:
  6. for line in f.readlines():
  7.             c += len(line)
  8.             w += len(line.split())
  9.             l += 1
  10. print(f"Number of char: {c}, line: {l} and word: {w}")
  11. if__name__ == "__main__":
  12. exit(main() or0)
  13.  
  14. ->identify different tokens and token types from a file.
  15. fromenumimportEnum
  16.  
  17. FILE_PATH = "./test.txt"
  18. classTokType(Enum):
  19. String = 1
  20. Constant = 2
  21. Special = 3
  22.  
  23. deftokens(data: list):
  24. toks = []
  25. cl = 0
  26. forlineindata:
  27. cl += 1
  28. fortokinline.split():
  29. try:
  30. int(tok)
  31. toks.append((cl, tok, TokType.Constant))
  32. exceptValueError:
  33. iftok.isalpha():
  34. toks.append((cl, tok, TokType.String))
  35. else:
  36. toks.append((cl, tok, TokType.Special))
  37. returntoks
  38. defmain() ->None:
  39. data = []
  40. withopen(FILE_PATH, "r") asf:
  41. data = f.readlines()
  42. toks = tokens(data)
  43. print(f"Total token(s): {len(toks)}")
  44. print("Tokens:-")
  45. fortokintoks:
  46. print(f"line: {tok[0]}\"{tok[1]}\"\tis\t{tok[2]}")
  47.  
  48. if__name__ == "__main__":
  49. exit(main() or0)
  50.  
  51. ->to implement lexical analyzer without using lex tool.
  52.  
  53. fromenumimportEnum
  54.  
  55. FILE_PATH = "./hello.c"
  56.  
  57. K_WORDS = ["int", "float", "char", "return", "double", "break", "continue", "if", "else", "for", "while", "do", "include"]
  58.  
  59. classTkType(Enum):
  60. Ident = 0
  61. Literal = 1
  62. Symbol = 2
  63. Const = 3
  64. Keyword = 4
  65.  
  66. deftoks(data: str):
  67. a = data
  68. tks = []
  69. buf = []
  70. n = len(data)
  71. i = 0
  72. l, c = 1, 1
  73. whilei<n:
  74. ifa[i] == ''ora[i] == '\t':
  75. c += 1
  76. i += 1
  77. continue
  78. ifa[i] == '\n':
  79. l += 1
  80. c = 1
  81. i += 1
  82. continue
  83. buf.clear()
  84. ifa[i].isalpha():
  85. whilei<nanda[i].isalpha():
  86. buf.append(a[i])
  87. i += 1
  88. c += 1
  89. i -= 1
  90. if"".join(buf) inK_WORDS:
  91. tks.append(((l, c - len(buf)), "".join(buf), TkType.Keyword))
  92. else:
  93. tks.append(((l, c - len(buf)), "".join(buf), TkType.Ident))
  94. elifa[i].isdigit():
  95. whilei<nanda[i].isdigit():
  96. buf.append(a[i])
  97. i += 1
  98. c += 1
  99. i -= 1
  100. tks.append(((l, c - len(buf)), "".join(buf), TkType.Const))
  101. elifa[i] == '"':
  102. i += 1
  103. whilei<nanda[i] != '"':
  104. buf.append(a[i])
  105. i += 1
  106. c += 1
  107. tks.append(((l, c - len(buf)), '"' + "".join(buf) + '"', TkType.Literal))
  108. else:
  109. tks.append(((l, c), a[i], TkType.Symbol))
  110. i += 1
  111. c += 1
  112. returntks
  113. defmain() ->None:
  114. data = ""
  115. withopen(FILE_PATH, "r") asf:
  116. data = f.read()
  117. tks = toks(data)
  118. print(f"TokenType\tline:col:Token")
  119. fortkintks:
  120. print(f"{tk[2]}\t{tk[0][0]}:{tk[0][1]}:{tk[1]}")
  121.  
  122. if__name__ == "__main__":
  123. exit(main() or0)
  124.  
  125.  
  126.  
  127.