def main(): num_args = len(sys.argv)-1 try: if num_args == 0: sys.stderr.write("Error: not enough arguments\n") if num_args > 2: sys.stderr.write("Error: too many arguments\n") if sys.argv[1][1] == 's': if num_args == 1: input_string = sys.stdin.read() s = Scanner(input_string) s.all() elif num_args == 2: # read from file f = open(sys.argv[2], 'r') input_line = "" for line in f: input_line += line s = Scanner(input_line) s.all() f.close() elif sys.argv[1][1] == 'c': sys.stderr.write("Error: invalid option\n") elif sys.argv[1][1] == 't': sys.stderr.write("Error: invalid option\n") elif sys.argv[1][1] == 'a': sys.stderr.write("Error: invalid option\n") elif sys.argv[1][1] == 'i': sys.stderr.write("Error: invalid option\n") else: sys.stderr.write("Error: invalid option\n") except Exception as e: pass
#if there are no argument except for the name of the program, the driver is supposed to generate code for an input program elif len(sys.argv) == 1: input_string = '' new_line = '' while new_line != 'stop\n': try: input_string += new_line new_line = raw_input() + '\n' except (EOFError, KeyboardInterrupt): break #construct the object of Scanner by it's constructor scanner = Scanner(input_string) #produce a list of tokens by calling it's function token_array = scanner.all() #if produces the error message, then display it if scanner.have_error() == True: print scanner.get_error_message() else: output = Output() factory = AstFactory(output) parser = Parser(token_array, output, factory) #print 'parse start' parser.parse() print 'parse success' if parser.get_error_message() != '': print parser.get_error_message() exit()