Ejemplo n.º 1
0
	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