def on_scan_pressed(self, event): # Clear output areas self.output.Clear() self.source.Clear() scanner = Scanner_Class() try: scanner.read_characters(self.edit.GetValue()) self.source.AppendText(scanner.source()) if self.opt_verbose.GetValue(): for line in scanner.output(): self.output.AppendText(str(line+"\n")) else: for line in scanner.tokens(): self.output.AppendText(str(line+"\n")) self.show_message("Scan completed successfully!") except Exception as strerror: self.source.AppendText(scanner.source()) self.show_error('Error: '+str(strerror))
def on_scan_pressed(self, event): # Clear output areas self.output.Clear() self.source.Clear() self.identifiers.Clear() scanner = Scanner_Class() try: scanner.read_characters(self.edit.GetValue()) self.source.AppendText(scanner.source()) ids = scanner.identifiers() self.identifiers.AppendText("Identifier\tTimes Used\n") for v in ids.keys(): self.identifiers.AppendText(v+":\t"+str(ids[v])+"\n") if self.out_file.GetValue() != '': try: file = open(self.out_file.GetValue(), 'w') if self.opt_verbose.GetValue(): for line in scanner.output(): file.write(str(line)+"\n") else: for line in scanner.tokens(): file.write(str(line)+"\n") file.write("\n\nDiscovered Identifier:Times Used\n") ids = scanner.identifiers() for v in ids.keys(): file.write(v+":"+str(ids[v])+"\n") file.close() except Exception as strerror: self.show_error('Error: '+str(strerror)) # Verbose output if self.opt_verbose.GetValue(): for line in scanner.output(): self.output.AppendText(str(line+"\n")) # Token only output else: for line in scanner.tokens(): self.output.AppendText(str(line+"\n")) self.show_message("Scan completed successfully!") except Exception as strerror: self.source.AppendText(scanner.source()) for line in scanner.output(): self.output.AppendText(str(line+"\n")) self.show_error('Error: '+str(strerror))
sys.stderr.write("Error: Input file required") sys.exit(1) except IndexError: if nogui: sys.stderr.write("Error: Input file required") sys.exit(1) # GUI mode if __name__ == "__main__" and not nogui: app = wxScannerApp() app.MainLoop() # Command line mode else: scanner = Scanner_Class() try: scanner.read_characters(in_file) except Exception as strerror: sys.stderr.write("Error: " + str(strerror)) sys.exit(1) if verbose: for line in scanner.output(): print line else: for line in scanner.tokens(): print line
def parse_file(self, filename): scanner = Scanner_Class(filename) self.source_text = scanner.source() self.clear_messages() ret = 1 stack = Stack() stack.push(1) current_token = scanner.next_token() while not stack.empty(): # Skip comments if current_token.type == 8: current_token = scanner.next_token() continue # Map all terminals to an integer token_number = self.translate(current_token) # Get head of the stack stacktop = stack.head.value; # Stats self.log("current_token: "+str(current_token.toString()), self.L_DEBUG) self.log("token_number: "+str(token_number), self.L_DEBUG) #self.log("stacktop: "+str(stacktop), self.L_DEBUG) #self.log("stack count: "+str(stack.count()), self.L_DEBUG) self.log("current stack: "+str(stack.toString()), self.L_DEBUG) # Non-terminal symbols if stacktop > 0: table_entry = self.next_table_entry(stacktop, abs(token_number)) if table_entry == 98: self.log("Scan error: dropping "+current_token.value, self.L_ERROR) current_token = scanner.next_token() elif table_entry == 99: self.log("Pop Error: popping "+str(stack.head.value), self.L_ERROR) stack.pop() elif table_entry <= len(self.stack_pushes): self.log("Fire "+str(table_entry), self.L_MATCH) stack.pop() for i in self.pushes(table_entry-1): if i != 0: stack.push(i) else: self.log("Error!", self.L_ERROR) # Terminals - Match and pop elif stacktop == token_number: self.log("Match and pop "+ str(current_token.value), self.L_MATCH) stack.pop() current_token = scanner.next_token() # Terminals - No Match :( else: self.log("Error -- Not Accepted", self.L_MATCH) break; # Success message if stack.empty(): self.log("Accept", self.L_MATCH) ret = 0 # End While return ret
try: in_file = sys.argv[len(sys.argv)-1] if len(sys.argv) == 1: in_file = "./source.txt" except IndexError: in_file = "./source.txt" # GUI mode if __name__ == "__main__" and not nogui: app = wxScannerApp() app.MainLoop() # Command line mode else: scanner = Scanner_Class() try: scanner.read_characters(in_file) except Exception as strerror: sys.stderr.write('Error: '+str(strerror)) sys.exit(1) if verbose: for line in scanner.output(): print line else: for line in scanner.tokens(): print line