Created
March 27, 2014 06:34
-
-
Save shangbo/9801572 to your computer and use it in GitHub Desktop.
"fundamentals of compiling"'s homework,
count the variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
# -*- coding:utf-8 -*- | |
# __author__ = "Shaun" | |
import sys | |
VAR = [] | |
def read_source(filename): | |
""" | |
read source from file | |
input: file name | |
output: a long string of file | |
""" | |
source = None | |
try: | |
f = open(filename, 'r+') | |
source = f.read() | |
f.close() | |
except IOError: | |
print "please input correct file name or path!" | |
finally: | |
return source | |
def deal_file(code): | |
""" | |
deal with the file | |
input: a string of file | |
output: a dict about the variable count | |
""" | |
line_list = code.split("\n") | |
for line in line_list: | |
if line != "": | |
get_var(line) | |
deal_symbol() | |
count_dict = count_var(line_list) | |
return count_dict | |
def get_var(line): | |
""" | |
Find the variable which was defined in the source | |
input: a line of the file | |
no output | |
""" | |
word_list = line.split(" ") | |
if word_list[0].strip(" ") == "Var" or word_list[0].strip(" ") == "Const": | |
for word in word_list[1:]: | |
if word.find(',') != '-1': | |
var_list = word.split(',') | |
VAR.extend(var_list) | |
def deal_symbol(): | |
""" | |
delete some symbol in the variable | |
no input | |
no output | |
""" | |
for i, var in enumerate(VAR): | |
if var.endswith(";"): | |
VAR[i] = VAR[i][:-1] | |
index = var.find("=") | |
if index != -1: | |
VAR[i] = VAR[i][0:index] | |
def count_var(line_list): | |
""" | |
count the variable according to some rules | |
input: a list of the file's line | |
output: a dict which stored variable's count | |
""" | |
count_list = [] | |
count_dict = {} | |
for line in line_list: | |
for var in VAR: | |
lower = var.lower() | |
upper = var.upper() | |
if line.find(lower) != -1: | |
is_alnum_1 = line[line.find(lower)-1].isalpha() and line[line.find(lower)-1].isdigit() | |
is_alnum_2 = line[line.find(lower)+1].isalpha() and line[line.find(lower)+1].isdigit() | |
if not is_alnum_1 and not is_alnum_2: | |
count_list.append(var) | |
elif line.find(upper) != -1: | |
is_alnum_1 = line[line.find(upper)-1].isalpha() and line[line.find(upper)-1].isdigit() | |
is_alnum_2 = line[line.find(upper)+1].isalpha() and line[line.find(upper)+1].isdigit() | |
if not is_alnum_1 and not is_alnum_2: | |
count_list.append(var) | |
for i in VAR: | |
count = count_list.count(i) | |
count_dict[i] = count | |
return count_dict | |
def main(): | |
arg = sys.argv | |
if len(arg) == 2: | |
file_name = arg[1] | |
code = read_source(file_name) | |
else: | |
print "arguments error!" | |
return 0 | |
count = deal_file(code) | |
print count | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment